1 | // This file is a part of the Framsticks GDK library.
|
---|
2 | // Copyright (C) 2002-2011 Szymon Ulatowski. See LICENSE.txt for details.
|
---|
3 | // Refer to http://www.framsticks.com/ for further information.
|
---|
4 |
|
---|
5 | #include "sstringutils.h"
|
---|
6 | #include "virtfile.h"
|
---|
7 | #include "framsg.h"
|
---|
8 |
|
---|
9 | int loadSString(const char* filename,SString& s,const char* framsgmodule,const char* error)
|
---|
10 | {
|
---|
11 | VirtFILE *f;
|
---|
12 | int ret=0;
|
---|
13 | if (f=Vfopen(filename,"rb"))
|
---|
14 | {
|
---|
15 | loadSString(f,s);
|
---|
16 | ret=1;
|
---|
17 | fclose(f);
|
---|
18 | }
|
---|
19 | else if (framsgmodule)
|
---|
20 | FMprintf(framsgmodule,"loadSString",FMLV_WARN,error?error:"can't open file \"%s\"",filename);
|
---|
21 | return ret;
|
---|
22 | }
|
---|
23 |
|
---|
24 | void loadSString(VirtFILE *f,SString& s)
|
---|
25 | {
|
---|
26 | static char buf[4000];
|
---|
27 | int len;
|
---|
28 | while(!f->Veof())
|
---|
29 | {
|
---|
30 | len=fread(buf,1,4000,f);
|
---|
31 | s.append(buf,len);
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | //////////////////////////
|
---|
36 |
|
---|
37 | /** "x~xx~xxx" -> "x\~xx\~xxx" */
|
---|
38 | int quoteTilde(SString &target)
|
---|
39 | {
|
---|
40 | const char* x=target;
|
---|
41 | SString tmp;
|
---|
42 | char *f;
|
---|
43 | while(1)
|
---|
44 | {
|
---|
45 | f=strchr((char*)x,'~');
|
---|
46 | if (f)
|
---|
47 | {
|
---|
48 | tmp.append(x,f-x);
|
---|
49 | tmp+="\\~";
|
---|
50 | x=f+1;
|
---|
51 | }
|
---|
52 | else
|
---|
53 | {
|
---|
54 | if (tmp.len()==0) return 0; // nothing was changed!
|
---|
55 | tmp+=x;
|
---|
56 | target=tmp;
|
---|
57 | return 1;
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | /** "x\~xx\~xxx" -> "x~xx~xxx" */
|
---|
63 | int unquoteTilde(SString &target)
|
---|
64 | {
|
---|
65 | const char* x=target;
|
---|
66 | SString tmp;
|
---|
67 | char *f;
|
---|
68 | while(1)
|
---|
69 | {
|
---|
70 | f=strchr((char*)x,'\\');
|
---|
71 | if (f)
|
---|
72 | {
|
---|
73 | tmp.append(x,f-x);
|
---|
74 | if (f[1]=='~')
|
---|
75 | {
|
---|
76 | tmp+='~';
|
---|
77 | x=f+2;
|
---|
78 | }
|
---|
79 | else
|
---|
80 | {
|
---|
81 | tmp+="\\";
|
---|
82 | x=f+1;
|
---|
83 | }
|
---|
84 | }
|
---|
85 | else
|
---|
86 | {
|
---|
87 | if (tmp.len()==0) return 0; // nothing was changed!
|
---|
88 | tmp+=x;
|
---|
89 | target=tmp;
|
---|
90 | return 1;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | /////////////////
|
---|
96 |
|
---|
97 | bool strContainsOneOf(const char* str,const char* chars)
|
---|
98 | {
|
---|
99 | while(*str)
|
---|
100 | {
|
---|
101 | if (strchr(chars,*str)) return 1;
|
---|
102 | str++;
|
---|
103 | }
|
---|
104 | return 0;
|
---|
105 | }
|
---|
106 |
|
---|
107 | //////////////
|
---|
108 |
|
---|
109 | bool sstringQuote(SString& target)
|
---|
110 | {
|
---|
111 | const char* x=target;
|
---|
112 | bool changed=0;
|
---|
113 | SString tmp;
|
---|
114 | tmp.memoryHint(target.len());
|
---|
115 | while(*x)
|
---|
116 | {
|
---|
117 | switch(*x)
|
---|
118 | {
|
---|
119 | case '\n': tmp+="\\n"; changed=1; break;
|
---|
120 | case '\r': tmp+="\\r"; changed=1; break;
|
---|
121 | case '\t': tmp+="\\t"; changed=1; break;
|
---|
122 | case '\"': tmp+="\\\""; changed=1; break;
|
---|
123 | default: tmp+=*x;
|
---|
124 | }
|
---|
125 | x++;
|
---|
126 | }
|
---|
127 | if (changed) target=tmp;
|
---|
128 | return changed;
|
---|
129 | }
|
---|
130 |
|
---|
131 | const char* skipQuoteString(const char* txt, const char* limit)
|
---|
132 | {
|
---|
133 | while(*txt)
|
---|
134 | {
|
---|
135 | if (*txt=='\"') return txt;
|
---|
136 | if (*txt=='\\') txt++;
|
---|
137 | txt++;
|
---|
138 | if (txt==limit) break;
|
---|
139 | }
|
---|
140 | return txt;
|
---|
141 | }
|
---|
142 |
|
---|
143 | int sstringUnquote(SString &target)
|
---|
144 | {
|
---|
145 | const char* x=target;
|
---|
146 | SString tmp;
|
---|
147 | char *f;
|
---|
148 | while(1)
|
---|
149 | {
|
---|
150 | f=strchr((char*)x,'\\');
|
---|
151 | if (f)
|
---|
152 | {
|
---|
153 | tmp.append(x,f-x);
|
---|
154 | switch(f[1])
|
---|
155 | {
|
---|
156 | case 'n': tmp+='\n'; break;
|
---|
157 | case 'r': tmp+='\r'; break;
|
---|
158 | case 't': tmp+='\t'; break;
|
---|
159 | case '\"': tmp+='\"'; break;
|
---|
160 | default: tmp+=f[1];
|
---|
161 | }
|
---|
162 | x=f+2;
|
---|
163 | }
|
---|
164 | else
|
---|
165 | {
|
---|
166 | if (tmp.len()==0) return 0; // nothing was changed!
|
---|
167 | tmp+=x;
|
---|
168 | target=tmp;
|
---|
169 | return 1;
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | int strFindField(const SString& txt,const SString& name,int &end)
|
---|
175 | {
|
---|
176 | const char* t=txt,*n;
|
---|
177 | int pos=0;
|
---|
178 | while(1)
|
---|
179 | {
|
---|
180 | n=strchr(t+pos,',');
|
---|
181 | if ((!strncmp(t+pos,name,name.len()))&&(t[pos+name.len()]=='='))
|
---|
182 | {
|
---|
183 | if (n) end=n-t; else end=txt.len();
|
---|
184 | return pos;
|
---|
185 | }
|
---|
186 | if (n) pos=n-t+1; else break;
|
---|
187 | }
|
---|
188 | return -1;
|
---|
189 | }
|
---|
190 |
|
---|
191 | SString strGetField(const SString& txt,const SString& name)
|
---|
192 | {
|
---|
193 | int p,e;
|
---|
194 | p=strFindField(txt,name,e);
|
---|
195 | if (p<0) return SString();
|
---|
196 | p+=name.len()+1;
|
---|
197 | return SString(txt.substr(p,e-p));
|
---|
198 | }
|
---|
199 |
|
---|
200 | void strSetField(SString& txt,const SString& name,const SString& value)
|
---|
201 | {
|
---|
202 | int p,e;
|
---|
203 | p=strFindField(txt,name,e);
|
---|
204 | if (p<0)
|
---|
205 | {
|
---|
206 | if (!value.len()) return;
|
---|
207 | char *t=txt.directAppend(1+name.len()+value.len());
|
---|
208 | char *b=t;
|
---|
209 | if (txt.len()) *(t++)=',';
|
---|
210 | strcpy(t,name); t+=name.len();
|
---|
211 | *(t++)+='=';
|
---|
212 | strcpy(t,value); t+=value.len();
|
---|
213 | txt.endAppend(t-b);
|
---|
214 | }
|
---|
215 | else
|
---|
216 | {
|
---|
217 | if (!value.len())
|
---|
218 | {
|
---|
219 | if (p>0) p--; else if (e<txt.len()) e++;
|
---|
220 | char *t=txt.directWrite(0);
|
---|
221 | memmove(t+p,t+e,txt.len()-e);
|
---|
222 | txt.endWrite(txt.len()+value.len()-(e-p));
|
---|
223 | }
|
---|
224 | else
|
---|
225 | {
|
---|
226 | p+=name.len()+1;
|
---|
227 | char *t=txt.directWrite(txt.len()+value.len()-(e-p));
|
---|
228 | memmove(t+p+value.len(),t+e,txt.len()-e);
|
---|
229 | memmove(t+p,value,value.len());
|
---|
230 | txt.endWrite(txt.len()+value.len()-(e-p));
|
---|
231 | }
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | SString trim(SString& s)
|
---|
236 | {
|
---|
237 | const unsigned char*b=(const unsigned char*)(const char*)s;
|
---|
238 | const unsigned char*e=b+s.len();
|
---|
239 | while((b<e)&&(*b<=' ')) b++;
|
---|
240 | while((b<e)&&(e[-1]<=' ')) e--;
|
---|
241 | if ((e-b)==s.len()) return s;
|
---|
242 | SString newstring;
|
---|
243 | char* t=newstring.directWrite();
|
---|
244 | memmove(t,b,e-b);
|
---|
245 | newstring.endWrite(e-b);
|
---|
246 | return newstring;
|
---|
247 | }
|
---|