[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
| 2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
| 3 | // See LICENSE.txt for details. |
---|
[109] | 4 | |
---|
| 5 | #include "sstringutils.h" |
---|
[382] | 6 | #include <common/virtfile/virtfile.h> |
---|
[375] | 7 | #include <common/log.h> |
---|
[109] | 8 | #include <common/nonstd.h> |
---|
[691] | 9 | #include <common/Convert.h> |
---|
[109] | 10 | |
---|
[529] | 11 | int loadSString(const char* filename, SString& s, const char* framsgmodule, const char* error, bool remove_cr) |
---|
[109] | 12 | { |
---|
[257] | 13 | VirtFILE *f; |
---|
| 14 | int ret = 0; |
---|
| 15 | if (f = Vfopen(filename, FOPEN_READ_BINARY)) |
---|
[109] | 16 | { |
---|
[529] | 17 | loadSString(f, s, remove_cr); |
---|
[257] | 18 | ret = 1; |
---|
[523] | 19 | delete f; |
---|
[109] | 20 | } |
---|
[257] | 21 | else if (framsgmodule) |
---|
[691] | 22 | logPrintf(framsgmodule, "loadSString", LOG_WARN, error ? error : "Can't open file \"%s\"", filename); |
---|
[257] | 23 | return ret; |
---|
[109] | 24 | } |
---|
| 25 | |
---|
[529] | 26 | void loadSString(VirtFILE *f, SString& s, bool remove_cr) |
---|
[109] | 27 | { |
---|
[257] | 28 | char buf[1024]; |
---|
| 29 | int len; |
---|
| 30 | while (!f->Veof()) |
---|
[109] | 31 | { |
---|
[523] | 32 | len = f->Vread(buf, 1, sizeof(buf)); |
---|
[257] | 33 | s.append(buf, len); |
---|
[109] | 34 | } |
---|
[529] | 35 | if (remove_cr) |
---|
| 36 | removeCR(s); |
---|
[109] | 37 | } |
---|
| 38 | |
---|
| 39 | //load single line, discarding any \r or \n found at the end, return false if nothing could be loaded (error or eof) |
---|
[257] | 40 | bool loadSStringLine(VirtFILE* f, SString& s) |
---|
[109] | 41 | { |
---|
[257] | 42 | char buf[100]; |
---|
| 43 | bool eolfound = false; |
---|
| 44 | bool ret = false; |
---|
| 45 | s = SString::empty(); |
---|
| 46 | while (!eolfound) |
---|
[109] | 47 | { |
---|
[523] | 48 | char *r = f->Vgets(buf, sizeof(buf)); |
---|
[257] | 49 | if (r == NULL) break; |
---|
| 50 | ret = true; |
---|
| 51 | int d = strlen(r); |
---|
| 52 | if (d > 0) |
---|
[109] | 53 | { |
---|
[257] | 54 | if (r[d - 1] == '\n') { d--; eolfound = true; } |
---|
| 55 | if (d > 0) if (r[d - 1] == '\r') d--; |
---|
| 56 | s += SString(r, d); |
---|
[109] | 57 | } |
---|
| 58 | } |
---|
[257] | 59 | return ret; |
---|
[109] | 60 | } |
---|
| 61 | |
---|
| 62 | ////////////////////////// |
---|
| 63 | |
---|
| 64 | /** "x~xx~xxx" -> "x\~xx\~xxx" */ |
---|
| 65 | int quoteTilde(SString &target) |
---|
| 66 | { |
---|
[348] | 67 | const char* x = target.c_str(); |
---|
[257] | 68 | SString tmp; |
---|
| 69 | char *f; |
---|
| 70 | while (1) |
---|
[109] | 71 | { |
---|
[257] | 72 | f = strchr((char*)x, '~'); |
---|
| 73 | if (f) |
---|
[109] | 74 | { |
---|
[257] | 75 | tmp.append(x, f - x); |
---|
| 76 | tmp += "\\~"; |
---|
| 77 | x = f + 1; |
---|
[109] | 78 | } |
---|
[257] | 79 | else |
---|
[109] | 80 | { |
---|
[257] | 81 | if (tmp.len() == 0) return 0; // nothing was changed! |
---|
| 82 | tmp += x; |
---|
| 83 | target = tmp; |
---|
| 84 | return 1; |
---|
[109] | 85 | } |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | /** "x\~xx\~xxx" -> "x~xx~xxx" */ |
---|
| 90 | int unquoteTilde(SString &target) |
---|
| 91 | { |
---|
[348] | 92 | const char* x = target.c_str(); |
---|
[257] | 93 | SString tmp; |
---|
| 94 | char *f; |
---|
| 95 | while (1) |
---|
[109] | 96 | { |
---|
[257] | 97 | f = strchr((char*)x, '\\'); |
---|
| 98 | if (f) |
---|
[109] | 99 | { |
---|
[257] | 100 | tmp.append(x, f - x); |
---|
| 101 | if (f[1] == '~') |
---|
[109] | 102 | { |
---|
[257] | 103 | tmp += '~'; |
---|
| 104 | x = f + 2; |
---|
[109] | 105 | } |
---|
[257] | 106 | else |
---|
[109] | 107 | { |
---|
[257] | 108 | tmp += "\\"; |
---|
| 109 | x = f + 1; |
---|
[109] | 110 | } |
---|
| 111 | } |
---|
[257] | 112 | else |
---|
[109] | 113 | { |
---|
[257] | 114 | if (tmp.len() == 0) return 0; // nothing was changed! |
---|
| 115 | tmp += x; |
---|
| 116 | target = tmp; |
---|
| 117 | return 1; |
---|
[109] | 118 | } |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | ///////////////// |
---|
| 123 | |
---|
[257] | 124 | bool strContainsOneOf(const char* str, const char* chars) |
---|
[109] | 125 | { |
---|
[257] | 126 | while (*str) |
---|
[109] | 127 | { |
---|
[257] | 128 | if (strchr(chars, *str)) return 1; |
---|
| 129 | str++; |
---|
[109] | 130 | } |
---|
[257] | 131 | return 0; |
---|
[109] | 132 | } |
---|
| 133 | |
---|
| 134 | ////////////// |
---|
| 135 | |
---|
| 136 | bool sstringQuote(SString& target) |
---|
| 137 | { |
---|
[348] | 138 | const char* x = target.c_str(); |
---|
[257] | 139 | bool changed = 0; |
---|
| 140 | SString tmp; |
---|
| 141 | tmp.memoryHint(target.len()); |
---|
| 142 | while (*x) |
---|
[109] | 143 | { |
---|
[257] | 144 | switch (*x) |
---|
[109] | 145 | { |
---|
[257] | 146 | case '\n': tmp += "\\n"; changed = 1; break; |
---|
| 147 | case '\r': tmp += "\\r"; changed = 1; break; |
---|
| 148 | case '\t': tmp += "\\t"; changed = 1; break; |
---|
| 149 | case '\"': tmp += "\\\""; changed = 1; break; |
---|
| 150 | case '\\': tmp += "\\\\"; changed = 1; break; |
---|
| 151 | default: tmp += *x; |
---|
[109] | 152 | } |
---|
[257] | 153 | x++; |
---|
[109] | 154 | } |
---|
[257] | 155 | if (changed) target = tmp; |
---|
| 156 | return changed; |
---|
[109] | 157 | } |
---|
| 158 | |
---|
| 159 | const char* skipQuoteString(const char* txt, const char* limit) |
---|
| 160 | { |
---|
[257] | 161 | while (*txt) |
---|
[109] | 162 | { |
---|
[257] | 163 | if (*txt == '\"') return txt; |
---|
| 164 | if (*txt == '\\') txt++; |
---|
| 165 | txt++; |
---|
| 166 | if (txt == limit) break; |
---|
[109] | 167 | } |
---|
[257] | 168 | return txt; |
---|
[109] | 169 | } |
---|
| 170 | |
---|
| 171 | int sstringUnquote(SString &target) |
---|
| 172 | { |
---|
[348] | 173 | const char* x = target.c_str(); |
---|
[257] | 174 | SString tmp; |
---|
| 175 | char *f; |
---|
| 176 | while (1) |
---|
[109] | 177 | { |
---|
[257] | 178 | f = strchr((char*)x, '\\'); |
---|
| 179 | if (f) |
---|
[109] | 180 | { |
---|
[257] | 181 | tmp.append(x, f - x); |
---|
| 182 | switch (f[1]) |
---|
[109] | 183 | { |
---|
[257] | 184 | case 'n': tmp += '\n'; break; |
---|
| 185 | case 'r': tmp += '\r'; break; |
---|
| 186 | case 't': tmp += '\t'; break; |
---|
| 187 | case '\"': tmp += '\"'; break; |
---|
| 188 | default: tmp += f[1]; |
---|
[109] | 189 | } |
---|
[257] | 190 | x = f + 2; |
---|
[109] | 191 | } |
---|
[257] | 192 | else |
---|
[109] | 193 | { |
---|
[257] | 194 | if (tmp.len() == 0) return 0; // nothing was changed! |
---|
| 195 | tmp += x; |
---|
| 196 | target = tmp; |
---|
| 197 | return 1; |
---|
[109] | 198 | } |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | |
---|
[257] | 202 | int strFindField(const SString& txt, const SString& name, int &end) |
---|
[109] | 203 | { |
---|
[348] | 204 | const char* t = txt.c_str(), *n; |
---|
[257] | 205 | int pos = 0; |
---|
| 206 | while (1) |
---|
[109] | 207 | { |
---|
[257] | 208 | n = strchr(t + pos, ','); |
---|
[348] | 209 | if ((!strncmp(t + pos, name.c_str(), name.len())) && (t[pos + name.len()] == '=')) |
---|
[109] | 210 | { |
---|
[257] | 211 | if (n) end = n - t; else end = txt.len(); |
---|
| 212 | return pos; |
---|
[109] | 213 | } |
---|
[257] | 214 | if (n) pos = n - t + 1; else break; |
---|
[109] | 215 | } |
---|
[257] | 216 | return -1; |
---|
[109] | 217 | } |
---|
| 218 | |
---|
[257] | 219 | SString strGetField(const SString& txt, const SString& name) |
---|
[109] | 220 | { |
---|
[257] | 221 | int p, e; |
---|
| 222 | p = strFindField(txt, name, e); |
---|
| 223 | if (p < 0) return SString(); |
---|
| 224 | p += name.len() + 1; |
---|
| 225 | return SString(txt.substr(p, e - p)); |
---|
[109] | 226 | } |
---|
| 227 | |
---|
[257] | 228 | void strSetField(SString& txt, const SString& name, const SString& value) |
---|
[109] | 229 | { |
---|
[257] | 230 | int p, e; |
---|
| 231 | p = strFindField(txt, name, e); |
---|
| 232 | if (p < 0) |
---|
[109] | 233 | { |
---|
[257] | 234 | if (!value.len()) return; |
---|
| 235 | char *t = txt.directAppend(1 + name.len() + value.len()); |
---|
| 236 | char *b = t; |
---|
| 237 | if (txt.len()) *(t++) = ','; |
---|
[348] | 238 | strcpy(t, name.c_str()); t += name.len(); |
---|
[257] | 239 | *(t++) = '='; |
---|
[348] | 240 | strcpy(t, value.c_str()); t += value.len(); |
---|
[257] | 241 | txt.endAppend(t - b); |
---|
[109] | 242 | } |
---|
[257] | 243 | else |
---|
[109] | 244 | { |
---|
[257] | 245 | if (!value.len()) |
---|
[109] | 246 | { |
---|
[257] | 247 | if (p > 0) p--; else if (e < txt.len()) e++; |
---|
| 248 | char *t = txt.directWrite(0); |
---|
| 249 | memmove(t + p, t + e, txt.len() - e); |
---|
| 250 | txt.endWrite(txt.len() + value.len() - (e - p)); |
---|
[109] | 251 | } |
---|
[257] | 252 | else |
---|
[109] | 253 | { |
---|
[257] | 254 | p += name.len() + 1; |
---|
| 255 | char *t = txt.directWrite(txt.len() + value.len() - (e - p)); |
---|
| 256 | memmove(t + p + value.len(), t + e, txt.len() - e); |
---|
[348] | 257 | memmove(t + p, value.c_str(), value.len()); |
---|
[257] | 258 | txt.endWrite(txt.len() + value.len() - (e - p)); |
---|
[109] | 259 | } |
---|
| 260 | } |
---|
| 261 | } |
---|
| 262 | |
---|
[512] | 263 | SString trim(const SString& s) |
---|
[109] | 264 | { |
---|
[348] | 265 | const unsigned char*b = (const unsigned char*)s.c_str(); |
---|
[257] | 266 | const unsigned char*e = b + s.len(); |
---|
| 267 | while ((b < e) && (*b <= ' ')) b++; |
---|
| 268 | while ((b < e) && (e[-1] <= ' ')) e--; |
---|
| 269 | if ((e - b) == s.len()) return s; |
---|
| 270 | SString newstring; |
---|
| 271 | char* t = newstring.directWrite(e - b); |
---|
| 272 | memmove(t, b, e - b); |
---|
| 273 | newstring.endWrite(e - b); |
---|
| 274 | return newstring; |
---|
[109] | 275 | } |
---|
| 276 | |
---|
| 277 | bool removeCR(SString& s) |
---|
| 278 | { |
---|
[348] | 279 | const char* p = s.c_str(); |
---|
[257] | 280 | const char* cr = strchr(p, '\r'); |
---|
| 281 | if (!cr) return false; |
---|
| 282 | char* begin = s.directWrite(); |
---|
| 283 | char* src = begin + (cr - p), *dst = src; |
---|
| 284 | while (*src) |
---|
| 285 | if (*src == '\r') |
---|
| 286 | src++; |
---|
| 287 | else |
---|
| 288 | *(dst++) = *(src++); |
---|
| 289 | s.endWrite(dst - begin); |
---|
| 290 | return true; |
---|
[109] | 291 | } |
---|
[210] | 292 | |
---|
[257] | 293 | bool matchWildcard(const SString& word, const SString& pattern) |
---|
[210] | 294 | { |
---|
[257] | 295 | if (pattern.len() == 0) |
---|
| 296 | return word.len() == 0; |
---|
| 297 | int aster = pattern.indexOf('*'); |
---|
| 298 | if (aster >= 0) |
---|
[210] | 299 | { |
---|
[257] | 300 | SString before = pattern.substr(0, aster); |
---|
| 301 | SString after = pattern.substr(aster + 1); |
---|
| 302 | if (!word.len()) return false; |
---|
[348] | 303 | if (before.len()) if (!word.startsWith(before.c_str())) return false; |
---|
[257] | 304 | if (after.len()) |
---|
| 305 | if ((word.len() < after.len()) |
---|
[348] | 306 | || (strcmp(after.c_str(), word.c_str() + word.len() - after.len()))) |
---|
[257] | 307 | return false; |
---|
| 308 | return true; |
---|
[210] | 309 | } |
---|
[257] | 310 | else |
---|
| 311 | return word == pattern; |
---|
[210] | 312 | } |
---|
| 313 | |
---|
[257] | 314 | bool matchWildcardList(const SString& word, const SString& patterns) |
---|
[210] | 315 | { |
---|
[257] | 316 | if (patterns.len() == 0) |
---|
| 317 | return word.len() == 0; |
---|
| 318 | int pos = 0; |
---|
| 319 | SString pattern; |
---|
| 320 | while (patterns.getNextToken(pos, pattern, ',')) |
---|
| 321 | if (matchWildcard(word, pattern)) |
---|
| 322 | return true; |
---|
| 323 | return false; |
---|
[210] | 324 | } |
---|
| 325 | |
---|
[691] | 326 | SString getUIDString(uint64_t uid, char prefix) |
---|
| 327 | { |
---|
| 328 | return SString::sprintf("%c" UINT64_FORMAT, prefix, uid); |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | bool parseUIDString(const char* str, char prefix, uint64_t &uid, bool err) |
---|
| 332 | { |
---|
| 333 | if ((str[0] == prefix) && (isdigit(str[1]))) |
---|
| 334 | { |
---|
| 335 | char* end; |
---|
| 336 | uid = strtoull(str + 1, &end, 10); |
---|
| 337 | if (end == (str + 1 + strlen(str + 1))) |
---|
| 338 | return true; |
---|
| 339 | } |
---|
| 340 | if (err) |
---|
| 341 | logPrintf("SString", "parseUIDString", LOG_ERROR, "Invalid uid: '%s'", str); |
---|
| 342 | return false; |
---|
| 343 | } |
---|