Subversion Repositories HomeAutomation

Rev

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