Kaynağa Gözat

gislib: add G_strcasestr()

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@47460 15284696-431f-4ddb-bdfa-cd5b030d7da7
Martin Landa 13 yıl önce
ebeveyn
işleme
fdecd8f1be
3 değiştirilmiş dosya ile 37 ekleme ve 1 silme
  1. 1 0
      include/gisdefs.h
  2. 1 1
      lib/gis/gislib.dox
  3. 35 0
      lib/gis/strings.c

+ 1 - 0
include/gisdefs.h

@@ -586,6 +586,7 @@ void G_str_to_upper(char *);
 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);

+ 1 - 1
lib/gis/gislib.dox

@@ -1978,7 +1978,7 @@ Get position of delimiter.
 
 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.

+ 35 - 0
lib/gis/strings.c

@@ -367,6 +367,41 @@ void G_squeeze(char *line)
 	*(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;