1 // ANNA - Anna is Not 'N' Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // https://bitbucket.org/testillano/anna
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 #include <anna/core/util/Tokenizer.hpp>
38 #include <anna/core/functions.hpp>
39 #include <anna/config/defines.hpp>
45 const int Tokenizer::MaxItem = 64;
47 Tokenizer::Tokenizer() :
49 a_activateStrip(false) {
50 a_items = new char* [MaxItem];
51 anna_memset(a_items, 0, sizeof(char*) * MaxItem);
55 Tokenizer::Tokenizer(const char* str, const char* separator) :
57 a_activateStrip(false) {
58 a_items = new char* [MaxItem];
59 anna_memset(a_items, 0, sizeof(char*) * MaxItem);
63 apply(str, separator);
68 Tokenizer::Tokenizer(const string& str, const char* separator) :
70 a_activateStrip(false) {
71 a_items = new char* [MaxItem];
72 anna_memset(a_items, 0, sizeof(char*) * MaxItem);
75 apply(str.c_str(), separator);
80 Tokenizer::~Tokenizer() {
84 int Tokenizer::apply(const char* str, const char* separator)
85 throw(RuntimeException) {
91 DataBlock mb(str, anna_strlen(str) + 1, false);
93 const char* buffer = a_dataBlock.getData();
94 char *token = const_cast <char*>(buffer);
97 while((token = strtok_r(token, separator, &last)) != NULL) {
98 if(a_activateStrip == true)
101 a_items [a_maxItem ++] = token;
104 if(a_maxItem == MaxItem) {
105 string msg("Tokenizer::apply | String: ");
107 msg += " | Separator: ";
109 msg += " | Tokenizer has not enough space";
110 throw RuntimeException(msg, ANNA_FILE_LOCATION);
117 const char* Tokenizer::at(const int i)
118 throw(RuntimeException) {
120 indexException(i, ANNA_FILE_LOCATION);
122 return data(begin() + i);
125 const char* Tokenizer::at(const int i) const
126 throw(RuntimeException) {
128 indexException(i, ANNA_FILE_LOCATION);
130 return data(begin() + i);
133 const char* Tokenizer::last() const
134 throw(RuntimeException) {
136 throw RuntimeException("There is any token to select", ANNA_FILE_LOCATION);
138 return data(begin() + a_maxItem - 1);
141 char* Tokenizer::strip(char* str)
146 while(*result != 0 && *result == ' ')
150 char* final = result + anna_strlen(result) - 1;
152 while(final > result && *final == ' ') {
162 void Tokenizer::indexException(const int index, const char* fromFile, const int fromLine) const
163 throw(RuntimeException) {
164 string msg(functions::asString("Index %d out of range [0,%d] | Items: ", index, a_maxItem));
166 for(const_iterator ii = begin(), maxii = end(); ii != maxii; ii ++) {
171 throw RuntimeException(msg, fromFile, fromLine);