]> de.git.xonotic.org Git - xonotic/darkplaces.git/commitdiff
rewrote PF_tokenize to not use memory allocations
authorhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Fri, 6 Feb 2004 05:20:02 +0000 (05:20 +0000)
committerhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Fri, 6 Feb 2004 05:20:02 +0000 (05:20 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@3872 d7cf8633-e32d-0410-b094-e92efae38249

pr_cmds.c

index 7eaa915f45e5b71d07b1c1ff3c38196074c417e1..3a0c9d0f974329e37f54228f4738220d89ce8f32 100644 (file)
--- a/pr_cmds.c
+++ b/pr_cmds.c
@@ -2969,30 +2969,26 @@ void PF_clientcommand (void)
 
 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
 //this function originally written by KrimZon, made shorter by LordHavoc
 
 //float(string s) tokenize = #441; // takes apart a string into individal words (access them with argv), returns how many
 //this function originally written by KrimZon, made shorter by LordHavoc
-char **tokens = NULL;
-int    max_tokens, num_tokens = 0;
+//20040203: rewritten by LordHavoc (no longer uses allocations)
+int num_tokens = 0;
+char *tokens[256], tokenbuf[4096];
 void PF_tokenize (void)
 {
 void PF_tokenize (void)
 {
+       int pos;
        const char *p;
        const char *p;
-       char *str;
-       str = G_STRING(OFS_PARM0);
+       p = G_STRING(OFS_PARM0);
 
 
-       if (tokens != NULL)
+       num_tokens = 0;
+       pos = 0;
+       while(COM_ParseToken(&p, false))
        {
        {
-               int i;
-               for (i=0;i<num_tokens;i++)
-                       Z_Free(tokens[i]);
-               Z_Free(tokens);
-               num_tokens = 0;
-       }
-
-       tokens = Z_Malloc(strlen(str) * sizeof(char *));
-       max_tokens = strlen(str);
-
-       for (p = str;COM_ParseToken(&p, false) && num_tokens < max_tokens;num_tokens++)
-       {
-               tokens[num_tokens] = Z_Malloc(strlen(com_token) + 1);
-               strcpy(tokens[num_tokens], com_token);
+               if (num_tokens >= (int)(sizeof(tokens)/sizeof(tokens[0])))
+                       break;
+               if (pos + strlen(com_token) + 1 > sizeof(tokenbuf))
+                       break;
+               tokens[num_tokens++] = tokenbuf + pos;
+               strcpy(tokenbuf + pos, com_token);
+               pos += strlen(com_token) + 1;
        }
 
        G_FLOAT(OFS_RETURN) = num_tokens;
        }
 
        G_FLOAT(OFS_RETURN) = num_tokens;