Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

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 
00031 char *terminate_string(char *string, char c);
00032 
00041 char *trim_string(char *string);
00042 
00047 char *trim_string(char *string, char c);
00048 
00062 WvString spacecat(WvStringParm a, WvStringParm b, char sep = ' ',
00063                   bool onesep = false);
00064 
00065     
00070 char *non_breaking(char *string);
00071 
00076 void replace_char(void *string, char c1, char c2, int length);
00077 
00081 char *snip_string(char *haystack, char *needle);
00082 
00083 #ifndef _WIN32
00084 
00088 char *strlwr(char *string);
00089 
00094 char *strupr(char *string);
00095 
00096 #endif
00097 
00099 bool is_word(const char *string);
00100 
00109 WvString hexdump_buffer(const void *buf, size_t len, bool charRep = true);
00110 
00115 bool isnewline(char c);
00116 
00124 WvString web_unescape(const char *str, bool no_space = false);
00125 
00126 
00131 WvString url_encode(WvStringParm stuff);
00132  
00133 
00137 WvString  diff_dates(time_t t1, time_t t2);
00138 
00139 
00144 WvString rfc822_date(time_t _when = -1);
00145 
00147 WvString rfc1123_date(time_t _when);
00148 
00154 WvString passwd_crypt(const char *str);
00155 
00161 WvString passwd_md5(const char *str);
00162 
00167 WvString backslash_escape(WvStringParm s1);
00168 
00170 int strcount(WvStringParm s, const char c);
00171 
00176 WvString encode_hostname_as_DN(WvStringParm hostname);
00177 
00184 WvString nice_hostname(WvStringParm name);
00185 
00191 WvString getfilename(WvStringParm fullname);
00192 WvString getdirname(WvStringParm fullname);
00193 
00198 WvString sizetoa(unsigned long long blocks, unsigned int blocksize=1);
00199 
00201 WvString sizektoa(unsigned int kbytes);
00202 
00206 WvString secondstoa(unsigned int total_seconds);
00207 
00212 int lookup(const char *str, const char * const *table,
00213     bool case_sensitive = false);
00214 
00222 template<class StringCollection>
00223 void strcoll_split(StringCollection &coll, WvStringParm _s,
00224     const char *splitchars = " \t", int limit = 0)
00225 {
00226     WvString s(_s);
00227     char *sptr = s.edit(), *eptr, oldc;
00228     
00229     // Simple if statement to catch (and add) empty (but not NULL) strings.
00230     if (sptr && !*sptr )
00231     {   
00232         WvString *emptyString = new WvString("");
00233         coll.add(emptyString, true);
00234     }
00235     
00236     // Needed to catch delimeters at the beginning of the string.
00237     bool firstrun = true;
00238 
00239     while (sptr && *sptr)
00240     {
00241         --limit;
00242 
00243         if (firstrun)
00244         {   
00245             firstrun = false;
00246         }
00247         else
00248         {
00249             sptr += strspn(sptr, splitchars);
00250         }
00251 
00252         if (limit)
00253         {
00254             eptr = sptr + strcspn(sptr, splitchars);
00255         }
00256         else
00257         {
00258             eptr = sptr + strlen(sptr);
00259         }
00260         
00261         oldc = *eptr;
00262         *eptr = 0;
00263         
00264         WvString *newstr = new WvString(sptr);
00265         coll.add(newstr, true);
00266         
00267         *eptr = oldc;
00268         sptr = eptr;
00269     }
00270 }
00271 
00272 
00286 template<class StringCollection>
00287 void strcoll_splitstrict(StringCollection &coll, WvStringParm _s,
00288     const char *splitchars = " \t", int limit = 0)
00289 {
00290     WvString s(_s);
00291     char *cur = s.edit();
00292 
00293     if (!cur) return;
00294 
00295     for (;;)
00296     {
00297         --limit;
00298         if (!limit)
00299         {
00300             coll.add(new WvString(cur), true);
00301             break;
00302         }
00303 
00304         int len = strcspn(cur, splitchars);
00305 
00306         char tmp = cur[len];
00307         cur[len] = 0;
00308         coll.add(new WvString(cur), true);
00309         cur[len] = tmp;
00310 
00311         if (!cur[len]) break;
00312         cur += len + 1;
00313     }
00314 }
00315 
00316 
00322 template<class StringCollection>
00323 WvString strcoll_join(const StringCollection &coll,
00324     const char *joinchars = " \t")
00325 {
00326     size_t joinlen = strlen(joinchars);
00327     size_t totlen = 1;
00328     typename StringCollection::Iter s(
00329         const_cast<StringCollection&>(coll));
00330     for (s.rewind(); s.next(); )
00331     {
00332         if (s->cstr())
00333             totlen += strlen(s->cstr());
00334         totlen += joinlen;
00335     }
00336     totlen -= joinlen; // no join chars at tail
00337     
00338     WvString total;
00339     total.setsize(totlen);
00340 
00341     char *te = total.edit();
00342     te[0] = 0;
00343     bool first = true;
00344     for (s.rewind(); s.next(); )
00345     {
00346         if (first)
00347             first = false;
00348         else
00349             strcat(te, joinchars);
00350         if (s->cstr()) 
00351             strcat(te, s->cstr());
00352     }
00353     return total;
00354 }
00355 
00360 WvString strreplace(WvStringParm s, WvStringParm a, WvStringParm b);
00361 
00363 WvString undupe(WvStringParm s, char c);
00364 
00365 WvString hostname();
00366 WvString fqdomainname();
00367 
00372 WvString metriculate(const off_t i);
00373 
00378 WvString afterstr(WvStringParm line, WvStringParm a);
00379 
00384 WvString beforestr(WvStringParm line, WvStringParm a);
00385 
00392 WvString substr(WvString line, unsigned int pos, unsigned int len);
00393 
00394 // Converts a string in decimal to an arbitrary numeric type
00395 template<class T>
00396 bool wvstring_to_num(WvStringParm str, T &n)
00397 {
00398     bool neg = false;
00399     n = 0;
00400 
00401     for (const char *p = str; *p; ++p)
00402     {
00403         if (isdigit(*p))
00404         {
00405             n = n * T(10) + T(*p - '0');
00406         }
00407         else if ((const char *)str == p
00408                 && *p == '-')
00409         {
00410             neg = true;
00411         }
00412         else return false;
00413     }
00414 
00415     if (neg)
00416         n = -n;
00417 
00418     return true;
00419 }
00420         
00421 #endif // __WVSTRUTILS_H

Generated on Sun Apr 3 14:46:41 2005 for WvStreams by  doxygen 1.4.2