Changeset 2406 for libcaca


Ignore:
Timestamp:
Jun 15, 2008, 3:50:02 PM (12 years ago)
Author:
Sam Hocevar
Message:
  • Export cucul_file_t operations in the public header.
  • Implement cucul_file_read() and cucul_file_write().
Location:
libcaca/trunk/cucul
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/cucul/cucul.h

    r2305 r2406  
    277277__extern int cucul_canvas_set_figfont(cucul_canvas_t *, char const *);
    278278__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 *);
    279292/*  @} */
    280293
  • libcaca/trunk/cucul/cucul_internals.h

    r2305 r2406  
    7878extern void _cucul_load_frame_info(cucul_canvas_t *);
    7979
    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 
    8680#endif /* __CUCUL_INTERNALS_H__ */
  • libcaca/trunk/cucul/figfont.c

    r2305 r2406  
    302302
    303303    /* Open font: if not found, try .tlf, then .flf */
    304     f = _cucul_file_open(path, "r");
     304    f = cucul_file_open(path, "r");
    305305#if !defined __KERNEL__ && defined HAVE_SNPRINTF
    306306
     
    313313        snprintf(altpath, 2047, "%s.tlf", path);
    314314        altpath[2047] = '\0';
    315         f = _cucul_file_open(altpath, "r");
     315        f = cucul_file_open(altpath, "r");
    316316    }
    317317    if(!f)
     
    319319        snprintf(altpath, 2047, "%s.flf", path);
    320320        altpath[2047] = '\0';
    321         f = _cucul_file_open(altpath, "r");
     321        f = cucul_file_open(altpath, "r");
    322322    }
    323323#endif
     
    333333    ff->full_layout = 0;
    334334    ff->codetag_count = 0;
    335     _cucul_file_gets(buf, 2048, f);
     335    cucul_file_gets(f, buf, 2048);
    336336    if(sscanf(buf, "%*[ft]lf2a%6s %u %u %u %i %u %u %u %u\n", hardblank,
    337337              &ff->height, &ff->baseline, &ff->max_length,
     
    340340    {
    341341        debug("figfont error: `%s' has invalid header: %s", path, buf);
    342         _cucul_file_close(f);
     342        cucul_file_close(f);
    343343        free(ff);
    344344        seterrno(EINVAL);
     
    352352        debug("figfont error: `%s' has invalid layout %i/%u",
    353353                path, ff->old_layout, ff->full_layout);
    354         _cucul_file_close(f);
     354        cucul_file_close(f);
    355355        free(ff);
    356356        seterrno(EINVAL);
     
    362362    /* Skip comment lines */
    363363    for(i = 0; i < comment_lines; i++)
    364         _cucul_file_gets(buf, 2048, f);
     364        cucul_file_gets(f, buf, 2048);
    365365
    366366    /* Read mandatory characters (32-127, 196, 214, 220, 228, 246, 252, 223)
     
    369369    ff->lookup = NULL;
    370370
    371     for(i = 0, size = 0; !_cucul_file_eof(f); ff->glyphs++)
     371    for(i = 0, size = 0; !cucul_file_eof(f); ff->glyphs++)
    372372    {
    373373        if((ff->glyphs % 2048) == 0)
     
    386386        else
    387387        {
    388             if(_cucul_file_gets(buf, 2048, f) == NULL)
     388            if(cucul_file_gets(f, buf, 2048) == NULL)
    389389                break;
    390390
     
    397397            {
    398398                for(j = 0; j < ff->height; j++)
    399                     _cucul_file_gets(buf, 2048, f);
     399                    cucul_file_gets(f, buf, 2048);
    400400                continue;
    401401            }
     
    424424                data = realloc(data, size += 2048);
    425425
    426             _cucul_file_gets(data + i, 2048, f);
     426            cucul_file_gets(f, data + i, 2048);
    427427            i = (uintptr_t)strchr(data + i, 0) - (uintptr_t)data;
    428428        }
    429429    }
    430430
    431     _cucul_file_close(f);
     431    cucul_file_close(f);
    432432
    433433    if(ff->glyphs < EXT_GLYPHS)
  • libcaca/trunk/cucul/file.c

    r2300 r2406  
    4747#   endif
    4848    FILE *f;
     49    int readonly;
    4950};
    5051#endif
    5152
    52 cucul_file_t *_cucul_file_open(char const *path, const char *mode)
     53cucul_file_t *cucul_file_open(char const *path, const char *mode)
    5354{
    5455#if defined __KERNEL__
     
    5657#else
    5758    cucul_file_t *fp = malloc(sizeof(*fp));
     59
     60    fp->readonly = !strchr(mode, 'r');
    5861
    5962#   if defined HAVE_ZLIB_H
     
    6164    unsigned int skip_size = 0;
    6265
    63     fp->gz = gzopen(path, "rb");
     66    fp->gz = gzopen(path, fp->readonly ? "rb" : "wb");
    6467    if(!fp->gz)
    6568    {
     
    7174    fp->zip = 0;
    7275
    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        }
    105111    }
    106112#   else
    107     fp->f = fopen(path, mode);
     113    fp->f = fopen(path, fmode);
    108114
    109115    if(!fp->f)
     
    118124}
    119125
    120 int _cucul_file_close(cucul_file_t *fp)
     126int cucul_file_close(cucul_file_t *fp)
    121127{
    122128#if defined __KERNEL__
     
    135141}
    136142
    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)
     143size_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
     156size_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
     170char *cucul_file_gets(cucul_file_t *fp, char *s, int size)
    149171{
    150172#if defined __KERNEL__
     
    179201}
    180202
     203int 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
    181214#if !defined __KERNEL__ && defined HAVE_ZLIB_H
    182215static int zipread(cucul_file_t *fp, void *buf, unsigned int len)
Note: See TracChangeset for help on using the changeset viewer.