IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 29812


Ignore:
Timestamp:
Nov 23, 2010, 1:48:00 PM (15 years ago)
Author:
eugene
Message:

update psMetadataConfigWrite and psSlurp to support I/O compresion

Location:
branches/eam_branches/ipp-20101103/psLib/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20101103/psLib/src/sys/psSlurp.c

    r29680 r29812  
    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)
     
    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;
  • branches/eam_branches/ipp-20101103/psLib/src/sys/psSlurp.h

    r15410 r29812  
    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
  • branches/eam_branches/ipp-20101103/psLib/src/types/psMetadataConfig.c

    r27056 r29812  
    16301630}
    16311631
    1632 
     1632# if (PS_SLURP_GZIP)
    16331633bool psMetadataConfigWrite(psMetadata *md,
    1634                            const char *filename)
     1634                           const char *filename, const char *compress)
    16351635{
    16361636    PS_ASSERT_METADATA_NON_NULL(md, NULL);
    16371637    PS_ASSERT_STRING_NON_EMPTY(filename, NULL);
    1638     FILE *file;
    1639     if ( !(file = fopen(filename, "w")) ) {
    1640         psError(PS_ERR_IO, true,
    1641                 "Failed to open specified file, %s\n", filename);
    1642         return false;
    1643     }
     1638
     1639    char modeString[4];
     1640
     1641    if (compress) {
     1642        if (strlen(compress) > 2) {
     1643            psError(PS_ERR_BAD_PARAMETER_VALUE, true, "invalid compression options %s", compress);
     1644            return false;
     1645        }
     1646        snprintf (modeString, 4, "w%s", compress);
     1647    } else {
     1648        strcpy (modeString, "w");
     1649    }   
     1650
     1651    gzFile file = gzopen(filename, modeString);
     1652    if (file == Z_NULL) {
     1653        psError(PS_ERR_IO, true, "Failed to open specified file, %s\n", filename);
     1654        return false;
     1655    }
     1656
    16441657    psString fileString = NULL;
    16451658    fileString = psMetadataConfigFormat(md);
    16461659    if (fileString == NULL) {
    1647         psError(PS_ERR_BAD_PARAMETER_NULL, false,
    1648                 "psMetadataConfigFormat returned NULL.\n");
    1649         return false;
    1650     }
    1651     if (fprintf(file, "%s", fileString) != strlen(fileString)) {
     1660        psError(PS_ERR_BAD_PARAMETER_NULL, false, "psMetadataConfigFormat returned NULL.\n");
     1661        return false;
     1662    }
     1663
     1664    int nbytes = gzwrite (file, fileString, strlen(fileString));
     1665    if (nbytes != strlen(fileString)) {
     1666        psError(PS_ERR_IO, true, "Failed to write contents of configuration file %s", filename);
     1667        psFree(fileString);
     1668        gzclose(file);
     1669        return false;
     1670    }
     1671    psFree(fileString);
     1672    if (gzclose(file) != Z_OK) {
     1673        psError(PS_ERR_IO, true, "Failed to close file, %s\n", filename);
     1674        return false;
     1675    }
     1676    return true;
     1677}
     1678
     1679# else
     1680
     1681bool psMetadataConfigWrite(psMetadata *md,
     1682                           const char *filename, const char *compress)
     1683{
     1684    PS_ASSERT_METADATA_NON_NULL(md, NULL);
     1685    PS_ASSERT_STRING_NON_EMPTY(filename, NULL);
     1686
     1687    FILE *file = fopen(filename, "w");
     1688    if (file == NULL) {
     1689        psError(PS_ERR_IO, true,
     1690                "Failed to open specified file, %s\n", filename);
     1691        return false;
     1692    }
     1693
     1694    psString fileString = NULL;
     1695    fileString = psMetadataConfigFormat(md);
     1696    if (fileString == NULL) {
     1697        psError(PS_ERR_BAD_PARAMETER_NULL, false, "psMetadataConfigFormat returned NULL.\n");
     1698        return false;
     1699    }
     1700
     1701    int nbytes = fwrite(fileString, 1, strlen(fileString), file);
     1702    if (nbytes != strlen(fileString)) {
    16521703        psError(PS_ERR_IO, true, "Failed to write contents of configuration file %s", filename);
    16531704        psFree(fileString);
     
    16571708    psFree(fileString);
    16581709    if (fclose(file) == EOF) {
    1659         psError(PS_ERR_IO, true,
    1660                 "Failed to close file, %s\n", filename);
     1710        psError(PS_ERR_IO, true, "Failed to close file, %s\n", filename);
    16611711        return false;
    16621712    }
    16631713    return true;
    16641714}
     1715# endif /* PS_SLURP_GZIP */
    16651716
    16661717bool psMetadataConfigPrint(FILE *stream,
  • branches/eam_branches/ipp-20101103/psLib/src/types/psMetadataConfig.h

    r11248 r29812  
    3333 *  a string, the formatting command must also be for a string. If the
    3434 *  metadata type is any other data type, printing is not allowed.
     35 *  Currently, this function does not compress the output file
    3536 *
    3637 * @return psMetadataItem* :    Pointer metadata item.
     
    8586bool psMetadataConfigWrite(
    8687    psMetadata *md,                    ///< The metadata to convert
    87     const char *filename               ///< Name of file to write
     88    const char *filename,              ///< Name of file to write
     89    const char *compress               ///< Output compression options
    8890);
    8991
    9092/** Converts a psMetadata structure (including any nested psMetadata) into a
    9193 *  configuration file formatted string that is written a file stream.
     94 *  Currently, this function does not compress the output file
    9295 *
    9396 *  @return bool:       True if successful, otherwise false.
Note: See TracChangeset for help on using the changeset viewer.