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 
00155 #ifndef _WIN32
00156 
00161 WvString passwd_crypt(const char *str);
00162 
00163 #endif
00164 
00169 WvString passwd_md5(const char *str);
00170 
00175 WvString backslash_escape(WvStringParm s1);
00176 
00178 int strcount(WvStringParm s, const char c);
00179 
00184 WvString encode_hostname_as_DN(WvStringParm hostname);
00185 
00192 WvString nice_hostname(WvStringParm name);
00193 
00199 WvString getfilename(WvStringParm fullname);
00200 WvString getdirname(WvStringParm fullname);
00201 
00202 /*
00203  * Possible rounding methods for numbers -- remember from school?
00204  */
00205 enum RoundingMethod
00206 {
00207     ROUND_DOWN,
00208     ROUND_DOWN_AT_POINT_FIVE,
00209     ROUND_UP_AT_POINT_FIVE,
00210     ROUND_UP
00211 };
00212 
00218 WvString sizetoa(unsigned long long blocks, unsigned long blocksize = 1,
00219                  RoundingMethod rounding_method = ROUND_UP_AT_POINT_FIVE);
00220 
00225 WvString sizektoa(unsigned long long kbytes,
00226                   RoundingMethod rounding_method = ROUND_UP_AT_POINT_FIVE);
00227 
00233 WvString sizeitoa(unsigned long long blocks, unsigned long blocksize = 1,
00234                   RoundingMethod rounding_method = ROUND_UP_AT_POINT_FIVE);
00235 
00240 WvString sizekitoa(unsigned long long kbytes,
00241                    RoundingMethod rounding_method = ROUND_UP_AT_POINT_FIVE);
00242 
00246 WvString secondstoa(unsigned int total_seconds);
00247 
00252 int lookup(const char *str, const char * const *table,
00253     bool case_sensitive = false);
00254 
00262 template<class StringCollection>
00263 void strcoll_split(StringCollection &coll, WvStringParm _s,
00264     const char *splitchars = " \t", int limit = 0)
00265 {
00266     WvString s(_s);
00267     char *sptr = s.edit(), *eptr, oldc;
00268     
00269     // Simple if statement to catch (and add) empty (but not NULL) strings.
00270     if (sptr && !*sptr )
00271     {   
00272         WvString *emptyString = new WvString("");
00273         coll.add(emptyString, true);
00274     }
00275     
00276     // Needed to catch delimeters at the beginning of the string.
00277     bool firstrun = true;
00278 
00279     while (sptr && *sptr)
00280     {
00281         --limit;
00282 
00283         if (firstrun)
00284         {   
00285             firstrun = false;
00286         }
00287         else
00288         {
00289             sptr += strspn(sptr, splitchars);
00290         }
00291 
00292         if (limit)
00293         {
00294             eptr = sptr + strcspn(sptr, splitchars);
00295         }
00296         else
00297         {
00298             eptr = sptr + strlen(sptr);
00299         }
00300         
00301         oldc = *eptr;
00302         *eptr = 0;
00303         
00304         WvString *newstr = new WvString(sptr);
00305         coll.add(newstr, true);
00306         
00307         *eptr = oldc;
00308         sptr = eptr;
00309     }
00310 }
00311 
00312 
00326 template<class StringCollection>
00327 void strcoll_splitstrict(StringCollection &coll, WvStringParm _s,
00328     const char *splitchars = " \t", int limit = 0)
00329 {
00330     WvString s(_s);
00331     char *cur = s.edit();
00332 
00333     if (!cur) return;
00334 
00335     for (;;)
00336     {
00337         --limit;
00338         if (!limit)
00339         {
00340             coll.add(new WvString(cur), true);
00341             break;
00342         }
00343 
00344         int len = strcspn(cur, splitchars);
00345 
00346         char tmp = cur[len];
00347         cur[len] = 0;
00348         coll.add(new WvString(cur), true);
00349         cur[len] = tmp;
00350 
00351         if (!cur[len]) break;
00352         cur += len + 1;
00353     }
00354 }
00355 
00356 
00357 #ifndef _WIN32 // don't have regex on win32
00358 
00365 template<class StringCollection>
00366 void strcoll_split(StringCollection &coll, WvStringParm s,
00367     const WvRegex &regex, int limit = 0)
00368 {
00369     int start = 0;
00370     int match_start, match_end;
00371     int count = 0;
00372     
00373     while ((limit == 0 || count < limit)
00374             && regex.continuable_match(&s[start], match_start, match_end)
00375             && match_end > 0)
00376     {
00377         WvString *substr = new WvString;
00378         int len = match_start;
00379         substr->setsize(len+1);
00380         memcpy(substr->edit(), &s[start], len);
00381         substr->edit()[len] = '\0';
00382         coll.add(substr, true);
00383         start += match_end;
00384         ++count;
00385     }
00386     
00387     if (limit == 0 || count < limit)
00388     {
00389         WvString *last = new WvString(&s[start]);
00390         last->unique();
00391         coll.add(last, true);
00392     }
00393 }
00394 #endif
00395 
00396 
00402 template<class StringCollection>
00403 WvString strcoll_join(const StringCollection &coll,
00404     const char *joinchars = " \t")
00405 {
00406     size_t joinlen = strlen(joinchars);
00407     size_t totlen = 1;
00408     typename StringCollection::Iter s(
00409         const_cast<StringCollection&>(coll));
00410     for (s.rewind(); s.next(); )
00411     {
00412         if (s->cstr())
00413             totlen += strlen(s->cstr());
00414         totlen += joinlen;
00415     }
00416     totlen -= joinlen; // no join chars at tail
00417     
00418     WvString total;
00419     total.setsize(totlen);
00420 
00421     char *te = total.edit();
00422     te[0] = 0;
00423     bool first = true;
00424     for (s.rewind(); s.next(); )
00425     {
00426         if (first)
00427             first = false;
00428         else
00429             strcat(te, joinchars);
00430         if (s->cstr()) 
00431             strcat(te, s->cstr());
00432     }
00433     return total;
00434 }
00435 
00440 WvString strreplace(WvStringParm s, WvStringParm a, WvStringParm b);
00441 
00443 WvString undupe(WvStringParm s, char c);
00444 
00446 WvString hostname();
00447 
00449 WvString fqdomainname();
00450 
00452 WvString wvgetcwd();
00453 
00458 WvString metriculate(const off_t i);
00459 
00464 WvString afterstr(WvStringParm line, WvStringParm a);
00465 
00470 WvString beforestr(WvStringParm line, WvStringParm a);
00471 
00478 WvString substr(WvString line, unsigned int pos, unsigned int len);
00479 
00484 WvString depunctuate(WvStringParm line);
00485 
00486 // Converts a string in decimal to an arbitrary numeric type
00487 template<class T>
00488 bool wvstring_to_num(WvStringParm str, T &n)
00489 {
00490     bool neg = false;
00491     n = 0;
00492 
00493     for (const char *p = str; *p; ++p)
00494     {
00495         if (isdigit(*p))
00496         {
00497             n = n * T(10) + T(*p - '0');
00498         }
00499         else if ((const char *)str == p
00500                 && *p == '-')
00501         {
00502             neg = true;
00503         }
00504         else return false;
00505     }
00506 
00507     if (neg)
00508         n = -n;
00509 
00510     return true;
00511 }
00512 
00513 /*
00514  * Before using the C-style string escaping functions below, please consider
00515  * using the functions in wvtclstring.h instead; they usualy lead to much more
00516  * human readable and manageable results, and allow representation of
00517  * lists of strings.
00518  */
00519 
00520 struct CStrExtraEscape
00521 {
00522     char ch;
00523     const char *esc;
00524 };
00525 extern const CStrExtraEscape CSTR_TCLSTR_ESCAPES[];
00526 
00527 // Converts data into a C-style string constant.
00528 //
00529 // If data is NULL, returns WvString::null; otherwise, returns an allocated
00530 // WvString containing the C-style string constant that represents the data.
00531 //
00532 // All printable characters including space except " and \ are represented with
00533 // escaping.
00534 //
00535 // The usual C escapes are performed, such as \n, \r, \", \\ and \0.
00536 //
00537 // All other characters are escaped in uppercase hex form, eg. \x9E
00538 //
00539 // The extra_escapes parameter allows for additional characters beyond
00540 // the usual ones escaped in C; setting it to CSTR_TCLSTR_ESCAPES will
00541 // escape { and } as < and >, which allows the resulting strings to be
00542 // TCL-string coded without ridiculous double-escaping.
00543 //
00544 WvString cstr_escape(const void *data, size_t size,
00545         const CStrExtraEscape extra_escapes[] = NULL);
00546 
00547 // Converts a C-style string constant into data.
00548 // 
00549 // This function does *not* include the trailing null that a C compiler would --
00550 //   if you want this null, put \0 at the end of the C-style string
00551 // 
00552 // If cstr is correctly formatted and max_size is large enough for the
00553 // resulting data, returns true and size will equal the size of the
00554 // resulting data.  If data is not NULL it will contain this data.
00555 //
00556 // If cstr is correctly formatted but max_size is too small for the resulting
00557 // data, returns false and size will equal the minimum value of min_size
00558 // for this function to have returned true.  If data is non-NULL it will
00559 // contain the first max_size bytes of resulting data.
00560 // 
00561 // If cstr is incorrectly formatted, returns false and size will equal 0.
00562 //
00563 // This functions works just as well on multiple, whitespace-separated
00564 // C-style strings as well.  This allows you to concatenate strings produced
00565 // by cstr_escape, and the result of cstr_unescape will be the data blocks
00566 // concatenated together.  This implies that the empty string corresponds
00567 // to a valid data block of length zero; however, a null string still returns
00568 // an error.
00569 //
00570 // The extra_escapes parameter must match that used in the call to 
00571 // cstr_escape used to produce the escaped strings.
00572 //
00573 bool cstr_unescape(WvStringParm cstr, void *data, size_t max_size, size_t &size,
00574         const CStrExtraEscape extra_escapes[] = NULL);
00575 
00576 // Reads the contents of a symlink.  Returns the contents, or WvString::null on error.
00577 WvString wvreadlink(WvStringParm path);
00578 
00579 #endif // __WVSTRUTILS_H

Generated on Wed Jul 12 17:53:21 2006 for WvStreams by  doxygen 1.4.7