GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
strings.c
Go to the documentation of this file.
1 
19 #include <string.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22 #include <sys/types.h>
23 #include <grass/gis.h>
24 
25 #ifndef NULL
26 #define NULL 0
27 #endif
28 
29 static char *G_strend(const char *S)
30 {
31  while (*S)
32  S++;
33  return (char *)S;
34 }
35 
46 char *G_strcpy(char *T, const char *F)
47 {
48  char *d = T;
49 
50  while ((*d++ = *F++)) ;
51  return (T);
52 }
53 
66 char *G_chrcpy(char *T, const char *F, int n)
67 {
68  char *d = T;
69 
70  while (n--)
71  *d++ = *F++;
72  *d = '\0';
73  return (T);
74 }
75 
90 char *G_strncpy(char *T, const char *F, int n)
91 {
92  char *d = T;
93 
94  while (n-- && *F)
95  *d++ = *F++;
96  *d = '\0';
97  return (T);
98 }
99 
109 char *G_strmov(char *T, const char *F)
110 {
111  char *d = T;
112 
113  while (*F)
114  *d++ = *F++;
115  return (T);
116 }
117 
131 char *G_chrmov(char *T, const char *F, int n)
132 {
133  char *d = T;
134 
135  while (n--)
136  *d++ = *F++;
137  return (T);
138 }
139 
153 char *G_strcat(char *T, const char *F)
154 {
155  G_strcpy(G_strend(T), F);
156  return (T);
157 }
158 
174 char *G_chrcat(char *T, const char *F, int n)
175 {
176  G_chrcpy(G_strend(T), F, n);
177  return (T);
178 }
179 
192 int G_strcasecmp(const char *x, const char *y)
193 {
194  int xx, yy;
195 
196  if (!x)
197  return y ? -1 : 0;
198  if (!y)
199  return x ? 1 : 0;
200  while (*x && *y) {
201  xx = *x++;
202  yy = *y++;
203  if (xx >= 'A' && xx <= 'Z')
204  xx = xx + 'a' - 'A';
205  if (yy >= 'A' && yy <= 'Z')
206  yy = yy + 'a' - 'A';
207  if (xx < yy)
208  return -1;
209  if (xx > yy)
210  return 1;
211  }
212  if (*x)
213  return 1;
214  if (*y)
215  return -1;
216  return 0;
217 }
218 
230 char *G_strstr(const char *mainString, const char *subString)
231 {
232  const char *p;
233  const char *q;
234  int length;
235 
236  p = subString;
237  q = mainString;
238  length = strlen(subString);
239 
240  do {
241  while (*q != '\0' && *q != *p) { /* match 1st subString char */
242  q++;
243  }
244  } while (*q != '\0' && strncmp(p, q, length) != 0 && q++);
245  /* Short-circuit evaluation is your friend */
246 
247  if (*q == '\0') { /* ran off end of mainString */
248  return NULL;
249  }
250  else {
251  return (char *)q;
252  }
253 }
254 
265 char *G_strdup(const char *string)
266 {
267  char *p;
268 
269  p = G_malloc(strlen(string) + 1);
270 
271  if (p != NULL) {
272  strcpy(p, string);
273  }
274 
275  return p;
276 }
277 
287 char *G_strchg(char *bug, char character, char new)
288 {
289  char *help = bug;
290 
291  while (*help) {
292  if (*help == character)
293  *help = new;
294  help++;
295  }
296  return bug;
297 }
298 
316 char *G_str_replace(char *buffer, const char *old_str, const char *new_str)
317 {
318 
319  char *B, *R;
320  const char *N;
321  char *replace;
322  int count, len;
323 
324  /* Make sure old_str and new_str are not NULL */
325  if (old_str == NULL || new_str == NULL)
326  return G_strdup(buffer);
327  /* Make sure buffer is not NULL */
328  if (buffer == NULL)
329  return NULL;
330 
331  /* Make sure old_str occurs */
332  B = strstr(buffer, old_str);
333  if (B == NULL)
334  /* return NULL; */
335  return G_strdup(buffer);
336 
337  if (strlen(new_str) > strlen(old_str)) {
338  /* Count occurences of old_str */
339  count = 0;
340  len = strlen(old_str);
341  B = buffer;
342  while (B != NULL && *B != '\0') {
343  B = G_strstr(B, old_str);
344  if (B != NULL) {
345  B += len;
346  count++;
347  }
348  }
349 
350  len = count * (strlen(new_str) - strlen(old_str))
351  + strlen(buffer);
352 
353  }
354  else
355  len = strlen(buffer);
356 
357  /* Allocate new replacement */
358  replace = G_malloc(len + 1);
359  if (replace == NULL)
360  return NULL;
361 
362  /* Replace old_str with new_str */
363  B = buffer;
364  R = replace;
365  len = strlen(old_str);
366  while (*B != '\0') {
367  if (*B == old_str[0] && strncmp(B, old_str, len) == 0) {
368  N = new_str;
369  while (*N != '\0')
370  *R++ = *N++;
371  B += len;
372  }
373  else {
374  *R++ = *B++;
375  }
376  }
377  *R = '\0';
378 
379  return replace;
380 }
381 
389 int G_strip(char *buf)
390 {
391  register char *a, *b;
392 
393  /* remove leading white space */
394  for (a = b = buf; *a == ' ' || *a == '\t'; a++) ;
395  if (a != b)
396  while ((*b++ = *a++)) ;
397  /* remove trailing white space */
398  for (a = buf; *a; a++) ;
399  if (a != buf) {
400  for (a--; *a == ' ' || *a == '\t'; a--) ;
401  a++;
402  *a = 0;
403  }
404 
405  return 0;
406 }
407 
418 char *G_chop(char *line)
419 {
420  register char *f = line, *t = line;
421 
422  while (isspace(*f)) /* go to first non white-space char */
423  f++;
424 
425  if (!*f) { /* no more chars in string */
426  *t = '\0';
427  return (line);
428  }
429 
430  for (t = line; *t; t++) /* go to end */
431  ;
432  while (isspace(*--t)) ;
433  *++t = '\0'; /* remove trailing white-spaces */
434 
435  t = line;
436  while (*f) /* copy */
437  *t++ = *f++;
438  *t = '\0';
439 
440  return (line);
441 }
442 
448 void G_str_to_upper(char *str)
449 {
450  int i = 0;
451 
452  if (!str)
453  return;
454 
455  while (str[i]) {
456  str[i] = toupper(str[i]);
457  i++;
458  }
459 }
460 
466 void G_str_to_lower(char *str)
467 {
468  int i = 0;
469 
470  if (!str)
471  return;
472 
473  while (str[i]) {
474  str[i] = tolower(str[i]);
475  i++;
476  }
477 }
478 
486 int G_str_to_sql(char *str)
487 {
488  int count;
489  char *c;
490 
491  count = 0;
492 
493  if (!str || !*str)
494  return 0;
495 
496  c = str;
497  while (*c) {
498  *c = toascii(*c);
499 
500  if (!(*c >= 'A' && *c <= 'Z') && !(*c >= 'a' && *c <= 'z') &&
501  !(*c >= '0' && *c <= '9')) {
502  *c = '_';
503  count++;
504  }
505  c++;
506  }
507 
508  c = str;
509  if (!(*c >= 'A' && *c <= 'Z') && !(*c >= 'a' && *c <= 'z')) {
510  *c = 'x';
511  count++;
512  }
513 
514  return count;
515 }
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
Definition: strings.c:192
char * G_chrcat(char *T, const char *F, int n)
This function is like G_strcat() except that not more than n characters from F are appended to the en...
Definition: strings.c:174
float b
Definition: named_colr.c:8
tuple q
Definition: forms.py:2019
char * G_strcat(char *T, const char *F)
This copies characters from the string F into the string T.
Definition: strings.c:153
char * G_chrcpy(char *T, const char *F, int n)
Copies characters from the string F into the string T.
Definition: strings.c:66
char * G_strdup(const char *string)
Copies the null-terminated string into a newly allocated string. The string is allocated using G_mall...
Definition: strings.c:265
char * G_strchg(char *bug, char character, char new)
Replace all occurencies of character in string bug with new.
Definition: strings.c:287
int count
#define NULL
Definition: ccmath.h:32
char * G_chop(char *line)
Chop leading and trailing white spaces:
Definition: strings.c:418
int y
Definition: plot.c:34
char * G_strcpy(char *T, const char *F)
Copies characters from the string F into the string T.
Definition: strings.c:46
void G_str_to_lower(char *str)
Convert string to lower case.
Definition: strings.c:466
char * G_str_replace(char *buffer, const char *old_str, const char *new_str)
Replace all occurencies of old_str in buffer with new_str.
Definition: strings.c:316
int G_strip(char *buf)
Removes all leading and trailing white space from string.
Definition: strings.c:389
char * G_strncpy(char *T, const char *F, int n)
This function is similar to G_chrcpy() but always copies at least n characters into the string T...
Definition: strings.c:90
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
char * G_strstr(const char *mainString, const char *subString)
Finds the first occurrence of the character C in the null-terminated string beginning at mainString...
Definition: strings.c:230
char * G_strmov(char *T, const char *F)
Copies characters from the string F (not including the terminating null character) into the string T...
Definition: strings.c:109
char * G_chrmov(char *T, const char *F, int n)
This copies characters from the string F (exactly n characters) into the string T.
Definition: strings.c:131
#define N
Definition: inverse.c:8
void G_str_to_upper(char *str)
Convert string to upper case.
Definition: strings.c:448
int n
Definition: dataquad.c:291
int G_str_to_sql(char *str)
Make string SQL compliant.
Definition: strings.c:486