IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 3205


Ignore:
Timestamp:
Feb 13, 2005, 1:21:18 AM (21 years ago)
Author:
jhoblitt
Message:

change psStringAppend() & psStringPrepend() to use stdarg

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/archive/psdb/psDB.c

    r3202 r3205  
    88 *  @author Joshua Hoblitt
    99 *
    10  *  @version $Revision: 1.17 $ $Name: not supported by cvs2svn $
    11  *  @date $Date: 2005-02-12 02:40:21 $
     10 *  @version $Revision: 1.18 $ $Name: not supported by cvs2svn $
     11 *  @date $Date: 2005-02-13 11:21:18 $
    1212 *
    1313 *  Copyright 2005 Joshua Hoblitt, University of Hawaii
     
    1515
    1616#include <stdio.h>
     17#include <stdarg.h>
    1718#include <string.h>
    1819#include <math.h>
     
    5051// general utility functions
    5152static char    *psDBIntToString(psU64 n);
    52 static size_t   psStringAppend(char **dest, const char *source);
    53 static size_t   psStringPrepend(char **dest, const char *source);
     53static ssize_t  psStringAppend(char **dest, const char *format, ...);
     54static ssize_t psStringPrepend(char **dest, const char *format, ...);
    5455
    5556// database utility functions
     
    12371238}
    12381239
    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
     1240static 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
     1276static 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
    12621299    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);
    12691314
    12701315    psFree(oldDest);
    12711316
    1272     return newSize;
     1317    return length;
    12731318}
    12741319
Note: See TracChangeset for help on using the changeset viewer.