#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "hlrmisc.h"
#include "log.h"
#include "format.h"
Go to the source code of this file.
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 *string, char begin, char end, Array substr) |
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 | stringCat (Array s1, char *s2) |
Appends null-terminated string s2 to s1. | |
void | stringCatChar (Stringa s, char c) |
Append single character 'c' to a Stringa 's'. | |
void | stringCatInt (Array s, int i) |
Convert i into a string and append it to string Array 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. | |
void | stringCpy (Array s1, char *s2) |
Copies null-terminated string s2 to s1. | |
Array | stringCreate (int initialSize) |
Create an array of char and make it null-terminated. | |
char * | stringCut (Array s, int pos, int len) |
Removes a string segment. | |
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 | stringTranslate (Array s, char *fromChars, char *toChars) |
Exactly like strTranslate(), but with a Stringa. | |
int | stringTrim (Array s, char *left, char *right) |
Exactly like strTrim(), but with a Stringa. | |
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 | textClone (Array a) |
Produce a clone of the Array of strings 'a'. | |
void | textDestroyFunc (Texta a) |
Free storage allocated to text a. | |
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 this1) |
wordIterDestroy_func. | |
char * | wordNextG (WordIter this1, int *lenP) |
wordNextG. |
Definition in file format.c.
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 stringCat | ( | Array | s1, | |
char * | s2 | |||
) |
void stringChop | ( | Stringa | s, | |
int | n | |||
) |
void stringCpy | ( | Array | s1, | |
char * | s2 | |||
) |
Array 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);} *
char* stringCut | ( | Array | s, | |
int | pos, | |||
int | len | |||
) |
Removes a string segment.
Removes the segment [pos..pos+len-1] from s and returns a pointer to a copy of this segment. If pos is beyond the end the string, nothing is done and NULL returned; If pos is within the string but pos+len-1 is beyond the end of the string or len is -1, the tail of string starting at pos is removed.
[in] | s | A valid Stringa |
[in] | pos | Start position of the segment to extract (based on 0), pos >= 0 |
[in] | len | Number of chars to cut out, -1 means as many as possible; it is ok if len asks for more than is actually present; if len is 0 s is not changed and an empty string returned. |
[out] | s | Without specified segment |
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 stringTranslate | ( | Array | s, | |
char * | fromChars, | |||
char * | toChars | |||
) |
int stringTrim | ( | Array | s, | |
char * | left, | |||
char * | right | |||
) |
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 textClone | ( | Array | a | ) |
void textDestroyFunc | ( | Texta | a | ) |
Free storage allocated to text a.
[in] | a | Array of char*; the char* should have been allocated with hlr_malloc() |
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 |