00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "../common/common.h"
00029 #include "infostring.h"
00030
00039 const char *Info_ValueForKey (const char *s, const char *key)
00040 {
00041 char pkey[512];
00042
00043 static char value[2][512];
00044
00045
00046 static int valueindex = 0;
00047 char *o;
00048
00049 valueindex ^= 1;
00050 if (*s == '\\' && *s != '\n')
00051 s++;
00052 while (1) {
00053 o = pkey;
00054 while (*s != '\\' && *s != '\n') {
00055 if (!*s)
00056 return "";
00057 *o++ = *s++;
00058 }
00059 *o = 0;
00060 s++;
00061
00062 o = value[valueindex];
00063
00064 while (*s != '\\' && *s != '\n' && *s)
00065 *o++ = *s++;
00066 *o = 0;
00067
00068 if (!Q_strcasecmp(key, pkey))
00069 return value[valueindex];
00070
00071 if (!*s)
00072 return "";
00073 s++;
00074 }
00075 }
00076
00077 const char *Info_BoolForKey (const char *s, const char *key)
00078 {
00079 const char *boolStr = Info_ValueForKey(s, key);
00080 if (boolStr[0] == '0' || boolStr[0] == '\0' || !strcmp(boolStr, "No"))
00081 return "No";
00082 return "Yes";
00083 }
00084
00085 int Info_IntegerForKey (const char *s, const char *key)
00086 {
00087 return atoi(Info_ValueForKey(s, key));
00088 }
00089
00096 void Info_RemoveKey (char *s, const char *key)
00097 {
00098 char *start;
00099 char pkey[512];
00100 char value[512];
00101 char *o;
00102
00103 if (strstr(key, "\\")) {
00104
00105 return;
00106 }
00107
00108 while (1) {
00109 start = s;
00110 if (*s == '\\')
00111 s++;
00112 o = pkey;
00113 while (*s != '\\') {
00114 if (!*s)
00115 return;
00116 *o++ = *s++;
00117 }
00118 *o = 0;
00119 s++;
00120
00121 o = value;
00122 while (*s != '\\' && *s) {
00123 if (!*s)
00124 return;
00125 *o++ = *s++;
00126 }
00127 *o = 0;
00128
00129 if (!strncmp(key, pkey, sizeof(pkey))) {
00130 const size_t size = strlen(s);
00131 memmove(start, s, size);
00132 start[size] = 0;
00133 return;
00134 }
00135
00136 if (!*s)
00137 return;
00138 }
00139
00140 }
00141
00142
00146 qboolean Info_Validate (const char *s)
00147 {
00148 if (strstr(s, "\""))
00149 return qfalse;
00150 if (strstr(s, ";"))
00151 return qfalse;
00152 return qtrue;
00153 }
00154
00158 void Info_SetValueForKeyAsInteger (char *s, const size_t size, const char *key, const int value)
00159 {
00160 Info_SetValueForKey(s, size, key, va("%i", value));
00161 }
00162
00173 void Info_SetValueForKey (char *s, const size_t size, const char *key, const char *value)
00174 {
00175 char newi[MAX_INFO_STRING];
00176
00177 if (strstr(key, "\\") || strstr(value, "\\")) {
00178 Com_Printf("Can't use keys or values with a \\\n");
00179 return;
00180 }
00181
00182 if (strstr(key, ";")) {
00183 Com_Printf("Can't use keys or values with a semicolon\n");
00184 return;
00185 }
00186
00187 if (strstr(key, "\"") || strstr(value, "\"")) {
00188 Com_Printf("Can't use keys or values with a \"\n");
00189 return;
00190 }
00191
00192 if (strlen(key) > MAX_INFO_KEY - 1) {
00193 Com_Printf("Keys must be < "STRINGIFY(MAX_INFO_KEY)" characters.\n");
00194 return;
00195 }
00196
00197 if (strlen(key) > MAX_INFO_VALUE - 1) {
00198 Com_Printf("Values must be < "STRINGIFY(MAX_INFO_VALUE)" characters.\n");
00199 return;
00200 }
00201
00202 Info_RemoveKey(s, key);
00203 if (!value || value[0] == '\0')
00204 return;
00205
00206 Com_sprintf(newi, sizeof(newi), "\\%s\\%s", key, value);
00207
00208 Q_strcat(newi, s, sizeof(newi));
00209 Q_strncpyz(s, newi, size);
00210 }
00211
00215 void Info_Print (const char *s)
00216 {
00217 if (*s == '\\')
00218 s++;
00219 while (*s) {
00220 char const* key;
00221 int key_len = 0;
00222 char const* value;
00223 int value_len = 0;
00224
00225 key = s;
00226 while (*s && *s != '\\') {
00227 ++s;
00228 ++key_len;
00229 }
00230
00231 if (!*s) {
00232 Com_Printf("%-20.*sMISSING VALUE\n", key_len, key);
00233 return;
00234 }
00235
00236 value = ++s;
00237 while (*s && *s != '\\') {
00238 ++s;
00239 ++value_len;
00240 }
00241
00242 if (*s)
00243 s++;
00244
00245 Com_Printf("%-20.*s%.*s\n", key_len, key, value_len, value);
00246 }
00247 }