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. |
---|
4 | |
---|
5 | #include "sstringutils.h" |
---|
6 | #include <common/virtfile/virtfile.h> |
---|
7 | #include <common/log.h> |
---|
8 | #include <common/nonstd.h> |
---|
9 | #include <common/Convert.h> |
---|
10 | |
---|
11 | int loadSString(const char* filename, SString& s, const char* framsgmodule, const char* error, bool remove_cr) |
---|
12 | { |
---|
13 | VirtFILE *f; |
---|
14 | int ret = 0; |
---|
15 | if (f = Vfopen(filename, FOPEN_READ_BINARY)) |
---|
16 | { |
---|
17 | loadSString(f, s, remove_cr); |
---|
18 | ret = 1; |
---|
19 | delete f; |
---|
20 | } |
---|
21 | else if (framsgmodule) |
---|
22 | logPrintf(framsgmodule, "loadSString", LOG_WARN, error ? error : "Can't open file \"%s\"", filename); |
---|
23 | return ret; |
---|
24 | } |
---|
25 | |
---|
26 | void loadSString(VirtFILE *f, SString& s, bool remove_cr) |
---|
27 | { |
---|
28 | char buf[1024]; |
---|
29 | int len; |
---|
30 | while (!f->Veof()) |
---|
31 | { |
---|
32 | len = f->Vread(buf, 1, sizeof(buf)); |
---|
33 | s.append(buf, len); |
---|
34 | } |
---|
35 | if (remove_cr) |
---|
36 | removeCR(s); |
---|
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) |
---|
40 | bool loadSStringLine(VirtFILE* f, SString& s) |
---|
41 | { |
---|
42 | char buf[100]; |
---|
43 | bool eolfound = false; |
---|
44 | bool ret = false; |
---|
45 | s = SString::empty(); |
---|
46 | while (!eolfound) |
---|
47 | { |
---|
48 | char *r = f->Vgets(buf, sizeof(buf)); |
---|
49 | if (r == NULL) break; |
---|
50 | ret = true; |
---|
51 | int d = strlen(r); |
---|
52 | if (d > 0) |
---|
53 | { |
---|
54 | if (r[d - 1] == '\n') { d--; eolfound = true; } |
---|
55 | if (d > 0) if (r[d - 1] == '\r') d--; |
---|
56 | s += SString(r, d); |
---|
57 | } |
---|
58 | } |
---|
59 | return ret; |
---|
60 | } |
---|
61 | |
---|
62 | ////////////////////////// |
---|
63 | |
---|
64 | /** "x~xx~xxx" -> "x\~xx\~xxx" */ |
---|
65 | int quoteTilde(SString &target) |
---|
66 | { |
---|
67 | const char* x = target.c_str(); |
---|
68 | SString tmp; |
---|
69 | char *f; |
---|
70 | while (1) |
---|
71 | { |
---|
72 | f = strchr((char*)x, '~'); |
---|
73 | if (f) |
---|
74 | { |
---|
75 | tmp.append(x, f - x); |
---|
76 | tmp += "\\~"; |
---|
77 | x = f + 1; |
---|
78 | } |
---|
79 | else |
---|
80 | { |
---|
81 | if (tmp.len() == 0) return 0; // nothing was changed! |
---|
82 | tmp += x; |
---|
83 | target = tmp; |
---|
84 | return 1; |
---|
85 | } |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | /** "x\~xx\~xxx" -> "x~xx~xxx" */ |
---|
90 | int unquoteTilde(SString &target) |
---|
91 | { |
---|
92 | const char* x = target.c_str(); |
---|
93 | SString tmp; |
---|
94 | char *f; |
---|
95 | while (1) |
---|
96 | { |
---|
97 | f = strchr((char*)x, '\\'); |
---|
98 | if (f) |
---|
99 | { |
---|
100 | tmp.append(x, f - x); |
---|
101 | if (f[1] == '~') |
---|
102 | { |
---|
103 | tmp += '~'; |
---|
104 | x = f + 2; |
---|
105 | } |
---|
106 | else |
---|
107 | { |
---|
108 | tmp += "\\"; |
---|
109 | x = f + 1; |
---|
110 | } |
---|
111 | } |
---|
112 | else |
---|
113 | { |
---|
114 | if (tmp.len() == 0) return 0; // nothing was changed! |
---|
115 | tmp += x; |
---|
116 | target = tmp; |
---|
117 | return 1; |
---|
118 | } |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | ///////////////// |
---|
123 | |
---|
124 | bool strContainsOneOf(const char* str, const char* chars) |
---|
125 | { |
---|
126 | while (*str) |
---|
127 | { |
---|
128 | if (strchr(chars, *str)) return 1; |
---|
129 | str++; |
---|
130 | } |
---|
131 | return 0; |
---|
132 | } |
---|
133 | |
---|
134 | ////////////// |
---|
135 | |
---|
136 | bool sstringQuote(SString& target) |
---|
137 | { |
---|
138 | const char* x = target.c_str(); |
---|
139 | bool changed = 0; |
---|
140 | SString tmp; |
---|
141 | tmp.memoryHint(target.len()); |
---|
142 | while (*x) |
---|
143 | { |
---|
144 | switch (*x) |
---|
145 | { |
---|
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; |
---|
152 | } |
---|
153 | x++; |
---|
154 | } |
---|
155 | if (changed) target = tmp; |
---|
156 | return changed; |
---|
157 | } |
---|
158 | |
---|
159 | const char* skipQuoteString(const char* txt, const char* limit) |
---|
160 | { |
---|
161 | while (*txt) |
---|
162 | { |
---|
163 | if (*txt == '\"') return txt; |
---|
164 | if (*txt == '\\') txt++; |
---|
165 | txt++; |
---|
166 | if (txt == limit) break; |
---|
167 | } |
---|
168 | return txt; |
---|
169 | } |
---|
170 | |
---|
171 | int sstringUnquote(SString &target) |
---|
172 | { |
---|
173 | const char* x = target.c_str(); |
---|
174 | SString tmp; |
---|
175 | char *f; |
---|
176 | while (1) |
---|
177 | { |
---|
178 | f = strchr((char*)x, '\\'); |
---|
179 | if (f) |
---|
180 | { |
---|
181 | tmp.append(x, f - x); |
---|
182 | switch (f[1]) |
---|
183 | { |
---|
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]; |
---|
189 | } |
---|
190 | x = f + 2; |
---|
191 | } |
---|
192 | else |
---|
193 | { |
---|
194 | if (tmp.len() == 0) return 0; // nothing was changed! |
---|
195 | tmp += x; |
---|
196 | target = tmp; |
---|
197 | return 1; |
---|
198 | } |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | int strFindField(const SString& txt, const SString& name, int &end) |
---|
203 | { |
---|
204 | const char* t = txt.c_str(), *n; |
---|
205 | int pos = 0; |
---|
206 | while (1) |
---|
207 | { |
---|
208 | n = strchr(t + pos, ','); |
---|
209 | if ((!strncmp(t + pos, name.c_str(), name.len())) && (t[pos + name.len()] == '=')) |
---|
210 | { |
---|
211 | if (n) end = n - t; else end = txt.len(); |
---|
212 | return pos; |
---|
213 | } |
---|
214 | if (n) pos = n - t + 1; else break; |
---|
215 | } |
---|
216 | return -1; |
---|
217 | } |
---|
218 | |
---|
219 | SString strGetField(const SString& txt, const SString& name) |
---|
220 | { |
---|
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)); |
---|
226 | } |
---|
227 | |
---|
228 | void strSetField(SString& txt, const SString& name, const SString& value) |
---|
229 | { |
---|
230 | int p, e; |
---|
231 | p = strFindField(txt, name, e); |
---|
232 | if (p < 0) |
---|
233 | { |
---|
234 | if (!value.len()) return; |
---|
235 | char *t = txt.directAppend(1 + name.len() + value.len()); |
---|
236 | char *b = t; |
---|
237 | if (txt.len()) *(t++) = ','; |
---|
238 | strcpy(t, name.c_str()); t += name.len(); |
---|
239 | *(t++) = '='; |
---|
240 | strcpy(t, value.c_str()); t += value.len(); |
---|
241 | txt.endAppend(t - b); |
---|
242 | } |
---|
243 | else |
---|
244 | { |
---|
245 | if (!value.len()) |
---|
246 | { |
---|
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)); |
---|
251 | } |
---|
252 | else |
---|
253 | { |
---|
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); |
---|
257 | memmove(t + p, value.c_str(), value.len()); |
---|
258 | txt.endWrite(txt.len() + value.len() - (e - p)); |
---|
259 | } |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | SString trim(const SString& s) |
---|
264 | { |
---|
265 | const unsigned char*b = (const unsigned char*)s.c_str(); |
---|
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; |
---|
275 | } |
---|
276 | |
---|
277 | bool removeCR(SString& s) |
---|
278 | { |
---|
279 | const char* p = s.c_str(); |
---|
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; |
---|
291 | } |
---|
292 | |
---|
293 | bool matchWildcard(const SString& word, const SString& pattern) |
---|
294 | { |
---|
295 | if (pattern.len() == 0) |
---|
296 | return word.len() == 0; |
---|
297 | int aster = pattern.indexOf('*'); |
---|
298 | if (aster >= 0) |
---|
299 | { |
---|
300 | SString before = pattern.substr(0, aster); |
---|
301 | SString after = pattern.substr(aster + 1); |
---|
302 | if (!word.len()) return false; |
---|
303 | if (before.len()) if (!word.startsWith(before.c_str())) return false; |
---|
304 | if (after.len()) |
---|
305 | if ((word.len() < after.len()) |
---|
306 | || (strcmp(after.c_str(), word.c_str() + word.len() - after.len()))) |
---|
307 | return false; |
---|
308 | return true; |
---|
309 | } |
---|
310 | else |
---|
311 | return word == pattern; |
---|
312 | } |
---|
313 | |
---|
314 | bool matchWildcardList(const SString& word, const SString& patterns) |
---|
315 | { |
---|
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; |
---|
324 | } |
---|
325 | |
---|
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 | } |
---|