Changeset 3866 for trunk/psLib/src/db/psDB.c
- Timestamp:
- May 6, 2005, 8:15:25 AM (21 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/db/psDB.c (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/db/psDB.c
r3849 r3866 12 12 * @author Joshua Hoblitt 13 13 * 14 * @version $Revision: 1.1 5$ $Name: not supported by cvs2svn $15 * @date $Date: 2005-05-0 5 21:11:39$14 * @version $Revision: 1.16 $ $Name: not supported by cvs2svn $ 15 * @date $Date: 2005-05-06 18:15:25 $ 16 16 * 17 17 * Copyright 2005 Joshua Hoblitt, University of Hawaii … … 83 83 // string utility functions 84 84 static char *psDBIntToString(psU64 n); 85 static ssize_t psStringAppend(char **dest, const char *format, ...);86 static ssize_t psStringPrepend(char **dest, const char *format, ...);87 85 88 86 … … 1596 1594 } 1597 1595 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 append1604 1605 if (!*dest) {1606 *dest = psStringCopy("");1607 oldLength = 0;1608 } else {1609 // size of existing string1610 oldLength = strlen(*dest);1611 }1612 1613 // find the size of the string to append1614 va_start(args, format);1615 // C99 guarentees vsnprintf() to work as expected with size = 01616 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. if1620 // 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 + \01629 *dest = psRealloc(*dest, length + 1);1630 1631 // append tail + \01632 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 prepend1644 char *oldDest; // copy of original string1645 1646 if (!*dest) {1647 // makes the string backup and concatination pointless1648 *dest = psStringCopy("");1649 length = 0;1650 } else {1651 // size of existing string1652 length = strlen(*dest);1653 }1654 1655 // find the size of the string to prepend1656 va_start(args, format);1657 // C99 guarentees vsnprintf() to work as expected with size = 01658 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. if1662 // it's a format error, return the error code.1663 if (headLength < 1) {1664 return headLength == 0 ? length : headLength;1665 }1666 1667 // backup original string1668 oldDest = psStringCopy(*dest);1669 1670 // new string length (sans \0)1671 length += headLength;1672 1673 // realloc string to head + string + \01674 *dest = psRealloc(*dest, length + 1);1675 1676 // copy the new head to the beginning of string1677 va_start(args, format);1678 vsnprintf(*dest, length + 1, format, args);1679 va_end(args);1680 1681 // append the original string1682 strncat(*dest, oldDest, length + 1);1683 1684 psFree(oldDest);1685 1686 return length;1687 }1688 1689 1596 #endif // BUILD_PSDB
Note:
See TracChangeset
for help on using the changeset viewer.
