Subversion Repositories HomeAutomation

Rev

Rev 1380 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

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