From fbddb742e3f36efc25a5e701b2e29e14d24e5d75 Mon Sep 17 00:00:00 2001 From: Cloudwalk Date: Sat, 31 Jul 2021 12:41:58 -0400 Subject: [PATCH] common: Move infostring functions to new com_infostring.c --- com_infostring.c | 163 +++++++++++++++++++++++++++++++++ com_infostring.h | 31 +++++++ common.c | 142 ---------------------------- common.h | 4 - darkplaces-sdl2-vs2017.vcxproj | 4 +- darkplaces-sdl2-vs2019.vcxproj | 2 + darkplaces.h | 1 + makefile.inc | 1 + 8 files changed, 201 insertions(+), 147 deletions(-) create mode 100644 com_infostring.c create mode 100644 com_infostring.h diff --git a/com_infostring.c b/com_infostring.c new file mode 100644 index 00000000..8412e489 --- /dev/null +++ b/com_infostring.c @@ -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 index 00000000..538c1ba2 --- /dev/null +++ b/com_infostring.h @@ -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 + +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 diff --git a/common.c b/common.c index 80ca5a37..5af54cae 100644 --- 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 diff --git a/common.h b/common.h index e359dd16..9b662173 100644 --- 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) diff --git a/darkplaces-sdl2-vs2017.vcxproj b/darkplaces-sdl2-vs2017.vcxproj index cf3101e7..c1168b76 100644 --- a/darkplaces-sdl2-vs2017.vcxproj +++ b/darkplaces-sdl2-vs2017.vcxproj @@ -1,4 +1,4 @@ - + @@ -220,6 +220,7 @@ + @@ -331,6 +332,7 @@ + diff --git a/darkplaces-sdl2-vs2019.vcxproj b/darkplaces-sdl2-vs2019.vcxproj index 70055977..e1faf1e5 100644 --- a/darkplaces-sdl2-vs2019.vcxproj +++ b/darkplaces-sdl2-vs2019.vcxproj @@ -221,6 +221,7 @@ + @@ -332,6 +333,7 @@ + diff --git a/darkplaces.h b/darkplaces.h index 599d711c..adf1581a 100644 --- a/darkplaces.h +++ b/darkplaces.h @@ -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" diff --git a/makefile.inc b/makefile.inc index f613591e..8c4b4d58 100644 --- a/makefile.inc +++ b/makefile.inc @@ -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 \ -- 2.39.2