IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 29933


Ignore:
Timestamp:
Dec 5, 2010, 9:28:28 PM (15 years ago)
Author:
eugene
Message:

add support for gziped I/O; use psStringRealloc in psSlurp to return a valid psString

Location:
trunk/psLib/src/sys
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/sys/psSlurp.c

    r12072 r29933  
    2727#include "psMemory.h"
    2828
    29 #define SLURP_SIZE 4096
     29# define SLURP_SIZE 4096
     30
     31# if (PS_SLURP_GZIP)
     32
     33psString psSlurpFD(int fd) {
     34
     35 gzFile file = gzdopen (fd, "r");
     36 if (file == Z_NULL) {
     37     psError(PS_ERR_IO, true, "Failed to open file\n");
     38     return NULL;
     39 }
     40 
     41 psString str = psSlurpGZIP(file);
     42
     43 return str;
     44}
     45
     46# else
    3047
    3148psString psSlurpFD(int fd)
     
    3855        // increase the allocated string size
    3956        size += SLURP_SIZE;
    40         str = psRealloc(str, size);
     57        str = psStringRealloc(str, size);
    4158
    4259        // read a block from the stream
     
    5976    return str;
    6077}
     78# endif
    6179
     80# if (PS_SLURP_GZIP)
     81psString psSlurpGZIP(gzFile fd)
     82{
     83    psString str = NULL;                // String to which to write
     84    size_t size  = 1;                   // bytes allocated -  make sure there is room for '\0'
     85    size_t used = 0;                    // bytes actually used
     86    ssize_t bytes;                      // Number of bytes read
     87    do {
     88        // increase the allocated string size
     89        size += SLURP_SIZE;
     90        str = psStringRealloc(str, size);
     91
     92        // read a block from the stream
     93        bytes = gzread(fd, str + used, SLURP_SIZE);
     94        if (bytes < 0) {
     95            // it's an error
     96            psError(PS_ERR_IO, true, "slurp failed on read");
     97            psFree(str);
     98            return NULL;
     99        }
     100
     101        // Increase the size of the known string
     102        used += bytes;
     103
     104    } while (bytes != 0);
     105
     106    // append '\0' to the end of the string
     107    str[used] = '\0';
     108
     109    return str;
     110}
     111# endif
    62112
    63113psString psSlurpFile(FILE *stream)
     
    70120{
    71121    PS_ASSERT_PTR_NON_NULL(filename, NULL);
     122   
     123# if (PS_SLURP_GZIP)
     124    gzFile fd = gzopen(filename, "r");
     125    if (fd == Z_NULL) {
     126        psError(PS_ERR_IO, true, "Failed to open specified file, %s\n", filename);
     127        return NULL;
     128    }
     129    psString text = psSlurpGZIP(fd);
     130
     131    if (gzclose(fd) != Z_OK) {
     132        psError(PS_ERR_IO, true, "Failed to close specified file, %s\n", filename);
     133        psFree(text);
     134        return NULL;
     135    }
     136
     137# else
    72138
    73139    int fd = open(filename, O_RDONLY);
     
    76142        return NULL;
    77143    }
    78 
    79144    psString text = psSlurpFD(fd);
    80145
     
    84149        return NULL;
    85150    }
     151# endif
    86152
    87153    return text;
  • trunk/psLib/src/sys/psSlurp.h

    r15410 r29933  
    1212
    1313#include <psString.h>
     14
     15# define PS_SLURP_GZIP 1
     16
     17# if (PS_SLURP_GZIP)
     18# include <zlib.h>
     19# endif
    1420
    1521/// @addtogroup FileIO Input/Output
     
    3137                    );
    3238
     39# if (PS_SLURP_GZIP)
     40psString psSlurpGZIP(gzFile fd);
     41# endif
     42
    3343/// @}
    3444#endif
Note: See TracChangeset for help on using the changeset viewer.