GRASS Programmer's Manual 6.4.1(2011)
|
00001 00019 #include <string.h> 00020 #include <stdlib.h> 00021 #include <ctype.h> 00022 #include <sys/types.h> 00023 #include <grass/gis.h> 00024 00025 #ifndef NULL 00026 #define NULL 0 00027 #endif 00028 00029 static char *G_strend(const char *S) 00030 { 00031 while (*S) 00032 S++; 00033 return (char *)S; 00034 } 00035 00046 char *G_strcpy(char *T, const char *F) 00047 { 00048 char *d = T; 00049 00050 while ((*d++ = *F++)) ; 00051 return (T); 00052 } 00053 00066 char *G_chrcpy(char *T, const char *F, int n) 00067 { 00068 char *d = T; 00069 00070 while (n--) 00071 *d++ = *F++; 00072 *d = '\0'; 00073 return (T); 00074 } 00075 00090 char *G_strncpy(char *T, const char *F, int n) 00091 { 00092 char *d = T; 00093 00094 while (n-- && *F) 00095 *d++ = *F++; 00096 *d = '\0'; 00097 return (T); 00098 } 00099 00109 char *G_strmov(char *T, const char *F) 00110 { 00111 char *d = T; 00112 00113 while (*F) 00114 *d++ = *F++; 00115 return (T); 00116 } 00117 00131 char *G_chrmov(char *T, const char *F, int n) 00132 { 00133 char *d = T; 00134 00135 while (n--) 00136 *d++ = *F++; 00137 return (T); 00138 } 00139 00153 char *G_strcat(char *T, const char *F) 00154 { 00155 G_strcpy(G_strend(T), F); 00156 return (T); 00157 } 00158 00174 char *G_chrcat(char *T, const char *F, int n) 00175 { 00176 G_chrcpy(G_strend(T), F, n); 00177 return (T); 00178 } 00179 00192 int G_strcasecmp(const char *x, const char *y) 00193 { 00194 int xx, yy; 00195 00196 if (!x) 00197 return y ? -1 : 0; 00198 if (!y) 00199 return x ? 1 : 0; 00200 while (*x && *y) { 00201 xx = *x++; 00202 yy = *y++; 00203 if (xx >= 'A' && xx <= 'Z') 00204 xx = xx + 'a' - 'A'; 00205 if (yy >= 'A' && yy <= 'Z') 00206 yy = yy + 'a' - 'A'; 00207 if (xx < yy) 00208 return -1; 00209 if (xx > yy) 00210 return 1; 00211 } 00212 if (*x) 00213 return 1; 00214 if (*y) 00215 return -1; 00216 return 0; 00217 } 00218 00230 char *G_strstr(const char *mainString, const char *subString) 00231 { 00232 const char *p; 00233 const char *q; 00234 int length; 00235 00236 p = subString; 00237 q = mainString; 00238 length = strlen(subString); 00239 00240 do { 00241 while (*q != '\0' && *q != *p) { /* match 1st subString char */ 00242 q++; 00243 } 00244 } while (*q != '\0' && strncmp(p, q, length) != 0 && q++); 00245 /* Short-circuit evaluation is your friend */ 00246 00247 if (*q == '\0') { /* ran off end of mainString */ 00248 return NULL; 00249 } 00250 else { 00251 return (char *)q; 00252 } 00253 } 00254 00265 char *G_strdup(const char *string) 00266 { 00267 char *p; 00268 00269 p = G_malloc(strlen(string) + 1); 00270 00271 if (p != NULL) { 00272 strcpy(p, string); 00273 } 00274 00275 return p; 00276 } 00277 00287 char *G_strchg(char *bug, char character, char new) 00288 { 00289 char *help = bug; 00290 00291 while (*help) { 00292 if (*help == character) 00293 *help = new; 00294 help++; 00295 } 00296 return bug; 00297 } 00298 00316 char *G_str_replace(char *buffer, const char *old_str, const char *new_str) 00317 { 00318 00319 char *B, *R; 00320 const char *N; 00321 char *replace; 00322 int count, len; 00323 00324 /* Make sure old_str and new_str are not NULL */ 00325 if (old_str == NULL || new_str == NULL) 00326 return G_strdup(buffer); 00327 /* Make sure buffer is not NULL */ 00328 if (buffer == NULL) 00329 return NULL; 00330 00331 /* Make sure old_str occurs */ 00332 B = strstr(buffer, old_str); 00333 if (B == NULL) 00334 /* return NULL; */ 00335 return G_strdup(buffer); 00336 00337 if (strlen(new_str) > strlen(old_str)) { 00338 /* Count occurences of old_str */ 00339 count = 0; 00340 len = strlen(old_str); 00341 B = buffer; 00342 while (B != NULL && *B != '\0') { 00343 B = G_strstr(B, old_str); 00344 if (B != NULL) { 00345 B += len; 00346 count++; 00347 } 00348 } 00349 00350 len = count * (strlen(new_str) - strlen(old_str)) 00351 + strlen(buffer); 00352 00353 } 00354 else 00355 len = strlen(buffer); 00356 00357 /* Allocate new replacement */ 00358 replace = G_malloc(len + 1); 00359 if (replace == NULL) 00360 return NULL; 00361 00362 /* Replace old_str with new_str */ 00363 B = buffer; 00364 R = replace; 00365 len = strlen(old_str); 00366 while (*B != '\0') { 00367 if (*B == old_str[0] && strncmp(B, old_str, len) == 0) { 00368 N = new_str; 00369 while (*N != '\0') 00370 *R++ = *N++; 00371 B += len; 00372 } 00373 else { 00374 *R++ = *B++; 00375 } 00376 } 00377 *R = '\0'; 00378 00379 return replace; 00380 } 00381 00389 int G_strip(char *buf) 00390 { 00391 register char *a, *b; 00392 00393 /* remove leading white space */ 00394 for (a = b = buf; *a == ' ' || *a == '\t'; a++) ; 00395 if (a != b) 00396 while ((*b++ = *a++)) ; 00397 /* remove trailing white space */ 00398 for (a = buf; *a; a++) ; 00399 if (a != buf) { 00400 for (a--; *a == ' ' || *a == '\t'; a--) ; 00401 a++; 00402 *a = 0; 00403 } 00404 00405 return 0; 00406 } 00407 00418 char *G_chop(char *line) 00419 { 00420 register char *f = line, *t = line; 00421 00422 while (isspace(*f)) /* go to first non white-space char */ 00423 f++; 00424 00425 if (!*f) { /* no more chars in string */ 00426 *t = '\0'; 00427 return (line); 00428 } 00429 00430 for (t = line; *t; t++) /* go to end */ 00431 ; 00432 while (isspace(*--t)) ; 00433 *++t = '\0'; /* remove trailing white-spaces */ 00434 00435 t = line; 00436 while (*f) /* copy */ 00437 *t++ = *f++; 00438 *t = '\0'; 00439 00440 return (line); 00441 } 00442 00448 void G_str_to_upper(char *str) 00449 { 00450 int i = 0; 00451 00452 if (!str) 00453 return; 00454 00455 while (str[i]) { 00456 str[i] = toupper(str[i]); 00457 i++; 00458 } 00459 } 00460 00466 void G_str_to_lower(char *str) 00467 { 00468 int i = 0; 00469 00470 if (!str) 00471 return; 00472 00473 while (str[i]) { 00474 str[i] = tolower(str[i]); 00475 i++; 00476 } 00477 } 00478 00486 int G_str_to_sql(char *str) 00487 { 00488 int count; 00489 char *c; 00490 00491 count = 0; 00492 00493 if (!str || !*str) 00494 return 0; 00495 00496 c = str; 00497 while (*c) { 00498 *c = toascii(*c); 00499 00500 if (!(*c >= 'A' && *c <= 'Z') && !(*c >= 'a' && *c <= 'z') && 00501 !(*c >= '0' && *c <= '9')) { 00502 *c = '_'; 00503 count++; 00504 } 00505 c++; 00506 } 00507 00508 c = str; 00509 if (!(*c >= 'A' && *c <= 'Z') && !(*c >= 'a' && *c <= 'z')) { 00510 *c = 'x'; 00511 count++; 00512 } 00513 00514 return count; 00515 }