Hooked up more of loader.c into zip.c
[cantaveria.git] / loader.c
blob3d036ffd5ae7e959535e40614e94b8c574e10628
1 /*
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
23 #include <string.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdio.h>
29 #include <loader.h>
30 #include <util.h>
31 #include <zip.h>
33 struct reader {
34 zip_file* f;
35 int next_c;
38 zip_archive* arc;
41 void loader_init(){
42 char* filename = "data.zip";
43 arc = zip_aropenf(filename);
44 if(arc == NULL){
45 fatal_error("loader: unable to load data archive \"%s\" (%s)\n", filename, zip_geterror());
47 boot_msg("loader: ... OK\n");
50 void loader_quit(){
51 zip_arclose(arc);
54 reader* data_open(char* dir, char* filename){
55 char buf[1024];
56 strcpy(buf, dir);
57 strcat(buf, filename);
58 return loader_open(buf);
61 reader* loader_open(char* filename){
62 char buf[1024] = "data/";
63 int L = strlen(buf);
64 strncpy(buf+L,filename,1024-L);
65 buf[1023] = 0;
67 reader* rd = xmalloc(sizeof(reader));
68 rd->f = zip_fopen(arc, filename);
69 if(!rd->f){
70 error_msg("loader: can't open %s (%s)\n", filename, zip_geterror());
71 free(rd);
72 return NULL;
74 return rd;
78 void loader_close(reader* rd){
79 zip_fclose(rd->f);
80 free(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);
90 if(!rd) return NULL;
92 /* somehow read all of rd into a buffer and return it */
94 return NULL;
97 int loader_readline(reader* rd, char* buf, int size){
98 char c;
99 int i = 0;
100 int n;
102 while(i < size){
103 n = loader_read(rd, &c, 1);
104 if(n == 0){ /* end of file */
105 buf[i] = '\0';
106 return 0;
109 if(n < 0){
110 error_msg("loader_readline: %s\n", zip_geterror());
111 return -1;
114 if(c == '\r'){ /* CRLF ? */
115 n = loader_read(rd, &c, 1);
116 if(n == 0){ /* file ended with CR... well take it */
117 buf[i] = '\0';
118 return 0;
121 if(n < 0){
122 error_msg("loader_readline: %s\n", zip_geterror());
123 return -1;
126 if(c != '\n'){
127 error_msg("loader_readline: I cannot read lines ending in CR and not CRLF\n");
128 return -1;
131 buf[i] = '\0';
132 return 0;
135 if(c == '\n'){ /* LF */
136 buf[i] = '\0';
137 return 0;
140 buf[i] = c;
141 i += 1;
144 error_msg("loader_readline: buffer size too small\n");
145 return 0;
148 int loader_scanline(reader* rd, char* format, ...){
149 char buf[256] = "";
150 va_list ap;
151 int ret;
153 if(loader_readline(rd, buf, 256) < 0){
154 return -1;
157 va_start(ap, format);
158 ret = vsscanf(buf,format,ap);
159 va_end(ap);
161 return ret;
167 /*binary i/o*/
168 int read_byte(reader* rd){
169 unsigned char c = 0;
170 loader_read(rd, &c, 1);
171 return c;
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);
194 S[L] = '\0';
195 loader_read(rd, S, L);
196 return S;
202 list* loader_readdir(char* path){
203 zip_dir* dir = zip_opendir(arc, path);
204 char* entry = zip_readdir(dir);
205 list* dirs = empty();
207 while(entry){
208 push(dirs, entry);
209 entry = zip_readdir(dir);
212 return dirs;
215 void loader_freedirlist(list* dirs){
216 list* ptr = dirs->next;
217 while(ptr){
218 free(ptr->item);
219 ptr = ptr->next;
221 list_recycle(dirs);