Chapter 1. Basic String Handling

Table of Contents
String Utilities (strutils.cc)
Version String Manipulation (verstring.cc)
Base64 Encoding Tools (base64.cc)

Here are some particularly simple C functions for manipulating character strings. In general, the only reason they sit in C++ files is to make it easier to link with our C++ functions -- they don't use many C++ features.

String Utilities (strutils.cc)

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.

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.

strlwr()

char *strlwr(char *string)

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

is_word()

bool is_word(char *string)

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

hexdump_buffer()

WvString hexdump_buffer(unsigned char *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.

hexify()

void hexify(char *obuf, unsigned char *ibuf, size_t len)

Write the contents of the binary string of length 'len' pointed to by 'ibuf' into the output buffer 'obuf' in hexadecimal format.

For example, if len==4, ibuf=="ABCDEF", then obuf will contain "41424344" with a terminating NUL character.

This is useful to turn arbitrary binary into a simple printable format, so that it can (for example) be written to a WvConf configuration file.

obuf must be a buffer with at least (len * 2) + 1 bytes available. (two digits for each byte of ibuf, plus a terminating NUL).

unhexify()

void unhexify(unsigned char *obuf, char *ibuf)

Reverse the operation performed by hexify(). obuf must be a buffer large enough to contain the entire binary output string; you can calculate this size with (strlen(ibuf) / 2). obuf will NOT be automatically NUL-terminated.