#include "stock.h" int blank(char *s) int nstrcmp(char *a, char *b); int pushstr(void **vstack, char *s) int pushstrn(void **vstack, char *s, int n) int pushstrc(void **vstack, int c ) char *popstr(void **vstack, int vfree) char *strconcat(char *s, ...) Tbl *split(char *s, char c) void str2lower(char *s) void str2upper(char *s) void strsub(char *old, char *pattern, char *replacement, char *new) void strtr(char *s, char *old, char *new) void strtrim(char *s) int whitespace(char *s) char *getaline(FILE *file, char *aline, int n) char *intactline(FILE *file, char **aline, int *size) void chomp (char *s) ;
chomp removes a linefeed from the end of a string (like the perl function of the same name).
whitespace returns non-zero if the null-terminated string s is composed entirely of whitespace characters: space, tab, or newline.
split splits a string into component pieces in the same way that awk does, returning a Tbl list with all the components. Repeated instances of the split character 'c' are equivalent to a single 'c', and leading split characters are ignored. split modifies the input string (similar to strtok(3.gz)) by placing null characters at strategic points. The returned list of character pointers point to places in the input string. This Tbl list should be freed with freetbl(tbl,0).
strconcat concatenates the input string arguments (which must be terminated with a null argument) together and returns the result in a newly allocated string.
strtr translates the characters in s according to the substitutions specified in the strings old and new.
strtrim trims leading and trailing whitespace characters from the string s.
str2lower converts upper case characters to lower case in the string s. str2upper converts lower case characters to upper case in the string s.
strsub looks for occurrences of the plain string pattern in old; any occurrences are replaced with replacement, and the result is left in new (the original string old is not modified).
Repeated calls to pushstr, pushstrn, and pushstrc accumulate the multiple input strings s (or char c for pushstrc) into a buffer which is kept in vstack -- vstack must be initialized to zero for the first call. The current length of the string is returned. popstr returns the resultant concatenated string. If the argument vfree is non-zero, the vstack structure is freed; however the caller must free the returned string. If the argument is zero, the returned char pointer must not be freed, as later calls to pushstr append to that string and may reallocate (move) it.
getaline is a replacement for gets(3.gz), but reads a line from an arbitrary input file, replacing a terminating linefeed with a null byte.
intactline is similar to getaline, but dynamically allocates the input buffer to accommodate the input line, also replacing the terminating linefeed with a null byte.
nstrcmp compares two pointers to strings, but allows the pointers to be null. Two null pointers returns 0, but one null and one not-null returns 1; otherwise the result from strcmp is returned.
void *stk = 0 ; char *a, *b ; pushstr ( &stk, "a" ) ; pushstr ( &stk, "B" ) ; pushstr ( &stk, "c" ) ; a = popstr(&stk, 1) ; free(a) ; a = strconcat("a", "B", "c", 0 ) ; strtr(a, "ac", "df" ) ; if ( strcmp(a,"dBf" ) != 0 ) { complain ( 0, "result of strtr('aBc', 'ac', 'df') is %s", a ) ; } a = strdup ( " Surrounded " ) ; strtrim(a) ; if ( strcmp(a, "Surrounded" ) != 0 ) { complain ( 0, "result of strtrim(' Surrounded ') is %s", a ) ; } a = strdup(" Replace 'e' with 'e;e' everywhere " ) ; allot(char *, b, 1024) ; strsub(a, "e", "e;e", &b ) ; if ( strcmp(b, " Re;eplace;e 'e;e' with 'e;e;e;e' e;eve;erywhe;ere;e " ) != 0 ) { complain ( 0, "strsub failed: \n\t'%s'\n=>\t'%s'", a, b ) ; }
patsub(3) morph(3) string(3.gz)