#include <stdio.h>
#include <string.h>
#include "plabla.h"
#include "array.h"
#include "hlrmisc.h"
Go to the source code of this file.
Data Structures | |
struct | wordIterStruct |
WordIter. More... | |
Defines | |
#define | strDiffer(s1, s2) (strcmp((s1),(s2)) != 0) |
Check if s1 and s2 are different. | |
#define | strEqual(s1, s2) (strcmp((s1),(s2)) == 0) |
Check if s1 and s2 are equal. | |
#define | string(stringa) arrp(stringa,0,char) |
Convert Stringa to char*. | |
#define | Stringa Array |
Array of char that is null-terminated (Stringa). | |
#define | stringCreateClear(s, n) {if(s) stringClear(s); else s=stringCreate(n);} |
stringCreateClear. | |
#define | stringCreateOnce(s, n) {if(!s) s=stringCreate(n);} |
stringCreateOnce. | |
#define | stringDestroy arrayDestroy |
Destroy a Stringa. | |
#define | stringLen(stringa) (arrayMax(stringa)-1) |
Get the length of a Stringa (not counting the null-termination character). | |
#define | strNDiffer(s1, s2, n) (strncmp((s1),(s2),n) != 0) |
Check if the first n characters of s1 and s2 are different. | |
#define | strNEqual(s1, s2, n) (strncmp((s1),(s2),n) == 0) |
Check if the first n characters of s1 and s2 are equal. | |
#define | strStartsWith(s1, s2) (strncmp((s1),(s2),strlen(s2)) == 0) |
Check if s1 starts with s2. | |
#define | strStartsWithC(s1, s2) (strncmp((s1),(s2),sizeof(s2)-1) == 0) |
Check if s1 starts with s2. | |
#define | Texta Array |
A Texta is an Array of char*. | |
#define | textAdd(t, s) (array((t),arrayMax(t),char*)=hlr_strdup(s)) |
Add a string to the end of a Texta. | |
#define | textCreate(initialSize) arrayCreate(initialSize,char*) |
Create a Texta. | |
#define | textCreateClear(t, n) {if(t) textClear(t); else t=textCreate(n);} |
textCreateClear. | |
#define | textDestroy(x) ((x) ? textDestroyFunc(x), x=NULL, 1 : 0) |
Destroy a Texta. | |
#define | textItem(text, index) arru((text),(index),char*) |
Get a pointer to the ith line in a Texta. | |
#define | wordFldIterCreate(s, seps) wordIterCreate(s,seps,0) |
wordFldIterCreate. | |
#define | wordIterDestroy(this) (wordIterDestroy_func(this), this=0) |
wordDestroy. | |
#define | wordNext(this1) (wordNextG((this1),NULL)) |
wordNext. | |
#define | wordTokIterCreate(s, seps) wordIterCreate(s,seps,1) |
wordTokIterCreate. | |
Typedefs | |
typedef struct wordIterStruct * | WordIter |
WordIter. | |
Functions | |
int | getLine (FILE *stream, char **buffer, int *buflen) |
Read an arbitrary long line from stream, functionality analog to gets(3S). | |
int | isBlankStr (char *s) |
Check if s is blank. | |
char * | strCaseStr (char *s, char *t) |
Case-insensitive version of strstr(3C) from the C-libarary. | |
char * | strCopySubstr (char *pszString, char cBegin, char cEnd, Array acSubstr) |
From a supplied string copy a substring delimited by two supplied characters, excluding these characters. | |
int | strEndsWith (char *s, char *suffix) |
Checks if string 's' end with 'suffix'. | |
int | stringAppendf (Stringa str, char *format,...) |
Same as stringPrintf(), but the result is appended to 'str'. | |
void | stringCatChar (Stringa s, char c) |
Append single character 'c' to a Stringa 's'. | |
void | stringChop (Stringa s, int n) |
Remove the trailing n chars from s. | |
void | stringClear (Array s1) |
Erases the contents of Array s1, leaving an empty string. | |
Stringa | stringCreate (int initialSize) |
Create an array of char and make it null-terminated. | |
void | stringInsert (Stringa s, int p, char *i) |
Insert string 'i' at position p into array string 's'. | |
void | stringNCat (Stringa s1, char *s2, int n) |
Appends first n chars from string s2 to s1. | |
void | stringNCpy (Stringa s1, char *s2, int n) |
Analogous to strncpy() from the C library. | |
char * | stringPrintBuf (char *format,...) |
Like sprintf() from the standard C library, but with unlimited length and own memory management. | |
int | stringPrintf (Stringa str, char *format,...) |
Formatted printing into a Stringa (similar to sprintf()). | |
void | stringTerminate (Array s) |
Terminate string. | |
void | stringTerminateI (Array s, int i) |
Terminate string at the i+1 char. | |
void | stringTerminateP (Array s, char *cp) |
Terminate string at position 'cp'. | |
int | stripNlGetLength (char *line) |
Strip the trailing character off line, if present and return the length of the resulting line. | |
void | strReplace (char **s1, char *s2) |
Replace previous contents of s1 with copy of s2. | |
void | strScramble (char *s) |
Encrypt the input string such that it is unreadable to humans and can easily be strUnscrambled() again. | |
int | strTranslate (char *s, char *fromChars, char *toChars) |
Translates each character from 's' which matches one of the characters in 'fromChars' with the corresponding character from 'toChars' or, if this position in 'toChars' is not filled, deletes this character from s, thus shortening 's'. | |
int | strTrim (char *s, char *left, char *right) |
Remove leading and trailing characters from s. | |
void | strUnscramble (char *s) |
Antidot for strScramble(). | |
void | textClear (Texta a) |
Free the strings in 'a' and make the array empty. | |
Texta | textFieldtok (char *s, char *sep) |
Splits string 's' in words separated by any of the characters from string 'sep'. | |
Texta | textFieldtokP (char *s, char *sep) |
Same as textFieldtok() but does not alter its input 's'. | |
void | textJoin (Stringa s, char *sep, Array a) |
textJoin. | |
Texta | textStrtok (char *s, char *sep) |
Splits string 's' in words separated by one or more characters from string 'sep'. | |
Texta | textStrtokP (char *s, char *sep) |
Same as textStrtok(), but does not alter its input 's'. | |
void | textUniqKeepOrder (Texta t) |
Remove duplicate strings from t without changing the order. | |
void | tolowerStr (char *s) |
Converts string to lowercase. | |
void | toupperStr (char *s) |
Converts string to uppercase. | |
WordIter | wordIterCreate (char *s, char *seps, int manySepsAreOne) |
Create an iterator returning the words of 's', broken at any char from 'seps'. | |
void | wordIterDestroy_func (WordIter thisOne) |
wordIterDestroy_func. | |
char * | wordNextG (WordIter this1, int *lenP) |
wordNextG. |
Definition in file format.h.
#define stringCreateClear | ( | s, | |||
n | ) | {if(s) stringClear(s); else s=stringCreate(n);} |
#define stringCreateOnce | ( | s, | |||
n | ) | {if(!s) s=stringCreate(n);} |
#define stringDestroy arrayDestroy |
#define strStartsWith | ( | s1, | |||
s2 | ) | (strncmp((s1),(s2),strlen(s2)) == 0) |
Check if s1 starts with s2.
strStartsWith() always works, but is slower than strStartsWithC()
#define strStartsWithC | ( | s1, | |||
s2 | ) | (strncmp((s1),(s2),sizeof(s2)-1) == 0) |
Check if s1 starts with s2.
Same as strStartsWith, but can only be used if s2 is a string constant, e.g. if (strStartsWithC(s, "CC ")) ...strStartsWith() always works, but is slower than strStartsWithC()
#define Texta Array |
#define textCreate | ( | initialSize | ) | arrayCreate(initialSize,char*) |
#define textCreateClear | ( | t, | |||
n | ) | {if(t) textClear(t); else t=textCreate(n);} |
#define textDestroy | ( | x | ) | ((x) ? textDestroyFunc(x), x=NULL, 1 : 0) |
#define wordFldIterCreate | ( | s, | |||
seps | ) | wordIterCreate(s,seps,0) |
#define wordIterDestroy | ( | this | ) | (wordIterDestroy_func(this), this=0) |
#define wordNext | ( | this1 | ) | (wordNextG((this1),NULL)) |
#define wordTokIterCreate | ( | s, | |||
seps | ) | wordIterCreate(s,seps,1) |
int getLine | ( | FILE * | stream, | |
char ** | buffer, | |||
int * | buflen | |||
) |
Read an arbitrary long line from stream, functionality analog to gets(3S).
usage example: char *line = NULL ; int buflen ; while (getLine(stdin, &line, &buflen)) { printf("%s", line) ; } hlr_free(line) ;
[in] | stream | |
[in] | buffer | Pointer to a string |
[in] | buflen | Current length of buffer, for interal use of getLine() |
[out] | buffer | Might be re-alloated |
[out] | buflen | Adjusted |
int isBlankStr | ( | char * | s | ) |
char* strCaseStr | ( | char * | s, | |
char * | t | |||
) |
Case-insensitive version of strstr(3C) from the C-libarary.
[in] | s | String to be searched in (subject) |
[in] | t | String to look for in s (query) |
char* strCopySubstr | ( | char * | string, | |
char | begin, | |||
char | end, | |||
Array | substr | |||
) |
From a supplied string copy a substring delimited by two supplied characters, excluding these characters.
[in] | string | String to copy from |
[in] | begin | Start copying after the leftmost occurrence of this character in string |
[in] | end | Stop copying before the leftmost occurrence of this character from occurrence of begin on; may be null-terminated to copy to the end of string |
[in] | substr | Stringa, must exist |
[out] | substr | Filled with string extracted; empty string if nothing extracted; |
int strEndsWith | ( | char * | s, | |
char * | suffix | |||
) |
int stringAppendf | ( | Stringa | str, | |
char * | format, | |||
... | ||||
) |
void stringChop | ( | Stringa | s, | |
int | n | |||
) |
Stringa stringCreate | ( | int | initialSize | ) |
Create an array of char and make it null-terminated.
* This pattern occurs very often: * Stringa a = NULL ; * if (a) * stringClear(a) ; * else * a = stringCreate(n) ; * * Therefore there is this shortcut: * #define stringCreateClear(s,n) {if(s) stringClear(s); else s=stringCreate(n);} *
void stringInsert | ( | Stringa | s, | |
int | p, | |||
char * | i | |||
) |
Insert string 'i' at position p into array string 's'.
[in] | s | Destination |
[in] | p | 0 .. arrayMax(s) (=strlen(string(s))) 0 means prepending 'i' to 's', arrayMax(s) means appending (same as stringCat()) |
[in] | i | Source |
[out] | s | Modified |
void stringNCat | ( | Stringa | s1, | |
char * | s2, | |||
int | n | |||
) |
Appends first n chars from string s2 to s1.
Same functionality as strncat().
[in] | s1 | Array of char, null-termintated |
[in] | s2 | String need not be null-terminated, but may not contain internal nulls |
[in] | n | Number of chars from s2 to be copied, if n <= 0, s1 is not changed. n larger than the length of s2 is ok, too. |
[out] | s1 | With first min(n,strlen(s2)) chars from s2 appended |
void stringNCpy | ( | Stringa | s1, | |
char * | s2, | |||
int | n | |||
) |
Analogous to strncpy() from the C library.
Analogous to stringNCat() from this module, except that s1 is cleared before appending.
char* stringPrintBuf | ( | char * | format, | |
... | ||||
) |
Like sprintf() from the standard C library, but with unlimited length and own memory management.
[in] | format | Template to be filled |
[in] | ... | Variable number of arguments, must match 'format' |
int stringPrintf | ( | Stringa | str, | |
char * | format, | |||
... | ||||
) |
Formatted printing into a Stringa (similar to sprintf()).
[in] | str | Target string which receives formatted printing as in sprintf(); must not be NULL; must be created using stringCreate() |
[in] | format | Format string as in sprintf(), only difference: argument position manipulation e.g. "%2$15.12s" or "%*3$s" is not supported |
[in] | ... | Variable argument list, must match format string, else behaviour is undefined |
[out] | str | Contains formatted output |
void stringTerminate | ( | Array | s | ) |
void stringTerminateI | ( | Array | s, | |
int | i | |||
) |
Terminate string at the i+1 char.
[in] | i | An index in s, 0 <= i <= stringLen (s), if i > stringLen (s) or i < 0 is an error |
[in] | s | A valid Stringa of length i |
[out] | s | *cp in s will be null-terminated and stringLen(s) is adjusted |
void stringTerminateP | ( | Array | s, | |
char * | cp | |||
) |
Terminate string at position 'cp'.
[in] | cp | A pointer to a position within the string |
[in] | s | A valid Stringa |
[out] | s | *cp in s will be null-terminated and stringLen(s) is adjusted |
int stripNlGetLength | ( | char * | line | ) |
void strReplace | ( | char ** | s1, | |
char * | s2 | |||
) |
Replace previous contents of s1 with copy of s2.
s2 can be NULL. This function is the same as strdup() from the C library, except that it free()s the target string before duplicating.
[in] | s1 | Place where a pointer to a string is stored |
[in] | s2 | Contents of s2 will replace contents of s1 |
[out] | s2 | Pervious contents free()d, new memory allocated |
void strScramble | ( | char * | s | ) |
int strTranslate | ( | char * | s, | |
char * | fromChars, | |||
char * | toChars | |||
) |
Translates each character from 's' which matches one of the characters in 'fromChars' with the corresponding character from 'toChars' or, if this position in 'toChars' is not filled, deletes this character from s, thus shortening 's'.
This function resembles the Unix command and the Perl function 'tr'.
example: strTranslate("abc", "ac", "b") modifies "abc" into "bb" and returns 2 strTranslate("a|b|c", "|", "|") just counts the number of '|' chars
[in] | s | |
[in] | fromChars | |
[in] | toChars | |
[out] | s |
int strTrim | ( | char * | s, | |
char * | left, | |||
char * | right | |||
) |
Remove leading and trailing characters from s.
example: strTrim("<<=text=>>", "=<", "=") returns 7 and leaves output "text=>>"
[in] | s | Zero-terminated string of char or NULL (nothing will happen) |
[in] | left | Set of chars to be removed from left end of s, NULL or empty string to leave beginning of s as is |
[in] | right | Set of chars to be removed from right end of s, NULL or empty string to leave tail of s as is |
[out] | s | Changed |
void textClear | ( | Texta | a | ) |
Free the strings in 'a' and make the array empty.
The strings must have been allocated using hlr_malloc() or hlr_strdup() if you expect hlr_getAllocCnt() to work.
Texta textFieldtok | ( | char * | s, | |
char * | sep | |||
) |
Splits string 's' in words separated by any of the characters from string 'sep'.
[in] | s | Input string |
[in] | sep | Separation character(s) |
[out] | s | The contents changed! |
Texta textFieldtokP | ( | char * | s, | |
char * | sep | |||
) |
Same as textFieldtok() but does not alter its input 's'.
Suffix 'P' stands for 'Preserving its input'
void textJoin | ( | Stringa | s, | |
char * | sep, | |||
Array | a | |||
) |
textJoin.
example: Texta t = textStrtok("a b c", " ") ; Stringa s = stringCreate(10) ; textJoin(s, "-", t) ; puts(string(s)) ; prints "a-b-c"
[in] | s | Contents will be overridden |
[in] | sep | Separator between elements, e.g. "," |
[in] | a | Array of char* (can be empty, but not NULL) |
[out] | s | Filled; empty string if empty Array |
Texta textStrtok | ( | char * | s, | |
char * | sep | |||
) |
Splits string 's' in words separated by one or more characters from string 'sep'.
Same result as repeated calls to strtok() from the standard C libary.
[in] | s | Input string |
[in] | sep | Separation character(s) |
[out] | s | The contents changed! |
Texta textStrtokP | ( | char * | s, | |
char * | sep | |||
) |
Same as textStrtok(), but does not alter its input 's'.
Suffix 'P' stands for 'Preserving its input'
void textUniqKeepOrder | ( | Texta | t | ) |
Remove duplicate strings from t without changing the order.
Note on runtime complexity: Execution time: O(n*n*log n) (where n is arrayMax(t)) The implementation below has a lot of room for improvments, e.g. - using a hashed lookup table for seen values would reduce runtime complexity to O(n) - if no hashed lookup table availabe: s = arrayCopy(t) -- O(n) arraySort(s) -- O(n * log n) remove duplicates in s -- O(n) f = arrayCopy(s) -- O(n) remove duplicates in t with lookup in s and flagging in f -- O(n * log n) would trade space for time
[in] | t | |
[in] | t | Duplicates removed, first occurences kept |
WordIter wordIterCreate | ( | char * | s, | |
char * | seps, | |||
int | manySepsAreOne | |||
) |
Create an iterator returning the words of 's', broken at any char from 'seps'.
If 'manySepsAreOne' is 1, consequtive stretches of separators are treated as one separator (so there are no empty words).
[in] | s | String to break |
[in] | seps | Set of word separator chars |
[in] | manySepsAreOne | 1 or 0 |
[out] | s | Is destroyed |
if 's' is the empty string, then in mode manySepsAreOne==1, wordNext() will immediately return NULL, in mode manySepsAreOne==0, wordNext() will return one empty string, then NULL
void wordIterDestroy_func | ( | WordIter | this1 | ) |
wordIterDestroy_func.
[in] | this1 | Created by wordIterCreate* |
char* wordNextG | ( | WordIter | this1, | |
int * | lenP | |||
) |
wordNextG.
[in] | this1 | Created by wordTokIterCreate() or wordFldIterCreate() |
[in] | lenP | Valid place to put an int; NULL if no interest |
[out] | lenP | If lenP not NULL: strlen() of result, computed efficiently |