[GRASS-SVN] r47460 - in grass/trunk: include lib/gis

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Aug 5 09:06:57 EDT 2011


Author: martinl
Date: 2011-08-05 06:06:57 -0700 (Fri, 05 Aug 2011)
New Revision: 47460

Modified:
   grass/trunk/include/gisdefs.h
   grass/trunk/lib/gis/gislib.dox
   grass/trunk/lib/gis/strings.c
Log:
gislib: add G_strcasestr()


Modified: grass/trunk/include/gisdefs.h
===================================================================
--- grass/trunk/include/gisdefs.h	2011-08-05 12:53:34 UTC (rev 47459)
+++ grass/trunk/include/gisdefs.h	2011-08-05 13:06:57 UTC (rev 47460)
@@ -586,6 +586,7 @@
 void G_str_to_lower(char *);
 int G_str_to_sql(char *);
 void G_squeeze(char *);
+char *G_strcasestr(const char *, const char *);
 
 /* tempfile.c */
 void G_init_tempfile(void);

Modified: grass/trunk/lib/gis/gislib.dox
===================================================================
--- grass/trunk/lib/gis/gislib.dox	2011-08-05 12:53:34 UTC (rev 47459)
+++ grass/trunk/lib/gis/gislib.dox	2011-08-05 13:06:57 UTC (rev 47460)
@@ -1978,7 +1978,7 @@
 
 String compare ignoring case (upper or lower).
 
- - G_strstr()
+ - G_strcasestr()
 
 Return a pointer to the first occurrence of subString in mainString,
 or NULL if no occurrences are found.

Modified: grass/trunk/lib/gis/strings.c
===================================================================
--- grass/trunk/lib/gis/strings.c	2011-08-05 12:53:34 UTC (rev 47459)
+++ grass/trunk/lib/gis/strings.c	2011-08-05 13:06:57 UTC (rev 47460)
@@ -367,6 +367,41 @@
 	*(line + l) = '\0';
 }
 
+/*!
+  \brief Finds the first occurrence of the sub-string in the
+  null-terminated string ignoring case (upper or lower)
+  
+  \param str string where to find sub-string
+  \param substr sub-string
+  
+  \return a pointer to the first occurrence of sub-string
+  \return NULL if no occurrences are found
+*/
+char *G_strcasestr(const char *str, const char *substr)
+{
+    const char *p;
+    const char *q;
+    int length;
+
+    p = substr;
+    q = str;
+    length = strlen(substr);
+
+    do {
+	/* match 1st substr char */
+	while (*q != '\0' && toupper(*q) != toupper(*p)) {
+	    q++;
+	}
+    } while (*q != '\0' && G_strncasecmp(p, q, length) != 0 && q++);
+    
+    if (*q == '\0') {	
+	/* ran off end of str */
+	return NULL;
+    }
+    
+    return (char *) q;
+}
+
 int _strncasecmp(const char *x, const char *y, int n)
 {
     int xx, yy, i;



More information about the grass-commit mailing list