Ticket #395: pslib-0.5.0-string_conslidation.patch
| File pslib-0.5.0-string_conslidation.patch, 8.9 KB (added by , 21 years ago) |
|---|
-
pslib/src/dataIO/psDB.
--------------------- PatchSet 235 Date: 2005/05/06 00:42:24 Author: jhoblitt Branch: HEAD Tag: (none) Log: move psStringAppend() & psStringPrepend() from psDB.c to psString.c and make them public Members: src/dataIO/psDB.c:1.1->1.2 src/sysUtils/psString.c:1.1->1.2 src/sysUtils/psString.h:1.1->1.2 diff -u pslib/src/dataIO/psDB.c:1.1 pslib/src/dataIO/psDB.c:1.2
old new 11 11 * @author Aaron Culliney 12 12 * @author Joshua Hoblitt 13 13 * 14 * @version $Revision: 1. 1$ $Name: $15 * @date $Date: 2005/0 4/06 01:12:58$14 * @version $Revision: 1.2 $ $Name: $ 15 * @date $Date: 2005/05/06 00:42:24 $ 16 16 * 17 17 * Copyright 2005 Joshua Hoblitt, University of Hawaii 18 18 */ … … 82 82 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 89 87 // public functions … … 1611 1609 return string; 1612 1610 } 1613 1611 1614 static ssize_t psStringAppend(char **dest, const char *format, ...)1615 {1616 va_list args;1617 size_t length; // complete string length (sans \0)1618 size_t oldLength; // original string length (sans \0)1619 ssize_t tailLength; // length of string to append1620 1621 if (!*dest) {1622 *dest = psStringCopy("");1623 oldLength = 0;1624 } else {1625 // size of existing string1626 oldLength = strlen(*dest);1627 }1628 1629 // find the size of the string to append1630 va_start(args, format);1631 // C99 guarentees vsnprintf() to work as expected with size = 01632 tailLength = vsnprintf(*dest, 0, format, args);1633 va_end(args);1634 1635 // if the new tail is zero length, return the length of the old string. if1636 // it's a format error, return the error code.1637 if (tailLength < 1) {1638 return tailLength == 0 ? oldLength : tailLength;1639 }1640 1641 // new string length (sans \0)1642 length = oldLength + tailLength;1643 1644 // realloc string to string + tail + \01645 *dest = psRealloc(*dest, length + 1);1646 1647 // append tail + \01648 va_start(args, format);1649 vsnprintf(*dest + oldLength, tailLength + 1, format, args);1650 va_end(args);1651 1652 return length;1653 }1654 1655 static ssize_t psStringPrepend(char **dest, const char *format, ...)1656 {1657 va_list args;1658 size_t length; // complete string length (sans \0)1659 ssize_t headLength; // length of string to prepend1660 char *oldDest; // copy of original string1661 1662 if (!*dest) {1663 // makes the string backup and concatination pointless1664 *dest = psStringCopy("");1665 length = 0;1666 } else {1667 // size of existing string1668 length = strlen(*dest);1669 }1670 1671 // find the size of the string to prepend1672 va_start(args, format);1673 // C99 guarentees vsnprintf() to work as expected with size = 01674 headLength = vsnprintf(*dest, 0, format, args);1675 va_end(args);1676 1677 // if the new head is zero length, return the length of the old string. if1678 // it's a format error, return the error code.1679 if (headLength < 1) {1680 return headLength == 0 ? length : headLength;1681 }1682 1683 // backup original string1684 oldDest = psStringCopy(*dest);1685 1686 // new string length (sans \0)1687 length += headLength;1688 1689 // realloc string to head + string + \01690 *dest = psRealloc(*dest, length + 1);1691 1692 // copy the new head to the beginning of string1693 va_start(args, format);1694 vsnprintf(*dest, length + 1, format, args);1695 va_end(args);1696 1697 // append the original string1698 strncat(*dest, oldDest, length + 1);1699 1700 psFree(oldDest);1701 1702 return length;1703 }1704 1705 1612 #endif // BUILD_PSDB -
pslib/src/sysUtils/psString.
diff -u pslib/src/sysUtils/psString.c:1.1 pslib/src/sysUtils/psString.c:1.2
old new 8 8 * 9 9 * @author Eric Van Alst, MHPCC 10 10 * 11 * @version $Revision: 1. 1$ $Name: $12 * @date $Date: 2005/0 2/17 19:26:24$11 * @version $Revision: 1.2 $ $Name: $ 12 * @date $Date: 2005/05/06 00:42:27 $ 13 13 * 14 14 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii 15 15 */ … … 54 54 // Return the string pointer 55 55 return returnValue; 56 56 } 57 58 ssize_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 99 ssize_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 } -
pslib/src/sysUtils/psString.
diff -u pslib/src/sysUtils/psString.h:1.1 pslib/src/sysUtils/psString.h:1.2
old new 9 9 * 10 10 * @author Eric Van Alst, MHPCC 11 11 * 12 * @version $Revision: 1. 1$ $Name: $13 * @date $Date: 2005/0 2/17 19:26:24$12 * @version $Revision: 1.2 $ $Name: $ 13 * @date $Date: 2005/05/06 00:42:27 $ 14 14 * 15 15 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii 16 16 */ … … 18 18 #ifndef PS_STRING_H 19 19 #define PS_STRING_H 20 20 21 #include <sys/types.h> 21 22 #include "psType.h" 22 23 23 24 /** This macro will convert the argument to a quoted string */ … … 66 67 /**< Number of bytes to allocate for string copy */ 67 68 ); 68 69 70 /** Appends a format onto a string 71 * 72 * This function shall allocate a new string if dest is NULL. dest shall be 73 * automatically extended to the size of the new string. 74 * 75 * @return The length of the new string (excluding '\0') 76 */ 77 78 ssize_t psStringAppend( 79 char **dest, ///< existing string 80 const char *format, ///< format to append 81 ... ///< format arguments 82 ); 83 84 /** Prepends a format onto a string 85 * 86 * This function shall allocate a new string if dest is NULL. dest shall be 87 * automatically extended to the size of the new string. 88 * 89 * @return The length of the new string (excluding '\0') 90 */ 91 92 ssize_t psStringPrepend( 93 char **dest, ///< existing string 94 const char *format, ///< format to append 95 ... ///< format arguments 96 ); 97 69 98 /* @} */// Doxygen - End of SystemGroup Functions 70 99 71 100 #endif
