Changeset 29933 for trunk/psLib/src/sys/psSlurp.c
- Timestamp:
- Dec 5, 2010, 9:28:28 PM (15 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/sys/psSlurp.c (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sys/psSlurp.c
r12072 r29933 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) … … 38 55 // increase the allocated string size 39 56 size += SLURP_SIZE; 40 str = ps Realloc(str, size);57 str = psStringRealloc(str, size); 41 58 42 59 // read a block from the stream … … 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;
Note:
See TracChangeset
for help on using the changeset viewer.
