Changeset 2406 for libcaca/trunk/cucul/file.c
- Timestamp:
- Jun 15, 2008, 3:50:02 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcaca/trunk/cucul/file.c
r2300 r2406 47 47 # endif 48 48 FILE *f; 49 int readonly; 49 50 }; 50 51 #endif 51 52 52 cucul_file_t * _cucul_file_open(char const *path, const char *mode)53 cucul_file_t *cucul_file_open(char const *path, const char *mode) 53 54 { 54 55 #if defined __KERNEL__ … … 56 57 #else 57 58 cucul_file_t *fp = malloc(sizeof(*fp)); 59 60 fp->readonly = !strchr(mode, 'r'); 58 61 59 62 # if defined HAVE_ZLIB_H … … 61 64 unsigned int skip_size = 0; 62 65 63 fp->gz = gzopen(path, "rb");66 fp->gz = gzopen(path, fp->readonly ? "rb" : "wb"); 64 67 if(!fp->gz) 65 68 { … … 71 74 fp->zip = 0; 72 75 73 /* Parse ZIP file and go to start of first file */ 74 gzread(fp->gz, buf, 4); 75 if(memcmp(buf, "PK\3\4", 4)) 76 { 77 gzseek(fp->gz, 0, SEEK_SET); 78 return fp; 79 } 80 81 fp->zip = 1; 82 83 gzseek(fp->gz, 22, SEEK_CUR); 84 85 gzread(fp->gz, buf, 2); /* Filename size */ 86 skip_size += (uint16_t)buf[0] | ((uint16_t)buf[1] << 8); 87 gzread(fp->gz, buf, 2); /* Extra field size */ 88 skip_size += (uint16_t)buf[0] | ((uint16_t)buf[1] << 8); 89 90 gzseek(fp->gz, skip_size, SEEK_CUR); 91 92 /* Initialise inflate stream */ 93 fp->stream.total_out = 0; 94 fp->stream.zalloc = NULL; 95 fp->stream.zfree = NULL; 96 fp->stream.opaque = NULL; 97 fp->stream.next_in = NULL; 98 fp->stream.avail_in = 0; 99 100 if(inflateInit2(&fp->stream, -MAX_WBITS)) 101 { 102 free(fp); 103 gzclose(fp->gz); 104 return NULL; 76 if(fp->readonly) 77 { 78 /* Parse ZIP file and go to start of first file */ 79 gzread(fp->gz, buf, 4); 80 if(memcmp(buf, "PK\3\4", 4)) 81 { 82 gzseek(fp->gz, 0, SEEK_SET); 83 return fp; 84 } 85 86 fp->zip = 1; 87 88 gzseek(fp->gz, 22, SEEK_CUR); 89 90 gzread(fp->gz, buf, 2); /* Filename size */ 91 skip_size += (uint16_t)buf[0] | ((uint16_t)buf[1] << 8); 92 gzread(fp->gz, buf, 2); /* Extra field size */ 93 skip_size += (uint16_t)buf[0] | ((uint16_t)buf[1] << 8); 94 95 gzseek(fp->gz, skip_size, SEEK_CUR); 96 97 /* Initialise inflate stream */ 98 fp->stream.total_out = 0; 99 fp->stream.zalloc = NULL; 100 fp->stream.zfree = NULL; 101 fp->stream.opaque = NULL; 102 fp->stream.next_in = NULL; 103 fp->stream.avail_in = 0; 104 105 if(inflateInit2(&fp->stream, -MAX_WBITS)) 106 { 107 free(fp); 108 gzclose(fp->gz); 109 return NULL; 110 } 105 111 } 106 112 # else 107 fp->f = fopen(path, mode);113 fp->f = fopen(path, fmode); 108 114 109 115 if(!fp->f) … … 118 124 } 119 125 120 int _cucul_file_close(cucul_file_t *fp)126 int cucul_file_close(cucul_file_t *fp) 121 127 { 122 128 #if defined __KERNEL__ … … 135 141 } 136 142 137 int _cucul_file_eof(cucul_file_t *fp) 138 { 139 #if defined __KERNEL__ 140 return 1; 141 #elif defined HAVE_ZLIB_H 142 return fp->zip ? fp->eof : gzeof(fp->gz); 143 #else 144 return feof(fp->f); 145 #endif 146 } 147 148 char *_cucul_file_gets(char *s, int size, cucul_file_t *fp) 143 size_t cucul_file_read(cucul_file_t *fp, void *ptr, size_t size) 144 { 145 #if defined __KERNEL__ 146 return 0; 147 #elif defined HAVE_ZLIB_H 148 if(fp->zip) 149 return zipread(fp, ptr, size); 150 return gzread(fp->gz, ptr, size); 151 #else 152 return fread(ptr, 1, size, fp->f); 153 #endif 154 } 155 156 size_t cucul_file_write(cucul_file_t *fp, const void *ptr, size_t size) 157 { 158 if(fp->readonly) 159 return 0; 160 161 #if defined __KERNEL__ 162 return 0; 163 #elif defined HAVE_ZLIB_H 164 return gzwrite(fp->gz, ptr, size); 165 #else 166 return fwrite(ptr, 1, size, fp->f); 167 #endif 168 } 169 170 char *cucul_file_gets(cucul_file_t *fp, char *s, int size) 149 171 { 150 172 #if defined __KERNEL__ … … 179 201 } 180 202 203 int cucul_file_eof(cucul_file_t *fp) 204 { 205 #if defined __KERNEL__ 206 return 1; 207 #elif defined HAVE_ZLIB_H 208 return fp->zip ? fp->eof : gzeof(fp->gz); 209 #else 210 return feof(fp->f); 211 #endif 212 } 213 181 214 #if !defined __KERNEL__ && defined HAVE_ZLIB_H 182 215 static int zipread(cucul_file_t *fp, void *buf, unsigned int len)
Note: See TracChangeset
for help on using the changeset viewer.