Changeset 29812
- Timestamp:
- Nov 23, 2010, 1:48:00 PM (15 years ago)
- Location:
- branches/eam_branches/ipp-20101103/psLib/src
- Files:
-
- 4 edited
-
sys/psSlurp.c (modified) (5 diffs)
-
sys/psSlurp.h (modified) (2 diffs)
-
types/psMetadataConfig.c (modified) (2 diffs)
-
types/psMetadataConfig.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ipp-20101103/psLib/src/sys/psSlurp.c
r29680 r29812 27 27 #include "psMemory.h" 28 28 29 #define SLURP_SIZE 4096 29 # define SLURP_SIZE 4096 30 31 # if (PS_SLURP_GZIP) 32 33 psString 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 30 47 31 48 psString psSlurpFD(int fd) … … 59 76 return str; 60 77 } 78 # endif 61 79 80 # if (PS_SLURP_GZIP) 81 psString 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 62 112 63 113 psString psSlurpFile(FILE *stream) … … 70 120 { 71 121 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 72 138 73 139 int fd = open(filename, O_RDONLY); … … 76 142 return NULL; 77 143 } 78 79 144 psString text = psSlurpFD(fd); 80 145 … … 84 149 return NULL; 85 150 } 151 # endif 86 152 87 153 return text; -
branches/eam_branches/ipp-20101103/psLib/src/sys/psSlurp.h
r15410 r29812 12 12 13 13 #include <psString.h> 14 15 # define PS_SLURP_GZIP 1 16 17 # if (PS_SLURP_GZIP) 18 # include <zlib.h> 19 # endif 14 20 15 21 /// @addtogroup FileIO Input/Output … … 31 37 ); 32 38 39 # if (PS_SLURP_GZIP) 40 psString psSlurpGZIP(gzFile fd); 41 # endif 42 33 43 /// @} 34 44 #endif -
branches/eam_branches/ipp-20101103/psLib/src/types/psMetadataConfig.c
r27056 r29812 1630 1630 } 1631 1631 1632 1632 # if (PS_SLURP_GZIP) 1633 1633 bool psMetadataConfigWrite(psMetadata *md, 1634 const char *filename )1634 const char *filename, const char *compress) 1635 1635 { 1636 1636 PS_ASSERT_METADATA_NON_NULL(md, NULL); 1637 1637 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 1644 1657 psString fileString = NULL; 1645 1658 fileString = psMetadataConfigFormat(md); 1646 1659 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 1681 bool 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)) { 1652 1703 psError(PS_ERR_IO, true, "Failed to write contents of configuration file %s", filename); 1653 1704 psFree(fileString); … … 1657 1708 psFree(fileString); 1658 1709 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); 1661 1711 return false; 1662 1712 } 1663 1713 return true; 1664 1714 } 1715 # endif /* PS_SLURP_GZIP */ 1665 1716 1666 1717 bool psMetadataConfigPrint(FILE *stream, -
branches/eam_branches/ipp-20101103/psLib/src/types/psMetadataConfig.h
r11248 r29812 33 33 * a string, the formatting command must also be for a string. If the 34 34 * metadata type is any other data type, printing is not allowed. 35 * Currently, this function does not compress the output file 35 36 * 36 37 * @return psMetadataItem* : Pointer metadata item. … … 85 86 bool psMetadataConfigWrite( 86 87 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 88 90 ); 89 91 90 92 /** Converts a psMetadata structure (including any nested psMetadata) into a 91 93 * configuration file formatted string that is written a file stream. 94 * Currently, this function does not compress the output file 92 95 * 93 96 * @return bool: True if successful, otherwise false.
Note:
See TracChangeset
for help on using the changeset viewer.
