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

Detailed Description

String APIs.

Definition in file qstring.c.

Function Documentation

◆ qstrtrim()

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 successful, otherwise returns NULL
Note
This modify source string directly.

Definition at line 55 of file qstring.c.

◆ qstrtrim_head()

char * qstrtrim_head ( char *  str)

Remove heading white spaces of the string.

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

Definition at line 90 of file qstring.c.

◆ qstrtrim_tail()

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 successful, otherwise returns NULL
Note
This modify source string directly.

Definition at line 116 of file qstring.c.

◆ qstrunchar()

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, otherwise returns NULL
Note
This modify source string directly.
char *str = strdup(" \"hello world\" ");
qstrtrim(str); // to remove white spaces
qstrunchar(str, '"', '"'); // to unquote
char * qstrtrim(char *str)
Remove white spaces(including CR, LF) from head and tail of the string.
Definition qstring.c:55
char * qstrunchar(char *str, char head, char tail)
Remove character from head and tail of the string.
Definition qstring.c:149

Definition at line 149 of file qstring.c.

◆ qstrreplace()

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, otherwise returns NULL
Note
The mode argument has two separated characters. 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.
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.
Definition qstring.c:236

Definition at line 236 of file qstring.c.

◆ qstrcpy()

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 325 of file qstring.c.

◆ qstrncpy()

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 344 of file qstring.c.

◆ qstrdupf()

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 363 of file qstring.c.

◆ qstrdup_between()

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 384 of file qstring.c.

◆ qmemdup()

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 413 of file qstring.c.

◆ qstrcatf()

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 435 of file qstring.c.

◆ qstrgets()

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);
}
char * qstrgets(char *buf, size_t size, char **offset)
Get one line from the string offset.
Definition qstring.c:468

Definition at line 468 of file qstring.c.

◆ qstrrev()

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 500 of file qstring.c.

◆ qstrupper()

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 523 of file qstring.c.

◆ qstrlower()

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 543 of file qstring.c.

◆ qstrtok()

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);
}
char * qstrtok(char *str, const char *delimiters, char *retstop, int *offset)
Split string into tokens.
Definition qstring.c:583
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 583 of file qstring.c.

◆ qstrtokenizer()

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 629 of file qstring.c.

◆ qstrunique()

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 660 of file qstring.c.

◆ qstr_comma_number()

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 691 of file qstring.c.

◆ qstrtest()

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.");
}
bool qstrtest(int(*testfunc)(int), const char *str)
Test for an alpha-numeric string.
Definition qstring.c:752
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 752 of file qstring.c.

◆ qstr_is_email()

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 767 of file qstring.c.

◆ qstr_is_ip4addr()

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.");
}
bool qstr_is_ip4addr(const char *str)
Test for an IPv4 address string.
Definition qstring.c:825

Definition at line 825 of file qstring.c.

◆ qstr_conv_encoding()

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 866 of file qstring.c.