42#include "utilities/qencode.h"
43#include "utilities/qhash.h"
44#include "utilities/qstring.h"
60 for (ss = str; *ss ==
' ' || *ss ==
'\t' || *ss ==
'\r' || *ss ==
'\n';
63 for (se = ss; *se !=
'\0'; se++)
67 && (*se ==
' ' || *se ==
'\t' || *se ==
'\r' || *se ==
'\n');
74 size_t len = (se - ss) + 1;
75 memmove(str, ss, len);
95 for (ss = str; *ss ==
' ' || *ss ==
'\t' || *ss ==
'\r' || *ss ==
'\n';
100 size_t len = strlen(ss) + 1;
101 memmove(str, ss, len);
121 for (se = str + strlen(str) - 1;
123 && (*se ==
' ' || *se ==
'\t' || *se ==
'\r' || *se ==
'\n');
153 int len = strlen(str);
154 if (len >= 2 && str[0] ==
head && str[len - 1] == tail) {
155 memmove(str, str + 1, len - 2);
236char *
qstrreplace(
const char *mode,
char *srcstr,
const char *tokstr,
238 if (mode == NULL || strlen(mode) != 2|| srcstr == NULL || tokstr == NULL
240 DEBUG(
"Unknown mode \"%s\".", mode);
244 char *newstr, *newp, *srcp, *tokenp, *retp;
245 newstr = newp = srcp = tokenp = retp = NULL;
247 char method = mode[0], memuse = mode[1];
248 int maxstrlen, tokstrlen;
252 maxstrlen = strlen(srcstr) * ((strlen(word) > 0) ? strlen(word) : 1);
253 newstr = (
char *) malloc(maxstrlen + 1);
257 for (srcp = (
char *) srcstr, newp = newstr; *srcp; srcp++) {
258 for (tokenp = (
char *) tokstr; *tokenp; tokenp++) {
259 if (*srcp == *tokenp) {
261 for (wordp = (
char *) word; *wordp; wordp++) {
271 }
else if (method ==
's') {
272 if (strlen(word) > strlen(tokstr)) {
273 maxstrlen = ((strlen(srcstr) / strlen(tokstr)) * strlen(word))
274 + (strlen(srcstr) % strlen(tokstr));
276 maxstrlen = strlen(srcstr);
278 newstr = (
char *) malloc(maxstrlen + 1);
282 tokstrlen = strlen(tokstr);
284 for (srcp = srcstr, newp = newstr; *srcp; srcp++) {
285 if (!strncmp(srcp, tokstr, tokstrlen)) {
287 for (wordp = (
char *) word; *wordp; wordp++)
289 srcp += tokstrlen - 1;
295 DEBUG(
"Unknown mode \"%s\".", mode);
302 else if (memuse ==
'r') {
303 strcpy(srcstr, newstr);
307 DEBUG(
"Unknown mode \"%s\".", mode);
325char *
qstrcpy(
char *dst,
size_t size,
const char *src) {
326 if (dst == NULL || size == 0 || src == NULL)
329 size_t nbytes = strlen(src);
330 return qstrncpy(dst, size, src, nbytes);
344char *
qstrncpy(
char *dst,
size_t size,
const char *src,
size_t nbytes) {
345 if (dst == NULL || size == 0 || src == NULL)
350 memmove((
void *) dst, (
void *) src, nbytes);
365 DYNAMIC_VSPRINTF(str, format);
369 char *dup = strdup(str);
386 if ((s = strstr(str, start)) == NULL)
391 if ((e = strstr(s, end)) == NULL)
396 char *buf = (
char *) malloc(
sizeof(
char) * (len + 1));
400 strncpy(buf, s, len);
414 if (data == NULL || size == 0) {
418 void *newdata = malloc(size);
419 if (newdata == NULL) {
423 memcpy(newdata, data, size);
435char *
qstrcatf(
char *str,
const char *format, ...) {
437 DYNAMIC_VSPRINTF(buf, format);
441 char *ret = strcat(str, buf);
468char *
qstrgets(
char *buf,
size_t size,
char **offset) {
469 if (offset == NULL || *offset == NULL || **offset ==
'\0')
473 char *from = *offset;
475 for (i = 0; *from !=
'\0' && i < (size - 1); i++, from++) {
505 for (p1 = str, p2 = str + (strlen(str) - 1); p2 > p1; p1++, p2--) {
528 for (cp = str; *cp; cp++)
529 if (*cp >=
'a' && *cp <=
'z')
548 for (cp = str; *cp; cp++)
549 if (*cp >=
'A' && *cp <=
'Z')
583char *
qstrtok(
char *str,
const char *delimiters,
char *retstop,
int *offset) {
584 char *tokensp, *tokenep;
586 tokensp = tokenep = (
char *) (str + *offset);
587 int numdel = strlen(delimiters);
588 for (; *tokenep; tokenep++) {
590 for (j = 0; j < numdel; j++) {
591 if (*tokenep == delimiters[j]) {
593 *retstop = delimiters[j];
596 *offset = tokenep - str;
604 if (tokensp != tokenep) {
605 *offset = tokenep - str;
630 qlist_t *list =
qlist(0);
635 char *dupstr = strdup(str);
638 for (i = 1, token =
qstrtok(dupstr, delimiters, NULL, &offset);
640 token =
qstrtok(dupstr, delimiters, NULL, &offset), i++) {
641 list->addlast(list, token, strlen(token) + 1);
664 GetSystemTimeAsFileTime(&ft);
665 usec = ft.dwLowDateTime % 1000000;
668 gettimeofday(&tv, NULL);
673 snprintf(uniquestr,
sizeof(uniquestr),
"%u%d%lu%ld%s", getpid(), rand(),
674 (
unsigned long)time(NULL), usec, (seed != NULL) ? seed :
"");
676 unsigned char md5hash[16];
677 qhashmd5(uniquestr, strlen(uniquestr), md5hash);
694 str = strp = (
char *) malloc(
sizeof(
char) * (14 + 1));
698 char buf[10 + 1], *bufp;
699 snprintf(buf,
sizeof(buf),
"%d", abs(number));
703 for (bufp = buf; *bufp !=
'\0'; strp++, bufp++) {
705 if ((strlen(bufp)) % 3 == 1 && *(bufp + 1) !=
'\0')
752bool qstrtest(
int (*testfunc)(
int),
const char *str) {
753 for (; *str; str++) {
754 if (testfunc(*str) == 0)
768 int i, alpa, dot, gol;
773 for (i = alpa = dot = gol = 0; email[i] !=
'\0'; i++) {
784 if ((i > 0) && (email[i - 1] ==
'@'))
786 if ((gol > 0) && (email[i - 1] ==
'.'))
793 if ((email[i] >=
'0') && (email[i] <=
'9'))
795 else if ((email[i] >=
'A') && (email[i] <=
'Z'))
797 else if ((email[i] >=
'a') && (email[i] <=
'z'))
799 else if ((email[i] ==
'-') || (email[i] ==
'_'))
807 if ((alpa <= 3) || (gol == 0) || (dot == 0))
826 char *dupstr = strdup(str);
830 for (s1 = dupstr, periodcnt = 0; (s2 = strchr(s1,
'.')) != NULL;
831 s1 = s2 + 1, periodcnt++) {
835 if (
qstrtest(isdigit, s1) ==
false || (n = atoi(s1)) <= 0 || n >= 256) {
867 const char *tocode,
float mag) {
872 char *fromstr = (
char *) str;
873 size_t fromsize = strlen(fromstr) + 1;
875 size_t tosize =
sizeof(char) * ((mag * (fromsize - 1)) + 1);
876 char *tostr = (
char *) malloc(tosize);
880 char *tostr1 = tostr;
882 iconv_t it = iconv_open(tocode, fromcode);
884 DEBUG(
"iconv_open() failed.");
888 int ret = iconv(it, &fromstr, &fromsize, &tostr, &tosize);
892 DEBUG(
"iconv() failed.");
char * qhex_encode(const void *bin, size_t size)
Encode data to Hexadecimal digit format.
bool qhashmd5(const void *data, size_t nbytes, void *retbuf)
Calculate 128-bit(16-bytes) MD5 hash.
static bool head(qhttpclient_t *client, const char *uri, int *rescode, qlisttbl_t *reqheaders, qlisttbl_t *resheaders)
qhttpclient->head(): Sends a HEAD request.
qlist_t * qlist(int options)
Create new qlist_t linked-list container.
char * qstrrev(char *str)
Reverse the order of characters in the string.
char * qstrtok(char *str, const char *delimiters, char *retstop, int *offset)
Split string into tokens.
char * qstrcpy(char *dst, size_t size, const char *src)
Copy src string to dst.
char * qstrunique(const char *seed)
Generate unique id.
qlist_t * qstrtokenizer(const char *str, const char *delimiters)
String Tokenizer.
char * qstrncpy(char *dst, size_t size, const char *src, size_t nbytes)
Copy src string to dst no more than n bytes.
char * qstrtrim(char *str)
Remove white spaces(including CR, LF) from head and tail of the string.
bool qstrtest(int(*testfunc)(int), const char *str)
Test for an alpha-numeric string.
char * qstrtrim_head(char *str)
Remove heading white spaces of the string.
char * qstrcatf(char *str, const char *format,...)
Append formatted string to the end of the source str.
char * qstrunchar(char *str, char head, char tail)
Remove character from head and tail of the string.
char * qstrdup_between(const char *str, const char *start, const char *end)
Duplicate a substing set.
bool qstr_is_ip4addr(const char *str)
Test for an IPv4 address string.
char * qstrdupf(const char *format,...)
Duplicate a formatted string.
void * qmemdup(const void *data, size_t size)
Duplicate a copy of memory data.
char * qstrtrim_tail(char *str)
Remove tailing white spaces(including CR, LF) of the string.
char * qstrgets(char *buf, size_t size, char **offset)
Get one line from the string offset.
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 * qstrupper(char *str)
Convert character to bigger character.
char * qstrlower(char *str)
Convert character to lower character.
char * qstr_comma_number(int number)
Convert integer to comma string.
char * qstr_conv_encoding(const char *str, const char *fromcode, const char *tocode, float mag)
Convert character encoding.
bool qstr_is_email(const char *email)
Test for an email-address formatted string.