00001 #include <ctype.h> 00002 #include "log.h" 00003 #include "format.h" 00004 #include "common.h" 00005 #include "stringUtil.h" 00006 00022 char* subString (char *str, int start, int end) 00023 { 00024 static Stringa buffer = NULL; 00025 int i; 00026 00027 stringCreateClear (buffer,100); 00028 i = start; 00029 while (i <= end) { 00030 stringCatChar (buffer,str[i]); 00031 i++; 00032 } 00033 return string (buffer); 00034 } 00035 00036 00037 00041 char *rStringIn(char *needle, char *haystack) 00042 { 00043 int nSize = strlen(needle); 00044 char *pos; 00045 for (pos = haystack + strlen(haystack) - nSize; pos >= haystack; pos -= 1) 00046 { 00047 if (memcmp(needle, pos, nSize) == 0) 00048 return pos; 00049 } 00050 return NULL; 00051 } 00052 00053 00054 00060 char *stringBetween(char *start, char *end, char *haystack) 00061 00062 { 00063 char *pos, *p; 00064 int len; 00065 if ((p = stringIn(start, haystack)) != NULL) 00066 { 00067 pos = p + strlen(start); 00068 if ((p = stringIn(end, pos)) != NULL) 00069 { 00070 len = p - pos; 00071 pos = cloneMem(pos, len + 1); 00072 pos[len] = 0; 00073 return pos; 00074 } 00075 } 00076 return NULL; 00077 } 00078 00079 00080 00085 char lastChar(char *s) 00086 { 00087 if (s == NULL || s[0] == 0) 00088 return 0; 00089 return s[strlen(s)-1]; 00090 } 00091 00092 00093 00097 void toggleCase(char *s, int size) 00098 { 00099 char c; 00100 int i; 00101 00102 for (i=0; i<size; ++i) 00103 { 00104 c = s[i]; 00105 if (isupper(c)) 00106 c = tolower(c); 00107 else if (islower(c)) 00108 c = toupper(c); 00109 s[i] = c; 00110 } 00111 } 00112 00113 00114 00118 void subChar(char *s, char oldChar, char newChar) 00119 { 00120 char c; 00121 for (;;) 00122 { 00123 c = *s; 00124 if (c == 0) 00125 break; 00126 if (c == oldChar) 00127 *s = newChar; 00128 ++s; 00129 } 00130 } 00131 00132 00133 00137 void stripChar(char *s, char c) 00138 { 00139 char *in = s, *out = s; 00140 char b; 00141 00142 for (;;) 00143 { 00144 b = *out = *in++; 00145 if (b == 0) 00146 break; 00147 if (b != c) 00148 ++out; 00149 } 00150 } 00151 00152 00153 00157 int countChars(char *s, char c) 00158 { 00159 char a; 00160 int count = 0; 00161 while ((a = *s++) != 0) 00162 if (a == c) 00163 ++count; 00164 return count; 00165 } 00166 00167 00168 00172 int countSame(char *a, char *b) 00173 { 00174 char c; 00175 int i; 00176 int count = 0; 00177 for (i=0; ; ++i) 00178 { 00179 c = a[i]; 00180 if (b[i] != c) 00181 break; 00182 if (c == 0) 00183 break; 00184 ++count; 00185 } 00186 return count; 00187 } 00188 00189 00190 00194 char *skipLeadingSpaces(char *s) 00195 { 00196 char c; 00197 if (s == NULL) return NULL; 00198 for (;;) 00199 { 00200 c = *s; 00201 if (!isspace(c)) 00202 return s; 00203 ++s; 00204 } 00205 } 00206 00207 00208 00212 char *skipToSpaces(char *s) 00213 { 00214 char c; 00215 if (s == NULL) 00216 return NULL; 00217 for (;;) 00218 { 00219 c = *s; 00220 if (c == 0) 00221 return NULL; 00222 if (isspace(c)) 00223 return s; 00224 ++s; 00225 } 00226 } 00227 00228 00229 00233 void eraseTrailingSpaces(char *s) 00234 { 00235 int len = strlen(s); 00236 int i; 00237 char c; 00238 00239 for (i=len-1; i>=0; --i) 00240 { 00241 c = s[i]; 00242 if (isspace(c)) 00243 s[i] = 0; 00244 else 00245 break; 00246 } 00247 } 00248 00249 00250 00254 void eraseWhiteSpace(char *s) 00255 00256 { 00257 char *in, *out; 00258 char c; 00259 00260 in = out = s; 00261 for (;;) 00262 { 00263 c = *in++; 00264 if (c == 0) 00265 break; 00266 if (!isspace(c)) 00267 *out++ = c; 00268 } 00269 *out++ = 0; 00270 } 00271 00272 00273 00277 char *trimSpaces(char *s) 00278 { 00279 if (s != NULL) 00280 { 00281 s = skipLeadingSpaces(s); 00282 eraseTrailingSpaces(s); 00283 } 00284 return s; 00285 } 00286 00287 00288 00292 int hasWhiteSpace(char *s) 00293 { 00294 char c; 00295 while ((c = *s++) != 0) 00296 if (isspace(c)) 00297 return 1; 00298 return 0; 00299 } 00300 00301 00302 00307 char *firstWordInLine(char *line) 00308 { 00309 char *e; 00310 line = skipLeadingSpaces(line); 00311 if ((e = skipToSpaces(line)) != NULL) 00312 *e = 0; 00313 return line; 00314 } 00315 00316 00317 00323 char *lastWordInLine(char *line) 00324 { 00325 char *s = line; 00326 char *word = NULL, *wordEnd = NULL; 00327 for (;;) 00328 { 00329 s = skipLeadingSpaces(s); 00330 if (s == NULL || s[0] == 0) 00331 break; 00332 word = s; 00333 s = wordEnd = skipToSpaces(s); 00334 if (s == NULL) 00335 break; 00336 } 00337 if (wordEnd != NULL) 00338 *wordEnd = 0; 00339 return word; 00340 } 00341 00342 00343 00347 char *addSuffix(char *head, char *suffix) 00348 { 00349 static Stringa buffer = NULL; 00350 00351 stringCreateClear (buffer,100); 00352 stringPrintf (buffer,"%s%s",head,suffix); 00353 return string (buffer); 00354 } 00355 00356 00357 00363 void chopSuffixAt(char *s, char c) 00364 { 00365 char *e = strrchr(s, c); 00366 if (e != NULL) 00367 *e = 0; 00368 } 00369 00370 00371 00375 void chopSuffix(char *s) 00376 { 00377 chopSuffixAt(s,'.'); 00378 } 00379 00380 00381 00386 char *chopPrefixAt(char *s, char c) 00387 { 00388 char *e = strchr(s, c); 00389 if (e == NULL) return s; 00390 *e++ = 0; 00391 return e; 00392 } 00393 00394 00395 00400 char *chopPrefix(char *s) 00401 { 00402 return chopPrefixAt(s, '.'); 00403 } 00404 00405 00406 00407 static char *naStr = "N/A"; 00408 static char *emptyStr = ""; 00409 00410 00411 00415 char *naForNull(char *s) 00416 { 00417 if (s == NULL) 00418 s = naStr; 00419 return s; 00420 } 00421 00422 00423 00427 char *naForEmpty(char *s) 00428 { 00429 if (s == NULL || s[0] == 0) 00430 s = naStr; 00431 return s; 00432 } 00433 00434 00435 00439 char *emptyForNull(char *s) 00440 { 00441 if (s == NULL) 00442 s = emptyStr; 00443 return s; 00444 } 00445 00446 00447 00451 char *nullIfAllSpace(char *s) 00452 { 00453 s = skipLeadingSpaces(s); 00454 if (s != NULL) 00455 if (s[0] == 0) 00456 s = NULL; 00457 return s; 00458 } 00459 00460 00461 00465 char *trueFalseString(int b) 00466 { 00467 return (b ? "true" : "false"); 00468 } 00469 00470 00471 00475 char *skipNumeric(char *s) 00476 { 00477 while (isdigit(*s)) 00478 ++s; 00479 return s; 00480 } 00481 00482 00483 00487 char *skipToNumeric(char *s) 00488 { 00489 while (*s != 0 && !isdigit(*s)) 00490 ++s; 00491 return s; 00492 } 00493 00494 00495 00499 char *insertWordEveryNthPosition (char *string, char *word, int n) 00500 { 00501 static Stringa buffer = NULL; 00502 int i,len; 00503 00504 stringCreateClear (buffer,100); 00505 len = strlen (string); 00506 i = 0; 00507 while (i < len) { 00508 stringCatChar (buffer,string[i]); 00509 i++; 00510 if ((i % n) == 0 && i != len) { 00511 stringCat (buffer,word); 00512 } 00513 } 00514 return string (buffer); 00515 }