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 time_t intl_gmtoff(time_t t);
00165 
00166 #ifndef _WIN32
00167 
00172 WvString passwd_crypt(const char *str);
00173 
00174 #endif
00175 
00180 WvString passwd_md5(const char *str);
00181 
00186 WvString backslash_escape(WvStringParm s1);
00187 
00189 int strcount(WvStringParm s, const char c);
00190 
00195 WvString encode_hostname_as_DN(WvStringParm hostname);
00196 
00203 WvString nice_hostname(WvStringParm name);
00204 
00210 WvString getfilename(WvStringParm fullname);
00211 WvString getdirname(WvStringParm fullname);
00212 
00213 /*
00214  * Possible rounding methods for numbers -- remember from school?
00215  */
00216 enum RoundingMethod
00217 {
00218     ROUND_DOWN,
00219     ROUND_DOWN_AT_POINT_FIVE,
00220     ROUND_UP_AT_POINT_FIVE,
00221     ROUND_UP
00222 };
00223 
00229 WvString sizetoa(unsigned long long blocks, unsigned long blocksize = 1,
00230                  RoundingMethod rounding_method = ROUND_UP_AT_POINT_FIVE);
00231 
00236 WvString sizektoa(unsigned long long kbytes,
00237                   RoundingMethod rounding_method = ROUND_UP_AT_POINT_FIVE);
00238 
00244 WvString sizeitoa(unsigned long long blocks, unsigned long blocksize = 1,
00245                   RoundingMethod rounding_method = ROUND_UP_AT_POINT_FIVE);
00246 
00251 WvString sizekitoa(unsigned long long kbytes,
00252                    RoundingMethod rounding_method = ROUND_UP_AT_POINT_FIVE);
00253 
00257 WvString secondstoa(unsigned int total_seconds);
00258 
00263 int lookup(const char *str, const char * const *table,
00264     bool case_sensitive = false);
00265 
00273 template<class StringCollection>
00274 void strcoll_split(StringCollection &coll, WvStringParm _s,
00275     const char *splitchars = " \t", int limit = 0)
00276 {
00277     WvString s(_s);
00278     char *sptr = s.edit(), *eptr, oldc;
00279     
00280     // Simple if statement to catch (and add) empty (but not NULL) strings.
00281     if (sptr && !*sptr )
00282     {   
00283         WvString *emptyString = new WvString("");
00284         coll.add(emptyString, true);
00285     }
00286     
00287     // Needed to catch delimeters at the beginning of the string.
00288     bool firstrun = true;
00289 
00290     while (sptr && *sptr)
00291     {
00292         --limit;
00293 
00294         if (firstrun)
00295         {   
00296             firstrun = false;
00297         }
00298         else
00299         {
00300             sptr += strspn(sptr, splitchars);
00301         }
00302 
00303         if (limit)
00304         {
00305             eptr = sptr + strcspn(sptr, splitchars);
00306         }
00307         else
00308         {
00309             eptr = sptr + strlen(sptr);
00310         }
00311         
00312         oldc = *eptr;
00313         *eptr = 0;
00314         
00315         WvString *newstr = new WvString(sptr);
00316         coll.add(newstr, true);
00317         
00318         *eptr = oldc;
00319         sptr = eptr;
00320     }
00321 }
00322 
00323 
00337 template<class StringCollection>
00338 void strcoll_splitstrict(StringCollection &coll, WvStringParm _s,
00339     const char *splitchars = " \t", int limit = 0)
00340 {
00341     WvString s(_s);
00342     char *cur = s.edit();
00343 
00344     if (!cur) return;
00345 
00346     for (;;)
00347     {
00348         --limit;
00349         if (!limit)
00350         {
00351             coll.add(new WvString(cur), true);
00352             break;
00353         }
00354 
00355         int len = strcspn(cur, splitchars);
00356 
00357         char tmp = cur[len];
00358         cur[len] = 0;
00359         coll.add(new WvString(cur), true);
00360         cur[len] = tmp;
00361 
00362         if (!cur[len]) break;
00363         cur += len + 1;
00364     }
00365 }
00366 
00367 
00368 #ifndef _WIN32 // don't have regex on win32
00369 
00376 template<class StringCollection>
00377 void strcoll_split(StringCollection &coll, WvStringParm s,
00378     const WvRegex &regex, int limit = 0)
00379 {
00380     int start = 0;
00381     int match_start, match_end;
00382     int count = 0;
00383     
00384     while ((limit == 0 || count < limit)
00385             && regex.continuable_match(&s[start], match_start, match_end)
00386             && match_end > 0)
00387     {
00388         WvString *substr = new WvString;
00389         int len = match_start;
00390         substr->setsize(len+1);
00391         memcpy(substr->edit(), &s[start], len);
00392         substr->edit()[len] = '\0';
00393         coll.add(substr, true);
00394         start += match_end;
00395         ++count;
00396     }
00397     
00398     if (limit == 0 || count < limit)
00399     {
00400         WvString *last = new WvString(&s[start]);
00401         last->unique();
00402         coll.add(last, true);
00403     }
00404 }
00405 #endif
00406 
00407 
00413 template<class StringCollection>
00414 WvString strcoll_join(const StringCollection &coll,
00415     const char *joinchars = " \t")
00416 {
00417     size_t joinlen = strlen(joinchars);
00418     size_t totlen = 1;
00419     typename StringCollection::Iter s(
00420         const_cast<StringCollection&>(coll));
00421     for (s.rewind(); s.next(); )
00422     {
00423         if (s->cstr())
00424             totlen += strlen(s->cstr());
00425         totlen += joinlen;
00426     }
00427     totlen -= joinlen; // no join chars at tail
00428     
00429     WvString total;
00430     total.setsize(totlen);
00431 
00432     char *te = total.edit();
00433     te[0] = 0;
00434     bool first = true;
00435     for (s.rewind(); s.next(); )
00436     {
00437         if (first)
00438             first = false;
00439         else
00440             strcat(te, joinchars);
00441         if (s->cstr()) 
00442             strcat(te, s->cstr());
00443     }
00444     return total;
00445 }
00446 
00451 WvString strreplace(WvStringParm s, WvStringParm a, WvStringParm b);
00452 
00454 WvString undupe(WvStringParm s, char c);
00455 
00457 WvString hostname();
00458 
00460 WvString fqdomainname();
00461 
00463 WvString wvgetcwd();
00464 
00469 WvString metriculate(const off_t i);
00470 
00475 WvString afterstr(WvStringParm line, WvStringParm a);
00476 
00481 WvString beforestr(WvStringParm line, WvStringParm a);
00482 
00489 WvString substr(WvString line, unsigned int pos, unsigned int len);
00490 
00495 WvString depunctuate(WvStringParm line);
00496 
00497 // Converts a string in decimal to an arbitrary numeric type
00498 template<class T>
00499 bool wvstring_to_num(WvStringParm str, T &n)
00500 {
00501     bool neg = false;
00502     n = 0;
00503 
00504     for (const char *p = str; *p; ++p)
00505     {
00506         if (isdigit(*p))
00507         {
00508             n = n * T(10) + T(*p - '0');
00509         }
00510         else if ((const char *)str == p
00511                 && *p == '-')
00512         {
00513             neg = true;
00514         }
00515         else return false;
00516     }
00517 
00518     if (neg)
00519         n = -n;
00520 
00521     return true;
00522 }
00523 
00524 /*
00525  * Before using the C-style string escaping functions below, please consider
00526  * using the functions in wvtclstring.h instead; they usualy lead to much more
00527  * human readable and manageable results, and allow representation of
00528  * lists of strings.
00529  */
00530 
00531 struct CStrExtraEscape
00532 {
00533     char ch;
00534     const char *esc;
00535 };
00536 extern const CStrExtraEscape CSTR_TCLSTR_ESCAPES[];
00537 
00539 //
00540 // If data is NULL, returns WvString::null; otherwise, returns an allocated
00541 // WvString containing the C-style string constant that represents the data.
00542 //
00543 // All printable characters including space except " and \ are represented with
00544 // escaping.
00545 //
00546 // The usual C escapes are performed, such as \n, \r, \", \\ and \0.
00547 //
00548 // All other characters are escaped in uppercase hex form, eg. \x9E
00549 //
00550 // The extra_escapes parameter allows for additional characters beyond
00551 // the usual ones escaped in C; setting it to CSTR_TCLSTR_ESCAPES will
00552 // escape { and } as < and >, which allows the resulting strings to be
00553 // TCL-string coded without ridiculous double-escaping.
00554 //
00555 WvString cstr_escape(const void *data, size_t size,
00556         const CStrExtraEscape extra_escapes[] = NULL);
00557 
00559 // 
00560 // This function does *not* include the trailing null that a C compiler would --
00561 //   if you want this null, put \0 at the end of the C-style string
00562 // 
00563 // If cstr is correctly formatted and max_size is large enough for the
00564 // resulting data, returns true and size will equal the size of the
00565 // resulting data.  If data is not NULL it will contain this data.
00566 //
00567 // If cstr is correctly formatted but max_size is too small for the resulting
00568 // data, returns false and size will equal the minimum value of min_size
00569 // for this function to have returned true.  If data is non-NULL it will
00570 // contain the first max_size bytes of resulting data.
00571 // 
00572 // If cstr is incorrectly formatted, returns false and size will equal 0.
00573 //
00574 // This functions works just as well on multiple, whitespace-separated
00575 // C-style strings as well.  This allows you to concatenate strings produced
00576 // by cstr_escape, and the result of cstr_unescape will be the data blocks
00577 // concatenated together.  This implies that the empty string corresponds
00578 // to a valid data block of length zero; however, a null string still returns
00579 // an error.
00580 //
00581 // The extra_escapes parameter must match that used in the call to 
00582 // cstr_escape used to produce the escaped strings.
00583 //
00584 bool cstr_unescape(WvStringParm cstr, void *data, size_t max_size, size_t &size,
00585         const CStrExtraEscape extra_escapes[] = NULL);
00586 
00587 static inline bool is_int(const char *str)
00588 {
00589     if (!str)
00590         return false;
00591     
00592     if (*str == '-')
00593         ++str;
00594     
00595     if (!*str)
00596         return false;
00597     
00598     while (*str)
00599         if (!isdigit(*str++))
00600             return false;
00601             
00602     return true;
00603 }
00604 
00607 WvString ptr2str(void* ptr);
00608 
00611 WvString wvreadlink(WvStringParm path);
00612 
00613 #endif // __WVSTRUTILS_H

Generated on Thu Jan 24 16:50:57 2008 for WvStreams by  doxygen 1.5.4