IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 6, 2005, 8:15:25 AM (21 years ago)
Author:
desonia
Message:

Bug #395 - consolidated psString functions introduced for psDB to psString.[ch]

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/sysUtils/psString.c

    r3264 r3866  
    99 *  @author Eric Van Alst, MHPCC
    1010 *
    11  *  @version $Revision: 1.15 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2005-02-17 19:26:24 $
     11 *  @version $Revision: 1.16 $ $Name: not supported by cvs2svn $
     12 *  @date $Date: 2005-05-06 18:15:25 $
    1313 *
    1414 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    5555    return returnValue;
    5656}
     57
     58ssize_t psStringAppend(char **dest, const char *format, ...)
     59{
     60    va_list         args;
     61    size_t          length;             // complete string length (sans \0)
     62    size_t          oldLength;          // original string length (sans \0)
     63    ssize_t         tailLength;         // length of string to append
     64
     65    if (!*dest) {
     66        *dest = psStringCopy("");
     67        oldLength = 0;
     68    } else {
     69        // size of existing string
     70        oldLength = strlen(*dest);
     71    }
     72
     73    // find the size of the string to append
     74    va_start(args, format);
     75    // C99 guarentees vsnprintf() to work as expected with size = 0
     76    tailLength = vsnprintf(*dest, 0, format, args);
     77    va_end(args);
     78
     79    // if the new tail is zero length, return the length of the old string.  if
     80    // it's a format error, return the error code.
     81    if (tailLength < 1) {
     82        return tailLength == 0 ? oldLength : tailLength;
     83    }
     84
     85    // new string length (sans \0)
     86    length = oldLength + tailLength;
     87
     88    // realloc string to string + tail + \0
     89    *dest = psRealloc(*dest, length + 1);
     90
     91    // append tail + \0
     92    va_start(args, format);
     93    vsnprintf(*dest + oldLength, tailLength + 1, format, args);
     94    va_end(args);
     95
     96    return length;
     97}
     98
     99ssize_t psStringPrepend(char **dest, const char *format, ...)
     100{
     101    va_list         args;
     102    size_t          length;             // complete string length (sans \0)
     103    ssize_t         headLength;         // length of string to prepend
     104    char            *oldDest;           // copy of original string
     105
     106    if (!*dest) {
     107        // makes the string backup and concatination pointless
     108        *dest = psStringCopy("");
     109        length = 0;
     110    } else {
     111        // size of existing string
     112        length = strlen(*dest);
     113    }
     114
     115    // find the size of the string to prepend
     116    va_start(args, format);
     117    // C99 guarentees vsnprintf() to work as expected with size = 0
     118    headLength = vsnprintf(*dest, 0, format, args);
     119    va_end(args);
     120
     121    // if the new head is zero length, return the length of the old string.  if
     122    // it's a format error, return the error code.
     123    if (headLength < 1) {
     124        return headLength == 0 ? length : headLength;
     125    }
     126
     127    // backup original string
     128    oldDest = psStringCopy(*dest);
     129
     130    // new string length (sans \0)
     131    length += headLength;
     132
     133    // realloc string to head + string + \0
     134    *dest = psRealloc(*dest, length + 1);
     135
     136    // copy the new head to the beginning of string
     137    va_start(args, format);
     138    vsnprintf(*dest, length + 1, format, args);
     139    va_end(args);
     140
     141    // append the original string
     142    strncat(*dest, oldDest, length + 1);
     143
     144    psFree(oldDest);
     145
     146    return length;
     147}
Note: See TracChangeset for help on using the changeset viewer.