WvStreams
|
Various little string functions. More...
#include <sys/types.h>
#include <time.h>
#include <ctype.h>
#include "wvstring.h"
#include "wvstringlist.h"
#include "wvhex.h"
#include "wvregex.h"
Go to the source code of this file.
Classes | |
struct | CStrExtraEscape |
Enumerations | |
enum | RoundingMethod { ROUND_DOWN, ROUND_DOWN_AT_POINT_FIVE, ROUND_UP_AT_POINT_FIVE, ROUND_UP } |
Functions | |
char * | terminate_string (char *string, char c) |
Add character c to the end of a string after removing terminating carriage returns/linefeeds if any. | |
char * | trim_string (char *string) |
Trims whitespace from the beginning and end of the character string, including carriage return / linefeed characters. | |
char * | trim_string (char *string, char c) |
Similar to above, but trims the string starting at the first occurrence of c. | |
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 them. | |
char * | non_breaking (const char *string) |
Replaces all whitespace characters in the string with non-breaking spaces ( ) for use with web stuff. | |
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'. | |
char * | snip_string (char *haystack, char *needle) |
Snip off the first part of 'haystack' if it consists of 'needle'. | |
char * | strlwr (char *string) |
In-place modify a character string so that all contained letters are in lower case. | |
char * | strupr (char *string) |
In-place modify a character string so that all contained letters are in upper case. | |
bool | is_word (const char *string) |
Returns true if all characters in 'string' are isalnum() (alphanumeric). | |
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'. | |
bool | isnewline (char c) |
Returns true if 'c' is a newline or carriage return character. | |
WvString | url_decode (WvStringParm str, bool no_space=false) |
Converts escaped characters (things like %20 etc.) from web URLS into their normal ASCII representations. | |
WvString | url_encode (WvStringParm str, WvStringParm unsafe="") |
Converts all those pesky spaces, colons, and other nasties into nice unreadable Quasi-Unicode codes. | |
WvString | diff_dates (time_t t1, time_t t2) |
Returns the difference between to dates in a human readable format. | |
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 | rfc1123_date (time_t _when) |
Returns an RFC1123-compatible date made out of _when. | |
WvString | local_date (time_t _when=-1) |
Return the local date (TZ applied) out of _when. | |
WvString | intl_time (time_t _when=-1) |
Return the local time (in format of ISO 8601) out of _when. | |
WvString | intl_date (time_t _when=-1) |
Return the local date (in format of ISO 8601) out of _when. | |
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 | passwd_crypt (const char *str) |
Similar to crypt(), but this randomly selects its own salt. | |
WvString | passwd_md5 (const char *str) |
Similar to crypt(), but this randomly selects its own salt. | |
WvString | backslash_escape (WvStringParm s1) |
Returns a string with a backslash in front of every non alphanumeric character in s1. | |
int | strcount (WvStringParm s, const char c) |
How many times does 'c' occur in "s"? | |
WvString | encode_hostname_as_DN (WvStringParm hostname) |
Example: encode_hostname_as_DN("www.fizzle.com") will result in dc=www,dc=fizzle,dc=com,cn=www.fizzle.com. | |
WvString | nice_hostname (WvStringParm name) |
Given a hostname, turn it into a "nice" one. | |
WvString | getfilename (WvStringParm fullname) |
Take a full path/file name and splits it up into respective pathname and filename. | |
WvString | getdirname (WvStringParm fullname) |
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-readable representation of blocks*blocksize. | |
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 | 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-readable representation of blocks*blocksize. | |
WvString | sizekitoa (unsigned long long kbytes, RoundingMethod rounding_method=ROUND_UP_AT_POINT_FIVE) |
Given a size in kilobytes, return a human readable size. | |
WvString | secondstoa (unsigned int total_seconds) |
Given a number of seconds, returns a formatted human-readable string saying how long the period is. | |
int | lookup (const char *str, const char *const *table, bool case_sensitive=false) |
Finds a string in an array and returns its index. | |
template<class StringCollection > | |
void | strcoll_split (StringCollection &coll, WvStringParm _s, const char *splitchars=" \t", int limit=0) |
Splits a string and adds each substring to a collection. | |
template<class StringCollection > | |
void | strcoll_splitstrict (StringCollection &coll, WvStringParm _s, const char *splitchars=" \t", int limit=0) |
Splits a string and adds each substring to a collection. | |
template<class StringCollection > | |
void | strcoll_split (StringCollection &coll, WvStringParm s, const WvRegex ®ex, int limit=0) |
Splits a string and adds each substring to a collection. | |
template<class StringCollection > | |
WvString | strcoll_join (const StringCollection &coll, const char *joinchars=" \t") |
Concatenates all strings in a collection and returns the result. | |
WvString | strreplace (WvStringParm s, WvStringParm a, WvStringParm b) |
Replace any instances of "a" with "b" in "s". | |
WvString | undupe (WvStringParm s, char c) |
Replace any consecutive instances of character c with a single one. | |
WvString | hostname () |
Do gethostname() without a fixed-length buffer. | |
WvString | fqdomainname () |
Get the fqdn of the local host, using gethostbyname() and gethostname() | |
WvString | wvgetcwd () |
Get the current working directory without a fixed-length buffer. | |
WvString | metriculate (const off_t i) |
Inserts SI-style spacing into a number (eg passing 9876543210 returns "9 876 543 210") | |
WvString | afterstr (WvStringParm line, WvStringParm a) |
Returns everything in line (exclusively) after a. | |
WvString | beforestr (WvStringParm line, WvStringParm a) |
Returns everything in line (exclusively) before 'a'. | |
WvString | substr (WvString line, unsigned int pos, unsigned int len) |
Returns the string of length len starting at pos in line. | |
WvString | depunctuate (WvStringParm line) |
Removes any trailing punctuation ('. | |
template<class T > | |
bool | wvstring_to_num (WvStringParm str, T &n) |
WvString | cstr_escape (const void *data, size_t size, const CStrExtraEscape extra_escapes[]=NULL) |
Converts data into a C-style string constant. | |
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 | ptr2str (void *ptr) |
Converts a pointer into a string, like glibc's p formatter would do. | |
Variables | |
const CStrExtraEscape | CSTR_TCLSTR_ESCAPES [] |
Various little string functions.
Definition in file wvstrutils.h.
char* terminate_string | ( | char * | string, |
char | c | ||
) |
Add character c to the end of a string after removing terminating carriage returns/linefeeds if any.
You need a buffer that's at least one character bigger than the current length of the string, including the terminating NULL.
Definition at line 32 of file strutils.cc.
References terminate_string().
Referenced by terminate_string().
char* trim_string | ( | char * | string | ) |
Trims whitespace from the beginning and end of the character string, including carriage return / linefeed characters.
Modifies the string in place. Returns the new first character of the string, which points either at 'string' itself or some character contained therein.
string is allowed to be NULL; returns NULL in that case.
Definition at line 59 of file strutils.cc.
References trim_string().
Referenced by WvLogBuffer::_end_line(), WvHttpStream::execute(), UniIniGen::refresh(), trim_string(), and url_decode().
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 them.
For example, spacecat("xx", "yy", ";"); returns "xx;yy", and spacecat("xx;;", "yy", ";") returns "xx;;;yy", and spacecat("xx;;", "yy", ";", true) returns "xx;yy".
This function is much faster than the more obvious WvString("%s;%s", a, b), so it's useful when you're producing a *lot* of string data.
Definition at line 114 of file strutils.cc.
References WvFastString::cstr(), WvString::edit(), and spacecat().
Referenced by UniClientGen::set(), UniClientGen::setv(), spacecat(), UniClientConn::writeonevalue(), and UniClientConn::writevalue().
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'.
Ignores terminating NULL, so make sure you set 'length' correctly.
Definition at line 178 of file strutils.cc.
References replace_char().
Referenced by replace_char().
char* strlwr | ( | char * | string | ) |
In-place modify a character string so that all contained letters are in lower case.
Returns 'string'.
Definition at line 201 of file strutils.cc.
References strlwr().
Referenced by strlwr().
char* strupr | ( | char * | string | ) |
In-place modify a character string so that all contained letters are in upper case.
Returns 'string'.
Definition at line 214 of file strutils.cc.
References strupr().
Referenced by strupr().
bool is_word | ( | const char * | string | ) |
Returns true if all characters in 'string' are isalnum() (alphanumeric).
Definition at line 228 of file strutils.cc.
References is_word().
Referenced by is_word().
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'.
It is formatted with 16 bytes per line; each line has an address offset, hex representation, and printable representation.
This is used mostly for debugging purposes. You can send the returned WvString object directly to a WvLog or any other WvStream for output.
Definition at line 245 of file strutils.cc.
References WvString::edit(), and hexdump_buffer().
Referenced by hexdump_buffer().
bool isnewline | ( | char | c | ) |
Returns true if 'c' is a newline or carriage return character.
Increases code readability a bit.
Definition at line 304 of file strutils.cc.
References isnewline().
Referenced by isnewline().
WvString url_decode | ( | WvStringParm | str, |
bool | no_space = false |
||
) |
Converts escaped characters (things like %20 etc.) from web URLS into their normal ASCII representations.
If you happen to be decoding PEM encoded stuff, or anything that has + signs in it that you don't want encoded as spaces, then set no_space to true, and it should "just work" for you.
Definition at line 311 of file strutils.cc.
References WvString::edit(), trim_string(), and url_decode().
Referenced by url_decode().
WvString url_encode | ( | WvStringParm | str, |
WvStringParm | unsafe = "" |
||
) |
Converts all those pesky spaces, colons, and other nasties into nice unreadable Quasi-Unicode codes.
The 'unsafe' parameter is a list of characters that are unsafe and should be escaped. If unspecified, all characters which are not part of the uric character class defined in RFC 2396 will be escaped. Note: The '' character is always escaped, as otherwise the string would not be decodable.
Definition at line 351 of file strutils.cc.
References WvBufBase< unsigned char >::getstr(), and url_encode().
Referenced by url_encode().
time_t intl_gmtoff | ( | time_t | t | ) |
Return the number of seconds by which localtime (at the given timestamp) is offset from GMT.
For example, in Eastern Standard Time, the offset is (-5*60*60) = -18000.
Definition at line 1294 of file strutils.cc.
References intl_gmtoff().
Referenced by intl_gmtoff().
WvString passwd_crypt | ( | const char * | str | ) |
Similar to crypt(), but this randomly selects its own salt.
This function is defined in strcrypt.cc. It chooses to use the DES engine.
Similar to crypt(), but this randomly selects its own salt.
When 2 identical strings are encrypted, they will not return the same encryption. Also, str does not need to be less than 8 chars as UNIX crypt says, although it only works on the first 8 characters.
Definition at line 13 of file strcrypt.cc.
References passwd_crypt().
Referenced by passwd_crypt().
WvString passwd_md5 | ( | const char * | str | ) |
Similar to crypt(), but this randomly selects its own salt.
This function is defined in strcrypt.cc. It chooses to use the MD5 engine.
Similar to crypt(), but this randomly selects its own salt.
When 2 identical strings are encrypted, they will not return the same encryption. Also, str does not need to be less than 8 chars as we're using the glibc md5 algorithm.
Definition at line 37 of file strcrypt.cc.
References passwd_md5().
Referenced by passwd_md5().
WvString nice_hostname | ( | WvStringParm | name | ) |
Given a hostname, turn it into a "nice" one.
It has to start with a letter/number, END with a letter/number, have underscores converted to hyphens, and have no more than one hyphen in a row. If we can't do this and have any sort of answer, return "UNKNOWN".
Definition at line 460 of file strutils.cc.
References WvString::edit(), and nice_hostname().
Referenced by nice_hostname().
WvString getfilename | ( | WvStringParm | fullname | ) |
Take a full path/file name and splits it up into respective pathname and filename.
This can also be useful for splitting the toplevel directory off a path.
Definition at line 506 of file strutils.cc.
References WvString::edit(), and getfilename().
Referenced by getfilename().
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-readable representation of blocks*blocksize.
This function uses SI prefixes.
Definition at line 708 of file strutils.cc.
References sizetoa().
Referenced by sizektoa(), and sizetoa().
WvString sizektoa | ( | unsigned long long | kbytes, |
RoundingMethod | rounding_method = ROUND_UP_AT_POINT_FIVE |
||
) |
Given a size in kilobyes, return a human readable size.
This function uses SI prefixes (1 MB = 1 000 KB = 1 000 000 B).
Definition at line 721 of file strutils.cc.
References sizektoa(), and sizetoa().
Referenced by sizektoa().
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-readable representation of blocks*blocksize.
This function uses IEC prefixes.
Definition at line 729 of file strutils.cc.
References sizeitoa().
Referenced by sizeitoa(), and sizekitoa().
WvString sizekitoa | ( | unsigned long long | kbytes, |
RoundingMethod | rounding_method = ROUND_UP_AT_POINT_FIVE |
||
) |
Given a size in kilobytes, return a human readable size.
This function uses IEC prefixes.
Definition at line 742 of file strutils.cc.
References sizeitoa(), and sizekitoa().
Referenced by sizekitoa().
int lookup | ( | const char * | str, |
const char *const * | table, | ||
bool | case_sensitive = false |
||
) |
Finds a string in an array and returns its index.
Returns -1 if not found.
Definition at line 850 of file strutils.cc.
References lookup().
Referenced by WvBase64Decoder::_encode(), and lookup().
void strcoll_split | ( | StringCollection & | coll, |
WvStringParm | _s, | ||
const char * | splitchars = " \t" , |
||
int | limit = 0 |
||
) |
Splits a string and adds each substring to a collection.
coll : the collection of strings to add to _s : the string to split splitchars : the set of delimiter characters limit : the maximum number of elements to split
Definition at line 278 of file wvstrutils.h.
References WvString::edit().
Referenced by WvStringList::split().
void strcoll_splitstrict | ( | StringCollection & | coll, |
WvStringParm | _s, | ||
const char * | splitchars = " \t" , |
||
int | limit = 0 |
||
) |
Splits a string and adds each substring to a collection.
this behaves differently in that it actually delimits the pieces as fields and returns them, it doesn't treat multiple delimeters as one and skip them.
ie., parm1::parm2 -> 'parm1','','parm2' when delimited with ':'
coll : the collection of strings to add to _s : the string to split splitchars : the set of delimiter characters limit : the maximum number of elements to split
Definition at line 342 of file wvstrutils.h.
References WvString::edit().
Referenced by WvStringList::splitstrict().
void strcoll_split | ( | StringCollection & | coll, |
WvStringParm | s, | ||
const WvRegex & | regex, | ||
int | limit = 0 |
||
) |
Splits a string and adds each substring to a collection.
coll : the collection of strings to add to _s : the string to split splitchars : the set of delimiter characters limit : the maximum number of elements to split
Definition at line 381 of file wvstrutils.h.
References WvRegex::continuable_match(), WvString::edit(), substr(), and WvString::unique().
WvString strcoll_join | ( | const StringCollection & | coll, |
const char * | joinchars = " \t" |
||
) |
Concatenates all strings in a collection and returns the result.
coll : the collection of strings to read from joinchars : the delimiter string to insert between strings
Definition at line 418 of file wvstrutils.h.
References WvString::edit().
Referenced by WvStringList::join().
WvString strreplace | ( | WvStringParm | s, |
WvStringParm | a, | ||
WvStringParm | b | ||
) |
Replace any instances of "a" with "b" in "s".
Kind of like sed, only much dumber.
Definition at line 797 of file strutils.cc.
References WvBufBase< unsigned char >::getstr(), WvBufBase< unsigned char >::putstr(), and strreplace().
Referenced by strreplace().
WvString afterstr | ( | WvStringParm | line, |
WvStringParm | a | ||
) |
Returns everything in line (exclusively) after a.
If a is not in line, "" is returned.
Definition at line 965 of file strutils.cc.
References afterstr(), and WvString::unique().
Referenced by afterstr().
WvString beforestr | ( | WvStringParm | line, |
WvStringParm | a | ||
) |
Returns everything in line (exclusively) before 'a'.
If a is not in line, line is returned.
Definition at line 981 of file strutils.cc.
References beforestr(), and WvString::unique().
Referenced by beforestr().
Returns the string of length len starting at pos in line.
Error checking prevents seg fault. If pos > line.len()-1 return "" if pos+len > line.len() simply return from pos to end of line
Definition at line 998 of file strutils.cc.
References WvFastString::cstr(), WvString::edit(), and substr().
Referenced by strcoll_split(), and substr().
WvString depunctuate | ( | WvStringParm | line | ) |
Removes any trailing punctuation ('.
', '?', or '!') from the line, and returns it in a new string. Does not modify line.
Definition at line 1306 of file strutils.cc.
References depunctuate(), and WvString::edit().
Referenced by depunctuate().
Converts a pointer into a string, like glibc's p formatter would do.
Definition at line 1318 of file strutils.cc.
References ptr2str().
Referenced by WvIStreamList::post_select(), WvIStreamList::pre_select(), and ptr2str().