1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
9 #include <anna/core/util/Tokenizer.hpp>
10 #include <anna/core/functions.hpp>
11 #include <anna/config/defines.hpp>
17 const int Tokenizer::MaxItem = 64;
19 Tokenizer::Tokenizer() :
21 a_activateStrip(false) {
22 a_items = new char* [MaxItem];
23 anna_memset(a_items, 0, sizeof(char*) * MaxItem);
27 Tokenizer::Tokenizer(const char* str, const char* separator) :
29 a_activateStrip(false) {
30 a_items = new char* [MaxItem];
31 anna_memset(a_items, 0, sizeof(char*) * MaxItem);
35 apply(str, separator);
40 Tokenizer::Tokenizer(const string& str, const char* separator) :
42 a_activateStrip(false) {
43 a_items = new char* [MaxItem];
44 anna_memset(a_items, 0, sizeof(char*) * MaxItem);
47 apply(str.c_str(), separator);
52 Tokenizer::~Tokenizer() {
56 int Tokenizer::apply(const char* str, const char* separator)
57 throw(RuntimeException) {
63 DataBlock mb(str, anna_strlen(str) + 1, false);
65 const char* buffer = a_dataBlock.getData();
66 char *token = const_cast <char*>(buffer);
69 while((token = strtok_r(token, separator, &last)) != NULL) {
70 if(a_activateStrip == true)
73 a_items [a_maxItem ++] = token;
76 if(a_maxItem == MaxItem) {
77 string msg("Tokenizer::apply | String: ");
79 msg += " | Separator: ";
81 msg += " | Tokenizer has not enough space";
82 throw RuntimeException(msg, ANNA_FILE_LOCATION);
89 const char* Tokenizer::at(const int i)
90 throw(RuntimeException) {
92 indexException(i, ANNA_FILE_LOCATION);
94 return data(begin() + i);
97 const char* Tokenizer::at(const int i) const
98 throw(RuntimeException) {
100 indexException(i, ANNA_FILE_LOCATION);
102 return data(begin() + i);
105 const char* Tokenizer::last() const
106 throw(RuntimeException) {
108 throw RuntimeException("There is any token to select", ANNA_FILE_LOCATION);
110 return data(begin() + a_maxItem - 1);
113 char* Tokenizer::strip(char* str)
118 while(*result != 0 && *result == ' ')
122 char* final = result + anna_strlen(result) - 1;
124 while(final > result && *final == ' ') {
134 void Tokenizer::indexException(const int index, const char* fromFile, const int fromLine) const
135 throw(RuntimeException) {
136 string msg(functions::asString("Index %d out of range [0,%d] | Items: ", index, a_maxItem));
138 for(const_iterator ii = begin(), maxii = end(); ii != maxii; ii ++) {
143 throw RuntimeException(msg, fromFile, fromLine);