Console has new feature.
[cantaveria.git] / loader.c
blobbcb512bcf0ab5c5e0deef292e724207726e94c36
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>
28 #include <zzip/lib.h>
32 #include <loader.h>
33 #include <util.h>
35 struct reader {
36 ZZIP_FILE* f;
37 int next_c;
40 ZZIP_DIR* zzip_dir;
41 int errno;
43 void loader_init(){
44 //zzip_dir = zzip_dir_open(filename, 0);
45 //if(!zzip_dir){
46 // report_error("loader: unable to open game data in %s (%s)\n",
47 // filename, strerror( errno ) );
48 // exit(-1);
49 // }
52 void loader_quit(){
53 //zzip_dir_close(zzip_dir);
56 reader* data_open(char* dir, char* filename){
57 char buf[1024];
58 strcpy(buf, dir);
59 strcat(buf, filename);
60 return loader_open(buf);
63 reader* loader_open(char* filename){
64 char buf[1024] = "data/";
65 int L = strlen(buf);
66 strncpy(buf+L,filename,1024-L);
67 buf[1023] = 0;
69 reader* rd = xmalloc(sizeof(reader));
70 //printf("loader: %s\n",buf);
71 rd->next_c = -1;
72 //rd->f = zzip_file_open(zzip_dir, buf, 0);
73 rd->f = zzip_open(buf, 0);
74 if(!rd->f){
75 //report_error("loader: unable to open %s (%s)\n",
76 // filename, zzip_strerror_of( zzip_dir ) );
77 report_error("loader: unable to open %s (%s)\n",
78 filename, strerror( errno ) );
79 free(rd);
80 return NULL;
82 return rd;
86 void loader_close(reader* rd){
87 //zzip_file_close(rd->f);
88 zzip_fclose(rd->f);
89 free(rd);
93 int loader_read(reader* rd, void* buf, int count){
94 return zzip_read(rd->f, buf, count);
97 unsigned char* loader_readall(char* filename, int* size){
98 ZZIP_STAT zs;
99 reader* rd = loader_open(filename);
100 if(!rd) return NULL;
101 if(zzip_fstat(rd->f, &zs) < 0){
102 report_error("loader: stat error on %s\n",filename);
103 return NULL;
105 int N = zs.st_size;
106 unsigned char* buf = xmalloc(N);
107 loader_read(rd,buf,N);
108 if(size) *size = N;
109 loader_close(rd);
110 return buf;
114 int loader_scanline(reader* rd, char* format, ...){
116 char buf[256];
117 int i=0;
118 while(i<255){
119 char c;
121 /* get next character */
122 if(rd->next_c != -1){
123 c = rd->next_c;
124 rd->next_c = -1;
126 else{
127 int n = loader_read(rd, &c, 1);
128 if(n==0){
129 break;
133 /* see if it is a end of line sequence */
134 if(c=='\r'){
135 int n = loader_read(rd, &c, 1);
136 if(n==0){
137 break;
139 if(c!='\n'){
140 rd->next_c = c;
142 break;
144 else if(c=='\n'){
145 break;
148 buf[i++] = c;
150 buf[i]='\0';
152 va_list ap;
153 va_start(ap, format);
155 int ret = vsscanf(buf,format,ap);
157 va_end(ap);
159 return ret;
165 /*binary i/o*/
166 unsigned char read_byte(reader* rd){
167 unsigned char c;
168 loader_read(rd, &c, 1);
169 return c;
172 short read_short(reader* rd){
173 unsigned char c[2];
174 loader_read(rd, c+0, 1);
175 loader_read(rd, c+1, 1);
176 return (c[0]<<8) | c[1];
179 int read_int(reader* rd){
180 unsigned char c[4];
181 loader_read(rd, c+0, 1);
182 loader_read(rd, c+1, 1);
183 loader_read(rd, c+2, 1);
184 loader_read(rd, c+3, 1);
185 return (c[0]<<24) | (c[1]<<16) | (c[2]<<8) | c[3];
188 char* read_string(reader* rd){
189 unsigned int L = read_int(rd);
190 if(L==0) return NULL;
191 char* S = xmalloc(L+1);
192 S[L] = '\0';
193 loader_read(rd, S, L);
194 return S;
200 char** loader_readdir(char* path){
201 char buf[1024] = "data/";
202 strcat(buf, path);
204 ZZIP_DIR* dir = zzip_opendir(buf);
206 int N = 0;
207 ZZIP_DIRENT* ent;
208 while( (ent = zzip_readdir(dir)) ) N++;
210 char** res = xmalloc((N+1)*sizeof(char*));
212 zzip_closedir(dir);
213 dir = zzip_opendir(buf);
215 int i = 0;
216 while(i < N+1){
217 ent = zzip_readdir(dir);
218 if(!ent){
219 res[i] = NULL;
220 i++;
222 else if(ent->d_name[0] == '.'){
224 else{
225 res[i] = xmalloc(strlen(ent->d_name)+1);
226 strcpy(res[i], ent->d_name);
227 i++;
231 return res;
234 void loader_freedirlist(char** list){
235 int i;
236 for(i=0; list[i]; i++){
237 free(list[i]);