added COM_ReadAndTokenizeLine, a useful parsing function
authorhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Sun, 5 Oct 2003 13:48:03 +0000 (13:48 +0000)
committerhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Sun, 5 Oct 2003 13:48:03 +0000 (13:48 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@3539 d7cf8633-e32d-0410-b094-e92efae38249

common.c
common.h

index e8c0893..0160927 100644 (file)
--- a/common.c
+++ b/common.c
@@ -884,6 +884,55 @@ int COM_StringBeginsWith(const char *s, const char *match)
        return true;
 }
 
+int COM_ReadAndTokenizeLine(const char **text, char **argv, int maxargc, char *tokenbuf, int tokenbufsize)
+{
+       int argc;
+       char *tokenbufend;
+       const char *l;
+       argc = 0;
+       tokenbufend = tokenbuf + tokenbufsize;
+       l = *text;
+       while (*l && *l != '\n')
+       {
+               if (*l > ' ')
+               {
+                       if (argc >= maxargc)
+                               return -1;
+                       argv[argc++] = tokenbuf;
+                       if (*l == '"')
+                       {
+                               l++;
+                               while (*l && *l != '"')
+                               {
+                                       if (tokenbuf >= tokenbufend)
+                                               return -1;
+                                       *tokenbuf++ = *l++;
+                               }
+                               if (*l == '"')
+                                       l++;
+                       }
+                       else
+                       {
+                               while (*l > ' ')
+                               {
+                                       if (tokenbuf >= tokenbufend)
+                                               return -1;
+                                       *tokenbuf++ = *l++;
+                               }
+                       }
+                       if (tokenbuf >= tokenbufend)
+                               return -1;
+                       *tokenbuf++ = 0;
+               }
+               else
+                       l++;
+       }
+       if (*l == '\n')
+               l++;
+       *text = l;
+       return argc;
+}
+
 // written by Elric, thanks Elric!
 char *SearchInfostring(const char *infostring, const char *key)
 {
index 832561a..a10242a 100644 (file)
--- a/common.h
+++ b/common.h
@@ -180,6 +180,8 @@ void COM_ToLowerString (const char *in, char *out, size_t size_out);
 void COM_ToUpperString (const char *in, char *out, size_t size_out);
 int COM_StringBeginsWith(const char *s, const char *match);
 
+int COM_ReadAndTokenizeLine(const char **text, char **argv, int maxargc, char *tokenbuf, int tokenbufsize);
+
 typedef struct stringlist_s
 {
        struct stringlist_s *next;