2 Copyright (C) 2006-2021 DarkPlaces contributors
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "darkplaces.h"
23 char *InfoString_GetValue(const char *buffer, const char *key, char *value, size_t valuelength)
29 keylength = strlen(key);
30 if (valuelength < 1 || !value)
32 Con_Printf("InfoString_GetValue: no room in value\n");
36 if (strchr(key, '\\'))
38 Con_Printf("InfoString_GetValue: key name \"%s\" contains \\ which is not possible in an infostring\n", key);
41 if (strchr(key, '\"'))
43 Con_Printf("InfoString_SetValue: key name \"%s\" contains \" which is not allowed in an infostring\n", key);
48 Con_Printf("InfoString_GetValue: can not look up a key with no name\n");
51 while (buffer[pos] == '\\')
53 if (!memcmp(buffer + pos+1, key, keylength) &&
54 (buffer[pos+1 + keylength] == 0 ||
55 buffer[pos+1 + keylength] == '\\'))
57 pos += 1 + (int)keylength; // Skip \key
58 if (buffer[pos] == '\\') pos++; // Skip \ before value.
59 for (j = 0;buffer[pos+j] && buffer[pos+j] != '\\' && j < (int)valuelength - 1;j++)
60 value[j] = buffer[pos+j];
64 if (buffer[pos] == '\\') pos++; // Skip \ before value.
65 for (pos++;buffer[pos] && buffer[pos] != '\\';pos++);
66 if (buffer[pos] == '\\') pos++; // Skip \ before value.
67 for (pos++;buffer[pos] && buffer[pos] != '\\';pos++);
69 // if we reach this point the key was not found
73 void InfoString_SetValue(char *buffer, size_t bufferlength, const char *key, const char *value)
81 keylength = strlen(key);
82 if (strchr(key, '\\') || strchr(value, '\\'))
84 Con_Printf("InfoString_SetValue: \"%s\" \"%s\" contains \\ which is not possible to store in an infostring\n", key, value);
87 if (strchr(key, '\"') || strchr(value, '\"'))
89 Con_Printf("InfoString_SetValue: \"%s\" \"%s\" contains \" which is not allowed in an infostring\n", key, value);
94 Con_Printf("InfoString_SetValue: can not set a key with no name\n");
97 while (buffer[pos] == '\\')
99 if (!memcmp(buffer + pos+1, key, keylength) &&
100 (buffer[pos+1 + keylength] == 0 ||
101 buffer[pos+1 + keylength] == '\\'))
103 if (buffer[pos] == '\\') pos++; // Skip \ before value.
104 for (;buffer[pos] && buffer[pos] != '\\';pos++);
105 if (buffer[pos] == '\\') pos++; // Skip \ before value.
106 for (;buffer[pos] && buffer[pos] != '\\';pos++);
108 // if we found the key, find the end of it because we will be replacing it
110 if (buffer[pos] == '\\')
112 pos2 += 1 + (int)keylength; // Skip \key
113 if (buffer[pos2] == '\\') pos2++; // Skip \ before value.
114 for (;buffer[pos2] && buffer[pos2] != '\\';pos2++);
116 if (bufferlength <= pos + 1 + strlen(key) + 1 + strlen(value) + strlen(buffer + pos2))
118 Con_Printf("InfoString_SetValue: no room for \"%s\" \"%s\" in infostring\n", key, value);
123 // set the key/value and append the remaining text
124 char tempbuffer[MAX_INPUTLINE];
125 dp_strlcpy(tempbuffer, buffer + pos2, sizeof(tempbuffer));
126 dpsnprintf(buffer + pos, bufferlength - pos, "\\%s\\%s%s", key, value, tempbuffer);
130 // just remove the key from the text
131 dp_strlcpy(buffer + pos, buffer + pos2, bufferlength - pos);
135 void InfoString_Print(char *buffer)
138 char key[MAX_INPUTLINE];
139 char value[MAX_INPUTLINE];
144 Con_Printf("InfoString_Print: corrupt string\n");
147 for (buffer++, i = 0;*buffer && *buffer != '\\';buffer++)
148 if (i < (int)sizeof(key)-1)
153 Con_Printf("InfoString_Print: corrupt string\n");
156 for (buffer++, i = 0;*buffer && *buffer != '\\';buffer++)
157 if (i < (int)sizeof(value)-1)
158 value[i++] = *buffer;
160 // empty value is an error case
161 Con_Printf("%20s %s\n", key, value[0] ? value : "NO VALUE");