Subversion Repositories HomeAutomation

Rev

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