2 Cantaveria - action adventure platform game
3 Copyright (C) 2009 2010 Evan Rinehart
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to
18 The Free Software Foundation, Inc.
19 51 Franklin Street, Fifth Floor
20 Boston, MA 02110-1301, USA
42 char* filename
= "data.zip";
43 arc
= zip_aropenf(filename
);
45 fatal_error("loader: unable to load data archive \"%s\" (%s)\n", filename
, zip_geterror());
47 boot_msg("loader: ... OK\n");
54 reader
* data_open(char* dir
, char* filename
){
57 strcat(buf
, filename
);
58 return loader_open(buf
);
61 reader
* loader_open(char* filename
){
62 char buf
[1024] = "data/";
64 strncpy(buf
+L
,filename
,1024-L
);
67 reader
* rd
= xmalloc(sizeof(reader
));
68 rd
->f
= zip_fopen(arc
, filename
);
70 error_msg("loader: can't open %s (%s)\n", filename
, zip_geterror());
78 void loader_close(reader
* rd
){
84 int loader_read(reader
* rd
, void* buf
, int count
){
85 return zip_fread(rd
->f
, buf
, count
);
88 unsigned char* loader_readall(char* filename
, int* size
){
89 reader
* rd
= loader_open(filename
);
92 /* somehow read all of rd into a buffer and return it */
97 int loader_readline(reader
* rd
, char* buf
, int size
){
103 n
= loader_read(rd
, &c
, 1);
104 if(n
== 0){ /* end of file */
110 error_msg("loader_readline: %s\n", zip_geterror());
114 if(c
== '\r'){ /* CRLF ? */
115 n
= loader_read(rd
, &c
, 1);
116 if(n
== 0){ /* file ended with CR... well take it */
122 error_msg("loader_readline: %s\n", zip_geterror());
127 error_msg("loader_readline: I cannot read lines ending in CR and not CRLF\n");
135 if(c
== '\n'){ /* LF */
144 error_msg("loader_readline: buffer size too small\n");
148 int loader_scanline(reader
* rd
, char* format
, ...){
153 if(loader_readline(rd
, buf
, 256) < 0){
157 va_start(ap
, format
);
158 ret
= vsscanf(buf
,format
,ap
);
168 int read_byte(reader
* rd
){
170 loader_read(rd
, &c
, 1);
174 int read_short(reader
* rd
){
175 unsigned char c
[2] = {0,0};
176 loader_read(rd
, c
+0, 1);
177 loader_read(rd
, c
+1, 1);
178 return (c
[0]<<8) | c
[1];
181 int read_int(reader
* rd
){
182 unsigned char c
[4] = {0,0,0,0};
183 loader_read(rd
, c
+0, 1);
184 loader_read(rd
, c
+1, 1);
185 loader_read(rd
, c
+2, 1);
186 loader_read(rd
, c
+3, 1);
187 return (c
[0]<<24) | (c
[1]<<16) | (c
[2]<<8) | c
[3];
190 char* read_string(reader
* rd
){
191 unsigned int L
= read_int(rd
);
192 if(L
==0) return NULL
;
193 char* S
= xmalloc(L
+1);
195 loader_read(rd
, S
, L
);
202 list* loader_readdir(char* path){
203 zip_dir* dir = zip_opendir(arc, path);
204 char* entry = zip_readdir(dir);
205 list* dirs = empty();
209 entry = zip_readdir(dir);
215 void loader_freedirlist(list* dirs){
216 list* ptr = dirs->next;