]> de.git.xonotic.org Git - xonotic/darkplaces.git/commitdiff
added DP_QC_TOKENIZEBYSEPARATOR extension
authorhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Thu, 29 Mar 2007 01:39:52 +0000 (01:39 +0000)
committerhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Thu, 29 Mar 2007 01:39:52 +0000 (01:39 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@7018 d7cf8633-e32d-0410-b094-e92efae38249

clvm_cmds.c
mvm_cmds.c
prvm_cmds.c
prvm_cmds.h
svvm_cmds.c

index cc65213ab823ebb75005736db57eff5ee0c07160..6e35193ae39de8937a208a2a8e0b2906129999a4 100644 (file)
@@ -3136,7 +3136,7 @@ VM_tan,                                                   // #475 float(float a) VM_tan (DP_QC_ASINACOSATANATAN2TAN)
 VM_strlennocol,                                        // #476 float(string s) : DRESK - String Length (not counting color codes) (DP_QC_STRINGCOLORFUNCTIONS)
 VM_strdecolorize,                              // #477 string(string s) : DRESK - Decolorized String (DP_QC_STRINGCOLORFUNCTIONS)
 VM_strftime,                                   // #478 string(float uselocaltime, string format, ...) (DP_QC_STRFTIME)
-NULL,                                                  // #479
+VM_tokenizebyseparator,                        // #479 float(string s) tokenizebyseparator (DP_QC_TOKENIZEBYSEPARATOR)
 NULL,                                                  // #480
 NULL,                                                  // #481
 NULL,                                                  // #482
index abd29d4bc9185c53ce366aadfff33ab5c0f5b2e6..a18d71f578a5cbae8ea981f57cb42574729e6d1b 100644 (file)
@@ -10,6 +10,7 @@ char *vm_m_extensions =
 "DP_QC_ASINACOSATANATAN2TAN "
 "DP_QC_STRFTIME "
 "DP_QC_STRINGCOLORFUNCTIONS "
+"DP_QC_TOKENIZEBYSEPARATOR "
 "DP_QC_UNLIMITEDTEMPSTRINGS";
 
 /*
@@ -1218,7 +1219,7 @@ VM_tan,                                           // #475 float(float a) VM_tan (DP_QC_ASINACOSATANATAN2TAN)
 VM_strlennocol,                                // #476 float(string s) : DRESK - String Length (not counting color codes) (DP_QC_STRINGCOLORFUNCTIONS)
 VM_strdecolorize,                      // #477 string(string s) : DRESK - Decolorized String (DP_QC_STRINGCOLORFUNCTIONS)
 VM_strftime,                           // #478 string(float uselocaltime, string format, ...) (DP_QC_STRFTIME)
-NULL,                                          // #479
+VM_tokenizebyseparator,                // #479 float(string s) tokenizebyseparator (DP_QC_TOKENIZEBYSEPARATOR)
 NULL,                                          // #480
 NULL,                                          // #481
 NULL,                                          // #482
index 3e4a4aff046977c54700765aeaf054f0890315f8..9a4f138db90948009b803bbb65b075bb34b26dad 100644 (file)
@@ -2013,11 +2013,6 @@ int tokens[256];
 void VM_tokenize (void)
 {
        const char *p;
-#if 0
-       size_t pos = 0;
-       char tokenbuf[MAX_INPUTLINE];
-       size_t tokenlen;
-#endif
 
        VM_SAFEPARMCOUNT(1,VM_tokenize);
 
@@ -2028,16 +2023,73 @@ void VM_tokenize (void)
        {
                if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
                        break;
-#if 0
-               tokenlen = strlen(com_token) + 1;
-               if (pos + tokenlen > sizeof(tokenbuf))
-                       break;
-               tokens[num_tokens++] = PRVM_SetEngineString(tokenbuf + pos);
-               memcpy(tokenbuf + pos, com_token, tokenlen);
-               pos += tokenlen;
-#else
                tokens[num_tokens++] = PRVM_SetTempString(com_token);
-#endif
+       }
+
+       PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
+}
+
+/*
+=========
+VM_tokenizebyseparator
+
+float tokenizebyseparator(string s, string separator1, ...)
+=========
+*/
+//float(string s, string separator1, ...) tokenizebyseparator = #479; // takes apart a string into individal words (access them with argv), returns how many
+//this function returns the token preceding each instance of a separator (of
+//which there can be multiple), and the text following the last separator
+//useful for parsing certain kinds of data like IP addresses
+//example:
+//numnumbers = tokenizebyseparator("10.1.2.3", ".");
+//returns 4 and the tokens "10" "1" "2" "3".
+void VM_tokenizebyseparator (void)
+{
+       int j, k;
+       int numseparators;
+       int separatorlen[7];
+       const char *separators[7];
+       const char *p;
+       char tokentext[MAX_INPUTLINE];
+
+       VM_SAFEPARMCOUNTRANGE(2, 8,VM_tokenizebyseparator);
+
+       p = PRVM_G_STRING(OFS_PARM0);
+
+       numseparators = 0;;
+       for (j = 1;j < prog->argc;j++)
+       {
+               // skip any blank separator strings
+               if (!PRVM_G_STRING(OFS_PARM0 + j)[0])
+                       continue;
+               separators[numseparators] = PRVM_G_STRING(OFS_PARM0 + j);
+               separatorlen[numseparators] = strlen(separators[numseparators]);
+               numseparators++;
+       }
+
+       num_tokens = 0;
+       for (num_tokens = 0;num_tokens < (int)(sizeof(tokens)/sizeof(tokens[0]));num_tokens++)
+       {
+               while (*p)
+               {
+                       for (k = 0;k < numseparators;k++)
+                       {
+                               if (!strncmp(p, separators[k], separatorlen[k]))
+                               {
+                                       p += separatorlen[k];
+                                       break;
+                               }
+                       }
+                       if (k < numseparators)
+                               break;
+                       if (j < (int)sizeof(tokentext[MAX_INPUTLINE]-1))
+                               tokentext[j++] = *p;
+                       p++;
+               }
+               tokentext[j] = 0;
+               tokens[num_tokens] = PRVM_SetTempString(tokentext);
+               if (!*p)
+                       break;
        }
 
        PRVM_G_FLOAT(OFS_RETURN) = num_tokens;
index 5856e569cb94d976d00049aca7b81ef8ef602000..2ee6ebcae8af21a42222053e5e393b1dd4fb6266 100644 (file)
@@ -295,6 +295,7 @@ void VM_strdecolorize(void);
 void VM_clcommand (void);
 
 void VM_tokenize (void);
+void VM_tokenizebyseparator (void);
 void VM_argv (void);
 
 void VM_isserver(void);
index 18d487330db50b7d360b773adefe28288fe09079..bbe14a7734eb2ccea85d487feada55d67ccbfac5 100644 (file)
@@ -70,6 +70,7 @@ char *vm_sv_extensions =
 "DP_QC_STRFTIME "
 "DP_QC_STRINGBUFFERS "
 "DP_QC_STRINGCOLORFUNCTIONS "
+"DP_QC_TOKENIZEBYSEPARATOR "
 "DP_QC_TRACEBOX "
 "DP_QC_TRACETOSS "
 "DP_QC_TRACE_MOVETYPE_HITMODEL "
@@ -3153,7 +3154,7 @@ VM_tan,                                                   // #475 float(float a) VM_tan (DP_QC_ASINACOSATANATAN2TAN)
 VM_strlennocol,                                        // #476 float(string s) : DRESK - String Length (not counting color codes) (DP_QC_STRINGCOLORFUNCTIONS)
 VM_strdecolorize,                              // #477 string(string s) : DRESK - Decolorized String (DP_SV_STRINGCOLORFUNCTIONS)
 VM_strftime,                                   // #478 string(float uselocaltime, string format, ...) (DP_QC_STRFTIME)
-NULL,                                                  // #479
+VM_tokenizebyseparator,                        // #479 float(string s) tokenizebyseparator (DP_QC_TOKENIZEBYSEPARATOR)
 NULL,                                                  // #480
 NULL,                                                  // #481
 NULL,                                                  // #482