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/dataIO/psDB.c

    r3849 r3866  
    1212 *  @author Joshua Hoblitt
    1313 *
    14  *  @version $Revision: 1.15 $ $Name: not supported by cvs2svn $
    15  *  @date $Date: 2005-05-05 21:11:39 $
     14 *  @version $Revision: 1.16 $ $Name: not supported by cvs2svn $
     15 *  @date $Date: 2005-05-06 18:15:25 $
    1616 *
    1717 *  Copyright 2005 Joshua Hoblitt, University of Hawaii
     
    8383// string utility functions
    8484static char    *psDBIntToString(psU64 n);
    85 static ssize_t  psStringAppend(char **dest, const char *format, ...);
    86 static ssize_t  psStringPrepend(char **dest, const char *format, ...);
    8785
    8886
     
    15961594}
    15971595
    1598 static ssize_t psStringAppend(char **dest, const char *format, ...)
    1599 {
    1600     va_list         args;
    1601     size_t          length;             // complete string length (sans \0)
    1602     size_t          oldLength;          // original string length (sans \0)
    1603     ssize_t         tailLength;         // length of string to append
    1604 
    1605     if (!*dest) {
    1606         *dest = psStringCopy("");
    1607         oldLength = 0;
    1608     } else {
    1609         // size of existing string
    1610         oldLength = strlen(*dest);
    1611     }
    1612 
    1613     // find the size of the string to append
    1614     va_start(args, format);
    1615     // C99 guarentees vsnprintf() to work as expected with size = 0
    1616     tailLength = vsnprintf(*dest, 0, format, args);
    1617     va_end(args);
    1618 
    1619     // if the new tail is zero length, return the length of the old string.  if
    1620     // it's a format error, return the error code.
    1621     if (tailLength < 1) {
    1622         return tailLength == 0 ? oldLength : tailLength;
    1623     }
    1624 
    1625     // new string length (sans \0)
    1626     length = oldLength + tailLength;
    1627 
    1628     // realloc string to string + tail + \0
    1629     *dest = psRealloc(*dest, length + 1);
    1630 
    1631     // append tail + \0
    1632     va_start(args, format);
    1633     vsnprintf(*dest + oldLength, tailLength + 1, format, args);
    1634     va_end(args);
    1635 
    1636     return length;
    1637 }
    1638 
    1639 static ssize_t psStringPrepend(char **dest, const char *format, ...)
    1640 {
    1641     va_list         args;
    1642     size_t          length;             // complete string length (sans \0)
    1643     ssize_t         headLength;         // length of string to prepend
    1644     char            *oldDest;           // copy of original string
    1645 
    1646     if (!*dest) {
    1647         // makes the string backup and concatination pointless
    1648         *dest = psStringCopy("");
    1649         length = 0;
    1650     } else {
    1651         // size of existing string
    1652         length = strlen(*dest);
    1653     }
    1654 
    1655     // find the size of the string to prepend
    1656     va_start(args, format);
    1657     // C99 guarentees vsnprintf() to work as expected with size = 0
    1658     headLength = vsnprintf(*dest, 0, format, args);
    1659     va_end(args);
    1660 
    1661     // if the new head is zero length, return the length of the old string.  if
    1662     // it's a format error, return the error code.
    1663     if (headLength < 1) {
    1664         return headLength == 0 ? length : headLength;
    1665     }
    1666 
    1667     // backup original string
    1668     oldDest = psStringCopy(*dest);
    1669 
    1670     // new string length (sans \0)
    1671     length += headLength;
    1672 
    1673     // realloc string to head + string + \0
    1674     *dest = psRealloc(*dest, length + 1);
    1675 
    1676     // copy the new head to the beginning of string
    1677     va_start(args, format);
    1678     vsnprintf(*dest, length + 1, format, args);
    1679     va_end(args);
    1680 
    1681     // append the original string
    1682     strncat(*dest, oldDest, length + 1);
    1683 
    1684     psFree(oldDest);
    1685 
    1686     return length;
    1687 }
    1688 
    16891596#endif // BUILD_PSDB
Note: See TracChangeset for help on using the changeset viewer.