[422] | 1 |
|
---|
| 2 | /*
|
---|
| 3 | *@@sourcefile bs_tokenizer.cpp:
|
---|
| 4 | * implements the BSTokenizer class
|
---|
| 5 | *
|
---|
| 6 | *@@header "helpers\xstring.h"
|
---|
| 7 | *@@header "cppbase\bs_tokenizer.h"
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | /*
|
---|
| 11 | * This file Copyright (C) 20011-2015 Paul Ratcliffe.
|
---|
| 12 | * This program is free software; you can redistribute it and/or modify
|
---|
| 13 | * it under the terms of the GNU General Public License as published by
|
---|
| 14 | * the Free Software Foundation, in version 2 as it comes in the COPYING
|
---|
| 15 | * file of this distribution.
|
---|
| 16 | * This program is distributed in the hope that it will be useful,
|
---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | * GNU General Public License for more details.
|
---|
| 20 | */
|
---|
| 21 |
|
---|
| 22 | #define OS2EMX_PLAIN_CHAR
|
---|
| 23 | // this is needed for "os2emx.h"; if this is defined,
|
---|
| 24 | // emx will define PSZ as _signed_ char, otherwise
|
---|
| 25 | // as unsigned char
|
---|
| 26 |
|
---|
| 27 | #include <os2.h>
|
---|
| 28 |
|
---|
| 29 | #include "setup.h"
|
---|
| 30 |
|
---|
| 31 | #include "helpers\xstring.h"
|
---|
| 32 |
|
---|
| 33 | // base includes
|
---|
| 34 | #include "cppbase\bs_tokenizer.h"
|
---|
| 35 |
|
---|
| 36 | #pragma hdrstop
|
---|
| 37 |
|
---|
| 38 | DEFINE_CLASS(BSTokenizer, BSRoot);
|
---|
| 39 |
|
---|
| 40 | BSString &BSTokenizer::Dequote(BSString &str)
|
---|
| 41 | {
|
---|
| 42 | size_t len = str.length();
|
---|
| 43 |
|
---|
| 44 | if (len && (str[0] == '\'' || str[0] == '"'))
|
---|
| 45 | {
|
---|
| 46 | if (str[len - 1] == str[0])
|
---|
| 47 | str.erase(--len);
|
---|
| 48 |
|
---|
| 49 | if (len)
|
---|
| 50 | str.erase(0, 1);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | return str;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 |
|
---|
| 57 | BSTokenizer::BSTokenizer()
|
---|
| 58 | : BSRoot(tBSTokenizer),
|
---|
| 59 | _size(0)
|
---|
| 60 | {
|
---|
| 61 | _pTokens = new list<BSString *>(STORE);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 | BSTokenizer::BSTokenizer(const BSString &str, const BSString &delimiters, BOOL unquote)
|
---|
| 66 | : BSRoot(tBSTokenizer),
|
---|
| 67 | _size(0)
|
---|
| 68 | {
|
---|
| 69 | _pTokens = new list<BSString *>(STORE);
|
---|
| 70 | Tokenize(str, delimiters, unquote);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | BSTokenizer::~BSTokenizer()
|
---|
| 75 | {
|
---|
| 76 | delete _pTokens;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 | size_t BSTokenizer::Tokenize(const BSString &str, const BSString &delimiters, BOOL unquote)
|
---|
| 81 | {
|
---|
| 82 | size_t firstPos, lastPos = 0, quotePos;
|
---|
| 83 | BSString token;
|
---|
| 84 |
|
---|
| 85 | _pTokens->clear();
|
---|
| 86 | _size = 0;
|
---|
| 87 | for(;;)
|
---|
| 88 | {
|
---|
| 89 | firstPos = str.find_first_not_of(delimiters.c_str(), lastPos);
|
---|
| 90 | if (firstPos == BSString::npos)
|
---|
| 91 | break;
|
---|
| 92 |
|
---|
| 93 | lastPos = str.find_first_of(delimiters.c_str(), firstPos);
|
---|
| 94 | if (lastPos != BSString::npos)
|
---|
| 95 | for (quotePos = firstPos;;)
|
---|
| 96 | {
|
---|
| 97 | quotePos = str.find_first_of("'\"", quotePos);
|
---|
| 98 | if (quotePos < lastPos)
|
---|
| 99 | {
|
---|
| 100 | quotePos = str.find_first_of(str.c_str()[quotePos], quotePos + 1);
|
---|
| 101 | if (quotePos != BSString::npos)
|
---|
| 102 | if (quotePos > lastPos)
|
---|
| 103 | lastPos = str.find_first_of(delimiters.c_str(), ++quotePos);
|
---|
| 104 | else
|
---|
| 105 | ++quotePos;
|
---|
| 106 | else
|
---|
| 107 | {
|
---|
| 108 | lastPos = quotePos;
|
---|
| 109 | break;
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | else
|
---|
| 113 | break;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | token = str.substr(firstPos, lastPos - firstPos);
|
---|
| 117 | if (unquote)
|
---|
| 118 | Dequote(token);
|
---|
| 119 |
|
---|
| 120 | _pTokens->push_back(new BSString(token));
|
---|
| 121 | ++_size;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | return _size;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 |
|
---|
| 128 | const char *BSTokenizer::GetString(size_t num) const
|
---|
| 129 | {
|
---|
| 130 | const char *p = "";
|
---|
| 131 |
|
---|
| 132 | if (num < Size())
|
---|
| 133 | {
|
---|
| 134 | list<BSString *>::iterator li = _pTokens->begin();
|
---|
| 135 |
|
---|
| 136 | for (size_t i = 0; i < num; ++i, ++li);
|
---|
| 137 | p = (*li)->c_str();
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | return p;
|
---|
| 141 | }
|
---|
| 142 |
|
---|