summaryrefslogtreecommitdiffstats
path: root/src/translators/pilotdb/strop.cpp
blob: 3b0deebe055b297905763067723650df66ec0ab8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*
 * palm-db-tools: Support Library: String Parsing Utility Functions
 * Copyright (C) 1999-2000 by Tom Dyas ([email protected])
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifh Floor, Boston, MA  02110-1301  USA
 */

#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
#include <sstream>

#include "strop.h"
#include <kdebug.h>

extern std::ostream* err;

void StrOps::lower(std::string& str)
{
    for (std::string::iterator p = str.begin(); p != str.end(); ++p) {
        if (isupper(*p))
            *p = tolower(*p);
    }
}

bool StrOps::string2boolean(const std::string& str)
{
    std::string value(str);

    StrOps::lower(value);

    if (value == "on")       return true;
    else if (str == "off")   return false;
    else if (str == "true")  return true;
    else if (str == "t")     return true;
    else if (str == "false") return false;
    else if (str == "f")     return false;
    else {
        int num = 0;

        std::istringstream(str.c_str()) >> num;
        return num != 0 ? true : false;
    }
}

std::string StrOps::type2string(PalmLib::FlatFile::Field::FieldType t)
{
    switch (t) {
    case PalmLib::FlatFile::Field::STRING:
        return "string";

    case PalmLib::FlatFile::Field::BOOLEAN:
        return "boolean";

    case PalmLib::FlatFile::Field::INTEGER:
        return "integer";

    case PalmLib::FlatFile::Field::FLOAT:
        return "float";

    case PalmLib::FlatFile::Field::DATE:
        return "date";

    case PalmLib::FlatFile::Field::TIME:
        return "time";

    case PalmLib::FlatFile::Field::DATETIME:
        return "datetime";

    case PalmLib::FlatFile::Field::NOTE:
        return "note";

    case PalmLib::FlatFile::Field::LIST:
        return "list";

    case PalmLib::FlatFile::Field::LINK:
        return "link";

    case PalmLib::FlatFile::Field::CALCULATED:
        return "calculated";

    case PalmLib::FlatFile::Field::LINKED:
        return "linked";

    default:
        // If we don't support the field type, then fake it as a string.
        return "string";
    }
}

PalmLib::FlatFile::Field::FieldType StrOps::string2type(std::string typestr)
{
    StrOps::lower(typestr);
    if (typestr == "string")
        return PalmLib::FlatFile::Field::STRING;
    else if (typestr == "str")
        return PalmLib::FlatFile::Field::STRING;
    else if (typestr == "note")
        return PalmLib::FlatFile::Field::NOTE;
    else if (typestr == "bool")
        return PalmLib::FlatFile::Field::BOOLEAN;
    else if (typestr == "boolean")
        return PalmLib::FlatFile::Field::BOOLEAN;
    else if (typestr == "integer")
        return PalmLib::FlatFile::Field::INTEGER;
    else if (typestr == "int")
        return PalmLib::FlatFile::Field::INTEGER;
    else if (typestr == "float")
        return PalmLib::FlatFile::Field::FLOAT;
    else if (typestr == "date")
        return PalmLib::FlatFile::Field::DATE;
    else if (typestr == "time")
        return PalmLib::FlatFile::Field::TIME;
    else if (typestr == "datetime")
        return PalmLib::FlatFile::Field::DATETIME;
    else if (typestr == "list")
        return PalmLib::FlatFile::Field::LIST;
    else if (typestr == "link")
        return PalmLib::FlatFile::Field::LINK;
    else if (typestr == "linked")
        return PalmLib::FlatFile::Field::LINKED;
    else if (typestr == "calculated")
        return PalmLib::FlatFile::Field::CALCULATED;
    else
        kdDebug() << "unknown field type" << endl;
    return PalmLib::FlatFile::Field::STRING;
}

std::string StrOps::strip_back(const std::string& str, const std::string& what)
{
    std::string result(str);
    std::string::reverse_iterator p = result.rbegin();

    while (p != result.rend()
           && (std::find(what.begin(), what.end(), *p) != what.end())) ++p;

    result.erase(p.base(), result.end());

    return result;
}

std::string StrOps::strip_front(const std::string& str,const std::string& what)
{
    std::string result(str);
    std::string::iterator p = result.begin();

    while (p != result.end()
           && (std::find(what.begin(), what.end(), *p) != what.end())) ++p;

    result.erase(result.begin(), p);

    return result;
}

StrOps::string_list_t StrOps::csv_to_array(const std::string& str, char delim, bool quoted_string)
{
    enum { STATE_NORMAL, STATE_TQUOTES } state;
    StrOps::string_list_t result;
    std::string elem;

    state = STATE_NORMAL;
    for (std::string::const_iterator p = str.begin(); p != str.end(); ++p) {
        switch (state) {
        case STATE_NORMAL:
            if (quoted_string && *p == '"') {
                state = STATE_TQUOTES;
            } else if (*p == delim) {
                result.push_back(elem);
                elem = "";
            } else {
                elem += *p;
            }
        break;

        case STATE_TQUOTES:
            if (quoted_string && *p == '"') {
                if ((p + 1) != str.end() && *(p+1) == '"') {
                    ++p;
                    elem += '"';
                } else {
                    state = STATE_NORMAL;
                }
            } else {
                elem += *p;
            }
        break;
        }
    }

    switch (state) {
    case STATE_NORMAL:
        result.push_back(elem);
    break;
    case STATE_TQUOTES:
        kdDebug() << "unterminated quotes" << endl;
    break;
    }

    return result;
}

StrOps::string_list_t
StrOps::str_to_array(const std::string& str, const std::string& delim,
                     bool multiple_delim, bool handle_comments)
{
    enum { STATE_NORMAL, STATE_COMMENT, STATE_TQUOTE_DOUBLE, STATE_TQUOTE_SINGLE,
           STATE_BACKSLASH, STATE_BACKSLASH_DOUBLETQUOTE } state;
    StrOps::string_list_t result;
    std::string elem;

    state = STATE_NORMAL;
    for (std::string::const_iterator p = str.begin(); p != str.end(); ++p) {
        switch (state) {
        case STATE_NORMAL:
            if (*p == '"') {
                state = STATE_TQUOTE_DOUBLE;
            } else if (*p == '\'') {
                state = STATE_TQUOTE_SINGLE;
            } else if (std::find(delim.begin(), delim.end(), *p) != delim.end()) {
                if (multiple_delim) {
                    ++p;
                    while (p != str.end()
                       && std::find(delim.begin(), delim.end(), *p) != delim.end()) {
                        ++p;
                    }
                    --p;
                }
                result.push_back(elem);
                elem = "";
            } else if (*p == '\\') {
                state = STATE_BACKSLASH;
            } else if (handle_comments && *p == '#') {
                state = STATE_COMMENT;
            } else {
                elem += *p;
            }
        break;

        case STATE_COMMENT:
        break;

        case STATE_TQUOTE_DOUBLE:
            if (*p == '"')
                state = STATE_NORMAL;
            else if (*p == '\\')
                state = STATE_BACKSLASH_DOUBLETQUOTE;
            else
                elem += *p;
        break;

        case STATE_TQUOTE_SINGLE:
            if (*p == '\'')
                state = STATE_NORMAL;
            else
                elem += *p;
            break;

        case STATE_BACKSLASH:
            elem += *p;
            state = STATE_NORMAL;
        break;

        case STATE_BACKSLASH_DOUBLETQUOTE:
            switch (*p) {
            case '\\':
                elem += '\\';
            break;

            case 'n':
                elem += '\n';
            break;

            case 'r':
                elem += '\r';
            break;

            case 't':
                elem += '\t';
            break;

            case 'v':
                elem += '\v';
            break;

            case '"':
                elem += '"';
            break;

            case 'x':
                {
                char buf[3];

                // Extract and check the first hexadecimal character.
                if ((p + 1) == str.end())
                    kdDebug() << "truncated escape" << endl;
                if (! isxdigit(*(p + 1)))
                    kdDebug() << "invalid hex character" << endl;
                buf[0] = *++p;

                // Extract and check the second (if any) hex character.
                if ((p + 1) != str.end() && isxdigit(*(p + 1))) {
                    buf[1] = *++p;
                    buf[2] = '\0';
                } else {
                    buf[1] = buf[2] = '\0';
                }

                std::istringstream stream(buf);
                stream.setf(std::ios::hex, std::ios::basefield);
                unsigned value;
                stream >> value;

                elem += static_cast<char> (value & 0xFFu);
            }
            break;
            }

            // Escape is done. Go back to the normal double quote state.
            state = STATE_TQUOTE_DOUBLE;
        break;
        }
    }

    switch (state) {
    case STATE_NORMAL:
        result.push_back(elem);
    break;

    case STATE_TQUOTE_DOUBLE:
        kdDebug() << "unterminated double quotes" << endl;
    break;

    case STATE_TQUOTE_SINGLE:
        kdDebug() << "unterminated single quotes" << endl;
    break;

    case STATE_BACKSLASH:
    case STATE_BACKSLASH_DOUBLETQUOTE:
        kdDebug() << "an escape character must follow a backslash" << endl;
    break;

    default:
    break;
    }

    return result;
}

PalmLib::pi_uint32_t
StrOps::get_current_time(void)
{
    time_t now;

    time(&now);
    return static_cast<PalmLib::pi_uint32_t> (now) + PalmLib::pi_uint32_t(2082844800);
}

char *
StrOps::strptime(const char *s, const char  *format,  struct tm *tm)
{
    char *data = (char *)s;
    char option = false;

    while (*format != 0) {
        if (*data == 0)
            return NULL;
        switch (*format) {
        case '%':
            option = true;
            format++;
        break;
        case 'd':
            if (option) {
                tm->tm_mday = strtol(data, &data, 10);
                if (tm->tm_mday < 1 || tm->tm_mday > 31)
                    return NULL;
            } else if (*data != 'd') {
                return  data;
            }
            option = false;
            format++;
        break;
        case 'm':
            if (option) {
                /* tm_mon between 0 and 11 */
                tm->tm_mon = strtol(data, &data, 10) - 1;
                if (tm->tm_mon < 0 || tm->tm_mon > 11)
                    return NULL;
            } else if (*data != 'm') {
                return  data;
            }
            option = false;
            format++;
        break;
        case 'y':
            if (option) {
                tm->tm_year = strtol(data, &data, 10);
                if (tm->tm_year < 60) tm->tm_year += 100;
            } else if (*data != 'y') {
                return  data;
            }
            option = false;
            format++;
        break;
        case 'Y':
            if (option) {
                tm->tm_year = strtol(data, &data, 10) - 1900;
            } else if (*data != 'Y') {
                return  data;
            }
            option = false;
            format++;
        break;
        case 'H':
            if (option) {
                tm->tm_hour = strtol(data, &data, 10);
                if (tm->tm_hour < 0 || tm->tm_hour > 23)
                    return NULL;
            } else if (*data != 'H') {
                return  data;
            }
            option = false;
            format++;
        break;
        case 'M':
            if (option) {
                tm->tm_min = strtol(data, &data, 10);
                if (tm->tm_min < 0 || tm->tm_min > 59)
                    return NULL;
            } else if (*data != 'M') {
                return  data;
            }
            option = false;
            format++;
        break;
        default:
            if (option)
                return data;
            if (*data != *format)
                return data;
            format++;
            data++;
        }
    }
    return data;
}

// Read a line from an istream w/o concern for buffer limitations.
std::string
StrOps::readline(std::istream& stream)
{
    std::string line;
    char buf[1024];

    while (1) {
        // Read the next line (or part thereof) from the stream.
        stream.getline(buf, sizeof(buf));
        // Bail out of the loop if the stream has reached end-of-file.
        if ((stream.eof() && !buf[0]) || stream.bad())
            break;

        // Append the data read to the result string.
        line.append(buf);

        // If the stream is good, then stop. Otherwise, clear the
        // error indicator so that getline will work again.
        if ((stream.eof() && buf[0]) || stream.good())
            break;
        stream.clear();
    }

    return line;
}

std::string
StrOps::quote_string(std::string str, bool extended_mode)
{
    std::string result;
    std::ostringstream error;

    if (extended_mode) {
        result += '"';
        for (std::string::iterator c = str.begin(); c != str.end(); ++c) {
            switch (*c) {
            case '\\':
                result += '\\';
                result += '\\';
            break;

            case '\r':
                result += '\\';
                result += 'r';
            break;

            case '\n':
                result += '\\';
                result += 'n';
            break;

            case '\t':
                result += '\\';
                result += 't';
            break;

            case '\v':
                result += '\\';
                result += 'v';
            break;

            case '"':
                result += '\\';
                result += '"';
            break;

            default:
                if (isprint(*c)) {
                    result += *c;
                } else {
                    std::ostringstream buf;
                    buf.width(2);
                    buf.setf(std::ios::hex, std::ios::basefield);
                    buf.setf(std::ios::left);
                    buf << ((static_cast<unsigned> (*c)) & 0xFF) << std::ends;

                    result += "\\x";
                    result += buf.str();
                }
            break;
            }
        }
        result += '"';
    } else {
        result += '"';
        for (std::string::iterator c = str.begin(); c != str.end(); ++c) {
            if (*c == '"') {
                result += "\"\"";
            } else if (*c == '\n' || *c == '\r') {
                error << "use extended csv mode for newlines\n";
                *err << error.str();
               kdDebug() << error.str().c_str() << endl;
            } else {
                result += *c;
            }
        }
        result += '"';
    }

    return result;
}

std::string
StrOps::concatenatepath(std::string p_Path,
        std::string p_FileName, std::string p_Ext)
{
    std::string l_FilePath;
#ifdef WIN32
    if (p_FileName[1] == ':' || p_FileName[0] == '\\')
        return p_FileName;
    else if (p_Path.empty())
      l_FilePath = p_FileName;
    else
        l_FilePath = p_Path + std::string("\\") + p_FileName;
#else
    if (p_FileName[0] == '/')
        return p_FileName;
    else if (p_Path.empty())
      l_FilePath = p_FileName;
    else
        l_FilePath = p_Path + std::string("/") + p_FileName;

#endif
    if (!p_Ext.empty() && (p_FileName.rfind(p_Ext) == std::string::npos))
        l_FilePath += p_Ext;

    return l_FilePath;
}