Changeset 2406
- Timestamp:
- Jun 15, 2008, 3:50:02 PM (15 years ago)
- Location:
- libcaca/trunk/cucul
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
libcaca/trunk/cucul/cucul.h
r2305 r2406 277 277 __extern int cucul_canvas_set_figfont(cucul_canvas_t *, char const *); 278 278 __extern int cucul_put_figchar(cucul_canvas_t *, uint32_t); 279 /* @} */ 280 281 /** \defgroup cucul_file libcucul file IO 282 * 283 * These functions allow to read and write files in a platform-independent 284 * way. 285 * @{ */ 286 __extern cucul_file_t *cucul_file_open(char const *, const char *); 287 __extern int cucul_file_close(cucul_file_t *); 288 __extern size_t cucul_file_read(cucul_file_t *, void *, size_t); 289 __extern size_t cucul_file_write(cucul_file_t *, const void *, size_t); 290 __extern char * cucul_file_gets(cucul_file_t *, char *, int); 291 __extern int cucul_file_eof(cucul_file_t *); 279 292 /* @} */ 280 293 -
libcaca/trunk/cucul/cucul_internals.h
r2305 r2406 78 78 extern void _cucul_load_frame_info(cucul_canvas_t *); 79 79 80 /* File functions */81 extern cucul_file_t *_cucul_file_open(const char *, const char *);82 extern int _cucul_file_close(cucul_file_t *);83 extern int _cucul_file_eof(cucul_file_t *);84 extern char *_cucul_file_gets(char *, int, cucul_file_t *);85 86 80 #endif /* __CUCUL_INTERNALS_H__ */ -
libcaca/trunk/cucul/figfont.c
r2305 r2406 302 302 303 303 /* Open font: if not found, try .tlf, then .flf */ 304 f = _cucul_file_open(path, "r");304 f = cucul_file_open(path, "r"); 305 305 #if !defined __KERNEL__ && defined HAVE_SNPRINTF 306 306 … … 313 313 snprintf(altpath, 2047, "%s.tlf", path); 314 314 altpath[2047] = '\0'; 315 f = _cucul_file_open(altpath, "r");315 f = cucul_file_open(altpath, "r"); 316 316 } 317 317 if(!f) … … 319 319 snprintf(altpath, 2047, "%s.flf", path); 320 320 altpath[2047] = '\0'; 321 f = _cucul_file_open(altpath, "r");321 f = cucul_file_open(altpath, "r"); 322 322 } 323 323 #endif … … 333 333 ff->full_layout = 0; 334 334 ff->codetag_count = 0; 335 _cucul_file_gets(buf, 2048, f);335 cucul_file_gets(f, buf, 2048); 336 336 if(sscanf(buf, "%*[ft]lf2a%6s %u %u %u %i %u %u %u %u\n", hardblank, 337 337 &ff->height, &ff->baseline, &ff->max_length, … … 340 340 { 341 341 debug("figfont error: `%s' has invalid header: %s", path, buf); 342 _cucul_file_close(f);342 cucul_file_close(f); 343 343 free(ff); 344 344 seterrno(EINVAL); … … 352 352 debug("figfont error: `%s' has invalid layout %i/%u", 353 353 path, ff->old_layout, ff->full_layout); 354 _cucul_file_close(f);354 cucul_file_close(f); 355 355 free(ff); 356 356 seterrno(EINVAL); … … 362 362 /* Skip comment lines */ 363 363 for(i = 0; i < comment_lines; i++) 364 _cucul_file_gets(buf, 2048, f);364 cucul_file_gets(f, buf, 2048); 365 365 366 366 /* Read mandatory characters (32-127, 196, 214, 220, 228, 246, 252, 223) … … 369 369 ff->lookup = NULL; 370 370 371 for(i = 0, size = 0; ! _cucul_file_eof(f); ff->glyphs++)371 for(i = 0, size = 0; !cucul_file_eof(f); ff->glyphs++) 372 372 { 373 373 if((ff->glyphs % 2048) == 0) … … 386 386 else 387 387 { 388 if( _cucul_file_gets(buf, 2048, f) == NULL)388 if(cucul_file_gets(f, buf, 2048) == NULL) 389 389 break; 390 390 … … 397 397 { 398 398 for(j = 0; j < ff->height; j++) 399 _cucul_file_gets(buf, 2048, f);399 cucul_file_gets(f, buf, 2048); 400 400 continue; 401 401 } … … 424 424 data = realloc(data, size += 2048); 425 425 426 _cucul_file_gets(data + i, 2048, f);426 cucul_file_gets(f, data + i, 2048); 427 427 i = (uintptr_t)strchr(data + i, 0) - (uintptr_t)data; 428 428 } 429 429 } 430 430 431 _cucul_file_close(f);431 cucul_file_close(f); 432 432 433 433 if(ff->glyphs < EXT_GLYPHS) -
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.