qLibc
qstring.c File Reference

String APIs. More...

Go to the source code of this file.

Functions

char * qstrtrim (char *str)
 Remove white spaces(including CR, LF) from head and tail of the string. More...
 
char * qstrtrim_head (char *str)
 Remove heading white spaces of the string. More...
 
char * qstrtrim_tail (char *str)
 Remove tailing white spaces(including CR, LF) of the string. More...
 
char * qstrunchar (char *str, char head, char tail)
 Remove character from head and tail of the string. More...
 
char * qstrreplace (const char *mode, char *srcstr, const char *tokstr, const char *word)
 Replace string or tokens as word from source string with given mode. More...
 
char * qstrcpy (char *dst, size_t size, const char *src)
 Copy src string to dst. More...
 
char * qstrncpy (char *dst, size_t size, const char *src, size_t nbytes)
 Copy src string to dst no more than n bytes. More...
 
char * qstrdupf (const char *format,...)
 Duplicate a formatted string. More...
 
char * qstrdup_between (const char *str, const char *start, const char *end)
 Duplicate a substing set. More...
 
void * qmemdup (const void *data, size_t size)
 Duplicate a copy of memory data. More...
 
char * qstrcatf (char *str, const char *format,...)
 Append formatted string to the end of the source str. More...
 
char * qstrgets (char *buf, size_t size, char **offset)
 Get one line from the string offset. More...
 
char * qstrrev (char *str)
 Reverse the order of characters in the string. More...
 
char * qstrupper (char *str)
 Convert character to bigger character. More...
 
char * qstrlower (char *str)
 Convert character to lower character. More...
 
char * qstrtok (char *str, const char *delimiters, char *retstop, int *offset)
 Split string into tokens. More...
 
qlist_t * qstrtokenizer (const char *str, const char *delimiters)
 String Tokenizer. More...
 
char * qstrunique (const char *seed)
 Generate unique id. More...
 
char * qstr_comma_number (int number)
 Convert integer to comma string. More...
 
bool qstrtest (int(*testfunc)(int), const char *str)
 Test for an alpha-numeric string. More...
 
bool qstr_is_email (const char *email)
 Test for an email-address formatted string. More...
 
bool qstr_is_ip4addr (const char *str)
 Test for an IPv4 address string. More...
 
char * qstr_conv_encoding (const char *str, const char *fromcode, const char *tocode, float mag)
 Convert character encoding. More...
 

Detailed Description

String APIs.

Definition in file qstring.c.

Function Documentation

char* qstrtrim ( char *  str)

Remove white spaces(including CR, LF) from head and tail of the string.

Parameters
strsource string
Returns
a pointer of source string if rsuccessful, otherewise returns NULL
Note
This modify source string directly.

Definition at line 55 of file qstring.c.

char* qstrtrim_head ( char *  str)

Remove heading white spaces of the string.

Parameters
strsource string
Returns
a pointer of source string if rsuccessful, otherewise returns NULL
Note
This modify source string directly.

Definition at line 90 of file qstring.c.

char* qstrtrim_tail ( char *  str)

Remove tailing white spaces(including CR, LF) of the string.

Parameters
strsource string
Returns
a pointer of source string if rsuccessful, otherewise returns NULL
Note
This modify source string directly.

Definition at line 116 of file qstring.c.

char* qstrunchar ( char *  str,
char  head,
char  tail 
)

Remove character from head and tail of the string.

Parameters
strsource string
headheading character
tailtailing character
Returns
a pointer of source string if successful, otherewise returns NULL
Note
This modify source string directly.
char *str = strdup(" \"hello world\" ");
qstrtrim(str); // to remove white spaces
qstrunchar(str, '"', '"'); // to unquote

Definition at line 149 of file qstring.c.

char* qstrreplace ( const char *  mode,
char *  srcstr,
const char *  tokstr,
const char *  word 
)

Replace string or tokens as word from source string with given mode.

Parameters
modereplacing mode
srcstrsource string
tokstrtoken or string
wordtarget word to be replaced
Returns
a pointer of malloced or source string depending on the mode if successful, otherewise returns NULL
Note
The mode argument has two separated character. First character is used to decide replacing method and can be 't' or 's'. The character 't' and 's' stand on [t]oken and [s]tring.

When 't' is given each character of the token string(third argument) will be compared with source string individually. If matched one is found. the character will be replaced with given work.

If 's' is given instead of 't'. Token string will be analyzed only one chunk word. So the replacement will be occured when the case of whole word matched.

Second character is used to decide returning memory type and can be 'n' or 'r' which are stand on [n]ew and [r]eplace.

When 'n' is given the result will be placed into new array so you should free the return string after using. Instead of this, you can also use 'r' character to modify source string directly. In this case, given source string should have enough space. Be sure that untouchable value can not be used for source string.

So there are four associatable modes such like below.

Mode "tn" : [t]oken replacing & putting the result into [n]ew array. Mode "tr" : [t]oken replacing & [r]eplace source string directly. Mode "sn" : [s]tring replacing & putting the result into [n]ew array. Mode "sr" : [s]tring replacing & [r]eplace source string directly.

char srcstr[256], *retstr;
char mode[4][2+1] = {"tn", "tr", "sn", "sr"};
for(i = 0; i < 4; i++) {
strcpy(srcstr, "Welcome to The qDecoder Project.");
printf("before %s : srcstr = %s\n", mode[i], srcstr);
retstr = qstrreplace(mode[i], srcstr, "The", "_");
printf("after %s : srcstr = %s\n", mode[i], srcstr);
printf(" retstr = %s\n\n", retstr);
if(mode[i][1] == 'n') free(retstr);
}
--[Result]--
before tn : srcstr = Welcome to The qDecoder Project.
after tn : srcstr = Welcome to The qDecoder Project.
retstr = W_lcom_ _o ___ qD_cod_r Proj_c_.
before tr : srcstr = Welcome to The qDecoder Project.
after tr : srcstr = W_lcom_ _o ___ qD_cod_r Proj_c_.
retstr = W_lcom_ _o ___ qD_cod_r Proj_c_.
before sn : srcstr = Welcome to The qDecoder Project.
after sn : srcstr = Welcome to The qDecoder Project.
retstr = Welcome to _ qDecoder Project.
before sr : srcstr = Welcome to The qDecoder Project.
after sr : srcstr = Welcome to _ qDecoder Project.
retstr = Welcome to _ qDecoder Project.

Definition at line 236 of file qstring.c.

char* qstrcpy ( char *  dst,
size_t  size,
const char *  src 
)

Copy src string to dst.

The dst string array will be always terminated by NULL character. Also allows overlap between src and dst.

Parameters
dsta pointer of the string to be copied
sizethe size of dst character arrary
srca pointer of source string
Returns
always returns a pointer of dst

Definition at line 320 of file qstring.c.

char* qstrncpy ( char *  dst,
size_t  size,
const char *  src,
size_t  nbytes 
)

Copy src string to dst no more than n bytes.

The dst string array will be always terminated by NULL character. Also allows overlap between src and dst.

Parameters
dsta pointer of the string to be copied
sizethe size of dst character arrary
srca pointer of source string
nbytesnumber of bytes to copy
Returns
always returns a pointer of dst

Definition at line 339 of file qstring.c.

char* qstrdupf ( const char *  format,
  ... 
)

Duplicate a formatted string.

Parameters
formatstring format
Returns
a pointer of malloced string if successful, otherwise returns NULL

Definition at line 358 of file qstring.c.

char* qstrdup_between ( const char *  str,
const char *  start,
const char *  end 
)

Duplicate a substing set.

Parameters
stra pointer of original string
startsubstring which is started with this
endsubstring which is ended with this
Returns
a pointer of malloced string if successful, otherwise returns NULL

Definition at line 379 of file qstring.c.

void* qmemdup ( const void *  data,
size_t  size 
)

Duplicate a copy of memory data.

Parameters
datasource data
sizedata size
Returns
a pointer of malloced data which's content is identical to source data.

Definition at line 406 of file qstring.c.

char* qstrcatf ( char *  str,
const char *  format,
  ... 
)

Append formatted string to the end of the source str.

Parameters
stra pointer of original string
formatstring format to append
Returns
a pointer of str if successful, otherwise returns NULL

Definition at line 428 of file qstring.c.

char* qstrgets ( char *  buf,
size_t  size,
char **  offset 
)

Get one line from the string offset.

Parameters
bufbuffer pointer
sizebuffer size
offseta offset pointer which point source string
Returns
a pointer of buffer if successful, otherwise(EOF) returns NULL
Note
CR and LF will not be stored.
char *text="Hello\nWorld";
char *offset = text;
char buf[1024];
while(qstrgets(buf, sizeof(buf), &offset) == NULL) {
printf("%s\n", buf);
}

Definition at line 461 of file qstring.c.

char* qstrrev ( char *  str)

Reverse the order of characters in the string.

Parameters
stra pointer of source string
Returns
always returns a pointer of str
Note
This modify str directly.

Definition at line 493 of file qstring.c.

char* qstrupper ( char *  str)

Convert character to bigger character.

Parameters
stra pointer of source string
Returns
always returns a pointer of str
Note
This modify str directly.

Definition at line 516 of file qstring.c.

char* qstrlower ( char *  str)

Convert character to lower character.

Parameters
stra pointer of source string
Returns
always returns a pointer of str
Note
This modify str directly.

Definition at line 536 of file qstring.c.

char* qstrtok ( char *  str,
const char *  delimiters,
char *  retstop,
int *  offset 
)

Split string into tokens.

Parameters
strsource string
delimitersstring that specifies a set of delimiters that may surround the token being extracted
retstopstop delimiter character will be stored. it can be NULL if you don't want to know.
offsetinteger pointer used for store last position. (must be reset to 0)
Returns
a pointer to the first byte of a token if successful, otherwise returns NULL.
char *str = strdup("Hello,world|Thank,you");
char *token;
int offset = 0;
while((token = qstrtok(str, "|,", NULL, &offset)) != NULL) {
printf("%s\n", token);
}
Note
This may modify str argument. The major difference between qstrtok() and standard strtok() is that qstrtok() can returns empty string tokens. If the str is "a:b::d", qstrtok() returns "a", "b", "", "d". But strtok() returns "a","b","d".

Definition at line 576 of file qstring.c.

qlist_t* qstrtokenizer ( const char *  str,
const char *  delimiters 
)

String Tokenizer.

Parameters
strsource string
delimitersstring that specifies a set of delimiters that may surround the token being extracted
Returns
qlist container pointer otherwise returns NULL.
qlist_t *tokens = qstr_tokenizer("a:b:c", ":");
char *str;
while((str = tokens->popfirst(tokens, NULL)) != NULL) {
printf("%s\n", str);
}
tokens->free(tokens);

Definition at line 622 of file qstring.c.

char* qstrunique ( const char *  seed)

Generate unique id.

Parameters
seedadditional seed string. this can be NULL
Returns
a pointer of malloced string
Note
The length of returned string is 32+1 bytes long including terminating NULL character. It's a good idea to call srand() once before calling this because it uses rand().

Definition at line 653 of file qstring.c.

char* qstr_comma_number ( int  number)

Convert integer to comma string.

Parameters
numberinteger
Returns
a pointer of malloced string which contains comma separated number if successful, otherwise returns NULL

Definition at line 684 of file qstring.c.

bool qstrtest ( int(*)(int)  testfunc,
const char *  str 
)

Test for an alpha-numeric string.

Parameters
testfunctest function for individual character
stra pointer of string
Returns
true for ok, otherwise returns false
if(qstrtest(isalnum, "hello1234") == true) {
printf("It is alpha-numeric string.");
}
if(qstrtest(isdigit, "0123456789") == true) {
printf("It is alpha-numeric string.");
}
Note
Basically you can use below test functios without creating your own version. Make sure <ctype.h> header should be included before using any of these functions. isalnum - checks for an alphanumeric character. isalpha - checks for an alphabetic character. isascii - checks whether c is a 7-bit unsigned char value that fits into the ASCII character set. isblank - checks for a blank character; that is, a space or a tab. iscntrl - checks for a control character. isdigit - checks for a digit (0 through 9). isgraph - checks for any printable character except space. islower - checks for a lower-case character. isprint - checks for any printable character including space. ispunct - checks for any printable character which is not a space or an alphanumeric character. isspace - checks for white-space characters. isupper - checks for an uppercase letter. isxdigit - checks for a hexadecimal digits. Please refer "man isalnum" for more details about these functions.

Definition at line 745 of file qstring.c.

bool qstr_is_email ( const char *  email)

Test for an email-address formatted string.

Parameters
emailemail-address formatted string
Returns
true if successful, otherwise returns false

Definition at line 760 of file qstring.c.

bool qstr_is_ip4addr ( const char *  str)

Test for an IPv4 address string.

Parameters
urlIPv4 address string
Returns
true if successful, otherwise returns false
if(qstr_is_ip4addr("1.2.3.4") == true) {
printf("It is IPv4 address string.");
}

Definition at line 818 of file qstring.c.

char* qstr_conv_encoding ( const char *  str,
const char *  fromcode,
const char *  tocode,
float  mag 
)

Convert character encoding.

Parameters
stradditional seed string. this can be NULL
fromcodeencoding type of str
tocodeencoding to convert
magmagnification between fromcode and tocode
Returns
a pointer of malloced converted string if successful, otherwise returns NULL
qCharEncode("KOREAN_EUCKR_STRING", "EUC-KR", "UTF-8", 1.5);

Definition at line 859 of file qstring.c.