]> git.xonotic.org Git - xonotic/darkplaces.git/commitdiff
common: Move infostring functions to new com_infostring.c
authorCloudwalk <cloudwalk009@gmail.com>
Sat, 31 Jul 2021 16:41:58 +0000 (12:41 -0400)
committerCloudwalk <cloudwalk009@gmail.com>
Sat, 31 Jul 2021 17:03:22 +0000 (13:03 -0400)
com_infostring.c [new file with mode: 0644]
com_infostring.h [new file with mode: 0644]
common.c
common.h
darkplaces-sdl2-vs2017.vcxproj
darkplaces-sdl2-vs2019.vcxproj
darkplaces.h
makefile.inc

diff --git a/com_infostring.c b/com_infostring.c
new file mode 100644 (file)
index 0000000..8412e48
--- /dev/null
@@ -0,0 +1,163 @@
+/*
+Copyright (C) 2006-2021 DarkPlaces contributors
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+*/
+
+#include "darkplaces.h"
+
+char *InfoString_GetValue(const char *buffer, const char *key, char *value, size_t valuelength)
+{
+       int pos = 0, j;
+       size_t keylength;
+       if (!key)
+               key = "";
+       keylength = strlen(key);
+       if (valuelength < 1 || !value)
+       {
+               Con_Printf("InfoString_GetValue: no room in value\n");
+               return NULL;
+       }
+       value[0] = 0;
+       if (strchr(key, '\\'))
+       {
+               Con_Printf("InfoString_GetValue: key name \"%s\" contains \\ which is not possible in an infostring\n", key);
+               return NULL;
+       }
+       if (strchr(key, '\"'))
+       {
+               Con_Printf("InfoString_SetValue: key name \"%s\" contains \" which is not allowed in an infostring\n", key);
+               return NULL;
+       }
+       if (!key[0])
+       {
+               Con_Printf("InfoString_GetValue: can not look up a key with no name\n");
+               return NULL;
+       }
+       while (buffer[pos] == '\\')
+       {
+               if (!memcmp(buffer + pos+1, key, keylength) &&
+                               (buffer[pos+1 + keylength] == 0 ||
+                                buffer[pos+1 + keylength] == '\\'))
+               {
+                       pos += 1 + (int)keylength;           // Skip \key
+                       if (buffer[pos] == '\\') pos++; // Skip \ before value.
+                       for (j = 0;buffer[pos+j] && buffer[pos+j] != '\\' && j < (int)valuelength - 1;j++)
+                               value[j] = buffer[pos+j];
+                       value[j] = 0;
+                       return value;
+               }
+               if (buffer[pos] == '\\') pos++; // Skip \ before value.
+               for (pos++;buffer[pos] && buffer[pos] != '\\';pos++);
+               if (buffer[pos] == '\\') pos++; // Skip \ before value.
+               for (pos++;buffer[pos] && buffer[pos] != '\\';pos++);
+       }
+       // if we reach this point the key was not found
+       return NULL;
+}
+
+void InfoString_SetValue(char *buffer, size_t bufferlength, const char *key, const char *value)
+{
+       int pos = 0, pos2;
+       size_t keylength;
+       if (!key)
+               key = "";
+       if (!value)
+               value = "";
+       keylength = strlen(key);
+       if (strchr(key, '\\') || strchr(value, '\\'))
+       {
+               Con_Printf("InfoString_SetValue: \"%s\" \"%s\" contains \\ which is not possible to store in an infostring\n", key, value);
+               return;
+       }
+       if (strchr(key, '\"') || strchr(value, '\"'))
+       {
+               Con_Printf("InfoString_SetValue: \"%s\" \"%s\" contains \" which is not allowed in an infostring\n", key, value);
+               return;
+       }
+       if (!key[0])
+       {
+               Con_Printf("InfoString_SetValue: can not set a key with no name\n");
+               return;
+       }
+       while (buffer[pos] == '\\')
+       {
+               if (!memcmp(buffer + pos+1, key, keylength) &&
+                               (buffer[pos+1 + keylength] == 0 ||
+                                buffer[pos+1 + keylength] == '\\'))
+                       break;
+               if (buffer[pos] == '\\') pos++; // Skip \ before value.
+               for (;buffer[pos] && buffer[pos] != '\\';pos++);
+               if (buffer[pos] == '\\') pos++; // Skip \ before value.
+               for (;buffer[pos] && buffer[pos] != '\\';pos++);
+       }
+       // if we found the key, find the end of it because we will be replacing it
+       pos2 = pos;
+       if (buffer[pos] == '\\')
+       {
+               pos2 += 1 + (int)keylength;  // Skip \key
+               if (buffer[pos2] == '\\') pos2++; // Skip \ before value.
+               for (;buffer[pos2] && buffer[pos2] != '\\';pos2++);
+       }
+       if (bufferlength <= pos + 1 + strlen(key) + 1 + strlen(value) + strlen(buffer + pos2))
+       {
+               Con_Printf("InfoString_SetValue: no room for \"%s\" \"%s\" in infostring\n", key, value);
+               return;
+       }
+       if (value[0])
+       {
+               // set the key/value and append the remaining text
+               char tempbuffer[MAX_INPUTLINE];
+               strlcpy(tempbuffer, buffer + pos2, sizeof(tempbuffer));
+               dpsnprintf(buffer + pos, bufferlength - pos, "\\%s\\%s%s", key, value, tempbuffer);
+       }
+       else
+       {
+               // just remove the key from the text
+               strlcpy(buffer + pos, buffer + pos2, bufferlength - pos);
+       }
+}
+
+void InfoString_Print(char *buffer)
+{
+       int i;
+       char key[MAX_INPUTLINE];
+       char value[MAX_INPUTLINE];
+       while (*buffer)
+       {
+               if (*buffer != '\\')
+               {
+                       Con_Printf("InfoString_Print: corrupt string\n");
+                       return;
+               }
+               for (buffer++, i = 0;*buffer && *buffer != '\\';buffer++)
+                       if (i < (int)sizeof(key)-1)
+                               key[i++] = *buffer;
+               key[i] = 0;
+               if (*buffer != '\\')
+               {
+                       Con_Printf("InfoString_Print: corrupt string\n");
+                       return;
+               }
+               for (buffer++, i = 0;*buffer && *buffer != '\\';buffer++)
+                       if (i < (int)sizeof(value)-1)
+                               value[i++] = *buffer;
+               value[i] = 0;
+               // empty value is an error case
+               Con_Printf("%20s %s\n", key, value[0] ? value : "NO VALUE");
+       }
+}
diff --git a/com_infostring.h b/com_infostring.h
new file mode 100644 (file)
index 0000000..538c1ba
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+Copyright (C) 2006-2021 DarkPlaces contributors
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+*/
+
+#ifndef INFOSTRING_H
+#define INFOSTRING_H
+
+#include "qtypes.h"
+#include <stdlib.h>
+
+char *InfoString_GetValue(const char *buffer, const char *key, char *value, size_t valuelength);
+void InfoString_SetValue(char *buffer, size_t bufferlength, const char *key, const char *value);
+void InfoString_Print(char *buffer);
+
+#endif
index 80ca5a377b1bdb5e72d645add42c6c6b489245f9..5af54cae43fdb921422ef8f02ba1ca9dfd8c5283 100644 (file)
--- a/common.c
+++ b/common.c
@@ -1300,148 +1300,6 @@ COM_StringDecolorize(const char *in, size_t size_in, char *out, size_t size_out,
 #undef APPEND
 }
 
-char *InfoString_GetValue(const char *buffer, const char *key, char *value, size_t valuelength)
-{
-       int pos = 0, j;
-       size_t keylength;
-       if (!key)
-               key = "";
-       keylength = strlen(key);
-       if (valuelength < 1 || !value)
-       {
-               Con_Printf("InfoString_GetValue: no room in value\n");
-               return NULL;
-       }
-       value[0] = 0;
-       if (strchr(key, '\\'))
-       {
-               Con_Printf("InfoString_GetValue: key name \"%s\" contains \\ which is not possible in an infostring\n", key);
-               return NULL;
-       }
-       if (strchr(key, '\"'))
-       {
-               Con_Printf("InfoString_SetValue: key name \"%s\" contains \" which is not allowed in an infostring\n", key);
-               return NULL;
-       }
-       if (!key[0])
-       {
-               Con_Printf("InfoString_GetValue: can not look up a key with no name\n");
-               return NULL;
-       }
-       while (buffer[pos] == '\\')
-       {
-               if (!memcmp(buffer + pos+1, key, keylength) &&
-                               (buffer[pos+1 + keylength] == 0 ||
-                                buffer[pos+1 + keylength] == '\\'))
-               {
-                       pos += 1 + (int)keylength;           // Skip \key
-                       if (buffer[pos] == '\\') pos++; // Skip \ before value.
-                       for (j = 0;buffer[pos+j] && buffer[pos+j] != '\\' && j < (int)valuelength - 1;j++)
-                               value[j] = buffer[pos+j];
-                       value[j] = 0;
-                       return value;
-               }
-               if (buffer[pos] == '\\') pos++; // Skip \ before value.
-               for (pos++;buffer[pos] && buffer[pos] != '\\';pos++);
-               if (buffer[pos] == '\\') pos++; // Skip \ before value.
-               for (pos++;buffer[pos] && buffer[pos] != '\\';pos++);
-       }
-       // if we reach this point the key was not found
-       return NULL;
-}
-
-void InfoString_SetValue(char *buffer, size_t bufferlength, const char *key, const char *value)
-{
-       int pos = 0, pos2;
-       size_t keylength;
-       if (!key)
-               key = "";
-       if (!value)
-               value = "";
-       keylength = strlen(key);
-       if (strchr(key, '\\') || strchr(value, '\\'))
-       {
-               Con_Printf("InfoString_SetValue: \"%s\" \"%s\" contains \\ which is not possible to store in an infostring\n", key, value);
-               return;
-       }
-       if (strchr(key, '\"') || strchr(value, '\"'))
-       {
-               Con_Printf("InfoString_SetValue: \"%s\" \"%s\" contains \" which is not allowed in an infostring\n", key, value);
-               return;
-       }
-       if (!key[0])
-       {
-               Con_Printf("InfoString_SetValue: can not set a key with no name\n");
-               return;
-       }
-       while (buffer[pos] == '\\')
-       {
-               if (!memcmp(buffer + pos+1, key, keylength) &&
-                               (buffer[pos+1 + keylength] == 0 ||
-                                buffer[pos+1 + keylength] == '\\'))
-                       break;
-               if (buffer[pos] == '\\') pos++; // Skip \ before value.
-               for (;buffer[pos] && buffer[pos] != '\\';pos++);
-               if (buffer[pos] == '\\') pos++; // Skip \ before value.
-               for (;buffer[pos] && buffer[pos] != '\\';pos++);
-       }
-       // if we found the key, find the end of it because we will be replacing it
-       pos2 = pos;
-       if (buffer[pos] == '\\')
-       {
-               pos2 += 1 + (int)keylength;  // Skip \key
-               if (buffer[pos2] == '\\') pos2++; // Skip \ before value.
-               for (;buffer[pos2] && buffer[pos2] != '\\';pos2++);
-       }
-       if (bufferlength <= pos + 1 + strlen(key) + 1 + strlen(value) + strlen(buffer + pos2))
-       {
-               Con_Printf("InfoString_SetValue: no room for \"%s\" \"%s\" in infostring\n", key, value);
-               return;
-       }
-       if (value[0])
-       {
-               // set the key/value and append the remaining text
-               char tempbuffer[MAX_INPUTLINE];
-               strlcpy(tempbuffer, buffer + pos2, sizeof(tempbuffer));
-               dpsnprintf(buffer + pos, bufferlength - pos, "\\%s\\%s%s", key, value, tempbuffer);
-       }
-       else
-       {
-               // just remove the key from the text
-               strlcpy(buffer + pos, buffer + pos2, bufferlength - pos);
-       }
-}
-
-void InfoString_Print(char *buffer)
-{
-       int i;
-       char key[MAX_INPUTLINE];
-       char value[MAX_INPUTLINE];
-       while (*buffer)
-       {
-               if (*buffer != '\\')
-               {
-                       Con_Printf("InfoString_Print: corrupt string\n");
-                       return;
-               }
-               for (buffer++, i = 0;*buffer && *buffer != '\\';buffer++)
-                       if (i < (int)sizeof(key)-1)
-                               key[i++] = *buffer;
-               key[i] = 0;
-               if (*buffer != '\\')
-               {
-                       Con_Printf("InfoString_Print: corrupt string\n");
-                       return;
-               }
-               for (buffer++, i = 0;*buffer && *buffer != '\\';buffer++)
-                       if (i < (int)sizeof(value)-1)
-                               value[i++] = *buffer;
-               value[i] = 0;
-               // empty value is an error case
-               Con_Printf("%20s %s\n", key, value[0] ? value : "NO VALUE");
-       }
-}
-
 //========================================================
 // strlcat and strlcpy, from OpenBSD
 
index e359dd16ae683f59784400ef7de54dab6d8723fa..9b6621736941341e187805e6a147b8dc9f3e8aed 100644 (file)
--- a/common.h
+++ b/common.h
@@ -355,10 +355,6 @@ void stringlistappend(stringlist_t *list, const char *text);
 void stringlistsort(stringlist_t *list, qbool uniq);
 void listdirectory(stringlist_t *list, const char *basepath, const char *path);
 
-char *InfoString_GetValue(const char *buffer, const char *key, char *value, size_t valuelength);
-void InfoString_SetValue(char *buffer, size_t bufferlength, const char *key, const char *value);
-void InfoString_Print(char *buffer);
-
 // strlcat and strlcpy, from OpenBSD
 // Most (all?) BSDs already have them
 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(MACOSX)
index cf3101e77cb63928f977513e643190bda82fe4af..c1168b76958f7d5b6278025651d275e5cbcc2e2f 100644 (file)
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>\r
+<?xml version="1.0" encoding="utf-8"?>\r
 <Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">\r
   <ItemGroup Label="ProjectConfigurations">\r
     <ProjectConfiguration Include="Debug|Win32">\r
     <ClCompile Include="com_ents.c" />\r
     <ClCompile Include="com_ents4.c" />\r
     <ClCompile Include="com_game.c" />\r
+    <ClCompile Include="com_infostring.c" />\r
     <ClCompile Include="com_msg.c" />\r
     <ClCompile Include="common.c" />\r
     <ClCompile Include="console.c" />\r
     <ClInclude Include="clvm_cmds.h" />\r
     <ClInclude Include="cmd.h" />\r
     <ClInclude Include="collision.h" />\r
+    <ClInclude Include="com_infostring.h" />\r
     <ClInclude Include="com_list.h" />\r
     <ClInclude Include="common.h" />\r
     <ClInclude Include="console.h" />\r
index 70055977964b6cfe80e1bbd5ec45f04d3163ea26..e1faf1e5d2589a46d7840129595bd78ca9071b38 100644 (file)
     <ClCompile Include="com_ents.c" />\r
     <ClCompile Include="com_ents4.c" />\r
     <ClCompile Include="com_game.c" />\r
+    <ClCompile Include="com_infostring.c" />\r
     <ClCompile Include="com_msg.c" />\r
     <ClCompile Include="common.c" />\r
     <ClCompile Include="console.c" />\r
     <ClInclude Include="clvm_cmds.h" />\r
     <ClInclude Include="cmd.h" />\r
     <ClInclude Include="collision.h" />\r
+    <ClInclude Include="com_infostring.h" />\r
     <ClInclude Include="com_list.h" />\r
     <ClInclude Include="common.h" />\r
     <ClInclude Include="console.h" />\r
index 599d711c65df2e14a2f9f912442c572a71b59d44..adf1581a41dd586c5479aa8caa21b65e1921196f 100644 (file)
@@ -45,6 +45,7 @@ extern char engineversion[128];
 #include "qdefs.h"
 #include "zone.h"
 #include "thread.h"
+#include "com_infostring.h"
 #include "common.h"
 #include "fs.h"
 #include "host.h"
index f613591e1f2044d708078f4daa2acff16f75841d..8c4b4d582b31ebe5f7bb4b200ac38c59c5633724 100644 (file)
@@ -80,6 +80,7 @@ OBJ_COMMON= \
        com_ents.o \
        com_ents4.o \
        com_game.o \
+       com_infostring.o \
        com_msg.o \
        common.o \
        console.o \