Changeset 3205
- Timestamp:
- Feb 13, 2005, 1:21:18 AM (21 years ago)
- File:
-
- 1 edited
-
trunk/archive/psdb/psDB.c (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/archive/psdb/psDB.c
r3202 r3205 8 8 * @author Joshua Hoblitt 9 9 * 10 * @version $Revision: 1.1 7$ $Name: not supported by cvs2svn $11 * @date $Date: 2005-02-1 2 02:40:21$10 * @version $Revision: 1.18 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2005-02-13 11:21:18 $ 12 12 * 13 13 * Copyright 2005 Joshua Hoblitt, University of Hawaii … … 15 15 16 16 #include <stdio.h> 17 #include <stdarg.h> 17 18 #include <string.h> 18 19 #include <math.h> … … 50 51 // general utility functions 51 52 static char *psDBIntToString(psU64 n); 52 static s ize_t psStringAppend(char **dest, const char *source);53 static s ize_t psStringPrepend(char **dest, const char *source);53 static ssize_t psStringAppend(char **dest, const char *format, ...); 54 static ssize_t psStringPrepend(char **dest, const char *format, ...); 54 55 55 56 // database utility functions … … 1237 1238 } 1238 1239 1239 static size_t psStringAppend(char **dest, const char *source) 1240 { 1241 size_t newSize; 1242 1243 // dest + source + \0 1244 newSize = strlen(*dest) + strlen(source) + 1; 1245 1246 *dest = psRealloc(*dest, newSize); 1247 1248 strncat(*dest, source, newSize); 1249 1250 return newSize; 1251 } 1252 1253 static size_t psStringPrepend(char **dest, const char *source) 1254 { 1255 size_t newSize; 1256 char *oldDest; 1257 1258 // dest + source + \0 1259 newSize = strlen(*dest) + strlen(source) + 1; 1260 1261 // backup dest 1240 static ssize_t psStringAppend(char **dest, const char *format, ...) 1241 { 1242 va_list args; 1243 size_t length; // complete string length (sans \0) 1244 size_t oldLength; // original string length (sans \0) 1245 ssize_t tailLength; // length of string to append 1246 1247 // size of existing string 1248 oldLength = strlen(*dest); 1249 1250 // find the size of the string to append 1251 va_start(args, format); 1252 // C99 guarentees vsnprintf() to work as expected with size = 0 1253 tailLength = vsnprintf(*dest, 0, format, args); 1254 va_end(args); 1255 1256 // if the new tail is zero length, return the length of the old string. if 1257 // it's a format error, return the error code. 1258 if (tailLength < 1) { 1259 return tailLength == 0 ? oldLength : tailLength; 1260 } 1261 1262 // new string length (sans \0) 1263 length = oldLength + tailLength; 1264 1265 // realloc string to string + tail + \0 1266 *dest = psRealloc(*dest, length + 1); 1267 1268 // append tail + \0 1269 va_start(args, format); 1270 vsnprintf(*dest + oldLength, tailLength + 1, format, args); 1271 va_end(args); 1272 1273 return length; 1274 } 1275 1276 static ssize_t psStringPrepend(char **dest, const char *format, ...) 1277 { 1278 va_list args; 1279 size_t length; // complete string length (sans \0) 1280 ssize_t headLength; // length of string to prepend 1281 char *oldDest; // copy of original string 1282 1283 // size of existing string 1284 length = strlen(*dest); 1285 1286 // find the size of the string to prepend 1287 va_start(args, format); 1288 // C99 guarentees vsnprintf() to work as expected with size = 0 1289 headLength = vsnprintf(*dest, 0, format, args); 1290 va_end(args); 1291 1292 // if the new head is zero length, return the length of the old string. if 1293 // it's a format error, return the error code. 1294 if (headLength < 1) { 1295 return headLength == 0 ? length : headLength; 1296 } 1297 1298 // backup original string 1262 1299 oldDest = psStringCopy(*dest); 1263 *dest = psRealloc(*dest, newSize); 1264 1265 // copy source to the beginning of the string then append the original 1266 // string 1267 strncpy(*dest, source, newSize); 1268 strncat(*dest, oldDest, newSize); 1300 1301 // new string length (sans \0) 1302 length += headLength; 1303 1304 // realloc string to head + string + \0 1305 *dest = psRealloc(*dest, length + 1); 1306 1307 // copy the new head to the beginning of string 1308 va_start(args, format); 1309 vsnprintf(*dest, length + 1, format, args); 1310 va_end(args); 1311 1312 // append the original string 1313 strncat(*dest, oldDest, length + 1); 1269 1314 1270 1315 psFree(oldDest); 1271 1316 1272 return newSize;1317 return length; 1273 1318 } 1274 1319
Note:
See TracChangeset
for help on using the changeset viewer.
