Chapter 1. Basic String Handling

Table of Contents
String Utilities (strutils.cc)
Version String Manipulation (verstring.cc)
Hexadecimal formating tools (WvHex.cc)
Crypto stuff (strcrypt.cc)

Here are some particularly simple C and C++ functions for manipulating strings. Most of these only sit in C++ files is to make it easier to link with our C++ functions--they don't use many C++ features. Functions that take or return WvStrings, however, are in C++.

String Utilities (strutils.cc)

backslash_escape()

WvString backslash_escape(WvStringParm s1)

Returns a WvString with a backslash in front of every non-alphanumeric character in s1.

encode_hostname_as_DN()

WvString encode_hostname_as_DN(WvStringParm hostname)

Takes a hostname and splits it into parts. For example, passing "www.fizzle.com" will return "dc=www, dc=fizzle, dc=com, cn=www.fizzle.com".

getfilename()

WvString getfilename(WvStringParm fullname)

Returns the filename from a path. Together with getdirname(), separates the filename and directory name within a path.

getdirname()

WvString getfilename(WvStringParm fullname)

Returns the directory name from a path.

hexdump_buffer()

WvString hexdump_buffer(const void *_buf, size_t len)

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.

isnewline()

bool isnewline(char c)

Returns true if 'c' is a newline or carriage return character. Increases code readability a bit.

is_word()

bool is_word(char *string)

Returns true if all characters in 'string' are isalnum() (alphanumeric).

lookup()

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.

nice_hostname()

WvString nice_hostname(WvStringParm name)

Converts a string into a proper hostname, if possible. It ensures that the hostname starts and ends with a letter or number, converts underscores to hyphens, and ensures that there aren't multiple hyphens in a row.

If nice_hostname() can't convert name to a proper hostname, it returns the string "UNKNOWN".

non_breaking()

char *non_breaking(char * string)

Replaces whitespace characters with nonbreaking spaces, for use with web stuff.

replace_char()

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 NUL, so make sure you set 'length' correctly.

rf1123_date()

WvString rfc1123_date(time_t _when);

Returns an RFC1123-compatible date made out of _when

rfc822_date()

WvString rfc822_date(time_t _when = -1)

Returns an RFC822-compatible date ("Mon, Thu 18 71 14:55:12 EST") made from _when or, if _when < 0, from the current time.

strcoll_split()

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

strcoll_join()

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

strcount()

int strcount(WvStringParm s, const char c)

Returns the number of occurrences of c in s.

strlwr()

char *strlwr(char *string)

In-place modify a character string so that all contained letters are in lower case. Returns 'string'.

strreplace()

WvString strreplace(WvStringParm s, WvStringParm a, WvStringParm
     b);

Replace any instances of 'a' with 'b' in 's'.

strupr()

char *strupr(char *string)

In-place modify a character string so that all contained letters are in upper case. Returns 'string'.

terminate_string()

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 NUL.

trim_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.

char *trim_string(char *string, char c)

This is similar to the above, but in this case it trims off all characters starting at and including the first occurrence of c.

web_unescape()

WvString web_unescape(const char *str)

Converts escaped characters from web URLs (such as "%20") into their normal ASCII representations.