wvstrutils.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  * FIXME: and some other assorted crap that belongs anywhere but here.
00008  */
00009 #ifndef __WVSTRUTILS_H
00010 #define __WVSTRUTILS_H
00011 
00012 #include <sys/types.h> // for off_t
00013 #include <time.h>
00014 #include <ctype.h>
00015 #include "wvstring.h"
00016 #include "wvstringlist.h"
00017 #include "wvhex.h"
00018 #ifndef _WIN32
00019 #include "wvregex.h"
00020 #endif
00021 
00034 char *terminate_string(char *string, char c);
00035 
00044 char *trim_string(char *string);
00045 
00050 char *trim_string(char *string, char c);
00051 
00065 WvString spacecat(WvStringParm a, WvStringParm b, char sep = ' ',
00066                   bool onesep = false);
00067 
00068     
00073 char *non_breaking(char *string);
00074 
00079 void replace_char(void *string, char c1, char c2, int length);
00080 
00084 char *snip_string(char *haystack, char *needle);
00085 
00086 #ifndef _WIN32
00087 
00091 char *strlwr(char *string);
00092 
00097 char *strupr(char *string);
00098 
00099 #endif
00100 
00102 bool is_word(const char *string);
00103 
00112 WvString hexdump_buffer(const void *buf, size_t len, bool charRep = true);
00113 
00118 bool isnewline(char c);
00119 
00127 WvString web_unescape(const char *str, bool no_space = false);
00128 
00129 
00134 WvString url_encode(WvStringParm stuff);
00135  
00136 
00140 WvString  diff_dates(time_t t1, time_t t2);
00141 
00142 
00147 WvString rfc822_date(time_t _when = -1);
00148 
00150 WvString rfc1123_date(time_t _when);
00151 
00153 WvString local_date(time_t _when = -1);
00154 
00156 WvString intl_time(time_t _when = -1);
00157 
00159 WvString intl_date(time_t _when = -1);
00160 
00162 WvString intl_datetime(time_t _when = -1);
00163 
00164 #ifndef _WIN32
00165 
00170 WvString passwd_crypt(const char *str);
00171 
00172 #endif
00173 
00178 WvString passwd_md5(const char *str);
00179 
00184 WvString backslash_escape(WvStringParm s1);
00185 
00187 int strcount(WvStringParm s, const char c);
00188 
00193 WvString encode_hostname_as_DN(WvStringParm hostname);
00194 
00201 WvString nice_hostname(WvStringParm name);
00202 
00208 WvString getfilename(WvStringParm fullname);
00209 WvString getdirname(WvStringParm fullname);
00210 
00211 /*
00212  * Possible rounding methods for numbers -- remember from school?
00213  */
00214 enum RoundingMethod
00215 {
00216     ROUND_DOWN,
00217     ROUND_DOWN_AT_POINT_FIVE,
00218     ROUND_UP_AT_POINT_FIVE,
00219     ROUND_UP
00220 };
00221 
00227 WvString sizetoa(unsigned long long blocks, unsigned long blocksize = 1,
00228                  RoundingMethod rounding_method = ROUND_UP_AT_POINT_FIVE);
00229 
00234 WvString sizektoa(unsigned long long kbytes,
00235                   RoundingMethod rounding_method = ROUND_UP_AT_POINT_FIVE);
00236 
00242 WvString sizeitoa(unsigned long long blocks, unsigned long blocksize = 1,
00243                   RoundingMethod rounding_method = ROUND_UP_AT_POINT_FIVE);
00244 
00249 WvString sizekitoa(unsigned long long kbytes,
00250                    RoundingMethod rounding_method = ROUND_UP_AT_POINT_FIVE);
00251 
00255 WvString secondstoa(unsigned int total_seconds);
00256 
00261 int lookup(const char *str, const char * const *table,
00262     bool case_sensitive = false);
00263 
00271 template<class StringCollection>
00272 void strcoll_split(StringCollection &coll, WvStringParm _s,
00273     const char *splitchars = " \t", int limit = 0)
00274 {
00275     WvString s(_s);
00276     char *sptr = s.edit(), *eptr, oldc;
00277     
00278     // Simple if statement to catch (and add) empty (but not NULL) strings.
00279     if (sptr && !*sptr )
00280     {   
00281         WvString *emptyString = new WvString("");
00282         coll.add(emptyString, true);
00283     }
00284     
00285     // Needed to catch delimeters at the beginning of the string.
00286     bool firstrun = true;
00287 
00288     while (sptr && *sptr)
00289     {
00290         --limit;
00291 
00292         if (firstrun)
00293         {   
00294             firstrun = false;
00295         }
00296         else
00297         {
00298             sptr += strspn(sptr, splitchars);
00299         }
00300 
00301         if (limit)
00302         {
00303             eptr = sptr + strcspn(sptr, splitchars);
00304         }
00305         else
00306         {
00307             eptr = sptr + strlen(sptr);
00308         }
00309         
00310         oldc = *eptr;
00311         *eptr = 0;
00312         
00313         WvString *newstr = new WvString(sptr);
00314         coll.add(newstr, true);
00315         
00316         *eptr = oldc;
00317         sptr = eptr;
00318     }
00319 }
00320 
00321 
00335 template<class StringCollection>
00336 void strcoll_splitstrict(StringCollection &coll, WvStringParm _s,
00337     const char *splitchars = " \t", int limit = 0)
00338 {
00339     WvString s(_s);
00340     char *cur = s.edit();
00341 
00342     if (!cur) return;
00343 
00344     for (;;)
00345     {
00346         --limit;
00347         if (!limit)
00348         {
00349             coll.add(new WvString(cur), true);
00350             break;
00351         }
00352 
00353         int len = strcspn(cur, splitchars);
00354 
00355         char tmp = cur[len];
00356         cur[len] = 0;
00357         coll.add(new WvString(cur), true);
00358         cur[len] = tmp;
00359 
00360         if (!cur[len]) break;
00361         cur += len + 1;
00362     }
00363 }
00364 
00365 
00366 #ifndef _WIN32 // don't have regex on win32
00367 
00374 template<class StringCollection>
00375 void strcoll_split(StringCollection &coll, WvStringParm s,
00376     const WvRegex &regex, int limit = 0)
00377 {
00378     int start = 0;
00379     int match_start, match_end;
00380     int count = 0;
00381     
00382     while ((limit == 0 || count < limit)
00383             && regex.continuable_match(&s[start], match_start, match_end)
00384             && match_end > 0)
00385     {
00386         WvString *substr = new WvString;
00387         int len = match_start;
00388         substr->setsize(len+1);
00389         memcpy(substr->edit(), &s[start], len);
00390         substr->edit()[len] = '\0';
00391         coll.add(substr, true);
00392         start += match_end;
00393         ++count;
00394     }
00395     
00396     if (limit == 0 || count < limit)
00397     {
00398         WvString *last = new WvString(&s[start]);
00399         last->unique();
00400         coll.add(last, true);
00401     }
00402 }
00403 #endif
00404 
00405 
00411 template<class StringCollection>
00412 WvString strcoll_join(const StringCollection &coll,
00413     const char *joinchars = " \t")
00414 {
00415     size_t joinlen = strlen(joinchars);
00416     size_t totlen = 1;
00417     typename StringCollection::Iter s(
00418         const_cast<StringCollection&>(coll));
00419     for (s.rewind(); s.next(); )
00420     {
00421         if (s->cstr())
00422             totlen += strlen(s->cstr());
00423         totlen += joinlen;
00424     }
00425     totlen -= joinlen; // no join chars at tail
00426     
00427     WvString total;
00428     total.setsize(totlen);
00429 
00430     char *te = total.edit();
00431     te[0] = 0;
00432     bool first = true;
00433     for (s.rewind(); s.next(); )
00434     {
00435         if (first)
00436             first = false;
00437         else
00438             strcat(te, joinchars);
00439         if (s->cstr()) 
00440             strcat(te, s->cstr());
00441     }
00442     return total;
00443 }
00444 
00449 WvString strreplace(WvStringParm s, WvStringParm a, WvStringParm b);
00450 
00452 WvString undupe(WvStringParm s, char c);
00453 
00455 WvString hostname();
00456 
00458 WvString fqdomainname();
00459 
00461 WvString wvgetcwd();
00462 
00467 WvString metriculate(const off_t i);
00468 
00473 WvString afterstr(WvStringParm line, WvStringParm a);
00474 
00479 WvString beforestr(WvStringParm line, WvStringParm a);
00480 
00487 WvString substr(WvString line, unsigned int pos, unsigned int len);
00488 
00493 WvString depunctuate(WvStringParm line);
00494 
00495 // Converts a string in decimal to an arbitrary numeric type
00496 template<class T>
00497 bool wvstring_to_num(WvStringParm str, T &n)
00498 {
00499     bool neg = false;
00500     n = 0;
00501 
00502     for (const char *p = str; *p; ++p)
00503     {
00504         if (isdigit(*p))
00505         {
00506             n = n * T(10) + T(*p - '0');
00507         }
00508         else if ((const char *)str == p
00509                 && *p == '-')
00510         {
00511             neg = true;
00512         }
00513         else return false;
00514     }
00515 
00516     if (neg)
00517         n = -n;
00518 
00519     return true;
00520 }
00521 
00522 /*
00523  * Before using the C-style string escaping functions below, please consider
00524  * using the functions in wvtclstring.h instead; they usualy lead to much more
00525  * human readable and manageable results, and allow representation of
00526  * lists of strings.
00527  */
00528 
00529 struct CStrExtraEscape
00530 {
00531     char ch;
00532     const char *esc;
00533 };
00534 extern const CStrExtraEscape CSTR_TCLSTR_ESCAPES[];
00535 
00536 // Converts data into a C-style string constant.
00537 //
00538 // If data is NULL, returns WvString::null; otherwise, returns an allocated
00539 // WvString containing the C-style string constant that represents the data.
00540 //
00541 // All printable characters including space except " and \ are represented with
00542 // escaping.
00543 //
00544 // The usual C escapes are performed, such as \n, \r, \", \\ and \0.
00545 //
00546 // All other characters are escaped in uppercase hex form, eg. \x9E
00547 //
00548 // The extra_escapes parameter allows for additional characters beyond
00549 // the usual ones escaped in C; setting it to CSTR_TCLSTR_ESCAPES will
00550 // escape { and } as < and >, which allows the resulting strings to be
00551 // TCL-string coded without ridiculous double-escaping.
00552 //
00553 WvString cstr_escape(const void *data, size_t size,
00554         const CStrExtraEscape extra_escapes[] = NULL);
00555 
00556 // Converts a C-style string constant into data.
00557 // 
00558 // This function does *not* include the trailing null that a C compiler would --
00559 //   if you want this null, put \0 at the end of the C-style string
00560 // 
00561 // If cstr is correctly formatted and max_size is large enough for the
00562 // resulting data, returns true and size will equal the size of the
00563 // resulting data.  If data is not NULL it will contain this data.
00564 //
00565 // If cstr is correctly formatted but max_size is too small for the resulting
00566 // data, returns false and size will equal the minimum value of min_size
00567 // for this function to have returned true.  If data is non-NULL it will
00568 // contain the first max_size bytes of resulting data.
00569 // 
00570 // If cstr is incorrectly formatted, returns false and size will equal 0.
00571 //
00572 // This functions works just as well on multiple, whitespace-separated
00573 // C-style strings as well.  This allows you to concatenate strings produced
00574 // by cstr_escape, and the result of cstr_unescape will be the data blocks
00575 // concatenated together.  This implies that the empty string corresponds
00576 // to a valid data block of length zero; however, a null string still returns
00577 // an error.
00578 //
00579 // The extra_escapes parameter must match that used in the call to 
00580 // cstr_escape used to produce the escaped strings.
00581 //
00582 bool cstr_unescape(WvStringParm cstr, void *data, size_t max_size, size_t &size,
00583         const CStrExtraEscape extra_escapes[] = NULL);
00584 
00585 static inline bool is_int(const char *str)
00586 {
00587     if (!str)
00588         return false;
00589     
00590     if (*str == '-')
00591         ++str;
00592     
00593     if (!*str)
00594         return false;
00595     
00596     while (*str)
00597         if (!isdigit(*str++))
00598             return false;
00599             
00600     return true;
00601 }
00602 
00603 // Reads the contents of a symlink.  Returns the contents, or WvString::null on error.
00604 WvString wvreadlink(WvStringParm path);
00605 
00606 #endif // __WVSTRUTILS_H

Generated on Fri Oct 5 18:20:28 2007 for WvStreams by  doxygen 1.5.3