Subversion Repositories HomeAutomation

Rev

Rev 1026 | Rev 1380 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1026 Rev 1227
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
 
-
 
207
    while (num)
206
    while (num)
208
    {
207
    {
209
        bin = char((num&1)+'0') + bin;
208
        bin = char((num&1)+'0') + bin;
210
        num >>= 1;
209
        num >>= 1;
211
    }
210
    }
212
 
211
 
213
    while (bin.size() < length)
212
    while (bin.size() < length)
214
        bin = "0" + bin;
213
        bin = "0" + bin;
215
 
214
   
-
 
215
    string bin2;
-
 
216
    for (int i = 0; i < length; i++)
-
 
217
    {
-
 
218
        bin2 = bin2 + bin[i];
-
 
219
    }
-
 
220
   
216
    return bin;
221
    return bin2;
217
}
222
}
218
 
223
 
219
string uint2hex(unsigned int num, int length)
224
string uint2hex(unsigned int num, int length)
220
{
225
{
221
    return bin2hex(uint2bin(num, length));
226
    return bin2hex(uint2bin(num, length));
222
}
227
}
223
 
228
 
224
vector<string> explode(string delimiter, string str)
229
vector<string> explode(string delimiter, string str)
225
{
230
{
226
    vector<string> parts;
231
    vector<string> parts;
227
    string::size_type startPos = 0;
232
    string::size_type startPos = 0;
228
    string::size_type endPos = 0;
233
    string::size_type endPos = 0;
229
 
234
 
230
    while ((endPos = str.find(delimiter, startPos)) != string::npos)
235
    while ((endPos = str.find(delimiter, startPos)) != string::npos)
231
    {
236
    {
232
        try
237
        try
233
        {
238
        {
234
            parts.push_back(str.substr(startPos, endPos-startPos));
239
            parts.push_back(str.substr(startPos, endPos-startPos));
235
        }
240
        }
236
        catch (std::out_of_range& e)
241
        catch (std::out_of_range& e)
237
        {
242
        {
238
            cout << "DEBUG: explode exception: " << e.what() << "\n";
243
            cout << "DEBUG: explode exception: " << e.what() << "\n";
239
            cout << "DEBUG: A str=" << str << " :: startPos=" << startPos << " :: endPos=" << endPos << endl;
244
            cout << "DEBUG: A str=" << str << " :: startPos=" << startPos << " :: endPos=" << endPos << endl;
240
            continue;
245
            continue;
241
        }
246
        }
242
       
247
       
243
        startPos = endPos+delimiter.length();
248
        startPos = endPos+delimiter.length();
244
    }
249
    }
245
 
250
 
246
    if (startPos < str.length())
251
    if (startPos < str.length())
247
    {
252
    {
248
        try
253
        try
249
        {
254
        {
250
            parts.push_back(str.substr(startPos, str.length()-startPos));
255
            parts.push_back(str.substr(startPos, str.length()-startPos));
251
        }
256
        }
252
        catch (std::out_of_range& e)
257
        catch (std::out_of_range& e)
253
        {
258
        {
254
            cout << "DEBUG: explode exception: " << e.what() << "\n";
259
            cout << "DEBUG: explode exception: " << e.what() << "\n";
255
            cout << "DEBUG: B str=" << str << " :: startPos=" << startPos << " :: endPos=" << endPos << endl;
260
            cout << "DEBUG: B str=" << str << " :: startPos=" << startPos << " :: endPos=" << endPos << endl;
256
        }
261
        }
257
    }
262
    }
258
 
263
 
259
    return parts;
264
    return parts;
260
}
265
}
261
 
266
 
262
string strtoupper(string s)
267
string strtoupper(string s)
263
{
268
{
264
    for (unsigned int n = 0; n < s.size(); n++)
269
    for (unsigned int n = 0; n < s.size(); n++)
265
        s[n] = (char)toupper(s[n]);
270
        s[n] = (char)toupper(s[n]);
266
 
271
 
267
    return s;
272
    return s;
268
}
273
}
269
 
274
 
270
string strtolower(string s)
275
string strtolower(string s)
271
{
276
{
272
    for (unsigned int n = 0; n < s.size(); n++)
277
    for (unsigned int n = 0; n < s.size(); n++)
273
        s[n] = (char)tolower(s[n]);
278
        s[n] = (char)tolower(s[n]);
274
 
279
 
275
    return s;
280
    return s;
276
}
281
}
277
 
282
 
278
string trim(string s)
283
string trim(string s)
279
{
284
{
280
    return trim(s, ' ');
285
    return trim(s, ' ');
281
}
286
}
282
 
287
 
283
string trim(const string s, char c)
288
string trim(const string s, char c)
284
{
289
{
285
    string result = s;
290
    string result = s;
286
    while (result[0] == c)
291
    while (result[0] == c)
287
        result.erase(0, 1);
292
        result.erase(0, 1);
288
 
293
 
289
    while (result[result.length()-1] == c)
294
    while (result[result.length()-1] == c)
290
        result.erase(result.length()-1, 1);
295
        result.erase(result.length()-1, 1);
291
 
296
 
292
    return result;
297
    return result;
293
}
298
}
294
 
299
 
295
int stoi(const string s)
300
int stoi(const string s)
296
{
301
{
297
    istringstream in(s);
302
    istringstream in(s);
298
    int i;
303
    int i;
299
    in >> i;
304
    in >> i;
300
    return i;
305
    return i;
301
}
306
}
302
 
307
 
303
string itos(int i)
308
string itos(int i)
304
{
309
{
305
    ostringstream out;
310
    ostringstream out;
306
    out << i;
311
    out << i;
307
    return out.str();
312
    return out.str();
308
}
313
}
309
 
314
 
310
unsigned int stou(const string s)
315
unsigned int stou(const string s)
311
{
316
{
312
    istringstream in(s);
317
    istringstream in(s);
313
    unsigned int i;
318
    unsigned int i;
314
    in >> i;
319
    in >> i;
315
    return i;
320
    return i;
316
}
321
}
317
 
322
 
318
string utos(unsigned int i)
323
string utos(unsigned int i)
319
{
324
{
320
    ostringstream out;
325
    ostringstream out;
321
    out << i;
326
    out << i;
322
    return out.str();
327
    return out.str();
323
}
328
}
324
 
329
 
325
float stof(const string s)
330
float stof(const string s)
326
{
331
{
327
    istringstream in(s);
332
    istringstream in(s);
328
    float f;
333
    float f;
329
    in >> f;
334
    in >> f;
330
    return f;
335
    return f;
331
}
336
}
332
 
337
 
333
string ftos(float f)
338
string ftos(float f)
334
{
339
{
335
    ostringstream out;
340
    ostringstream out;
336
    out << f;
341
    out << f;
337
    return out.str();
342
    return out.str();
338
}
343
}
339
 
344
 
340
string str_replace(string search, string replace, string str)
345
string str_replace(string search, string replace, string str)
341
{
346
{
342
    vector<string> parts = explode(search, str);
347
    vector<string> parts = explode(search, str);
343
 
348
 
344
    string result;
349
    string result;
345
    for (int n = 0; n < parts.size(); n++)
350
    for (int n = 0; n < parts.size(); n++)
346
    {
351
    {
347
        result += parts[n];
352
        result += parts[n];
348
 
353
 
349
        if (n < parts.size()-1)
354
        if (n < parts.size()-1)
350
            result += replace;
355
            result += replace;
351
    }
356
    }
352
 
357
 
353
    return result;
358
    return result;
354
}
359
}
355
 
360
 
356
string file_get_contents(string filename)
361
string file_get_contents(string filename)
357
{
362
{
358
    ifstream srcfile(filename.c_str());
363
    ifstream srcfile(filename.c_str());
359
 
364
 
360
    if (!srcfile.is_open())
365
    if (!srcfile.is_open())
361
    {
366
    {
362
        srcfile.close();
367
        srcfile.close();
363
        return "";
368
        return "";
364
    }
369
    }
365
 
370
 
366
    string buffer = "";
371
    string buffer = "";
367
    string line;
372
    string line;
368
 
373
 
369
    while (!srcfile.eof())
374
    while (!srcfile.eof())
370
    {
375
    {
371
        getline(srcfile, line);
376
        getline(srcfile, line);
372
 
377
 
373
        if (srcfile.eof())
378
        if (srcfile.eof())
374
            break;
379
            break;
375
       
380
       
376
        buffer += line + "\n";
381
        buffer += line + "\n";
377
    };
382
    };
378
 
383
 
379
    srcfile.close();
384
    srcfile.close();
380
 
385
 
381
    return buffer;
386
    return buffer;
382
}
387
}
383
 
388
 
384
bool file_exists(string filename)
389
bool file_exists(string filename)
385
{
390
{
386
    ifstream file(filename.c_str());
391
    ifstream file(filename.c_str());
387
    if (file.is_open())
392
    if (file.is_open())
388
    {
393
    {
389
        file.close();
394
        file.close();
390
        return true;
395
        return true;
391
    }
396
    }
392
 
397
 
393
    return false;
398
    return false;
394
}
399
}
395
 
400
 
396
string escape(string in)
401
string escape(string in)
397
{
402
{
398
    string out;
403
    string out;
399
 
404
 
400
    for (int n = 0; n < in.length(); n++)
405
    for (int n = 0; n < in.length(); n++)
401
    {
406
    {
402
        switch (in[n])
407
        switch (in[n])
403
        {
408
        {
404
            case '\r':
409
            case '\r':
405
                break;
410
                break;
406
 
411
 
407
            case '\n':
412
            case '\n':
408
                out += "\\n";
413
                out += "\\n";
409
                break;
414
                break;
410
 
415
 
411
            case '\'':
416
            case '\'':
412
                out += "\\'";
417
                out += "\\'";
413
                break;
418
                break;
414
 
419
 
415
            case '"':
420
            case '"':
416
                out += "\\\"";
421
                out += "\\\"";
417
                break;
422
                break;
418
 
423
 
419
            default:
424
            default:
420
                out += in[n];
425
                out += in[n];
421
                break;
426
                break;
422
        }
427
        }
423
    }
428
    }
424
 
429
 
425
    return out;
430
    return out;
426
}
431
}
427
 
432
 
428
string rpad(string in, int length, char c)
433
string rpad(string in, int length, char c)
429
{
434
{
430
    while (in.length() < length)
435
    while (in.length() < length)
431
    {
436
    {
432
        in += c;
437
        in += c;
433
    }
438
    }
434
 
439
 
435
    return in;
440
    return in;
436
}
441
}
437
 
442
 
438
string lpad(string in, int length, char c)
443
string lpad(string in, int length, char c)
439
{
444
{
440
    while (in.length() < length)
445
    while (in.length() < length)
441
    {
446
    {
442
        in = c + in;
447
        in = c + in;
443
    }
448
    }
444
 
449
 
445
    return in;
450
    return in;
446
}
451
}
447
 
452