Go to the documentation of this file.
10 #define __WVSTRUTILS_H
12 #include <sys/types.h>
18 #include "wvstringlist.h"
81 void replace_char(
void *
string,
char c1,
char c2,
int length);
93 char *
strlwr(
char *
string);
99 char *
strupr(
char *
string);
104 bool is_word(
const char *
string);
195 int strcount(WvStringParm s,
const char c);
217 WvString getdirname(WvStringParm fullname);
225 ROUND_DOWN_AT_POINT_FIVE,
226 ROUND_UP_AT_POINT_FIVE,
235 WvString sizetoa(
unsigned long long blocks,
unsigned long blocksize = 1,
236 RoundingMethod rounding_method = ROUND_UP_AT_POINT_FIVE);
243 RoundingMethod rounding_method = ROUND_UP_AT_POINT_FIVE);
251 RoundingMethod rounding_method = ROUND_UP_AT_POINT_FIVE);
258 RoundingMethod rounding_method = ROUND_UP_AT_POINT_FIVE);
269 int lookup(
const char *str,
const char *
const *table,
270 bool case_sensitive =
false);
279 template<
class StringCollection>
281 const char *splitchars =
" \t",
int limit = 0)
284 char *sptr = s.
edit(), *eptr, oldc;
290 coll.add(emptyString,
true);
294 bool firstrun =
true;
296 while (sptr && *sptr)
306 sptr += strspn(sptr, splitchars);
311 eptr = sptr + strcspn(sptr, splitchars);
315 eptr = sptr + strlen(sptr);
322 coll.add(newstr,
true);
343 template<
class StringCollection>
345 const char *splitchars =
" \t",
int limit = 0)
348 char *cur = s.
edit();
361 int len = strcspn(cur, splitchars);
368 if (!cur[len])
break;
374 #ifndef _WIN32 // don't have regex on win32
382 template<
class StringCollection>
384 const WvRegex ®ex,
int limit = 0)
387 int match_start, match_end;
390 while ((limit == 0 || count < limit)
395 int len = match_start;
404 if (limit == 0 || count < limit)
408 coll.add(last,
true);
419 template<
class StringCollection>
421 const char *joinchars =
" \t")
423 size_t joinlen = strlen(joinchars);
425 typename StringCollection::Iter s(
426 const_cast<StringCollection&
>(coll));
427 for (s.rewind(); s.next(); )
430 totlen += strlen(s->cstr());
436 total.setsize(totlen);
438 char *te = total.
edit();
441 for (s.rewind(); s.next(); )
446 strcat(te, joinchars);
448 strcat(te, s->cstr());
505 bool wvstring_to_num(WvStringParm str, T &n)
510 for (
const char *p = str; *p; ++p)
514 n = n * T(10) + T(*p -
'0');
516 else if ((
const char *)str == p
590 bool cstr_unescape(WvStringParm cstr,
void *data,
size_t max_size,
size_t &size,
593 static inline bool is_int(
const char *str)
605 if (!isdigit(*str++))
615 #endif // __WVSTRUTILS_H
char * edit()
make the string editable, and return a non-const (char*)
WvString intl_datetime(time_t _when=-1)
Return the local date and time (in format of ISO 8601) out of _when.
time_t intl_gmtoff(time_t t)
Return the number of seconds by which localtime (at the given timestamp) is offset from GMT.
WvString intl_time(time_t _when=-1)
Return the local time (in format of ISO 8601) out of _when.
int lookup(const char *str, const char *const *table, bool case_sensitive=false)
Finds a string in an array and returns its index.
WvString fqdomainname()
Get the fqdn of the local host, using gethostbyname() and gethostname()
WvString getfilename(WvStringParm fullname)
Take a full path/file name and splits it up into respective pathname and filename.
WvString strcoll_join(const StringCollection &coll, const char *joinchars=" \t")
Concatenates all strings in a collection and returns the result.
WvString url_decode(WvStringParm str, bool no_space=false)
Converts escaped characters (things like %20 etc.) from web URLS into their normal ASCII representati...
WvString passwd_md5(const char *str)
Similar to crypt(), but this randomly selects its own salt.
WvString sizetoa(unsigned long long blocks, unsigned long blocksize=1, RoundingMethod rounding_method=ROUND_UP_AT_POINT_FIVE)
Given a number of blocks and a blocksize (default==1 byte), return a WvString containing a human-read...
bool isnewline(char c)
Returns true if 'c' is a newline or carriage return character.
WvString rfc1123_date(time_t _when)
Returns an RFC1123-compatible date made out of _when.
WvString undupe(WvStringParm s, char c)
Replace any consecutive instances of character c with a single one.
WvString diff_dates(time_t t1, time_t t2)
Returns the difference between to dates in a human readable format.
char * trim_string(char *string)
Trims whitespace from the beginning and end of the character string, including carriage return / line...
WvString passwd_crypt(const char *str)
Similar to crypt(), but this randomly selects its own salt.
bool continuable_match(WvStringParm string, int &match_start, int &match_end, WVREGEX_REGS_DECL) const
Match a given string against the compiled regular expression, capturing the start and end positions o...
WvString spacecat(WvStringParm a, WvStringParm b, char sep=' ', bool onesep=false)
return the string formed by concatenating string 'a' and string 'b' with the 'sep' character between ...
WvString local_date(time_t _when=-1)
Return the local date (TZ applied) out of _when.
WvString encode_hostname_as_DN(WvStringParm hostname)
Example: encode_hostname_as_DN("www.fizzle.com") will result in dc=www,dc=fizzle,dc=com,...
void replace_char(void *string, char c1, char c2, int length)
Replace all instances of c1 with c2 for the first 'length' characters in 'string'.
WvString is an implementation of a simple and efficient printable-string class.
WvString afterstr(WvStringParm line, WvStringParm a)
Returns everything in line (exclusively) after a.
WvString url_encode(WvStringParm str, WvStringParm unsafe="")
Converts all those pesky spaces, colons, and other nasties into nice unreadable Quasi-Unicode codes.
WvString depunctuate(WvStringParm line)
Removes any trailing punctuation ('.
WvString sizekitoa(unsigned long long kbytes, RoundingMethod rounding_method=ROUND_UP_AT_POINT_FIVE)
Given a size in kilobytes, return a human readable size.
bool is_word(const char *string)
Returns true if all characters in 'string' are isalnum() (alphanumeric).
char * non_breaking(const char *string)
Replaces all whitespace characters in the string with non-breaking spaces ( ) for use with web stuff.
WvString nice_hostname(WvStringParm name)
Given a hostname, turn it into a "nice" one.
WvRegex – Unified support for regular expressions.
WvString substr(WvString line, unsigned int pos, unsigned int len)
Returns the string of length len starting at pos in line.
int strcount(WvStringParm s, const char c)
How many times does 'c' occur in "s"?
WvString beforestr(WvStringParm line, WvStringParm a)
Returns everything in line (exclusively) before 'a'.
char * snip_string(char *haystack, char *needle)
Snip off the first part of 'haystack' if it consists of 'needle'.
WvString rfc822_date(time_t _when=-1)
Returns an RFC822-compatible date made out of _when, or, if _when < 0, out of the current time.
WvString sizektoa(unsigned long long kbytes, RoundingMethod rounding_method=ROUND_UP_AT_POINT_FIVE)
Given a size in kilobyes, return a human readable size.
WvString intl_date(time_t _when=-1)
Return the local date (in format of ISO 8601) out of _when.
char * strlwr(char *string)
In-place modify a character string so that all contained letters are in lower case.
WvString & unique()
make the buf and str pointers owned only by this WvString.
void strcoll_splitstrict(StringCollection &coll, WvStringParm _s, const char *splitchars=" \t", int limit=0)
Splits a string and adds each substring to a collection.
void strcoll_split(StringCollection &coll, WvStringParm _s, const char *splitchars=" \t", int limit=0)
Splits a string and adds each substring to a collection.
WvString metriculate(const off_t i)
Inserts SI-style spacing into a number (eg passing 9876543210 returns "9 876 543 210")
char * strupr(char *string)
In-place modify a character string so that all contained letters are in upper case.
WvString hexdump_buffer(const void *buf, size_t len, bool charRep=true)
Produce a hexadecimal dump of the data buffer in 'buf' of length 'len'.
WvString sizeitoa(unsigned long long blocks, unsigned long blocksize=1, RoundingMethod rounding_method=ROUND_UP_AT_POINT_FIVE)
Given a number of blocks and a blocksize (default==1 byte), return a WvString containing a human-read...
WvString secondstoa(unsigned int total_seconds)
Given a number of seconds, returns a formatted human-readable string saying how long the period is.
WvString cstr_escape(const void *data, size_t size, const CStrExtraEscape extra_escapes[]=NULL)
Converts data into a C-style string constant.
WvString wvgetcwd()
Get the current working directory without a fixed-length buffer.
WvString hostname()
Do gethostname() without a fixed-length buffer.
char * terminate_string(char *string, char c)
Add character c to the end of a string after removing terminating carriage returns/linefeeds if any.
WvString backslash_escape(WvStringParm s1)
Returns a string with a backslash in front of every non alphanumeric character in s1.
bool cstr_unescape(WvStringParm cstr, void *data, size_t max_size, size_t &size, const CStrExtraEscape extra_escapes[]=NULL)
Converts a C-style string constant into data.
WvString strreplace(WvStringParm s, WvStringParm a, WvStringParm b)
Replace any instances of "a" with "b" in "s".
WvString ptr2str(void *ptr)
Converts a pointer into a string, like glibc's p formatter would do.