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