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
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 #include <string.h>
00063 #include <stdlib.h>
00064 #include <ctype.h>
00065 #include <sys/types.h>
00066 #include <grass/gis.h>
00067
00068 #ifndef NULL
00069 #define NULL 0
00070 #endif
00071
00072 static char *G_strend (char *S)
00073 {
00074 while (*S)
00075 S++;
00076 return (S);
00077 }
00078
00079 char *G_strcpy (char *T, const char *F)
00080 {
00081 char *d = T;
00082
00083 while ((*d++ = *F++))
00084 ;
00085 return (T);
00086 }
00087
00088 char *G_chrcpy (char *T, const char *F, int n)
00089 {
00090 char *d = T;
00091
00092 while (n--)
00093 *d++ = *F++;
00094 *d = '\0';
00095 return (T);
00096 }
00097
00098 char *G_strncpy (char *T, const char *F, int n)
00099 {
00100 char *d = T;
00101
00102 while (n-- && *F)
00103 *d++ = *F++;
00104 *d = '\0';
00105 return (T);
00106 }
00107
00108 char *G_strmov (char *T, const char *F)
00109 {
00110 char *d = T;
00111
00112 while (*F)
00113 *d++ = *F++;
00114 return (T);
00115 }
00116
00117 char *G_chrmov (char *T, const char *F, int n)
00118 {
00119 char *d = T;
00120
00121 while (n--)
00122 *d++ = *F++;
00123 return (T);
00124 }
00125
00126 char *G_strcat (char *T, const char *F)
00127 {
00128 G_strcpy (G_strend (T), F);
00129 return (T);
00130 }
00131
00132 char *G_chrcat (char *T, const char *F, int n)
00133 {
00134 G_chrcpy (G_strend (T), F, n);
00135 return (T);
00136 }
00137
00138 int G_strcasecmp(const char *x, const char *y)
00139 {
00140 int xx,yy;
00141
00142 if (!x)
00143 return y ? -1 : 0;
00144 if (!y)
00145 return x ? 1 : 0;
00146 while (*x && *y)
00147 {
00148 xx = *x++;
00149 yy = *y++;
00150 if (xx >= 'A' && xx <= 'Z')
00151 xx = xx + 'a' - 'A';
00152 if (yy >= 'A' && yy <= 'Z')
00153 yy = yy + 'a' - 'A';
00154 if (xx < yy) return -1;
00155 if (xx > yy) return 1;
00156 }
00157 if (*x) return 1;
00158 if (*y) return -1;
00159 return 0;
00160 }
00161
00162
00163 char *G_strstr(char *mainString, const char *subString)
00164 {
00165 const char *p;
00166 char *q;
00167 int length;
00168
00169 p = subString;
00170 q = mainString;
00171 length = strlen(subString);
00172
00173 do {
00174 while (*q != '\0' && *q != *p) {
00175 q++;
00176 }
00177 } while (*q != '\0' && strncmp(p, q, length) != 0 && q++);
00178
00179
00180 if (*q == '\0') {
00181 return NULL;
00182 } else {
00183 return q;
00184 }
00185 }
00186
00187
00188 char *G_strdup(const char *string)
00189 {
00190 char *p;
00191
00192 p = G_malloc (strlen(string) + 1);
00193
00194 if (p != NULL) {
00195 strcpy(p, string);
00196 }
00197
00198 return p;
00199 }
00200
00201
00202 char *G_strchg(char* bug, char character, char new) {
00203
00204
00205
00206 char *help = bug;
00207 while(*help) {
00208 if (*help==character)
00209 *help=new;
00210 help++;
00211 }
00212 return bug;
00213 }
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229 char *G_str_replace(char* buffer, const char* old_str, const char* new_str)
00230 {
00231
00232 char *B, *R;
00233 const char *N;
00234 char *replace;
00235 int count, len;
00236
00237
00238 if (old_str == NULL || new_str == NULL)
00239 return G_strdup (buffer);
00240
00241 if (buffer == NULL)
00242 return NULL;
00243
00244
00245 B = strstr (buffer, old_str);
00246 if (B == NULL)
00247
00248 return G_strdup (buffer);
00249
00250 if (strlen (new_str) > strlen (old_str)) {
00251
00252 count = 0;
00253 len = strlen (old_str);
00254 B = buffer;
00255 while(B != NULL && *B != '\0') {
00256 B = G_strstr (B, old_str);
00257 if (B != NULL) {
00258 B += len;
00259 count++;
00260 }
00261 }
00262
00263 len = count * (strlen(new_str) - strlen(old_str))
00264 + strlen(buffer);
00265
00266 }
00267 else
00268 len = strlen(buffer);
00269
00270
00271 replace = G_malloc (len + 1);
00272 if (replace == NULL)
00273 return NULL;
00274
00275
00276 B = buffer;
00277 R = replace;
00278 len = strlen (old_str);
00279 while(*B != '\0') {
00280 if (*B == old_str[0] && strncmp (B, old_str, len) == 0) {
00281 N = new_str;
00282 while (*N != '\0')
00283 *R++ = *N++;
00284 B += len;
00285 }
00286 else {
00287 *R++ = *B++;
00288 }
00289 }
00290 *R='\0';
00291
00292 return replace;
00293 }
00294
00295
00296
00297
00298
00299
00300
00301
00302 int G_strip ( register char *buf)
00303 {
00304 register char *a, *b;
00305
00306
00307 for (a = b = buf; *a == ' ' || *a == '\t'; a++)
00308 ;
00309 if (a != b)
00310 while ((*b++ = *a++))
00311 ;
00312
00313 for (a = buf; *a; a++)
00314 ;
00315 if (a != buf)
00316 {
00317 for (a--; *a == ' ' || *a == '\t'; a--)
00318 ;
00319 a++;
00320 *a = 0;
00321 }
00322
00323 return 0;
00324 }
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00349 char *G_chop (char *line)
00350 {
00351 register char *f = line, *t = line;
00352
00353 while (isspace (*f))
00354 f++;
00355
00356 if (! *f)
00357 {
00358 *t = '\0';
00359 return (line);
00360 }
00361
00362 for (t = line; *t; t++)
00363 ;
00364 while ( isspace (*--t) )
00365 ;
00366 *++t = '\0';
00367
00368 t = line;
00369 while (*f)
00370 *t++ = *f++;
00371 *t = '\0';
00372
00373 return (line);
00374 }
00375
00381 void G_str_to_upper (char *str)
00382 {
00383 int i = 0;
00384
00385 if (!str) return;
00386
00387 while ( str[i] )
00388 {
00389 str[i] = toupper(str[i]);
00390 i++;
00391 }
00392 }
00393
00394
00400 void G_str_to_lower (char *str)
00401 {
00402 int i = 0;
00403
00404 if (!str) return;
00405
00406 while ( str[i] )
00407 {
00408 str[i] = tolower(str[i]);
00409 i++;
00410 }
00411 }
00412