src/lib/common.c 1.134

1.134 2009/10/20 19:49:24 tdreszer
Added findWordByDemiliter which allows searching a string for a word, but the word must be set off by a delimiter.
Index: src/lib/common.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/lib/common.c,v
retrieving revision 1.133
retrieving revision 1.134
diff -b -B -U 4 -r1.133 -r1.134
--- src/lib/common.c	24 Sep 2009 22:20:59 -0000	1.133
+++ src/lib/common.c	20 Oct 2009 19:49:24 -0000	1.134
@@ -995,8 +995,41 @@
     return startsWithWord(firstWord,line);
 return (startsWith(firstWord,line) && line[strlen(firstWord)] == delimit);
 }
 
+char * findWordByDelimiter(char *word,char delimit, char *line)
+/* Return pointer to first occurance of word in line broken by 'delimit' char
+   Comparison is case sensitive. Delimit of ' ' uses isspace() */
+{
+int ix;
+char *p=line;
+while(*p!='\0')
+    {
+    for (ix = 0;
+         word[ix] != '\0' && word[ix] == *p;
+         ix++,p++); // advance as long as they match
+    if(ix == strlen(word))
+        {
+        if(*p=='\0'
+        || *p==delimit
+        || (delimit == ' ' && isspace(*p)))
+            return p - ix; // matched and delimited
+        }
+        for(;    *p!='\0'
+              && *p!=delimit
+              && (delimit != ' ' || !isspace(*p));
+              p++); // advance to next delimit
+        if(*p!='\0')
+            {
+            p++;
+            continue;  // delimited so start again after delimit
+            }
+        else
+            break;
+    }
+return NULL;
+}
+
 char *rStringIn(char *needle, char *haystack)
 /* Return last position of needle in haystack, or NULL if it's not there. */
 {
 int nSize = strlen(needle);