00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <stdbool.h>
00033 #include <string.h>
00034 #include <stdarg.h>
00035 #include <unistd.h>
00036 #include <ctype.h>
00037 #include <sys/types.h>
00038 #include <sys/time.h>
00039 #include "qDecoder.h"
00040 #include "qInternal.h"
00041
00051 char *qStrTrim(char *str) {
00052 int i, j;
00053
00054 if (str == NULL) return NULL;
00055 for (j = 0; str[j] == ' ' || str[j] == '\t' || str[j] == '\r' || str[j] == '\n'; j++);
00056 for (i = 0; str[j] != '\0'; i++, j++) str[i] = str[j];
00057 for (i--; (i >= 0) && (str[i] == ' ' || str[i] == '\t' || str[i] == '\r' || str[i] == '\n'); i--);
00058 str[i+1] = '\0';
00059
00060 return str;
00061 }
00062
00072 char *qStrTrimTail(char *str) {
00073 int i;
00074
00075 if (str == NULL)return NULL;
00076 for (i = strlen(str) - 1; (i >= 0) && (str[i] == ' ' || str[i] == '\t' || str[i] == '\r' || str[i] == '\n'); i--);
00077 str[i+1] = '\0';
00078
00079 return str;
00080 }
00081
00099 char *qStrUnchar(char *str, char head, char tail) {
00100 if (str == NULL) return NULL;
00101
00102 int len = strlen(str);
00103 if (len >= 2 && str[0] == head && str[len-1] == tail) {
00104 memmove(str, str + 1, len - 2);
00105 str[len - 2] = '\0';
00106 }
00107
00108 return str;
00109 }
00110
00182 char *qStrReplace(const char *mode, char *srcstr, const char *tokstr, const char *word) {
00183 if (mode == NULL || strlen(mode) != 2 || srcstr == NULL || tokstr == NULL || word == NULL) {
00184 DEBUG("Unknown mode \"%s\".", mode);
00185 return NULL;
00186 }
00187
00188 char *newstr, *newp, *srcp, *tokenp, *retp;
00189 newstr = newp = srcp = tokenp = retp = NULL;
00190
00191 char method = mode[0], memuse = mode[1];
00192 int maxstrlen, tokstrlen;
00193
00194
00195 if (method == 't') {
00196 maxstrlen = strlen(srcstr) * ( (strlen(word) > 0) ? strlen(word) : 1 );
00197 newstr = (char*)malloc(maxstrlen + 1);
00198
00199 for (srcp = (char*)srcstr, newp = newstr; *srcp; srcp++) {
00200 for (tokenp = (char*)tokstr; *tokenp; tokenp++) {
00201 if (*srcp == *tokenp) {
00202 char *wordp;
00203 for (wordp = (char*)word; *wordp; wordp++) *newp++ = *wordp;
00204 break;
00205 }
00206 }
00207 if (!*tokenp) *newp++ = *srcp;
00208 }
00209 *newp = '\0';
00210 } else if (method == 's') {
00211 if (strlen(word) > strlen(tokstr)) maxstrlen = ((strlen(srcstr) / strlen(tokstr)) * strlen(word)) + (strlen(srcstr) % strlen(tokstr));
00212 else maxstrlen = strlen(srcstr);
00213 newstr = (char*)malloc(maxstrlen + 1);
00214 tokstrlen = strlen(tokstr);
00215
00216 for (srcp = srcstr, newp = newstr; *srcp; srcp++) {
00217 if (!strncmp(srcp, tokstr, tokstrlen)) {
00218 char *wordp;
00219 for (wordp = (char*)word; *wordp; wordp++) *newp++ = *wordp;
00220 srcp += tokstrlen - 1;
00221 } else *newp++ = *srcp;
00222 }
00223 *newp = '\0';
00224 } else {
00225 DEBUG("Unknown mode \"%s\".", mode);
00226 return NULL;
00227 }
00228
00229
00230 if (memuse == 'n') retp = newstr;
00231 else if (memuse == 'r') {
00232 strcpy(srcstr, newstr);
00233 free(newstr);
00234 retp = srcstr;
00235 } else {
00236 DEBUG("Unknown mode \"%s\".", mode);
00237 free(newstr);
00238 return NULL;
00239 }
00240
00241 return retp;
00242 }
00243
00256 char *qStrCpy(char *dst, size_t dstsize, const char *src, size_t nbytes) {
00257 if(dst == NULL || dstsize == 0 || src == NULL || nbytes == 0) return dst;
00258
00259 if(nbytes >= dstsize) nbytes = dstsize - 1;
00260 strncpy(dst, src, nbytes);
00261 dst[nbytes] = '\0';
00262
00263 return dst;
00264 }
00265
00275 char *qStrUpper(char *str) {
00276 char *cp;
00277
00278 if (!str) return NULL;
00279 for (cp = str; *cp; cp++) if (*cp >= 'a' && *cp <= 'z') *cp -= 32;
00280 return str;
00281 }
00282
00292 char *qStrLower(char *str) {
00293 char *cp;
00294
00295 if (!str) return NULL;
00296 for (cp = str; *cp; cp++) if (*cp >= 'A' && *cp <= 'Z') *cp += 32;
00297 return str;
00298 }
00299
00300
00309 char *qStrCaseStr(const char *s1, const char *s2) {
00310 if (s1 == NULL || s2 == NULL) return NULL;
00311
00312 char *s1p = strdup(s1);
00313 char *s2p = strdup(s2);
00314 if (s1p == NULL || s2p == NULL) {
00315 if(s1p != NULL) free(s1p);
00316 if(s2p != NULL) free(s2p);
00317 return NULL;
00318 }
00319
00320 qStrUpper(s1p);
00321 qStrUpper(s2p);
00322
00323 char *sp = strstr(s1p, s2p);
00324 if (sp != NULL) sp = (char*)s1 + (sp - s1p);
00325 free(s1p);
00326 free(s2p);
00327
00328 return sp;
00329 }
00330
00340 char *qStrRev(char *str) {
00341 if (str == NULL) return str;
00342
00343 char *p1, *p2;
00344 for (p1 = str, p2 = str + (strlen(str) - 1); p2 > p1; p1++, p2--) {
00345 char t = *p1;
00346 *p1 = *p2;
00347 *p2 = t;
00348 }
00349
00350 return str;
00351 }
00352
00366 char *qStrTok(char *str, const char *delimiters, char *retstop) {
00367 static char *tokenep;
00368 char *tokensp;
00369 int i, j;
00370
00371 if (str != NULL) tokensp = tokenep = str;
00372 else tokensp = tokenep;
00373
00374 for (i = strlen(delimiters); *tokenep; tokenep++) {
00375 for (j = 0; j < i; j++) {
00376 if (*tokenep == delimiters[j]) {
00377 if (retstop != NULL) *retstop = delimiters[j];
00378 *tokenep = '\0';
00379 tokenep++;
00380 return tokensp;
00381 }
00382 }
00383 }
00384
00385 if (retstop != NULL) *retstop = '\0';
00386 if (tokensp != tokenep) return tokensp;
00387 return NULL;
00388 }
00389
00412 Q_ENTRY *qStrTokenizer(char *str, const char *delimiters) {
00413 Q_ENTRY *entry = qEntryInit();
00414 char *token;
00415 int i;
00416 for(i = 1, token = qStrTok(str, delimiters, NULL); token != NULL; token = qStrTok(NULL, delimiters, NULL), i++) {
00417 char key[10+1];
00418 sprintf(key, "%d", i);
00419 qEntryPutStr(entry, key, token, false);
00420 }
00421
00422 return entry;
00423 }
00424
00432 char *qStrCommaNumber(int number) {
00433 char *str, *strp;
00434
00435 str = strp = (char*)malloc(sizeof(char) * (14+1));
00436 if(str == NULL) return NULL;
00437
00438 char buf[10+1], *bufp;
00439 snprintf(buf, sizeof(buf), "%d", abs(number));
00440
00441 if (number < 0) *strp++ = '-';
00442 for (bufp = buf; *bufp != '\0'; strp++, bufp++) {
00443 *strp = *bufp;
00444 if ((strlen(bufp)) % 3 == 1 && *(bufp + 1) != '\0') *(++strp) = ',';
00445 }
00446 *strp = '\0';
00447
00448 return str;
00449 }
00450
00459 char *qStrCatf(char *str, const char *format, ...) {
00460 char buf[MAX_LINEBUF];
00461 va_list arglist;
00462
00463 va_start(arglist, format);
00464 vsnprintf(buf, sizeof(buf), format, arglist);
00465 va_end(arglist);
00466
00467 return strcat(str, buf);
00468 }
00469
00479 char *qStrDupBetween(const char *str, const char *start, const char *end) {
00480 char *s;
00481 if ((s = strstr(str, start)) == NULL) return NULL;
00482 s += strlen(start);
00483
00484 char *e;
00485 if ((e = strstr(s, end)) == NULL) return NULL;
00486
00487 int len = e - s;
00488
00489 char *buf = (char*)malloc(sizeof(char) * (len + 1));
00490 qStrCpy(buf, len + 1, s, len);
00491
00492 return buf;
00493 }
00494
00504 char *qStrUnique(const char *seed) {
00505 static int count = 0;
00506
00507 if(count == 0) {
00508 srandom(time(NULL));
00509 }
00510 count++;
00511
00512 long int usec;
00513 #ifdef _WIN32
00514 usec = 0;
00515 #else
00516 struct timeval tv;
00517 gettimeofday(&tv, NULL);
00518 usec = tv.tv_usec;
00519 #endif
00520
00521 char szSeed[128];
00522 snprintf(szSeed, sizeof(szSeed), "%u%d%ld%lu%ld%s", getpid(), count, random(), (unsigned long int)time(NULL), usec, (seed!=NULL?seed:""));
00523 return qHashMd5Str(szSeed, strlen(szSeed));
00524 }
00525
00537 bool qStrIsAlnum(const char *str) {
00538 for (; *str; str++) {
00539 if(isalnum(*str) == 0) return false;
00540 }
00541 return true;
00542 }
00543
00544 #ifdef __linux__
00545 #include <iconv.h>
00546 #endif
00547
00562 char *qStrConvEncoding(const char *str, const char *fromcode, const char *tocode, float mag) {
00563 #ifdef __linux__
00564 if(str == NULL) return NULL;
00565
00566 char *fromstr = (char*)str;
00567 size_t fromsize = strlen(fromstr) + 1;
00568
00569 size_t tosize = sizeof(char) * ((mag * (fromsize - 1)) + 1);
00570 char *tostr = (char *)malloc(tosize);
00571 if(tostr == NULL) return NULL;
00572 char *tostr1 = tostr;
00573
00574 iconv_t it = iconv_open(tocode, fromcode);
00575 if(it < 0) {
00576 DEBUG("iconv_open() failed.");
00577 return NULL;
00578 }
00579
00580 int ret = iconv(it, &fromstr, &fromsize, &tostr, &tosize);
00581 iconv_close(it);
00582
00583 if(ret < 0) {
00584 DEBUG("iconv() failed.");
00585 free(tostr1);
00586 return NULL;
00587 }
00588
00589 return tostr1;
00590 #else
00591 return NULL;
00592 #endif
00593 }