Subversion Repositories HomeAutomation

Rev

Rev 1011 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1011 Rev 1024
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
 
206
 
207
    while (num)
207
    while (num)
208
    {
208
    {
209
        bin = char((num&1)+'0') + bin;
209
        bin = char((num&1)+'0') + bin;
210
        num >>= 1;
210
        num >>= 1;
211
    }
211
    }
212
 
212
 
213
    while (bin.size() < length)
213
    while (bin.size() < length)
214
        bin = "0" + bin;
214
        bin = "0" + bin;
215
 
215
 
216
    return bin;
216
    return bin;
217
}
217
}
218
 
218
 
219
string uint2hex(unsigned int num, int length)
219
string uint2hex(unsigned int num, int length)
220
{
220
{
221
    return bin2hex(uint2bin(num, length));
221
    return bin2hex(uint2bin(num, length));
222
}
222
}
223
 
223
 
224
vector<string> explode(string delimiter, string str)
224
vector<string> explode(string delimiter, string str)
225
{
225
{
226
    vector<string> parts;
226
    vector<string> parts;
227
    string::size_type startPos = 0;
227
    string::size_type startPos = 0;
228
    string::size_type endPos = 0;
228
    string::size_type endPos = 0;
229
 
229
 
230
    while ((endPos = str.find(delimiter, startPos)) != string::npos)
230
    while ((endPos = str.find(delimiter, startPos)) != string::npos)
231
    {
231
    {
232
        try
232
        try
233
        {
233
        {
234
            parts.push_back(str.substr(startPos, endPos-startPos));
234
            parts.push_back(str.substr(startPos, endPos-startPos));
235
        }
235
        }
236
        catch (std::out_of_range& e)
236
        catch (std::out_of_range& e)
237
        {
237
        {
238
            cout << "DEBUG: explode exception: " << e.what() << "\n";
238
            cout << "DEBUG: explode exception: " << e.what() << "\n";
239
            cout << "DEBUG: A str=" << str << " :: startPos=" << startPos << " :: endPos=" << endPos << endl;
239
            cout << "DEBUG: A str=" << str << " :: startPos=" << startPos << " :: endPos=" << endPos << endl;
240
            continue;
240
            continue;
241
        }
241
        }
242
       
242
       
243
        startPos = endPos+delimiter.length();
243
        startPos = endPos+delimiter.length();
244
    }
244
    }
245
 
245
 
246
    if (startPos < str.length())
246
    if (startPos < str.length())
247
    {
247
    {
248
        try
248
        try
249
        {
249
        {
250
            parts.push_back(str.substr(startPos, str.length()-startPos));
250
            parts.push_back(str.substr(startPos, str.length()-startPos));
251
        }
251
        }
252
        catch (std::out_of_range& e)
252
        catch (std::out_of_range& e)
253
        {
253
        {
254
            cout << "DEBUG: explode exception: " << e.what() << "\n";
254
            cout << "DEBUG: explode exception: " << e.what() << "\n";
255
            cout << "DEBUG: B str=" << str << " :: startPos=" << startPos << " :: endPos=" << endPos << endl;
255
            cout << "DEBUG: B str=" << str << " :: startPos=" << startPos << " :: endPos=" << endPos << endl;
256
        }
256
        }
257
    }
257
    }
258
 
258
 
259
    return parts;
259
    return parts;
260
}
260
}
261
 
261
 
262
string strtoupper(string s)
262
string strtoupper(string s)
263
{
263
{
264
    for (unsigned int n = 0; n < s.size(); n++)
264
    for (unsigned int n = 0; n < s.size(); n++)
265
        s[n] = (char)toupper(s[n]);
265
        s[n] = (char)toupper(s[n]);
266
 
266
 
267
    return s;
267
    return s;
268
}
268
}
269
 
269
 
270
string strtolower(string s)
270
string strtolower(string s)
271
{
271
{
272
    for (unsigned int n = 0; n < s.size(); n++)
272
    for (unsigned int n = 0; n < s.size(); n++)
273
        s[n] = (char)tolower(s[n]);
273
        s[n] = (char)tolower(s[n]);
274
 
274
 
275
    return s;
275
    return s;
276
}
276
}
277
 
277
 
278
string trim(string s)
278
string trim(string s)
279
{
279
{
280
    return trim(s, ' ');
280
    return trim(s, ' ');
281
}
281
}
282
 
282
 
283
string trim(const string s, char c)
283
string trim(const string s, char c)
284
{
284
{
285
    string result = s;
285
    string result = s;
286
    while (result[0] == c)
286
    while (result[0] == c)
287
        result.erase(0, 1);
287
        result.erase(0, 1);
288
 
288
 
289
    while (result[result.length()-1] == c)
289
    while (result[result.length()-1] == c)
290
        result.erase(result.length()-1, 1);
290
        result.erase(result.length()-1, 1);
291
 
291
 
292
    return result;
292
    return result;
293
}
293
}
294
 
294
 
295
int stoi(const string s)
295
int stoi(const string s)
296
{
296
{
297
    istringstream in(s);
297
    istringstream in(s);
298
    int i;
298
    int i;
299
    in >> i;
299
    in >> i;
300
    return i;
300
    return i;
301
}
301
}
302
 
302
 
303
string itos(int i)
303
string itos(int i)
304
{
304
{
305
    ostringstream out;
305
    ostringstream out;
306
    out << i;
306
    out << i;
307
    return out.str();
307
    return out.str();
308
}
308
}
309
 
309
 
310
unsigned int stou(const string s)
310
unsigned int stou(const string s)
311
{
311
{
312
    istringstream in(s);
312
    istringstream in(s);
313
    unsigned int i;
313
    unsigned int i;
314
    in >> i;
314
    in >> i;
315
    return i;
315
    return i;
316
}
316
}
317
 
317
 
318
string utos(unsigned int i)
318
string utos(unsigned int i)
319
{
319
{
320
    ostringstream out;
320
    ostringstream out;
321
    out << i;
321
    out << i;
322
    return out.str();
322
    return out.str();
323
}
323
}
324
 
324
 
325
float stof(const string s)
325
float stof(const string s)
326
{
326
{
327
    istringstream in(s);
327
    istringstream in(s);
328
    float f;
328
    float f;
329
    in >> f;
329
    in >> f;
330
    return f;
330
    return f;
331
}
331
}
332
 
332
 
333
string ftos(float f)
333
string ftos(float f)
334
{
334
{
335
    ostringstream out;
335
    ostringstream out;
336
    out << f;
336
    out << f;
337
    return out.str();
337
    return out.str();
338
}
338
}
339
 
339
 
340
string str_replace(string search, string replace, string str)
340
string str_replace(string search, string replace, string str)
341
{
341
{
342
    vector<string> parts = explode(search, str);
342
    vector<string> parts = explode(search, str);
343
 
343
 
344
    string result;
344
    string result;
345
    for (int n = 0; n < parts.size(); n++)
345
    for (int n = 0; n < parts.size(); n++)
346
    {
346
    {
347
        result += parts[n];
347
        result += parts[n];
348
 
348
 
349
        if (n < parts.size()-1)
349
        if (n < parts.size()-1)
350
            result += replace;
350
            result += replace;
351
    }
351
    }
352
 
352
 
353
    return result;
353
    return result;
354
}
354
}
355
 
355
 
356
string file_get_contents(string filename)
356
string file_get_contents(string filename)
357
{
357
{
358
    ifstream srcfile(filename.c_str());
358
    ifstream srcfile(filename.c_str());
359
 
359
 
360
    if (!srcfile.is_open())
360
    if (!srcfile.is_open())
361
    {
361
    {
362
        srcfile.close();
362
        srcfile.close();
363
        return "";
363
        return "";
364
    }
364
    }
365
 
365
 
366
    string buffer = "";
366
    string buffer = "";
367
    string line;
367
    string line;
368
 
368
 
369
    while (!srcfile.eof())
369
    while (!srcfile.eof())
370
    {
370
    {
371
        getline(srcfile, line);
371
        getline(srcfile, line);
372
 
372
 
373
        if (srcfile.eof())
373
        if (srcfile.eof())
374
            break;
374
            break;
375
       
375
       
376
        buffer += line + "\n";
376
        buffer += line + "\n";
377
    };
377
    };
378
 
378
 
379
    srcfile.close();
379
    srcfile.close();
380
 
380
 
381
    return buffer;
381
    return buffer;
382
}
382
}
383
 
383
 
384
bool file_exists(string filename)
384
bool file_exists(string filename)
385
{
385
{
386
    ifstream file(filename.c_str());
386
    ifstream file(filename.c_str());
387
    if (file.is_open())
387
    if (file.is_open())
388
    {
388
    {
389
        file.close();
389
        file.close();
390
        return true;
390
        return true;
391
    }
391
    }
392
 
392
 
393
    return false;
393
    return false;
394
}
394
}
395
 
395
 
396
string escape(string in)
396
string escape(string in)
397
{
397
{
398
    string out;
398
    string out;
399
 
399
 
400
    for (int n = 0; n < in.length(); n++)
400
    for (int n = 0; n < in.length(); n++)
401
    {
401
    {
402
        switch (in[n])
402
        switch (in[n])
403
        {
403
        {
404
            case '\r':
404
            case '\r':
405
                break;
405
                break;
406
 
406
 
407
            case '\n':
407
            case '\n':
408
                out += "\\n";
408
                out += "\\n";
409
                break;
409
                break;
410
 
410
 
411
            case '\'':
411
            case '\'':
412
                out += "\\'";
412
                out += "\\'";
413
                break;
413
                break;
414
 
414
 
415
            case '"':
415
            case '"':
416
                out += "\\\"";
416
                out += "\\\"";
417
                break;
417
                break;
418
 
418
 
419
            default:
419
            default:
420
                out += in[n];
420
                out += in[n];
421
                break;
421
                break;
422
        }
422
        }
423
    }
423
    }
424
 
424
 
425
    return out;
425
    return out;
426
}
426
}
427
 
427
 
428
string rpad(string in, int length, char c)
428
string rpad(string in, int length, char c)
429
{
429
{
430
    while (in.length() < length)
430
    while (in.length() < length)
431
    {
431
    {
432
        in += c;
432
        in += c;
433
    }
433
    }
434
 
434
 
435
    return in;
435
    return in;
436
}
436
}
437
 
437
 
438
string lpad(string in, int length, char c)
438
string lpad(string in, int length, char c)
439
{
439
{
440
    while (in.length() < length)
440
    while (in.length() < length)
441
    {
441
    {
442
        in = c + in;
442
        in = c + in;
443
    }
443
    }
444
 
444
 
445
    return in;
445
    return in;
446
}
446
}
447
 
-
 
448
 
-
 
449
// FileTools
-
 
450
 
-
 
451
vector<string> FileTools::GetDirList(string path)
-
 
452
{
-
 
453
    DIR *dir = opendir(path.c_str());
-
 
454
    vector<string> list;
-
 
455
 
-
 
456
    if (!dir)
-
 
457
        return list;
-
 
458
 
-
 
459
    dirent *d;
-
 
460
    while ((d = readdir(dir)))
-
 
461
    {
-
 
462
        if (d->d_name[0] == '.')
-
 
463
            continue;
-
 
464
 
-
 
465
        list.push_back(d->d_name);
-
 
466
    }
-
 
467
    closedir(dir);
-
 
468
 
-
 
469
    return list;
-
 
470
}
-
 
471
 
-
 
472
string FileTools::GetUniqeFilename(string path, string prefix, string postfix)
-
 
473
{
-
 
474
    time_t now;
-
 
475
    time(&now);
-
 
476
 
-
 
477
    string filename = path + prefix + itos(now);
-
 
478
 
-
 
479
    int index = 0;
-
 
480
    while (1)
-
 
481
    {
-
 
482
        string file = filename + itos(index) + postfix;
-
 
483
        if (!Exist(file))
-
 
484
            break;
-
 
485
        index++;
-
 
486
    }
-
 
487
 
-
 
488
    filename += itos(index) + postfix;
-
 
489
 
-
 
490
    return filename;
-
 
491
}
-
 
492
 
-
 
493
bool FileTools::SaveFile(string filepath, string data)
-
 
494
{
-
 
495
    ofstream destfile(filepath.c_str());
-
 
496
    if (!destfile.is_open())
-
 
497
    {
-
 
498
        destfile.close();
-
 
499
        return false;
-
 
500
    }
-
 
501
 
-
 
502
    destfile << data;
-
 
503
 
-
 
504
    destfile.close();
-
 
505
    return true;
-
 
506
}
-
 
507
 
-
 
508
string FileTools::Get(string src)
-
 
509
{
-
 
510
    ifstream srcfile(src.c_str());
-
 
511
    if (!srcfile.is_open())
-
 
512
    {
-
 
513
        srcfile.close();
-
 
514
        return "";
-
 
515
    }
-
 
516
 
-
 
517
    string buffer = "";
-
 
518
    string line;
-
 
519
    while (!srcfile.eof())
-
 
520
    {
-
 
521
        getline(srcfile, line);
-
 
522
        if (srcfile.eof())
-
 
523
            break;
-
 
524
        buffer += line + "\n";
-
 
525
    };
-
 
526
 
-
 
527
    srcfile.close();
-
 
528
 
-
 
529
    return buffer;
-
 
530
}
-
 
531
 
-
 
532
bool FileTools::Copy(string src, string dest)
-
 
533
{
-
 
534
    ifstream srcfile(src.c_str());
-
 
535
    if (!srcfile.is_open())
-
 
536
    {
-
 
537
        srcfile.close();
-
 
538
        return false;
-
 
539
    }
-
 
540
 
-
 
541
    ofstream destfile(dest.c_str());
-
 
542
    if (!destfile.is_open())
-
 
543
    {
-
 
544
        destfile.close();
-
 
545
        srcfile.close();
-
 
546
        return false;
-
 
547
    }
-
 
548
 
-
 
549
    string line;
-
 
550
    while (!srcfile.eof())
-
 
551
    {
-
 
552
        getline(srcfile, line);
-
 
553
        if (srcfile.eof())
-
 
554
            break;
-
 
555
        destfile << line << endl;
-
 
556
    };
-
 
557
 
-
 
558
    destfile.close();
-
 
559
    srcfile.close();
-
 
560
 
-
 
561
    return true;
-
 
562
}
-
 
563
 
-
 
564
bool FileTools::Exist(string filename)
-
 
565
{
-
 
566
    ifstream file(filename.c_str());
-
 
567
    if (file.is_open())
-
 
568
    {
-
 
569
        file.close();
-
 
570
        return true;
-
 
571
    }
-
 
572
 
-
 
573
    return false;
-
 
574
}
-
 
575
 
-
 
576
 
-
 
577
 
-
 
578
 
-
 
579
 
-
 
580
/*
-
 
581
void bd()
-
 
582
{
-
 
583
         int n,b=0,a[6],i=0,t=0;
-
 
584
         clrscr();
-
 
585
         printf("Conversion from Binary to Decimal\n");
-
 
586
         printf("Enter Binary Number\n");
-
 
587
         scanf("%d",&n);
-
 
588
         while(n!=0)
-
 
589
         {
-
 
590
          a[i]=n%10;
-
 
591
          n=n/10;
-
 
592
          if(a[i]!=1 && a[i]!=0)
-
 
593
           t=1;
-
 
594
          i++;
-
 
595
         }
-
 
596
         a[i]=2;
-
 
597
         n=1;
-
 
598
         for(i=0;a[i]!=2;i++)
-
 
599
         {
-
 
600
          b=b+a[i]*n;
-
 
601
          n=n*2;
-
 
602
         }
-
 
603
         if(t==0)
-
 
604
          printf("Decimal Equivalent=%d",B);
-
 
605
         else if(t==1)
-
 
606
          printf("Entered number is not binary.");
-
 
607
 
-
 
608
}
-
 
609
 
-
 
610
void db()
-
 
611
{
-
 
612
         int dec,bin,n,i=0,a[10];
-
 
613
         clrscr();
-
 
614
         printf("Conversion from Decimal to Binary\n");
-
 
615
         printf("Input decimal no.");
-
 
616
         scanf("%d",&dec);
-
 
617
         do
-
 
618
         {
-
 
619
         a[i]=dec%2;
-
 
620
         dec=dec/2;
-
 
621
         i++;
-
 
622
         }while(dec!=0);
-
 
623
         for(n=i-1;n>=0;n--)
-
 
624
         printf("%d",a[n]);
-
 
625
}
-
 
626
 
-
 
627
void doc()
-
 
628
{
-
 
629
         int n,i,a[10];
-
 
630
         clrscr();
-
 
631
         printf("Conversion from Decimal to Octal\n");
-
 
632
         printf("Enter a Decimal number\n");
-
 
633
         scanf("%d",&n);
-
 
634
         i=0;
-
 
635
         printf("Octal equavalent of %d is ",n);
-
 
636
         while(n!=0)
-
 
637
         {
-
 
638
          a[i]=n%8;
-
 
639
          n=n/8;
-
 
640
          i++;
-
 
641
         }
-
 
642
         i--;
-
 
643
         for(;i>=0;i--)
-
 
644
          printf("%d",a[i]);
-
 
645
}
-
 
646
 
-
 
647
void dh(long n)
-
 
648
{
-
 
649
         long i;
-
 
650
         if(n>0)
-
 
651
         {
-
 
652
          i=n%16;
-
 
653
          n=n/16;
-
 
654
          dh(n);
-
 
655
          if(i>=10)
-
 
656
          {
-
 
657
           switch(i)
-
 
658
           {
-
 
659
            case 10:
-
 
660
             printf("A");
-
 
661
             break;
-
 
662
            case 11:
-
 
663
             printf("B");
-
 
664
             break;
-
 
665
            case 12:
-
 
666
             printf("C");
-
 
667
             break;
-
 
668
            case 13:
-
 
669
             printf("D");
-
 
670
             break;
-
 
671
            case 14:
-
 
672
             printf("E");
-
 
673
             break;
-
 
674
            case 15:
-
 
675
             printf("F");
-
 
676
             break;
-
 
677
           }
-
 
678
          }
-
 
679
          else
-
 
680
           printf("%ld",i);
-
 
681
         }
-
 
682
}
-
 
683
 
-
 
684
void od()
-
 
685
{
-
 
686
         unsigned long n,i=0,a,p=1,t=0;
-
 
687
         clrscr();
-
 
688
         printf("Conversion from Octal to Decimal\n");
-
 
689
         printf("Enter a Octal number\n");
-
 
690
         scanf("%ld",&n);
-
 
691
         i=0;
-
 
692
         printf("Decimal equavalent of %ld",n);
-
 
693
         while(n!=0)
-
 
694
         {
-
 
695
          a=n%10;
-
 
696
          if(a>7)
-
 
697
           t=1;
-
 
698
          n=n/10;
-
 
699
          i=i+a*p;
-
 
700
          p=p*8;
-
 
701
         }
-
 
702
         if(t==0)
-
 
703
          printf("= %ld",i);
-
 
704
         else if(t==1)
-
 
705
          printf(" can't be calculated because it is not an Octal Number.\n");
-
 
706
}
-
 
707
 
-
 
708
void ob()
-
 
709
{
-
 
710
         int n,a[6],i=0,t=0;
-
 
711
         clrscr();
-
 
712
         printf("Convertion from Octal to Binary.\n");
-
 
713
         printf("Enter an Octal Number.\n");
-
 
714
         scanf("%d",&n);
-
 
715
         while(n!=0)
-
 
716
         {
-
 
717
          a[i]=n%10;
-
 
718
          n=n/10;
-
 
719
          if(a[i]>7)
-
 
720
           t=1;
-
 
721
          i++;
-
 
722
         }
-
 
723
         i--;
-
 
724
         if(t==0)
-
 
725
          for(;i>=0;i--)
-
 
726
          {
-
 
727
           switch(a[i])
-
 
728
           {
-
 
729
            case 0:
-
 
730
             printf("000");
-
 
731
             break;
-
 
732
            case 1:
-
 
733
             printf("001");
-
 
734
             break;
-
 
735
            case 2:
-
 
736
             printf("010");
-
 
737
             break;
-
 
738
            case 3:
-
 
739
             printf("011");
-
 
740
             break;
-
 
741
            case 4:
-
 
742
             printf("100");
-
 
743
             break;
-
 
744
            case 5:
-
 
745
             printf("101");
-
 
746
             break;
-
 
747
            case 6:
-
 
748
             printf("110");
-
 
749
             break;
-
 
750
            case 7:
-
 
751
             printf("111");
-
 
752
             break;
-
 
753
           }
-
 
754
          }
-
 
755
         if(t==1)
-
 
756
          printf("Not a Octal number\n");
-
 
757
}
-
 
758
 
-
 
759
void bo()
-
 
760
{
-
 
761
         int i=0,a[5],t=0;
-
 
762
         long int n;
-
 
763
         clrscr();
-
 
764
         printf("Convertion From Binary to Octal\n");
-
 
765
         printf("Enter a Binary number\n");
-
 
766
         scanf("%ld",&n);
-
 
767
         while(n!=0)
-
 
768
         {
-
 
769
          a[i]=n%1000;
-
 
770
          n=n/1000;
-
 
771
          if(a[i]>111)
-
 
772
           t=1;
-
 
773
          i++;
-
 
774
         }
-
 
775
         i--;
-
 
776
         if(t==0)
-
 
777
          for(;i>=0;i--)
-
 
778
          {
-
 
779
           switch(a[i])
-
 
780
           {
-
 
781
            case 0:
-
 
782
             printf("0");
-
 
783
             break;
-
 
784
            case 1:
-
 
785
             printf("1");
-
 
786
             break;
-
 
787
            case 10:
-
 
788
             printf("2");
-
 
789
             break;
-
 
790
            case 11:
-
 
791
             printf("3");
-
 
792
             break;
-
 
793
            case 100:
-
 
794
             printf("4");
-
 
795
             break;
-
 
796
            case 101:
-
 
797
             printf("5");
-
 
798
             break;
-
 
799
            case 110:
-
 
800
             printf("6");
-
 
801
             break;
-
 
802
            case 111:
-
 
803
             printf("7");
-
 
804
             break;
-
 
805
            default:
-
 
806
   printf("\nEntered number is not binary.\nPrinted value is not correct.\n");
-
 
807
             break;
-
 
808
           }
-
 
809
          }
-
 
810
         if(t==1)
-
 
811
          printf("Number is not Binary\n");
-
 
812
}
-
 
813
 
-
 
814
void bh()
-
 
815
{
-
 
816
         int i=0,a[5],t=0;
-
 
817
         long int n;
-
 
818
         clrscr();
-
 
819
         printf("Convertion from Binary to Hexadecimal\n");
-
 
820
         printf("Enter a Binary number\n");
-
 
821
         scanf("%ld",&n);
-
 
822
         while(n!=0)
-
 
823
         {
-
 
824
          a[i]=n%10000;
-
 
825
          n=n/10000;
-
 
826
          if(a[i]>1111)
-
 
827
           t=1;
-
 
828
          i++;
-
 
829
         }
-
 
830
         i--;
-
 
831
         if(t==0)
-
 
832
          for(;i>=0;i--)
-
 
833
          {
-
 
834
           switch(a[i])
-
 
835
           {
-
 
836
            case 0:
-
 
837
             printf("0");
-
 
838
             break;
-
 
839
            case 1:
-
 
840
             printf("1");
-
 
841
             break;
-
 
842
            case 10:
-
 
843
             printf("2");
-
 
844
             break;
-
 
845
            case 11:
-
 
846
             printf("3");
-
 
847
             break;
-
 
848
            case 100:
-
 
849
             printf("4");
-
 
850
             break;
-
 
851
            case 101:
-
 
852
             printf("5");
-
 
853
             break;
-
 
854
            case 110:
-
 
855
             printf("6");
-
 
856
             break;
-
 
857
            case 111:
-
 
858
             printf("7");
-
 
859
             break;
-
 
860
            case 1000:
-
 
861
             printf("8");
-
 
862
             break;
-
 
863
            case 1001:
-
 
864
             printf("9");
-
 
865
             break;
-
 
866
            case 1010:
-
 
867
             printf("A");
-
 
868
             break;
-
 
869
            case 1011:
-
 
870
             printf("B");
-
 
871
             break;
-
 
872
            case 1100:
-
 
873
             printf("C");
-
 
874
             break;
-
 
875
            case 1101:
-
 
876
             printf("D");
-
 
877
             break;
-
 
878
            case 1110:
-
 
879
             printf("E");
-
 
880
             break;
-
 
881
            case 1111:
-
 
882
             printf("F");
-
 
883
             break;
-
 
884
            default:
-
 
885
     printf("\nEntered number is not binary.\nPrinted value is not correct.\n");
-
 
886
             break;
-
 
887
           }
-
 
888
          }
-
 
889
         if(t==1)
-
 
890
          printf("Number is not Binary\n");
-
 
891
}
-
 
892
 
-
 
893
void hb()
-
 
894
{
-
 
895
         int i;
-
 
896
         char s[20];
-
 
897
         clrscr();
-
 
898
         printf("Convertion from Hexadecimal to Binary\n");
-
 
899
         printf("Enter a Hexadecimal number\n");
-
 
900
         scanf("%s",s);
-
 
901
         //gets(s);
-
 
902
         printf("Binary Equivalent=");
-
 
903
         for(i=0;s[i]!=NULL;i++)
-
 
904
         {
-
 
905
          switch(s[i])
-
 
906
           {
-
 
907
            case '0':
-
 
908
             printf("0000");
-
 
909
             break;
-
 
910
            case '1':
-
 
911
             printf("0001");
-
 
912
             break;
-
 
913
            case '2':
-
 
914
             printf("0010");
-
 
915
             break;
-
 
916
            case '3':
-
 
917
             printf("0011");
-
 
918
             break;
-
 
919
            case '4':
-
 
920
             printf("0100");
-
 
921
             break;
-
 
922
            case '5':
-
 
923
             printf("0101");
-
 
924
             break;
-
 
925
            case '6':
-
 
926
             printf("0110");
-
 
927
             break;
-
 
928
            case '7':
-
 
929
             printf("0111");
-
 
930
             break;
-
 
931
            case '8':
-
 
932
             printf("1000");
-
 
933
             break;
-
 
934
            case '9':
-
 
935
             printf("1001");
-
 
936
             break;
-
 
937
            case 'a':
-
 
938
            case 'A':
-
 
939
             printf("1010");
-
 
940
             break;
-
 
941
            case 'b':
-
 
942
            case 'B':
-
 
943
             printf("1011");
-
 
944
             break;
-
 
945
            case 'c':
-
 
946
            case 'C':
-
 
947
             printf("1100");
-
 
948
             break;
-
 
949
            case 'd':
-
 
950
            case 'D':
-
 
951
             printf("1101");
-
 
952
             break;
-
 
953
            case 'e':
-
 
954
            case 'E':
-
 
955
             printf("1110");
-
 
956
             break;
-
 
957
            case 'f':
-
 
958
            case 'F':
-
 
959
             printf("1111");
-
 
960
             break;
-
 
961
            default:
-
 
962
       printf("\nEntered number is not Hexadecimal.\nPrinted value is not correct.\n");
-
 
963
             break;
-
 
964
           }
-
 
965
          }
-
 
966
}
-
 
967
 
-
 
968
void hd()
-
 
969
{
-
 
970
         int i,a[20];
-
 
971
         unsigned long int h=0,m=1;
-
 
972
         char s[20];
-
 
973
         clrscr();
-
 
974
         printf("Convertion from Hexadecimal to Decimal\n");
-
 
975
         printf("Enter a Hexadecimal number\n");
-
 
976
         scanf("%s",s);
-
 
977
         printf("Decimal Equivalent=");
-
 
978
         for(i=0;s[i]!=NULL;i++)
-
 
979
         {
-
 
980
          switch(s[i])
-
 
981
           {
-
 
982
            case '0':
-
 
983
             a[i]=0;
-
 
984
             break;
-
 
985
            case '1':
-
 
986
             a[i]=1;
-
 
987
             break;
-
 
988
            case '2':
-
 
989
             a[i]=2;
-
 
990
             break;
-
 
991
            case '3':
-
 
992
             a[i]=3;
-
 
993
             break;
-
 
994
            case '4':
-
 
995
             a[i]=4;
-
 
996
             break;
-
 
997
            case '5':
-
 
998
             a[i]=5;
-
 
999
             break;
-
 
1000
            case '6':
-
 
1001
             a[i]=6;
-
 
1002
             break;
-
 
1003
            case '7':
-
 
1004
             a[i]=7;
-
 
1005
             break;
-
 
1006
            case '8':
-
 
1007
             a[i]=8;
-
 
1008
             break;
-
 
1009
            case '9':
-
 
1010
             a[i]=9;
-
 
1011
             break;
-
 
1012
            case 'a':
-
 
1013
            case 'A':
-
 
1014
             a[i]=10;
-
 
1015
             break;
-
 
1016
            case 'b':
-
 
1017
            case 'B':
-
 
1018
             a[i]=11;
-
 
1019
             break;
-
 
1020
            case 'c':
-
 
1021
            case 'C':
-
 
1022
             a[i]=12;
-
 
1023
             break;
-
 
1024
            case 'd':
-
 
1025
            case 'D':
-
 
1026
             a[i]=13;
-
 
1027
             break;
-
 
1028
            case 'e':
-
 
1029
            case 'E':
-
 
1030
             a[i]=14;
-
 
1031
             break;
-
 
1032
            case 'f':
-
 
1033
            case 'F':
-
 
1034
             a[i]=15;
-
 
1035
             break;
-
 
1036
            default:
-
 
1037
    printf("\nEntered number is not Hexadecimal.\nPrinted value is not correct.\n");
-
 
1038
             break;
-
 
1039
           }
-
 
1040
         }
-
 
1041
         i--;
-
 
1042
         for(;i>=0;i--)
-
 
1043
         {
-
 
1044
          h=h+a[i]*m;
-
 
1045
          m=m*16;
-
 
1046
         }
-
 
1047
          printf("%ld ",h);
-
 
1048
}
-
 
1049
 
-
 
1050
void oh(long n)
-
 
1051
{
-
 
1052
         long i;
-
 
1053
         if(n>0)
-
 
1054
         {
-
 
1055
          i=n%16;
-
 
1056
          n=n/16;
-
 
1057
          oh(n);
-
 
1058
          if(i>=10)
-
 
1059
          {
-
 
1060
           switch(i)
-
 
1061
           {
-
 
1062
            case 10:
-
 
1063
             printf("A");
-
 
1064
             break;
-
 
1065
            case 11:
-
 
1066
             printf("B");
-
 
1067
             break;
-
 
1068
            case 12:
-
 
1069
             printf("C");
-
 
1070
             break;
-
 
1071
            case 13:
-
 
1072
             printf("D");
-
 
1073
             break;
-
 
1074
            case 14:
-
 
1075
             printf("E");
-
 
1076
             break;
-
 
1077
            case 15:
-
 
1078
             printf("F");
-
 
1079
             break;
-
 
1080
           }
-
 
1081
          }
-
 
1082
          else
-
 
1083
           printf("%ld",i);
-
 
1084
         }
-
 
1085
}
-
 
1086
 
-
 
1087
void ho()
-
 
1088
{
-
 
1089
         int i,a[20];
-
 
1090
         unsigned long int h=0,m=1;
-
 
1091
         char s[20];
-
 
1092
         clrscr();
-
 
1093
         printf("Convertion from Hexadecimal to Octal\n");
-
 
1094
         printf("Enter a Hexadecimal number\n");
-
 
1095
         scanf("%s",s);
-
 
1096
         // Converting hex to dec first
-
 
1097
         for(i=0;s[i]!=NULL;i++)
-
 
1098
         {
-
 
1099
          switch(s[i])
-
 
1100
           {
-
 
1101
            case '0':
-
 
1102
             a[i]=0;
-
 
1103
             break;
-
 
1104
            case '1':
-
 
1105
             a[i]=1;
-
 
1106
             break;
-
 
1107
            case '2':
-
 
1108
             a[i]=2;
-
 
1109
             break;
-
 
1110
            case '3':
-
 
1111
             a[i]=3;
-
 
1112
             break;
-
 
1113
            case '4':
-
 
1114
             a[i]=4;
-
 
1115
             break;
-
 
1116
            case '5':
-
 
1117
             a[i]=5;
-
 
1118
             break;
-
 
1119
            case '6':
-
 
1120
             a[i]=6;
-
 
1121
             break;
-
 
1122
            case '7':
-
 
1123
             a[i]=7;
-
 
1124
             break;
-
 
1125
            case '8':
-
 
1126
             a[i]=8;
-
 
1127
             break;
-
 
1128
            case '9':
-
 
1129
             a[i]=9;
-
 
1130
             break;
-
 
1131
            case 'a':
-
 
1132
            case 'A':
-
 
1133
             a[i]=10;
-
 
1134
             break;
-
 
1135
            case 'b':
-
 
1136
            case 'B':
-
 
1137
             a[i]=11;
-
 
1138
             break;
-
 
1139
            case 'c':
-
 
1140
            case 'C':
-
 
1141
             a[i]=12;
-
 
1142
             break;
-
 
1143
            case 'd':
-
 
1144
            case 'D':
-
 
1145
             a[i]=13;
-
 
1146
             break;
-
 
1147
            case 'e':
-
 
1148
            case 'E':
-
 
1149
             a[i]=14;
-
 
1150
             break;
-
 
1151
            case 'f':
-
 
1152
            case 'F':
-
 
1153
             a[i]=15;
-
 
1154
             break;
-
 
1155
            default:
-
 
1156
      printf("\nEntered number is not Hexadecimal.\nPrinted value is not correct.\n");
-
 
1157
             break;
-
 
1158
           }
-
 
1159
         }
-
 
1160
         i--;
-
 
1161
         for(;i>=0;i--)
-
 
1162
         {
-
 
1163
          h=h+a[i]*m;
-
 
1164
          m=m*16;
-
 
1165
         }
-
 
1166
         // Now convering from decimal to octal (h)
-
 
1167
         i=0;
-
 
1168
         printf("Octal equavalent=");
-
 
1169
         while(h!=0)
-
 
1170
         {
-
 
1171
          a[i]=h%8;
-
 
1172
          h=h/8;
-
 
1173
          i++;
-
 
1174
         }
-
 
1175
         i--;
-
 
1176
         for(;i>=0;i--)
-
 
1177
          printf("%d",a[i]);
-
 
1178
}*/
-
 
1179
 
447