Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

strutils.h

Go to the documentation of this file.
00001 /* -*- Mode: C++ -*- 00002 * Worldvisions Weaver Software: 00003 * Copyright (C) 1997-2002 Net Integration Technologies, Inc. 00004 * 00005 * Various little string functions... 00006 * 00007 */ 00008 00009 00010 #ifndef __STRUTILS_H 00011 #define __STRUTILS_H 00012 00013 #include <sys/types.h> // off_t 00014 #include <time.h> 00015 #include "wvstring.h" 00016 #include "wvstringlist.h" 00017 #include "wvhex.h" 00018 00019 #ifdef _WIN32 00020 #define strncasecmp _strnicmp 00021 #define strcasecmp _stricmp 00022 #endif 00023 00024 /** 00025 * Add character c to the end of a string after removing 00026 * terminating carriage returns/linefeeds if any. 00027 * 00028 * You need a buffer that's at least one character bigger than the 00029 * current length of the string, including the terminating NULL. 00030 */ 00031 char *terminate_string(char *string, char c); 00032 00033 /** 00034 * Trims whitespace from the beginning and end of the character string, 00035 * including carriage return / linefeed characters. Modifies the string 00036 * in place. Returns the new first character of the string, which points 00037 * either at 'string' itself or some character contained therein. 00038 * 00039 * string is allowed to be NULL; returns NULL in that case. 00040 */ 00041 char *trim_string(char *string); 00042 00043 /** 00044 * Similar to above, but trims the string starting at the first occurrence of 00045 * c. 00046 */ 00047 char *trim_string(char *string, char c); 00048 00049 /** 00050 * Replaces all whitespace characters in the string with non-breaking spaces 00051 * (&nbsp;) for use with web stuff. 00052 */ 00053 char *non_breaking(char *string); 00054 00055 /** 00056 * Replace all instances of c1 with c2 for the first 'length' characters in 00057 * 'string'. Ignores terminating NULL, so make sure you set 'length' correctly. 00058 */ 00059 void replace_char(void *string, char c1, char c2, int length); 00060 00061 /** 00062 * Snip off the first part of 'haystack' if it consists of 'needle'. 00063 */ 00064 char *snip_string(char *haystack, char *needle); 00065 00066 #ifndef _WIN32 00067 /** 00068 * In-place modify a character string so that all contained letters are 00069 * in lower case. Returns 'string'. 00070 */ 00071 char *strlwr(char *string); 00072 00073 /** 00074 * In-place modify a character string so that all contained letters are 00075 * in upper case. Returns 'string'. 00076 */ 00077 char *strupr(char *string); 00078 00079 #endif 00080 00081 /** Returns true if all characters in 'string' are isalnum() (alphanumeric). */ 00082 bool is_word(const char *string); 00083 00084 /** 00085 * Produce a hexadecimal dump of the data buffer in 'buf' of length 'len'. 00086 * It is formatted with 16 bytes per line; each line has an address offset, 00087 * hex representation, and printable representation. 00088 * 00089 * This is used mostly for debugging purposes. You can send the returned 00090 * WvString object directly to a WvLog or any other WvStream for output. 00091 */ 00092 WvString hexdump_buffer(const void *buf, size_t len, bool charRep = true); 00093 00094 /** 00095 * Returns true if 'c' is a newline or carriage return character. 00096 * Increases code readability a bit. 00097 */ 00098 bool isnewline(char c); 00099 00100 /** 00101 * Converts escaped characters (things like %20 etc.) from web URLS into their 00102 * normal ASCII representations. 00103 */ 00104 WvString web_unescape(const char *str); 00105 00106 00107 /** 00108 * Converts all those pesky spaces, colons, and other nasties into nice unreadable 00109 * Quasi-Unicode codes 00110 */ 00111 WvString url_encode(WvStringParm stuff); 00112 00113 00114 /** 00115 * Returns an RFC822-compatible date made out of _when, or, if _when < 0, out of 00116 * the current time. 00117 */ 00118 WvString rfc822_date(time_t _when = -1); 00119 00120 /** Returns an RFC1123-compatible date made out of _when */ 00121 WvString rfc1123_date(time_t _when); 00122 00123 /** 00124 * Similar to crypt(), but this randomly selects its own salt. 00125 * This function is defined in strcrypt.cc. 00126 */ 00127 WvString passwd_crypt(const char *str); 00128 00129 /** 00130 * Returns a string with a backslash in front of every non alphanumeric 00131 * character in s1. 00132 */ 00133 WvString backslash_escape(WvStringParm s1); 00134 00135 /** How many times does 'c' occur in "s"? */ 00136 int strcount(WvStringParm s, const char c); 00137 00138 /** 00139 * Example: encode_hostname_as_DN("www.fizzle.com") 00140 * will result in dc=www,dc=fizzle,dc=com,cn=www.fizzle.com 00141 */ 00142 WvString encode_hostname_as_DN(WvStringParm hostname); 00143 00144 /** 00145 * Given a hostname, turn it into a "nice" one. It has to start with a 00146 * letter/number, END with a letter/number, have underscores converted to 00147 * hyphens, and have no more than one hyphen in a row. If we can't do this 00148 * and have any sort of answer, return "UNKNOWN". 00149 */ 00150 WvString nice_hostname(WvStringParm name); 00151 00152 /** 00153 * Take a full path/file name and splits it up into respective pathname and 00154 * filename. This can also be useful for splitting the toplevel directory off a 00155 * path. 00156 */ 00157 WvString getfilename(WvStringParm fullname); 00158 WvString getdirname(WvStringParm fullname); 00159 00160 /** 00161 * Given a number of blocks and a blocksize (default==1 byte), return a 00162 * WvString containing a human-readable representation of blocks*blocksize. 00163 */ 00164 WvString sizetoa(long long blocks, int blocksize=1); 00165 00166 /** 00167 * Finds a string in an array and returns its index. 00168 * Returns -1 if not found. 00169 */ 00170 int lookup(const char *str, const char * const *table, 00171 bool case_sensitive = false); 00172 00173 /** 00174 * Splits a string and adds each substring to a collection. 00175 * coll : the collection of strings to add to 00176 * _s : the string to split 00177 * splitchars : the set of delimiter characters 00178 * limit : the maximum number of elements to split 00179 */ 00180 template<class StringCollection> 00181 void strcoll_split(StringCollection &coll, WvStringParm _s, 00182 const char *splitchars = " \t", int limit = 0) 00183 { 00184 WvString s(_s); 00185 char *sptr = s.edit(), *eptr, oldc; 00186 00187 while (sptr && *sptr) 00188 { 00189 --limit; 00190 if (limit) 00191 { 00192 sptr += strspn(sptr, splitchars); 00193 eptr = sptr + strcspn(sptr, splitchars); 00194 } 00195 else 00196 { 00197 sptr += strspn(sptr, splitchars); 00198 eptr = sptr + strlen(sptr); 00199 } 00200 00201 oldc = *eptr; 00202 *eptr = 0; 00203 00204 WvString *newstr = new WvString(sptr); 00205 coll.add(newstr, true); 00206 00207 *eptr = oldc; 00208 sptr = eptr; 00209 } 00210 } 00211 00212 /** 00213 * Splits a string and adds each substring to a collection. 00214 * this behaves differently in that it actually delimits the 00215 * pieces as fields and returns them, it doesn't treat multiple 00216 * delimeters as one and skip them. 00217 * 00218 * ie., parm1::parm2 -> 'parm1','','parm2' when delimited with ':' 00219 * 00220 * coll : the collection of strings to add to 00221 * _s : the string to split 00222 * splitchars : the set of delimiter characters 00223 * limit : the maximum number of elements to split 00224 */ 00225 template<class StringCollection> 00226 void strcoll_splitstrict(StringCollection &coll, WvStringParm _s, 00227 const char *splitchars = " \t", int limit = 0) 00228 { 00229 WvString s(_s); 00230 char *sptr = s.edit(), *eptr, oldc; 00231 00232 bool start = true; 00233 while (sptr && *sptr) 00234 { 00235 int len = strspn(sptr,splitchars); 00236 sptr += len; 00237 00238 --limit; 00239 00240 for (bool unseen = true; len > 0 && limit; (len -= strlen(splitchars)),--limit) 00241 { 00242 if ((!start) && (unseen)) 00243 { unseen = false; continue; } 00244 00245 coll.add(new WvString(""), true); 00246 } 00247 00248 start = false; 00249 00250 if (limit) 00251 eptr = sptr + strcspn(sptr,splitchars); 00252 else 00253 eptr = sptr + strlen(sptr); 00254 00255 oldc = *eptr; 00256 *eptr = '\0'; 00257 00258 if (limit) 00259 coll.add(new WvString(sptr), true); 00260 00261 *eptr = oldc; 00262 sptr = eptr; 00263 } 00264 } 00265 00266 /** 00267 * Concatenates all strings in a collection and returns the result. 00268 * coll : the collection of strings to read from 00269 * joinchars : the delimiter string to insert between strings 00270 */ 00271 template<class StringCollection> 00272 WvString strcoll_join(const StringCollection &coll, 00273 const char *joinchars = " \t") 00274 { 00275 size_t joinlen = strlen(joinchars); 00276 size_t totlen = 1; 00277 typename StringCollection::Iter s( 00278 const_cast<StringCollection&>(coll)); 00279 for (s.rewind(); s.next(); ) 00280 { 00281 if (s->cstr()) 00282 totlen += strlen(s->cstr()); 00283 totlen += joinlen; 00284 } 00285 totlen -= joinlen; // no join chars at tail 00286 00287 WvString total; 00288 total.setsize(totlen); 00289 00290 char *te = total.edit(); 00291 te[0] = 0; 00292 bool first = true; 00293 for (s.rewind(); s.next(); ) 00294 { 00295 if (first) 00296 first = false; 00297 else 00298 strcat(te, joinchars); 00299 if (s->cstr()) 00300 strcat(te, s->cstr()); 00301 } 00302 return total; 00303 } 00304 00305 /** 00306 * Replace any instances of "a" with "b" in "s". Kind of like sed, only 00307 * much dumber. 00308 */ 00309 WvString strreplace(WvStringParm s, WvStringParm a, WvStringParm b); 00310 00311 /** 00312 * Replace any consecutive instances of character c with a single one 00313 */ 00314 WvString undupe(WvStringParm s, char c); 00315 00316 WvString hostname(); 00317 00318 WvString fqdomainname(); 00319 00320 /** 00321 * Inserts SI-style spacing into a number 00322 * (eg passing 9876543210 returns "9 876 543 210") 00323 */ 00324 WvString metriculate(const off_t i); 00325 00326 #endif // __STRUTILS_H

Generated on Tue Oct 5 01:09:19 2004 for WvStreams by doxygen 1.3.7