00001 /* @file psString.h 00002 * 00003 * @brief string utility functions 00004 * 00005 * String utility functions defined shall provide basic string copying 00006 * capabilities. 00007 * 00008 * @author EAM, IfA 00009 * @author Eric Van Alst, MHPCC 00010 * @author David Robbins, MHPCC 00011 * 00012 * @version $Revision: 1.36 $ $Name: $ 00013 * @date $Date: 2007/02/03 05:35:30 $ 00014 * 00015 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii 00016 */ 00017 00018 #ifndef PS_STRING_H 00019 #define PS_STRING_H 00020 00021 /// @addtogroup SysUtils System Utilities 00022 /// @{ 00023 00024 #include <sys/types.h> 00025 #include <stdarg.h> 00026 #include "psType.h" 00027 #include "psList.h" 00028 00029 /** This macro will convert the argument to a quoted string */ 00030 #define PS_STRING(S) #S 00031 00032 /** This macro returns a (static buffer containing) "file:line" */ 00033 const char *p_psFileLine(const char *file, int line); 00034 #define PS_FILE_LINE p_psFileLine(__FILE__,__LINE__) 00035 00036 /** Allocates a new psString. 00037 * 00038 * @return psString: Newly allocated string of length n. 00039 */ 00040 psString psStringAlloc( 00041 long nChar ///< Size of psString to allocate. 00042 ); 00043 00044 /** Checks the type of a particular pointer. 00045 * 00046 * Uses the appropriate deallocation function in psMemBlock to check the ptr datatype. 00047 * 00048 * @return bool: True if the pointer matches a psString structure, false otherwise. 00049 */ 00050 bool psMemCheckString( 00051 psPtr ptr ///< the pointer whose type to check 00052 ); 00053 00054 /** Copies the input string 00055 * 00056 * This function shall allocate memory to the length of the input string plus 00057 * one and copy the input string to the newly allocated memory. If 'string' 00058 * is 'NULL' then 'NULL' is returned. 00059 * 00060 * @return psString: Copy of input string 00061 */ 00062 psString psStringCopy( 00063 const char *string ///< Input string of characters to copy 00064 ); 00065 00066 /** Copies the input string up to the specified number of characters 00067 * 00068 * This function shall allocate memory to the length specified by nChar 00069 * plus one and copy the input string to the newly allocated memory. 00070 * This function will only copy nChar bytes from the input to new string, 00071 * so if the input string is larger than nChar characters the copied 00072 * string will be a substring of the input string. If the input string 00073 * is smaller than nChar bytes then the remaining bytes allocated will 00074 * be set to NULL. If 'string' is 'NULL' then 'NULL' is returned. 00075 * 00076 * @return psString: Copy of input string 00077 */ 00078 psString psStringNCopy( 00079 const char *string, ///< Input string of characters to copy 00080 unsigned int nChar ///< Number of bytes to allocate for string copy 00081 ); 00082 00083 /** Appends a format onto a string 00084 * 00085 * This function shall allocate a new string if dest is NULL. dest shall be 00086 * automatically extended to the size of the new string. 00087 * 00088 * @return ssize_t: The length of the new string (excluding '\0') 00089 */ 00090 #ifdef __GNUC__ 00091 ssize_t psStringAppend( 00092 char **dest, ///< existing string 00093 const char *format, ///< format to append 00094 ... ///< format arguments 00095 ) __attribute__((format(printf, 2, 3))); 00096 #else // __GNUC__ 00097 ssize_t psStringAppend( 00098 char **dest, ///< existing string 00099 const char *format, ///< format to append 00100 ... ///< format arguments 00101 ); 00102 #endif // __GNUC__ 00103 00104 /** Appends a format onto a string 00105 * 00106 * This function shall allocate a new string if dest is NULL. dest shall be 00107 * automatically extended to the size of the new string. 00108 * 00109 * @return ssize_t: The length of the new string (excluding '\0') 00110 */ 00111 ssize_t psStringAppendV( 00112 char **dest, ///< existing string 00113 const char *format, ///< format to append 00114 va_list ap ///< va_list of format arguments 00115 ); 00116 00117 /** Prepends a format onto a string 00118 * 00119 * This function shall allocate a new string if dest is NULL. dest shall be 00120 * automatically extended to the size of the new string. 00121 * 00122 * @return ssize_t: The length of the new string (excluding '\0') 00123 */ 00124 #ifdef __GNUC__ 00125 ssize_t psStringPrepend( 00126 char **dest, ///< existing string 00127 const char *format, ///< format to append 00128 ... ///< format arguments 00129 ) __attribute__((format(printf, 2, 3))); 00130 # else // __GNUC__ 00131 00132 ssize_t psStringPrepend( 00133 char **dest, ///< existing string 00134 const char *format, ///< format to append 00135 ... ///< format arguments 00136 ); 00137 #endif // __GNUC__ 00138 00139 /** Prepends a format onto a string 00140 * 00141 * This function shall allocate a new string if dest is NULL. dest shall be 00142 * automatically extended to the size of the new string. 00143 * 00144 * @return ssize_t: The length of the new string (excluding '\0') 00145 */ 00146 ssize_t psStringPrependV( 00147 char **dest, ///< existing string 00148 const char *format, ///< format to append 00149 va_list ap ///< va_list of format arguments 00150 ); 00151 00152 /** Procedure to split the input string into a psList of psStrings. 00153 * 00154 * The string is split at any one of the characters in splitters. Split 00155 * strings of zero length should not be included in the output list. 00156 * 00157 * @return psList*: The list of (split) psStrings. 00158 */ 00159 psList *psStringSplit( 00160 const char *string, ///< String to split 00161 const char *splitters, ///< Characters on which to split 00162 bool multipleAreSignificant ///< Are multiple occurences significant? 00163 ); 00164 00165 /** Procedure to split the input string into a psArray of psStrings. 00166 * 00167 * The string is split at any one of the characters in splitters. Split 00168 * strings of zero length should not be included in the output list. 00169 * 00170 * @return psArray*: The array of (split) psStrings. 00171 */ 00172 psArray *psStringSplitArray( 00173 const char *string, ///< String to split 00174 const char *splitters, ///< Characters on which to split 00175 bool multipleAreSignificant ///< Are multiple occurences significant? 00176 ); 00177 00178 // Given the input string, search for all copies of the key, and replace with the replacement value 00179 // the input string may be freed if not needed 00180 /** Procedure to search an input string and substitute strings where desired. 00181 * 00182 * The input string is searched for all instances of the key, which is then replaced with 00183 * the replacement value wherever found. The input string may be freed if not needed. 00184 * 00185 * @return ssize_t: the length of the new string (excluding '\0') 00186 */ 00187 ssize_t psStringSubstitute ( 00188 psString *input, ///< ptr to input string to be modified 00189 const char *replace, ///< replacement value 00190 const char *key ///< string to be replaced in input 00191 ); 00192 00193 // strip whitespace from head and tail of string 00194 /** Procedure to strip the whitespace from the head and tail of a string. 00195 * 00196 * @return size_t: Number of whitespaces removed. 00197 */ 00198 size_t psStringStrip( 00199 char *string ///< input string to be stripped. 00200 ); 00201 00202 /// Given a CVS keyword string, strip off the CVS-specific keyword to get the value 00203 psString psStringStripCVS(const char *string, ///< The string, something like "$CVSKEYWORD: Value$" 00204 const char *tagName ///< The name of the tag to remove, something like "CVSKEYWORD" 00205 ); 00206 00207 #define PS_ASSERT_STRING_NON_EMPTY(NAME, RVAL) \ 00208 if (!(NAME) || strlen(NAME) == 0) { \ 00209 psError(PS_ERR_BAD_PARAMETER_VALUE, true, \ 00210 "Error: String %s is NULL or empty.", \ 00211 #NAME); \ 00212 return(RVAL); \ 00213 } 00214 00215 #ifndef DOXYGEN 00216 #if NO_STRCASESTR 00217 // this means that the system's libc does not provide strcasestr() 00218 #define strcasestr(...) p_psstrcasestr(__VA_ARGS__) 00219 extern char *p_psstrcasestr (const char *haystack, const char *needle) 00220 __THROW __attribute_pure__ __nonnull ((1, 2)); 00221 #endif // if NO_STRCASESTR 00222 #endif // ifndef DOXYGEN 00223 00224 /// @} 00225 #endif // #ifndef PS_STRING_H
1.5.1