Rev 1024 | Rev 1227 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1024 | Rev 1026 | ||
|---|---|---|---|
| 1 | 1 | ||
| 2 | #include <string> |
2 | #include <string> |
| 3 | 3 | ||
| 4 | /*************************************************************************** |
4 | /*************************************************************************** |
| 5 | * Copyright (C) 2004 by Mattias Runge * |
5 | * Copyright (C) 2004 by Mattias Runge * |
| 6 | * mattias@mrunge.se * |
6 | * mattias@mrunge.se * |
| 7 | * * |
7 | * * |
| 8 | * This program is free software; you can redistribute it and/or modify * |
8 | * This program is free software; you can redistribute it and/or modify * |
| 9 | * it under the terms of the GNU General Public License as published by * |
9 | * it under the terms of the GNU General Public License as published by * |
| 10 | * the Free Software Foundation; either version 2 of the License, or * |
10 | * the Free Software Foundation; either version 2 of the License, or * |
| 11 | * (at your option) any later version. * |
11 | * (at your option) any later version. * |
| 12 | * * |
12 | * * |
| 13 | * This program is distributed in the hope that it will be useful, * |
13 | * This program is distributed in the hope that it will be useful, * |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 | * GNU General Public License for more details. * |
16 | * GNU General Public License for more details. * |
| 17 | * * |
17 | * * |
| 18 | * You should have received a copy of the GNU General Public License * |
18 | * You should have received a copy of the GNU General Public License * |
| 19 | * along with this program; if not, write to the * |
19 | * along with this program; if not, write to the * |
| 20 | * Free Software Foundation, Inc., * |
20 | * Free Software Foundation, Inc., * |
| 21 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
21 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 22 | ***************************************************************************/ |
22 | ***************************************************************************/ |
| 23 | #include "tools.h" |
23 | #include "tools.h" |
| 24 | 24 | ||
| 25 | string niceTime() |
25 | string niceTime() |
| 26 | { |
26 | { |
| 27 | string result; |
27 | string result; |
| 28 | struct tm tmStruct; |
28 | struct tm tmStruct; |
| 29 | time_t t = time(NULL); |
29 | time_t t = time(NULL); |
| 30 | localtime_r(&t, &tmStruct); |
30 | localtime_r(&t, &tmStruct); |
| 31 | 31 | ||
| 32 | result += lpad(itos(tmStruct.tm_hour), 2, '0'); |
32 | result += lpad(itos(tmStruct.tm_hour), 2, '0'); |
| 33 | result += ":"; |
33 | result += ":"; |
| 34 | result += lpad(itos(tmStruct.tm_min), 2, '0'); |
34 | result += lpad(itos(tmStruct.tm_min), 2, '0'); |
| 35 | result += ":"; |
35 | result += ":"; |
| 36 | result += lpad(itos(tmStruct.tm_sec), 2, '0'); |
36 | result += lpad(itos(tmStruct.tm_sec), 2, '0'); |
| 37 | 37 | ||
| 38 | return result; |
38 | return result; |
| 39 | } |
39 | } |
| 40 | 40 | ||
| 41 | unsigned int hex2uint(string hex) |
41 | unsigned int hex2uint(string hex) |
| 42 | { |
42 | { |
| 43 | return bin2uint(hex2bin(hex)); |
43 | return bin2uint(hex2bin(hex)); |
| 44 | } |
44 | } |
| 45 | 45 | ||
| 46 | unsigned int bin2uint(string bin) |
46 | unsigned int bin2uint(string bin) |
| 47 | { |
47 | { |
| 48 | unsigned int num = 0; |
48 | unsigned int num = 0; |
| 49 | 49 | ||
| 50 | for (int n = bin.length()-1; n >= 0; n--) |
50 | for (int n = bin.length()-1; n >= 0; n--) |
| 51 | { |
51 | { |
| 52 | if (bin[n] == '1') |
52 | if (bin[n] == '1') |
| 53 | num += (unsigned int)pow(2.0f, (int)(bin.length()-1-n)); |
53 | num += (unsigned int)pow(2.0f, (int)(bin.length()-1-n)); |
| 54 | } |
54 | } |
| 55 | 55 | ||
| 56 | return num; |
56 | return num; |
| 57 | } |
57 | } |
| 58 | 58 | ||
| 59 | string invert(string bin) |
59 | string invert(string bin) |
| 60 | { |
60 | { |
| 61 | string inverted; |
61 | string inverted; |
| 62 | 62 | ||
| 63 | for (int n = 0; n < bin.length(); n++) |
63 | for (int n = 0; n < bin.length(); n++) |
| 64 | inverted += (bin[n] == '1' ? '0' : '1'); |
64 | inverted += (bin[n] == '1' ? '0' : '1'); |
| 65 | 65 | ||
| 66 | return inverted; |
66 | return inverted; |
| 67 | } |
67 | } |
| 68 | 68 | ||
| 69 | float bin2float(string bin) |
69 | float bin2float(string bin) |
| 70 | { |
70 | { |
| 71 | float num = 0; |
71 | float num = 0; |
| 72 | 72 | ||
| 73 | if (bin.length() == 0) |
73 | if (bin.length() == 0) |
| 74 | return 0.0f; |
74 | return 0.0f; |
| 75 | 75 | ||
| 76 | bool isNegative = false;; |
76 | bool isNegative = false;; |
| 77 | 77 | ||
| 78 | if (bin[0] == '1') |
78 | if (bin[0] == '1') |
| 79 | { |
79 | { |
| 80 | bin = invert(bin); |
80 | bin = invert(bin); |
| 81 | isNegative = true; |
81 | isNegative = true; |
| 82 | } |
82 | } |
| 83 | 83 | ||
| 84 | for (int n = bin.length()-1; n > 0; n--) |
84 | for (int n = bin.length()-1; n > 0; n--) |
| 85 | { |
85 | { |
| 86 | if (bin[n] == '1') |
86 | if (bin[n] == '1') |
| 87 | num += pow(2.0f, (int)(bin.length()-1-n)); |
87 | num += pow(2.0f, (int)(bin.length()-1-n)); |
| 88 | } |
88 | } |
| 89 | 89 | ||
| 90 | num /= 64.0f; |
90 | num /= 64.0f; |
| 91 | 91 | ||
| 92 | if (isNegative) |
92 | if (isNegative) |
| 93 | num = -num; |
93 | num = -num; |
| 94 | 94 | ||
| 95 | return num; |
95 | return num; |
| 96 | } |
96 | } |
| 97 | 97 | ||
| 98 | string bin2hex(string bin) |
98 | string bin2hex(string bin) |
| 99 | { |
99 | { |
| 100 | string hex; |
100 | string hex; |
| 101 | 101 | ||
| 102 | while (bin.size() > 0) |
102 | while (bin.size() > 0) |
| 103 | { |
103 | { |
| 104 | string part; |
104 | string part; |
| 105 | 105 | ||
| 106 | try |
106 | try |
| 107 | { |
107 | { |
| 108 | part = bin.substr(0, 4); |
108 | part = bin.substr(0, 4); |
| 109 | } |
109 | } |
| 110 | catch (std::out_of_range& e) |
110 | catch (std::out_of_range& e) |
| 111 | { |
111 | { |
| 112 | cout << "DEBUG: bin2hex exception: " << e.what() << "\n"; |
112 | cout << "DEBUG: bin2hex exception: " << e.what() << "\n"; |
| 113 | cout << "DEBUG: A bin=" << bin << endl; |
113 | cout << "DEBUG: A bin=" << bin << endl; |
| 114 | continue; |
114 | continue; |
| 115 | } |
115 | } |
| 116 | 116 | ||
| 117 | bin.erase(0, part.size()); |
117 | bin.erase(0, part.size()); |
| 118 | 118 | ||
| 119 | while (part.size() < 4) |
119 | while (part.size() < 4) |
| 120 | part = "0" + part; |
120 | part = "0" + part; |
| 121 | 121 | ||
| 122 | if (part == "0000") |
122 | if (part == "0000") |
| 123 | hex += '0'; |
123 | hex += '0'; |
| 124 | else if (part == "0001") |
124 | else if (part == "0001") |
| 125 | hex += '1'; |
125 | hex += '1'; |
| 126 | else if (part == "0010") |
126 | else if (part == "0010") |
| 127 | hex += '2'; |
127 | hex += '2'; |
| 128 | else if (part == "0011") |
128 | else if (part == "0011") |
| 129 | hex += '3'; |
129 | hex += '3'; |
| 130 | else if (part == "0100") |
130 | else if (part == "0100") |
| 131 | hex += '4'; |
131 | hex += '4'; |
| 132 | else if (part == "0101") |
132 | else if (part == "0101") |
| 133 | hex += '5'; |
133 | hex += '5'; |
| 134 | else if (part == "0110") |
134 | else if (part == "0110") |
| 135 | hex += '6'; |
135 | hex += '6'; |
| 136 | else if (part == "0111") |
136 | else if (part == "0111") |
| 137 | hex += '7'; |
137 | hex += '7'; |
| 138 | else if (part == "1000") |
138 | else if (part == "1000") |
| 139 | hex += '8'; |
139 | hex += '8'; |
| 140 | else if (part == "1001") |
140 | else if (part == "1001") |
| 141 | hex += '9'; |
141 | hex += '9'; |
| 142 | else if (part == "1010") |
142 | else if (part == "1010") |
| 143 | hex += 'a'; |
143 | hex += 'a'; |
| 144 | else if (part == "1011") |
144 | else if (part == "1011") |
| 145 | hex += 'b'; |
145 | hex += 'b'; |
| 146 | else if (part == "1100") |
146 | else if (part == "1100") |
| 147 | hex += 'c'; |
147 | hex += 'c'; |
| 148 | else if (part == "1101") |
148 | else if (part == "1101") |
| 149 | hex += 'd'; |
149 | hex += 'd'; |
| 150 | else if (part == "1110") |
150 | else if (part == "1110") |
| 151 | hex += 'e'; |
151 | hex += 'e'; |
| 152 | else if (part == "1111") |
152 | else if (part == "1111") |
| 153 | hex += 'f'; |
153 | hex += 'f'; |
| 154 | } |
154 | } |
| 155 | 155 | ||
| 156 | return hex; |
156 | return hex; |
| 157 | } |
157 | } |
| 158 | 158 | ||
| 159 | string float2bin(float num, int length) |
159 | string float2bin(float num, int length) |
| 160 | { |
160 | { |
| 161 | string bin; |
161 | string bin; |
| 162 | 162 | ||
| 163 | ///FIXME |
163 | ///FIXME |
| 164 | 164 | ||
| 165 | while (bin.size() < length) |
165 | while (bin.size() < length) |
| 166 | bin = "0" + bin; |
166 | bin = "0" + bin; |
| 167 | 167 | ||
| 168 | return bin; |
168 | return bin; |
| 169 | } |
169 | } |
| 170 | 170 | ||
| 171 | string hex2bin(string hex) |
171 | string hex2bin(string hex) |
| 172 | { |
172 | { |
| 173 | hex = strtolower(hex); |
173 | hex = strtolower(hex); |
| 174 | 174 | ||
| 175 | string bin; |
175 | string bin; |
| 176 | 176 | ||
| 177 | for (int n = 0; n < hex.length(); n++) |
177 | for (int n = 0; n < hex.length(); n++) |
| 178 | { |
178 | { |
| 179 | switch (hex[n]) |
179 | switch (hex[n]) |
| 180 | { |
180 | { |
| 181 | case '0': bin += "0000"; break; |
181 | case '0': bin += "0000"; break; |
| 182 | case '1': bin += "0001"; break; |
182 | case '1': bin += "0001"; break; |
| 183 | case '2': bin += "0010"; break; |
183 | case '2': bin += "0010"; break; |
| 184 | case '3': bin += "0011"; break; |
184 | case '3': bin += "0011"; break; |
| 185 | case '4': bin += "0100"; break; |
185 | case '4': bin += "0100"; break; |
| 186 | case '5': bin += "0101"; break; |
186 | case '5': bin += "0101"; break; |
| 187 | case '6': bin += "0110"; break; |
187 | case '6': bin += "0110"; break; |
| 188 | case '7': bin += "0111"; break; |
188 | case '7': bin += "0111"; break; |
| 189 | case '8': bin += "1000"; break; |
189 | case '8': bin += "1000"; break; |
| 190 | case '9': bin += "1001"; break; |
190 | case '9': bin += "1001"; break; |
| 191 | case 'a': bin += "1010"; break; |
191 | case 'a': bin += "1010"; break; |
| 192 | case 'b': bin += "1011"; break; |
192 | case 'b': bin += "1011"; break; |
| 193 | case 'c': bin += "1100"; break; |
193 | case 'c': bin += "1100"; break; |
| 194 | case 'd': bin += "1101"; break; |
194 | case 'd': bin += "1101"; break; |
| 195 | case 'e': bin += "1110"; break; |
195 | case 'e': bin += "1110"; break; |
| 196 | case 'f': bin += "1111"; break; |
196 | case 'f': bin += "1111"; break; |
| 197 | } |
197 | } |
| 198 | } |
198 | } |
| 199 | 199 | ||
| 200 | return bin; |
200 | return bin; |
| 201 | } |
201 | } |
| 202 | 202 | ||
| 203 | string uint2bin(unsigned int num, int length) |
203 | string uint2bin(unsigned int num, int length) |
| 204 | { |
204 | { |
| 205 | string bin; |
205 | string bin; |
| 206 | 206 | ||
| 207 | while (num) |
207 | while (num) |
| 208 | { |
208 | { |
| 209 | bin = char((num&1)+'0') + bin; |
209 | bin = char((num&1)+'0') + bin; |
| 210 | num >>= 1; |
210 | num >>= 1; |
| 211 | } |
211 | } |
| 212 | 212 | ||
| 213 | while (bin.size() < length) |
213 | while (bin.size() < length) |
| 214 | bin = "0" + bin; |
214 | bin = "0" + bin; |
| 215 | 215 | ||
| 216 | return bin; |
216 | return bin; |
| 217 | } |
217 | } |
| 218 | 218 | ||
| 219 | string uint2hex(unsigned int num, int length) |
219 | string uint2hex(unsigned int num, int length) |
| 220 | { |
220 | { |
| 221 | return bin2hex(uint2bin(num, length)); |
221 | return bin2hex(uint2bin(num, length)); |
| 222 | } |
222 | } |
| 223 | 223 | ||
| 224 | vector<string> explode(string delimiter, string str) |
224 | vector<string> explode(string delimiter, string str) |
| 225 | { |
225 | { |
| 226 | vector<string> parts; |
226 | vector<string> parts; |
| 227 | string::size_type startPos = 0; |
227 | string::size_type startPos = 0; |
| 228 | string::size_type endPos = 0; |
228 | string::size_type endPos = 0; |
| 229 | 229 | ||
| 230 | while ((endPos = str.find(delimiter, startPos)) != string::npos) |
230 | while ((endPos = str.find(delimiter, startPos)) != string::npos) |
| 231 | { |
231 | { |
| 232 | try |
232 | try |
| 233 | { |
233 | { |
| 234 | parts.push_back(str.substr(startPos, endPos-startPos)); |
234 | parts.push_back(str.substr(startPos, endPos-startPos)); |
| 235 | } |
235 | } |
| 236 | catch (std::out_of_range& e) |
236 | catch (std::out_of_range& e) |
| 237 | { |
237 | { |
| 238 | cout << "DEBUG: explode exception: " << e.what() << "\n"; |
238 | cout << "DEBUG: explode exception: " << e.what() << "\n"; |
| 239 | cout << "DEBUG: A str=" << str << " :: startPos=" << startPos << " :: endPos=" << endPos << endl; |
239 | cout << "DEBUG: A str=" << str << " :: startPos=" << startPos << " :: endPos=" << endPos << endl; |
| 240 | continue; |
240 | continue; |
| 241 | } |
241 | } |
| 242 | 242 | ||
| 243 | startPos = endPos+delimiter.length(); |
243 | startPos = endPos+delimiter.length(); |
| 244 | } |
244 | } |
| 245 | 245 | ||
| 246 | if (startPos < str.length()) |
246 | if (startPos < str.length()) |
| 247 | { |
247 | { |
| 248 | try |
248 | try |
| 249 | { |
249 | { |
| 250 | parts.push_back(str.substr(startPos, str.length()-startPos)); |
250 | parts.push_back(str.substr(startPos, str.length()-startPos)); |
| 251 | } |
251 | } |
| 252 | catch (std::out_of_range& e) |
252 | catch (std::out_of_range& e) |
| 253 | { |
253 | { |
| 254 | cout << "DEBUG: explode exception: " << e.what() << "\n"; |
254 | cout << "DEBUG: explode exception: " << e.what() << "\n"; |
| 255 | cout << "DEBUG: B str=" << str << " :: startPos=" << startPos << " :: endPos=" << endPos << endl; |
255 | cout << "DEBUG: B str=" << str << " :: startPos=" << startPos << " :: endPos=" << endPos << endl; |
| 256 | } |
256 | } |
| 257 | } |
257 | } |
| 258 | 258 | ||
| 259 | return parts; |
259 | return parts; |
| 260 | } |
260 | } |
| 261 | 261 | ||
| 262 | string strtoupper(string s) |
262 | string strtoupper(string s) |
| 263 | { |
263 | { |
| 264 | for (unsigned int n = 0; n < s.size(); n++) |
264 | for (unsigned int n = 0; n < s.size(); n++) |
| 265 | s[n] = (char)toupper(s[n]); |
265 | s[n] = (char)toupper(s[n]); |
| 266 | 266 | ||
| 267 | return s; |
267 | return s; |
| 268 | } |
268 | } |
| 269 | 269 | ||
| 270 | string strtolower(string s) |
270 | string strtolower(string s) |
| 271 | { |
271 | { |
| 272 | for (unsigned int n = 0; n < s.size(); n++) |
272 | for (unsigned int n = 0; n < s.size(); n++) |
| 273 | s[n] = (char)tolower(s[n]); |
273 | s[n] = (char)tolower(s[n]); |
| 274 | 274 | ||
| 275 | return s; |
275 | return s; |
| 276 | } |
276 | } |
| 277 | 277 | ||
| 278 | string trim(string s) |
278 | string trim(string s) |
| 279 | { |
279 | { |
| 280 | return trim(s, ' '); |
280 | return trim(s, ' '); |
| 281 | } |
281 | } |
| 282 | 282 | ||
| 283 | string trim(const string s, char c) |
283 | string trim(const string s, char c) |
| 284 | { |
284 | { |
| 285 | string result = s; |
285 | string result = s; |
| 286 | while (result[0] == c) |
286 | while (result[0] == c) |
| 287 | result.erase(0, 1); |
287 | result.erase(0, 1); |
| 288 | 288 | ||
| 289 | while (result[result.length()-1] == c) |
289 | while (result[result.length()-1] == c) |
| 290 | result.erase(result.length()-1, 1); |
290 | result.erase(result.length()-1, 1); |
| 291 | 291 | ||
| 292 | return result; |
292 | return result; |
| 293 | } |
293 | } |
| 294 | 294 | ||
| 295 | int stoi(const string s) |
295 | int stoi(const string s) |
| 296 | { |
296 | { |
| 297 | istringstream in(s); |
297 | istringstream in(s); |
| 298 | int i; |
298 | int i; |
| 299 | in >> i; |
299 | in >> i; |
| 300 | return i; |
300 | return i; |
| 301 | } |
301 | } |
| 302 | 302 | ||
| 303 | string itos(int i) |
303 | string itos(int i) |
| 304 | { |
304 | { |
| 305 | ostringstream out; |
305 | ostringstream out; |
| 306 | out << i; |
306 | out << i; |
| 307 | return out.str(); |
307 | return out.str(); |
| 308 | } |
308 | } |
| 309 | 309 | ||
| 310 | unsigned int stou(const string s) |
310 | unsigned int stou(const string s) |
| 311 | { |
311 | { |
| 312 | istringstream in(s); |
312 | istringstream in(s); |
| 313 | unsigned int i; |
313 | unsigned int i; |
| 314 | in >> i; |
314 | in >> i; |
| 315 | return i; |
315 | return i; |
| 316 | } |
316 | } |
| 317 | 317 | ||
| 318 | string utos(unsigned int i) |
318 | string utos(unsigned int i) |
| 319 | { |
319 | { |
| 320 | ostringstream out; |
320 | ostringstream out; |
| 321 | out << i; |
321 | out << i; |
| 322 | return out.str(); |
322 | return out.str(); |
| 323 | } |
323 | } |
| 324 | 324 | ||
| 325 | float stof(const string s) |
325 | float stof(const string s) |
| 326 | { |
326 | { |
| 327 | istringstream in(s); |
327 | istringstream in(s); |
| 328 | float f; |
328 | float f; |
| 329 | in >> f; |
329 | in >> f; |
| 330 | return f; |
330 | return f; |
| 331 | } |
331 | } |
| 332 | 332 | ||
| 333 | string ftos(float f) |
333 | string ftos(float f) |
| 334 | { |
334 | { |
| 335 | ostringstream out; |
335 | ostringstream out; |
| 336 | out << f; |
336 | out << f; |
| 337 | return out.str(); |
337 | return out.str(); |
| 338 | } |
338 | } |
| 339 | 339 | ||
| 340 | string str_replace(string search, string replace, string str) |
340 | string str_replace(string search, string replace, string str) |
| 341 | { |
341 | { |
| 342 | vector<string> parts = explode(search, str); |
342 | vector<string> parts = explode(search, str); |
| 343 | 343 | ||
| 344 | string result; |
344 | string result; |
| 345 | for (int n = 0; n < parts.size(); n++) |
345 | for (int n = 0; n < parts.size(); n++) |
| 346 | { |
346 | { |
| 347 | result += parts[n]; |
347 | result += parts[n]; |
| 348 | 348 | ||
| 349 | if (n < parts.size()-1) |
349 | if (n < parts.size()-1) |
| 350 | result += replace; |
350 | result += replace; |
| 351 | } |
351 | } |
| 352 | 352 | ||
| 353 | return result; |
353 | return result; |
| 354 | } |
354 | } |
| 355 | 355 | ||
| 356 | string file_get_contents(string filename) |
356 | string file_get_contents(string filename) |
| 357 | { |
357 | { |
| 358 | ifstream srcfile(filename.c_str()); |
358 | ifstream srcfile(filename.c_str()); |
| 359 | 359 | ||
| 360 | if (!srcfile.is_open()) |
360 | if (!srcfile.is_open()) |
| 361 | { |
361 | { |
| 362 | srcfile.close(); |
362 | srcfile.close(); |
| 363 | return ""; |
363 | return ""; |
| 364 | } |
364 | } |
| 365 | 365 | ||
| 366 | string buffer = ""; |
366 | string buffer = ""; |
| 367 | string line; |
367 | string line; |
| 368 | 368 | ||
| 369 | while (!srcfile.eof()) |
369 | while (!srcfile.eof()) |
| 370 | { |
370 | { |
| 371 | getline(srcfile, line); |
371 | getline(srcfile, line); |
| 372 | 372 | ||
| 373 | if (srcfile.eof()) |
373 | if (srcfile.eof()) |
| 374 | break; |
374 | break; |
| 375 | 375 | ||
| 376 | buffer += line + "\n"; |
376 | buffer += line + "\n"; |
| 377 | }; |
377 | }; |
| 378 | 378 | ||
| 379 | srcfile.close(); |
379 | srcfile.close(); |
| 380 | 380 | ||
| 381 | return buffer; |
381 | return buffer; |
| 382 | } |
382 | } |
| 383 | 383 | ||
| 384 | bool file_exists(string filename) |
384 | bool file_exists(string filename) |
| 385 | { |
385 | { |
| 386 | ifstream file(filename.c_str()); |
386 | ifstream file(filename.c_str()); |
| 387 | if (file.is_open()) |
387 | if (file.is_open()) |
| 388 | { |
388 | { |
| 389 | file.close(); |
389 | file.close(); |
| 390 | return true; |
390 | return true; |
| 391 | } |
391 | } |
| 392 | 392 | ||
| 393 | return false; |
393 | return false; |
| 394 | } |
394 | } |
| 395 | 395 | ||
| 396 | string escape(string in) |
396 | string escape(string in) |
| 397 | { |
397 | { |
| 398 | string out; |
398 | string out; |
| 399 | 399 | ||
| 400 | for (int n = 0; n < in.length(); n++) |
400 | for (int n = 0; n < in.length(); n++) |
| 401 | { |
401 | { |
| 402 | switch (in[n]) |
402 | switch (in[n]) |
| 403 | { |
403 | { |
| 404 | case '\r': |
404 | case '\r': |
| 405 | break; |
405 | break; |
| 406 | 406 | ||
| 407 | case '\n': |
407 | case '\n': |
| 408 | out += "\\n"; |
408 | out += "\\n"; |
| 409 | break; |
409 | break; |
| 410 | 410 | ||
| 411 | case '\'': |
411 | case '\'': |
| 412 | out += "\\'"; |
412 | out += "\\'"; |
| 413 | break; |
413 | break; |
| 414 | 414 | ||
| 415 | case '"': |
415 | case '"': |
| 416 | out += "\\\""; |
416 | out += "\\\""; |
| 417 | break; |
417 | break; |
| 418 | 418 | ||
| 419 | default: |
419 | default: |
| 420 | out += in[n]; |
420 | out += in[n]; |
| 421 | break; |
421 | break; |
| 422 | } |
422 | } |
| 423 | } |
423 | } |
| 424 | 424 | ||
| 425 | return out; |
425 | return out; |
| 426 | } |
426 | } |
| 427 | 427 | ||
| 428 | string rpad(string in, int length, char c) |
428 | string rpad(string in, int length, char c) |
| 429 | { |
429 | { |
| 430 | while (in.length() < length) |
430 | while (in.length() < length) |
| 431 | { |
431 | { |
| 432 | in += c; |
432 | in += c; |
| 433 | } |
433 | } |
| 434 | 434 | ||
| 435 | return in; |
435 | return in; |
| 436 | } |
436 | } |
| 437 | 437 | ||
| 438 | string lpad(string in, int length, char c) |
438 | string lpad(string in, int length, char c) |
| 439 | { |
439 | { |
| 440 | while (in.length() < length) |
440 | while (in.length() < length) |
| 441 | { |
441 | { |
| 442 | in = c + in; |
442 | in = c + in; |
| 443 | } |
443 | } |
| 444 | 444 | ||
| 445 | return in; |
445 | return in; |
| 446 | } |
446 | } |
| 447 | 447 | ||