00001
00014
00015
00016 #ifdef __arm__
00017 #define _GNU_SOURCE
00018 #endif
00019 #include <GKlib.h>
00020
00021
00022
00023
00039
00040 char *gk_strchr_replace(char *str, char *fromlist, char *tolist)
00041 {
00042 gk_idx_t i, j, k;
00043 size_t len, fromlen, tolen;
00044
00045 len = strlen(str);
00046 fromlen = strlen(fromlist);
00047 tolen = strlen(tolist);
00048
00049 for (i=j=0; i<len; i++) {
00050 for (k=0; k<fromlen; k++) {
00051 if (str[i] == fromlist[k]) {
00052 if (k < tolen)
00053 str[j++] = tolist[k];
00054 break;
00055 }
00056 }
00057 if (k == fromlen)
00058 str[j++] = str[i];
00059 }
00060 str[j] = '\0';
00061
00062 return str;
00063 }
00064
00065
00066
00067
00096
00097 int gk_strstr_replace(char *str, char *pattern, char *replacement, char *options,
00098 char **new_str)
00099 {
00100 gk_idx_t i;
00101 int j, rc, flags, global, nmatches;
00102 size_t len, rlen, nlen, offset, noffset;
00103 regex_t re;
00104 regmatch_t matches[10];
00105
00106
00107
00108 flags = REG_EXTENDED;
00109 if (strchr(options, 'i') != NULL)
00110 flags = flags | REG_ICASE;
00111 global = (strchr(options, 'g') != NULL ? 1 : 0);
00112
00113
00114
00115 if ((rc = regcomp(&re, pattern, flags)) != 0) {
00116 len = regerror(rc, &re, NULL, 0);
00117 *new_str = gk_cmalloc(len, "gk_strstr_replace: new_str");
00118 regerror(rc, &re, *new_str, len);
00119 return 0;
00120 }
00121
00122
00123 len = strlen(str);
00124 nlen = 2*len;
00125 noffset = 0;
00126 *new_str = gk_cmalloc(nlen+1, "gk_strstr_replace: new_str");
00127
00128
00129
00130 rlen = strlen(replacement);
00131 offset = 0;
00132 nmatches = 0;
00133 do {
00134 rc = regexec(&re, str+offset, 10, matches, 0);
00135
00136 if (rc == REG_ESPACE) {
00137 gk_free((void **)new_str, LTERM);
00138 *new_str = gk_strdup("regexec ran out of memory.");
00139 regfree(&re);
00140 return 0;
00141 }
00142 else if (rc == REG_NOMATCH) {
00143 if (nlen-noffset < len-offset) {
00144 nlen += (len-offset) - (nlen-noffset);
00145 *new_str = (char *)gk_realloc(*new_str, (nlen+1)*sizeof(char), "gk_strstr_replace: new_str");
00146 }
00147 strcpy(*new_str+noffset, str+offset);
00148 noffset += (len-offset);
00149 break;
00150 }
00151 else {
00152 nmatches++;
00153
00154
00155 if (matches[0].rm_so > 0) {
00156 if (nlen-noffset < matches[0].rm_so) {
00157 nlen += matches[0].rm_so - (nlen-noffset);
00158 *new_str = (char *)gk_realloc(*new_str, (nlen+1)*sizeof(char), "gk_strstr_replace: new_str");
00159 }
00160 strncpy(*new_str+noffset, str+offset, matches[0].rm_so);
00161 noffset += matches[0].rm_so;
00162 }
00163
00164
00165 for (i=0; i<rlen; i++) {
00166 switch (replacement[i]) {
00167 case '\\':
00168 if (i+1 < rlen) {
00169 if (nlen-noffset < 1) {
00170 nlen += nlen + 1;
00171 *new_str = (char *)gk_realloc(*new_str, (nlen+1)*sizeof(char), "gk_strstr_replace: new_str");
00172 }
00173 *new_str[noffset++] = replacement[++i];
00174 }
00175 else {
00176 gk_free((void **)new_str, LTERM);
00177 *new_str = gk_strdup("Error in replacement string. Missing character following '\'.");
00178 regfree(&re);
00179 return 0;
00180 }
00181 break;
00182
00183 case '$':
00184 if (i+1 < rlen) {
00185 j = (int)(replacement[++i] - '0');
00186 if (j < 0 || j > 9) {
00187 gk_free((void **)new_str, LTERM);
00188 *new_str = gk_strdup("Error in captured subexpression specification.");
00189 regfree(&re);
00190 return 0;
00191 }
00192
00193 if (nlen-noffset < matches[j].rm_eo-matches[j].rm_so) {
00194 nlen += nlen + (matches[j].rm_eo-matches[j].rm_so);
00195 *new_str = (char *)gk_realloc(*new_str, (nlen+1)*sizeof(char), "gk_strstr_replace: new_str");
00196 }
00197
00198 strncpy(*new_str+noffset, str+offset+matches[j].rm_so, matches[j].rm_eo);
00199 noffset += matches[j].rm_eo-matches[j].rm_so;
00200 }
00201 else {
00202 gk_free((void **)new_str, LTERM);
00203 *new_str = gk_strdup("Error in replacement string. Missing subexpression number folloing '$'.");
00204 regfree(&re);
00205 return 0;
00206 }
00207 break;
00208
00209 default:
00210 if (nlen-noffset < 1) {
00211 nlen += nlen + 1;
00212 *new_str = (char *)gk_realloc(*new_str, (nlen+1)*sizeof(char), "gk_strstr_replace: new_str");
00213 }
00214 (*new_str)[noffset++] = replacement[i];
00215 }
00216 }
00217
00218
00219 offset += matches[0].rm_eo;
00220
00221 if (!global) {
00222
00223 if (nlen-noffset < len-offset) {
00224 nlen += (len-offset) - (nlen-noffset);
00225 *new_str = (char *)gk_realloc(*new_str, (nlen+1)*sizeof(char), "gk_strstr_replace: new_str");
00226 }
00227 strcpy(*new_str+noffset, str+offset);
00228 noffset += (len-offset);
00229 }
00230 }
00231 } while (global);
00232
00233 (*new_str)[noffset] = '\0';
00234
00235 regfree(&re);
00236 return nmatches + 1;
00237
00238 }
00239
00240
00241
00242
00256
00257 char *gk_strtprune(char *str, char *rmlist)
00258 {
00259 gk_idx_t i, j;
00260 size_t len;
00261
00262 len = strlen(rmlist);
00263
00264 for (i=strlen(str)-1; i>=0; i--) {
00265 for (j=0; j<len; j++) {
00266 if (str[i] == rmlist[j])
00267 break;
00268 }
00269 if (j == len)
00270 break;
00271 }
00272
00273 str[i+1] = '\0';
00274
00275 return str;
00276 }
00277
00278
00279
00293
00294 char *gk_strhprune(char *str, char *rmlist)
00295 {
00296 gk_idx_t i, j;
00297 size_t len;
00298
00299 len = strlen(rmlist);
00300
00301 for (i=0; str[i]; i++) {
00302 for (j=0; j<len; j++) {
00303 if (str[i] == rmlist[j])
00304 break;
00305 }
00306 if (j == len)
00307 break;
00308 }
00309
00310 if (i>0) {
00311 for (j=0; str[i]; i++, j++)
00312 str[j] = str[i];
00313 str[j] = '\0';
00314 }
00315
00316 return str;
00317 }
00318
00319
00320
00330
00331 char *gk_strtoupper(char *str)
00332 {
00333 int i;
00334
00335 for (i=0; str[i]!='\0'; str[i]=toupper(str[i]), i++);
00336 return str;
00337 }
00338
00339
00340
00350
00351 char *gk_strtolower(char *str)
00352 {
00353 int i;
00354
00355 for (i=0; str[i]!='\0'; str[i]=tolower(str[i]), i++);
00356 return str;
00357 }
00358
00359
00360
00374
00375 char *gk_strdup(char *orgstr)
00376 {
00377 int len;
00378 char *str=NULL;
00379
00380 if (orgstr != NULL) {
00381 len = strlen(orgstr)+1;
00382 str = gk_malloc(len*sizeof(char), "gk_strdup: str");
00383 strcpy(str, orgstr);
00384 }
00385
00386 return str;
00387 }
00388
00389
00390
00407
00408 int gk_strcasecmp(char *s1, char *s2)
00409 {
00410 int i=0;
00411
00412 if (strlen(s1) != strlen(s2))
00413 return 0;
00414
00415 while (s1[i] != '\0') {
00416 if (tolower(s1[i]) != tolower(s2[i]))
00417 return 0;
00418 i++;
00419 }
00420
00421 return 1;
00422 }
00423
00424
00425
00435
00436 int gk_strrcmp(char *s1, char *s2)
00437 {
00438 int i1 = strlen(s1)-1;
00439 int i2 = strlen(s2)-1;
00440
00441 while ((i1 >= 0) && (i2 >= 0)) {
00442 if (s1[i1] != s2[i2])
00443 return (s1[i1] - s2[i2]);
00444 i1--;
00445 i2--;
00446 }
00447
00448
00449
00450 if (i1 < i2)
00451 return -1;
00452 if (i1 > i2)
00453 return 1;
00454 return 0;
00455 }
00456
00457
00458
00459
00472
00473 char *gk_time2str(time_t time)
00474 {
00475 static char datestr[128];
00476 struct tm *tm;
00477
00478 tm = localtime(&time);
00479
00480 if (strftime(datestr, 128, "%m/%d/%Y %H:%M:%S", tm) == 0)
00481 return NULL;
00482 else
00483 return datestr;
00484 }
00485
00486
00487
00488 #if !defined(WIN32) && !defined(__MINGW32__)
00489
00490 #include <time.h>
00491
00503
00504 time_t gk_str2time(char *str)
00505 {
00506 struct tm time;
00507 time_t rtime;
00508
00509 memset(&time, '\0', sizeof(time));
00510
00511 if (strptime(str, "%m/%d/%Y %H:%M:%S", &time) == NULL)
00512 return -1;
00513
00514 rtime = mktime(&time);
00515 return (rtime < 0 ? 0 : rtime);
00516 }
00517 #endif
00518
00519
00520
00521
00522
00523
00524 int gk_GetStringID(gk_StringMap_t *strmap, char *key)
00525 {
00526 int i;
00527
00528 for (i=0; strmap[i].name; i++) {
00529 if (gk_strcasecmp(key, strmap[i].name))
00530 return strmap[i].id;
00531 }
00532
00533 return -1;
00534 }