Ignore:
Timestamp:
Jun 15, 2008, 3:50:02 PM (15 years ago)
Author:
Sam Hocevar
Message:
  • Export cucul_file_t operations in the public header.
  • Implement cucul_file_read() and cucul_file_write().
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.