Added font file loading.
[cantaveria.git] / loader.c
blob94b3966a7f4c260a24f92cb25f1286414bcefa3c
1 /*
2 Cantaveria - action adventure platform game
3 Copyright (C) 2009 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(char* filename){
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* loader_open(char* filename){
57 char buf[1024] = "data/";
58 int L = strlen(buf);
59 strncpy(buf+L,filename,1024-L);
60 buf[1023] = 0;
62 reader* rd = xmalloc(sizeof(reader));
63 //printf("loader: %s\n",buf);
64 rd->next_c = -1;
65 //rd->f = zzip_file_open(zzip_dir, buf, 0);
66 rd->f = zzip_open(buf, 0);
67 if(!rd->f){
68 //report_error("loader: unable to open %s (%s)\n",
69 // filename, zzip_strerror_of( zzip_dir ) );
70 report_error("loader: unable to open %s (%s)\n",
71 filename, strerror( errno ) );
72 free(rd);
73 return NULL;
75 return rd;
79 void loader_close(reader* rd){
80 //zzip_file_close(rd->f);
81 zzip_fclose(rd->f);
82 free(rd);
86 int loader_read(reader* rd, void* buf, int count){
87 //return zzip_file_read(rd->f, buf, count);
88 return zzip_read(rd->f, buf, count);
91 unsigned char* loader_readall(char* filename, int* size){
92 ZZIP_STAT zs;
93 reader* rd = loader_open(filename);
94 if(!rd) return NULL;
95 if(zzip_fstat(rd->f, &zs) < 0){
96 report_error("loader: stat error on %s\n",filename);
97 return NULL;
99 int N = zs.st_size;
100 unsigned char* buf = xmalloc(N);
101 loader_read(rd,buf,N);
102 if(size) *size = N;
103 loader_close(rd);
104 return buf;
108 int loader_scanline(reader* rd, char* format, ...){
110 char buf[256];
111 int i=0;
112 while(i<255){
113 char c;
115 /* get next character */
116 if(rd->next_c != -1){
117 c = rd->next_c;
118 rd->next_c = -1;
120 else{
121 int n = loader_read(rd, &c, 1);
122 if(n==0){
123 break;
127 /* see if it is a end of line sequence */
128 if(c=='\r'){
129 int n = loader_read(rd, &c, 1);
130 if(n==0){
131 break;
133 if(c!='\n'){
134 rd->next_c = c;
136 break;
138 else if(c=='\n'){
139 break;
142 buf[i++] = c;
144 buf[i]='\0';
146 va_list ap;
147 va_start(ap, format);
149 int ret = vsscanf(buf,format,ap);
151 va_end(ap);
153 return ret;