strings.c

Go to the documentation of this file.
00001 /*
00002  * string/chring movement functions
00003  *
00004 ** G_strcpy (T, F)
00005 ** G_strncpy (T, F, n)  copy F up to null or n, always copy null
00006 ** G_chrcpy (T, F, n)
00007 ** G_strmov (T, F)
00008 ** G_chrmov (T, F, n)
00009 ** G_strcat (T, F)
00010 ** G_chrcat (T, F, n)
00011 **     char *T, *F;
00012 **     int n;
00013  *
00014  * G_strcpy (T, F)    copy F up to null, copy null
00015  * G_chrcpy (T, F, n) copy F up to n,    copy null
00016  * 
00017  * G_strmov (T, F)    copy F up to null
00018  * G_chrmov (T, F, n) copy F up to n
00019  * 
00020  * G_strcat (T, F)    cat F up to null, copy null
00021  * G_chrcat (T, F, n) cat F up to n,    copy null
00022  *
00023  * the -cpy and -cat functions are for null-terminated destinations;
00024  * the -mov functions are for non-null-terminated ('chring') destinations.
00025  * all functions return 'T'.
00026  *
00027  * Author Dave Gerdes (USACERL)
00028  *
00029  *
00030  * G_strcasecmp(a, b) char *a, *b;
00031  *   string compare ignoring case (upper or lower)
00032  *   returns: -1 a<b; 0 a==b; 1 a>b
00033  *
00034  * Author Michael Shapiro (USACERL)
00035  *
00036  *
00037  * G_strstr(mainString, subString)
00038  *      Return a pointer to the first occurrence of subString
00039  *      in mainString, or NULL if no occurrences are found.
00040  * G_strdup(string)
00041  *      Return a pointer to a string that is a duplicate of the string
00042  *      given to G_strdup.  The duplicate is created using malloc.
00043  *      If unable to allocate the required space, NULL is returned.
00044  *
00045  * Author: Amit Parghi (USACERL), 1993 02 23
00046  *
00047  * G_strchg(char* bug, char character, char new) {
00048  *      replace all occurencies of character in string(inplace) with new
00049  *
00050  * Author: Bernhard Reiter (Intevation GmbH, Germany)
00051  *
00052  * char * G_str_replace(char* buffer, char* old_str, char* new_str)
00053  *  Replace all occurencies of old_str in buffer with new_str
00054  *  Author Beverly Wallace (LMCO)
00055  */
00056 
00057 #include <sys/types.h>
00058 #include <string.h>
00059 #include <stdlib.h>
00060 #include "gis.h"
00061 
00062 #ifndef NULL
00063 #define NULL            0
00064 #endif
00065 
00066 static char *G_strend (char *S)
00067 {
00068     while (*S)
00069         S++;
00070     return (S);
00071 }
00072 
00073 char *G_strcpy (char *T, const char *F)
00074 {
00075     char *d = T;
00076 
00077     while ((*d++ = *F++))
00078         ;
00079     return (T);
00080 }
00081 
00082 char *G_chrcpy (char *T, const char *F, int n)
00083 {
00084     char *d = T;
00085 
00086     while (n--)
00087         *d++ = *F++;
00088     *d = '\0';
00089     return (T);
00090 }
00091 
00092 char *G_strncpy (char *T, const char *F, int n)
00093 {
00094     char *d = T;
00095 
00096     while (n-- && *F)
00097         *d++ = *F++;
00098     *d = '\0';
00099     return (T);
00100 }
00101 
00102 char *G_strmov (char *T, const char *F)
00103 {
00104     char *d = T;
00105 
00106     while (*F)
00107         *d++ = *F++;
00108     return (T);
00109 }
00110 
00111 char *G_chrmov (char *T, const char *F, int n)
00112 {
00113     char *d = T;
00114 
00115     while (n--)
00116         *d++ = *F++;
00117     return (T);
00118 }
00119 
00120 char *G_strcat (char *T, const char *F)
00121 {
00122     G_strcpy (G_strend (T), F);
00123     return (T);
00124 }
00125 
00126 char *G_chrcat (char *T, const char *F, int n)
00127 {
00128     G_chrcpy (G_strend (T), F, n);
00129     return (T);
00130 }
00131 
00132 int G_strcasecmp(const char *x, const char *y)
00133 {
00134     int xx,yy;
00135 
00136     if (!x)
00137         return y ? -1 : 0;
00138     if (!y)
00139         return x ? 1 : 0;
00140     while (*x && *y)
00141     {
00142         xx = *x++;
00143         yy = *y++;
00144         if (xx >= 'A' && xx <= 'Z')
00145             xx = xx + 'a' - 'A';
00146         if (yy >= 'A' && yy <= 'Z')
00147             yy = yy + 'a' - 'A';
00148         if (xx < yy) return -1;
00149         if (xx > yy) return 1;
00150     }
00151     if (*x) return 1;
00152     if (*y) return -1;
00153     return 0;
00154 }
00155 
00156 char *G_strstr(char *mainString, const char *subString)
00157 {
00158     const char *p;
00159     char *q;
00160     int length;
00161 
00162     p = subString;
00163     q = mainString;
00164     length = strlen(subString);
00165 
00166     do {
00167         while (*q != '\0' && *q != *p) {        /* match 1st subString char */
00168             q++;
00169         }
00170     } while (*q != '\0' && strncmp(p, q, length) != 0 && q++);
00171                                 /* Short-circuit evaluation is your friend */
00172 
00173     if (*q == '\0') {                           /* ran off end of mainString */
00174         return NULL;
00175     } else {
00176         return q;
00177     }
00178 }
00179 
00180 
00181 char *G_strdup(const char *string)
00182 {
00183     char *p;
00184 
00185     p = malloc(strlen(string) + 1);
00186 
00187     if (p != NULL) {
00188         strcpy(p, string);
00189     }
00190 
00191     return p;
00192 }
00193 
00194 
00195 char *G_strchg(char* bug, char character, char new) {
00196  /* replace all occurencies of "character" in string(bug) with
00197   * "new", returns new string */
00198 
00199  char *help = bug;
00200  while(*help) {
00201         if (*help==character)
00202                 *help=new;
00203         help++;
00204         }
00205  return bug;
00206 }
00207 
00208 /*--------------------------------------------------------------------
00209   \brief Replace all occurencies of old_str in buffer with new_str.
00210   \return Returns the newly allocated string, input buffer is unchanged 
00211   
00212    Author Beverly Wallace (LMCO) 3/11/04, slightly modified RB/MN
00213  
00214   Code example:
00215   \verbatim
00216       char *name;
00217       name = G_str_replace ( inbuf, ".exe", "" );
00218       ... 
00219       free(name);
00220   \endverbatim
00221 --------------------------------------------------------------------*/
00222 char *G_str_replace(char* buffer, const char* old_str, const char* new_str) 
00223 {
00224 
00225         char *B, *R;
00226         const char *N;
00227         char *replace;
00228         int count, len;
00229 
00230         /* Make sure old_str and new_str are not NULL */
00231         if (old_str == NULL || new_str == NULL)
00232                 return G_strdup (buffer);
00233         /* Make sure buffer is not NULL */
00234         if (buffer == NULL)
00235                 return NULL;
00236 
00237         /* Make sure old_str occurs */
00238         B = strstr (buffer, old_str);
00239         if (B == NULL)
00240                 /* return NULL; */
00241                 return G_strdup (buffer);
00242 
00243         if (strlen (new_str) > strlen (old_str)) {
00244                 /* Count occurences of old_str */
00245                 count = 0;
00246                 len = strlen (old_str);
00247                 B = buffer;
00248                 while(B != NULL && *B != '\0') {
00249                         B = G_strstr (B, old_str);
00250                         if (B != NULL) {
00251                                 B += len;
00252                                 count++;
00253                         }
00254                 }
00255         
00256                 len = count * (strlen(new_str) - strlen(old_str)) 
00257                         + strlen(buffer);
00258 
00259         }
00260         else 
00261                 len = strlen(buffer);
00262 
00263         /* Allocate new replacement */
00264         replace = G_malloc (len + 1);
00265         if (replace == NULL)
00266                 return NULL;
00267 
00268         /* Replace old_str with new_str */
00269         B = buffer;
00270         R = replace;
00271         len = strlen (old_str);
00272         while(*B != '\0') {
00273                 if (*B == old_str[0] && strncmp (B, old_str, len) == 0) {
00274                         N = new_str;
00275                         while (*N != '\0')
00276                                 *R++ = *N++;
00277                         B += len;
00278                 }
00279                 else {
00280                         *R++ = *B++;
00281                 }
00282         }
00283         *R='\0';
00284 
00285         return replace;
00286 }

Generated on Wed Aug 23 17:49:23 2006 for GRASS by  doxygen 1.4.7