Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1309 runge 1
/*
2
 * Utils.cpp
3
 *
4
 *  Created on: Apr 26, 2009
5
 *      Author: Mattias Runge
6
 */
7
 
8
#include "Utils.h"
9
 
10
namespace atom {
11
namespace utils {
12
 
13
string bytes2string(byte_list bytes)
14
{
15
    string str = "";
16
 
17
    for (unsigned int n = 0; n < bytes.size(); n++)
18
    {
19
        str += bytes[n];
20
    }
21
 
22
    return str;
23
}
24
 
25
string niceTime()
26
{
27
    string result;
28
    struct tm tmStruct;
29
    time_t t = time(NULL);
30
    localtime_r(&t, &tmStruct);
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
 
41
unsigned int hex2uint(string hex)
42
{
43
    return bin2uint(hex2bin(hex));
44
}
45
 
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') num += (unsigned int)pow(2.0f, (int)(bin.length()
53
                - 1 - n));
54
    }
55
 
56
    return num;
57
}
58
 
59
string invert(string bin)
60
{
61
    string inverted;
62
 
63
    for (unsigned int n = 0; n < bin.length(); n++)
64
        inverted += (bin[n] == '1' ? '0' : '1');
65
 
66
    return inverted;
67
}
68
 
69
float bin2float(string bin)
70
{
71
    float num = 0;
72
 
73
    if (bin.length() == 0) return 0.0f;
74
 
75
    bool isNegative = false;
76
    ;
77
 
78
    if (bin[0] == '1')
79
    {
80
        bin = invert(bin);
81
        isNegative = true;
82
    }
83
 
84
    for (int n = bin.length() - 1; n > 0; n--)
85
    {
86
        if (bin[n] == '1') num += pow(2.0f, (int)(bin.length() - 1 - n));
87
    }
88
 
89
    num /= 64.0f;
90
 
91
    if (isNegative) num = -num;
92
 
93
    return num;
94
}
95
 
96
string bin2hex(string bin)
97
{
98
    string hex;
99
 
100
    while (bin.size() > 0)
101
    {
102
        string part;
103
 
104
        try
105
        {
106
            part = bin.substr(0, 4);
107
        }
108
        catch (std::out_of_range& e)
109
        {
110
            cout << "DEBUG: bin2hex exception: " << e.what() << "\n";
111
            cout << "DEBUG: A bin=" << bin << endl;
112
            continue;
113
        }
114
 
115
        bin.erase(0, part.size());
116
 
117
        while (part.size() < 4)
118
            part = "0" + part;
119
 
120
        if (part == "0000")
121
            hex += '0';
122
        else if (part == "0001")
123
            hex += '1';
124
        else if (part == "0010")
125
            hex += '2';
126
        else if (part == "0011")
127
            hex += '3';
128
        else if (part == "0100")
129
            hex += '4';
130
        else if (part == "0101")
131
            hex += '5';
132
        else if (part == "0110")
133
            hex += '6';
134
        else if (part == "0111")
135
            hex += '7';
136
        else if (part == "1000")
137
            hex += '8';
138
        else if (part == "1001")
139
            hex += '9';
140
        else if (part == "1010")
141
            hex += 'a';
142
        else if (part == "1011")
143
            hex += 'b';
144
        else if (part == "1100")
145
            hex += 'c';
146
        else if (part == "1101")
147
            hex += 'd';
148
        else if (part == "1110")
149
            hex += 'e';
150
        else if (part == "1111") hex += 'f';
151
    }
152
 
153
    return hex;
154
}
155
 
156
string float2bin(float num, unsigned int length)
157
{
158
    string bin;
159
 
160
    ///FIXME
161
 
162
    while (bin.size() < length)
163
        bin = "0" + bin;
164
 
165
    return bin;
166
}
167
 
168
string hex2bin(string hex)
169
{
170
    hex = strtolower(hex);
171
 
172
    string bin;
173
 
174
    for (unsigned int n = 0; n < hex.length(); n++)
175
    {
176
        switch (hex[n])
177
        {
178
        case '0':
179
            bin += "0000";
180
            break;
181
        case '1':
182
            bin += "0001";
183
            break;
184
        case '2':
185
            bin += "0010";
186
            break;
187
        case '3':
188
            bin += "0011";
189
            break;
190
        case '4':
191
            bin += "0100";
192
            break;
193
        case '5':
194
            bin += "0101";
195
            break;
196
        case '6':
197
            bin += "0110";
198
            break;
199
        case '7':
200
            bin += "0111";
201
            break;
202
        case '8':
203
            bin += "1000";
204
            break;
205
        case '9':
206
            bin += "1001";
207
            break;
208
        case 'a':
209
            bin += "1010";
210
            break;
211
        case 'b':
212
            bin += "1011";
213
            break;
214
        case 'c':
215
            bin += "1100";
216
            break;
217
        case 'd':
218
            bin += "1101";
219
            break;
220
        case 'e':
221
            bin += "1110";
222
            break;
223
        case 'f':
224
            bin += "1111";
225
            break;
226
        }
227
    }
228
 
229
    return bin;
230
}
231
 
232
string uint2bin(unsigned int num, unsigned int length)
233
{
234
    string bin;
235
 
236
    while (num)
237
    {
238
        bin = char((num & 1) + '0') + bin;
239
        num >>= 1;
240
    }
241
 
242
    while (bin.size() < length)
243
        bin = "0" + bin;
244
 
245
    return bin;
246
}
247
 
248
string uint2hex(unsigned int num, unsigned int length)
249
{
250
    return bin2hex(uint2bin(num, length));
251
}
252
 
253
vector<string> explode(string delimiter, string str)
254
{
255
    vector<string> parts;
256
    string::size_type startPos = 0;
257
    string::size_type endPos = 0;
258
 
259
    while ((endPos = str.find(delimiter, startPos)) != string::npos)
260
    {
261
        try
262
        {
263
            parts.push_back(str.substr(startPos, endPos - startPos));
264
        }
265
        catch (std::out_of_range& e)
266
        {
267
            cout << "DEBUG: explode exception: " << e.what() << "\n";
268
            cout << "DEBUG: A str=" << str << " :: startPos=" << startPos
269
                    << " :: endPos=" << endPos << endl;
270
            continue;
271
        }
272
 
273
        startPos = endPos + delimiter.length();
274
    }
275
 
276
    if (startPos < str.length())
277
    {
278
        try
279
        {
280
            parts.push_back(str.substr(startPos, str.length() - startPos));
281
        }
282
        catch (std::out_of_range& e)
283
        {
284
            cout << "DEBUG: explode exception: " << e.what() << "\n";
285
            cout << "DEBUG: B str=" << str << " :: startPos=" << startPos
286
                    << " :: endPos=" << endPos << endl;
287
        }
288
    }
289
 
290
    return parts;
291
}
292
 
293
string strtoupper(string s)
294
{
295
    for (unsigned int n = 0; n < s.size(); n++)
296
        s[n] = (char)toupper(s[n]);
297
 
298
    return s;
299
}
300
 
301
string strtolower(string s)
302
{
303
    for (unsigned int n = 0; n < s.size(); n++)
304
        s[n] = (char)tolower(s[n]);
305
 
306
    return s;
307
}
308
 
309
string trim(string s)
310
{
311
    return trim(s, ' ');
312
}
313
 
314
string trim(const string s, char c)
315
{
316
    string result = s;
317
    while (result[0] == c)
318
        result.erase(0, 1);
319
 
320
    while (result[result.length() - 1] == c)
321
        result.erase(result.length() - 1, 1);
322
 
323
    return result;
324
}
325
 
326
int stoi(const string s)
327
{
328
    istringstream in(s);
329
    int i;
330
    in >> i;
331
    return i;
332
}
333
 
334
string itos(int i)
335
{
336
    ostringstream out;
337
    out << i;
338
    return out.str();
339
}
340
 
341
unsigned int stou(const string s)
342
{
343
    istringstream in(s);
344
    unsigned int i;
345
    in >> i;
346
    return i;
347
}
348
 
349
string utos(unsigned int i)
350
{
351
    ostringstream out;
352
    out << i;
353
    return out.str();
354
}
355
 
356
float stof(const string s)
357
{
358
    istringstream in(s);
359
    float f;
360
    in >> f;
361
    return f;
362
}
363
 
364
string ftos(float f)
365
{
366
    ostringstream out;
367
    out << f;
368
    return out.str();
369
}
370
 
371
string str_replace(string search, string replace, string str)
372
{
373
    vector<string> parts = explode(search, str);
374
 
375
    string result;
376
    for (unsigned int n = 0; n < parts.size(); n++)
377
    {
378
        result += parts[n];
379
 
380
        if (n < parts.size() - 1) result += replace;
381
    }
382
 
383
    return result;
384
}
385
 
386
string file_get_contents(string filename)
387
{
388
    ifstream srcfile(filename.c_str());
389
 
390
    if (!srcfile.is_open())
391
    {
392
        srcfile.close();
393
        return "";
394
    }
395
 
396
    string buffer = "";
397
    string line;
398
 
399
    while (!srcfile.eof())
400
    {
401
        getline(srcfile, line);
402
 
403
        if (srcfile.eof()) break;
404
 
405
        buffer += line + "\n";
406
    };
407
 
408
    srcfile.close();
409
 
410
    return buffer;
411
}
412
 
413
bool file_exists(string filename)
414
{
415
    ifstream file(filename.c_str());
416
    if (file.is_open())
417
    {
418
        file.close();
419
        return true;
420
    }
421
 
422
    return false;
423
}
424
 
425
string escape(string in)
426
{
427
    string out;
428
 
429
    for (unsigned int n = 0; n < in.length(); n++)
430
    {
431
        switch (in[n])
432
        {
433
        case '\r':
434
            break;
435
 
436
        case '\n':
437
            out += "\\n";
438
            break;
439
 
440
        case '\'':
441
            out += "\\'";
442
            break;
443
 
444
        case '"':
445
            out += "\\\"";
446
            break;
447
 
448
        default:
449
            out += in[n];
450
            break;
451
        }
452
    }
453
 
454
    return out;
455
}
456
 
457
string rpad(string in, unsigned int length, char c)
458
{
459
    while (in.length() < length)
460
    {
461
        in += c;
462
    }
463
 
464
    return in;
465
}
466
 
467
string lpad(string in, unsigned int length, char c)
468
{
469
    while (in.length() < length)
470
    {
471
        in = c + in;
472
    }
473
 
474
    return in;
475
}
476
 
477
}
478
}