strings.c

Go to the documentation of this file.
00001 /* TODO: merge interesting functions from 
00002    ../datetime/scan.c
00003    here
00004 */
00005 
00006 /*
00007  * string/chring movement functions
00008  *
00009 ** G_strcpy (T, F)
00010 ** G_strncpy (T, F, n)  copy F up to null or n, always copy null
00011 ** G_chrcpy (T, F, n)
00012 ** G_strmov (T, F)
00013 ** G_chrmov (T, F, n)
00014 ** G_strcat (T, F)
00015 ** G_chrcat (T, F, n)
00016 **     char *T, *F;
00017 **     int n;
00018  *
00019  * G_strcpy (T, F)    copy F up to null, copy null
00020  * G_chrcpy (T, F, n) copy F up to n,    copy null
00021  * 
00022  * G_strmov (T, F)    copy F up to null
00023  * G_chrmov (T, F, n) copy F up to n
00024  * 
00025  * G_strcat (T, F)    cat F up to null, copy null
00026  * G_chrcat (T, F, n) cat F up to n,    copy null
00027  *
00028  * the -cpy and -cat functions are for null-terminated destinations;
00029  * the -mov functions are for non-null-terminated ('chring') destinations.
00030  * all functions return 'T'.
00031  *
00032  * Author Dave Gerdes (USACERL)
00033  *
00034  *
00035  * G_strcasecmp(a, b) char *a, *b;
00036  *   string compare ignoring case (upper or lower)
00037  *   returns: -1 a<b; 0 a==b; 1 a>b
00038  *
00039  * Author Michael Shapiro (USACERL)
00040  *
00041  *
00042  * G_strstr(mainString, subString)
00043  *      Return a pointer to the first occurrence of subString
00044  *      in mainString, or NULL if no occurrences are found.
00045  * G_strdup(string)
00046  *      Return a pointer to a string that is a duplicate of the string
00047  *      given to G_strdup.  The duplicate is created using malloc.
00048  *      If unable to allocate the required space, NULL is returned.
00049  *
00050  * Author: Amit Parghi (USACERL), 1993 02 23
00051  *
00052  * G_strchg(char* bug, char character, char new) {
00053  *      replace all occurencies of character in string(inplace) with new
00054  *
00055  * Author: Bernhard Reiter (Intevation GmbH, Germany)
00056  *
00057  * char * G_str_replace(char* buffer, char* old_str, char* new_str)
00058  *  Replace all occurencies of old_str in buffer with new_str
00059  *  Author Beverly Wallace (LMCO)
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) {        /* match 1st subString char */
00175             q++;
00176         }
00177     } while (*q != '\0' && strncmp(p, q, length) != 0 && q++);
00178                                 /* Short-circuit evaluation is your friend */
00179 
00180     if (*q == '\0') {                           /* ran off end of mainString */
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  /* replace all occurencies of "character" in string(bug) with
00204   * "new", returns new string */
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   \brief Replace all occurencies of old_str in buffer with new_str.
00217   \return Returns the newly allocated string, input buffer is unchanged 
00218   
00219    Author Beverly Wallace (LMCO) 3/11/04, slightly modified RB/MN
00220  
00221   Code example:
00222   \verbatim
00223       char *name;
00224       name = G_str_replace ( inbuf, ".exe", "" );
00225       ... 
00226       G_free (name);
00227   \endverbatim
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         /* Make sure old_str and new_str are not NULL */
00238         if (old_str == NULL || new_str == NULL)
00239                 return G_strdup (buffer);
00240         /* Make sure buffer is not NULL */
00241         if (buffer == NULL)
00242                 return NULL;
00243 
00244         /* Make sure old_str occurs */
00245         B = strstr (buffer, old_str);
00246         if (B == NULL)
00247                 /* return NULL; */
00248                 return G_strdup (buffer);
00249 
00250         if (strlen (new_str) > strlen (old_str)) {
00251                 /* Count occurences of old_str */
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         /* Allocate new replacement */
00271         replace = G_malloc (len + 1);
00272         if (replace == NULL)
00273                 return NULL;
00274 
00275         /* Replace old_str with new_str */
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  *  G_strip(buf)
00296  *     char *buf         buffer to be worked on
00297  *
00298  *  'buf' is rewritten in place with leading and trailing white
00299  *  space removed.
00300  ******************************************************************/
00301 
00302 int G_strip ( register char *buf)
00303 {
00304     register char *a, *b;
00305 
00306 /* remove leading white space */
00307     for (a = b = buf; *a == ' ' || *a == '\t'; a++)
00308             ;
00309     if (a != b)
00310         while ((*b++ = *a++))
00311             ;
00312 /* remove trailing white space */
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  * G_chop - chop leading and trailing white spaces: 
00327  *          space, \f, \n, \r, \t, \v
00328  *        - returns pointer to string
00329  *    
00330  * char *G_chop (char *s)
00331  *
00332  * modified copy of G_squeeze();    RB March 2000
00333  *                          <Radim.Blazek@dhv.cz>
00334  *
00335  */
00336 
00337 
00338 
00349 char *G_chop (char *line)
00350 {
00351     register char *f = line, *t = line;
00352 
00353     while (isspace (*f))        /* go to first non white-space char */
00354         f++;
00355 
00356     if (! *f)                   /* no more chars in string */
00357     {
00358         *t = '\0';
00359         return (line);
00360     }
00361 
00362     for (t = line; *t; t++)     /* go to end */
00363         ;
00364     while ( isspace (*--t) )    
00365         ;
00366     *++t = '\0';                /* remove trailing white-spaces */
00367 
00368     t = line;
00369     while (*f)                  /* copy */   
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 

Generated on Wed Dec 19 14:59:06 2007 for GRASS by  doxygen 1.5.4