Remove dynamic exceptions
[anna.git] / include / anna / core / util / Tokenizer.hpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
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 //
7
8
9 #ifndef anna_core_util_Tokenizer_hpp
10 #define anna_core_util_Tokenizer_hpp
11
12 #include <string>
13
14 #include <anna/core//DataBlock.hpp>
15
16 namespace anna {
17
18 class RuntimeException;
19
20 /**
21    Tokenize the input string into several elements
22 */
23 class Tokenizer {
24
25   int _apply(const char* str, const char* separator) noexcept(false);
26
27 public:
28   typedef char* const* const_iterator;
29
30   /**
31      Constructor.
32   */
33   Tokenizer();
34
35   /**
36      Constructor.
37
38      @param str Cadena sobre la que aplicar la separacion.
39      @param separator Caracteres que van a actuar como separador de las subcadenas contenidas en el
40      primer parametro.
41   */
42   Tokenizer(const char* str, const char* separator);
43
44   /**
45      Constructor.
46
47      @param str Cadena sobre la que aplicar la separacion.
48      @param separator Caracteres que van a actuar como separador de las subcadenas contenidas en el
49      primer parametro.
50   */
51   Tokenizer(const std::string& str, const char* separator);
52
53   /**
54    * Destructor.
55    */
56   virtual ~Tokenizer();
57
58   // Accesores
59   /**
60      Devuelve el estado del indicador de activacion de eliminacion de espacios de los extremos.
61      \return El estado del indicador de activacion de eliminacion de espacios de los extremos.
62   */
63   bool activateStrip() const { return a_activateStrip; }
64
65   // Operadores
66
67   /**
68      Activa y/o desactiva que activa el sistema que permite recoger los elementos retornadodos
69      por esta clase sin espacios por delante y por detras.
70      \param _activateStrip Parametro que indica el estado de activacion o desactivacion.
71   */
72   void activateStrip(const bool _activateStrip) { a_activateStrip = _activateStrip; }
73
74   /**
75     @return El elemento que ocupa la posicion i-esima.
76     \warning Este método tiene una eficiencia de O(1), mejor usar iteradores.
77   */
78   const char* at(const int i) noexcept(false);
79
80   /**
81     @return El elemento que ocupa la posicion i-esima.
82     \warning Este método tiene una eficiencia de O(1), mejor usar iteradores.
83   */
84   const char* at(const int i) const noexcept(false);
85
86   /**
87     @return El elemento que ocupa la posicion i-esima.
88     \warning Este método tiene una eficiencia de O(1), mejor usar iteradores.
89   */
90   const char* operator [](const int i) noexcept(false) { return at(i); }
91
92   /**
93     @return El elemento que ocupa la posicion i-esima.
94     \warning Este método tiene una eficiencia de O(1), mejor usar iteradores.
95   */
96   const char* operator [](const int i) const noexcept(false) { return at(i); }
97
98   /**
99      Process the separation over the string str with the separator provided.
100
101      Internally used strtok_r has these imitations: sequence of two or more contiguous delimiter
102      bytes in the parsed string is considered to be a single delimiter. Delimiter bytes at the start
103      or end of the string are ignored. Put another way: the tokens returned by strtok() are always
104      nonempty strings. To override these limitations, the string provided can be internally modified
105      inserting a artificial token in order to cheat on strtok_r. For this feature, you may provide
106      '<null>' or whatever string (non-empty) you prefer.
107
108      @param str String to apply the separation.
109      @param separator Characters used as separator within the string tokenized
110      @param tokenizeContiguous If provided, it will be the artificial token used internally. The
111             resulting tokens shall store this string in case of contiguous separators. NULL by
112             default (original strtok_r behaviour).
113
114      @return Number of tokens
115   */
116   int apply(const char* str, const char* separator, const char *tokenizeContiguous = NULL) noexcept(false);
117   int apply(const std::string& str, const char* separator, const char *tokenizeContiguous = NULL) noexcept(false) {
118     return apply(str.c_str(), separator, tokenizeContiguous);
119   }
120
121
122   // Metodos
123   /**
124      @return El ultimo elemento obtenido la aplicar la separacion.
125   */
126   const char* last() const noexcept(false);
127
128   /**
129    * Devuelve el número de elementos obtenidos en la separación.
130    * \return el número de elementos obtenidos en la separación.
131    */
132   int size() const { return a_maxItem; }
133
134   /**
135    * Devuelve el iterador el comiento de los elementos obtenidos por #apply
136    * \return el iterador el comiento de los elementos obtenidos por #apply
137    */
138   const_iterator begin() const { return a_items; }
139
140   /**
141    * Devuelve el iterador al final de los elementos obtenidos por #apply
142    * \return el iterador al final de los elementos obtenidos por #apply
143    */
144   const_iterator end() const { return a_items + a_maxItem; }
145
146   /**
147      Devuelve la cadena referenciada por el iterator recibido como parametro.
148      \return la cadena referenciada por el iterator recibido como parametro.
149   */
150   static const char* data(const_iterator ii) { return *ii; }
151
152 private:
153   static const int MaxItem;
154
155   anna::DataBlock a_dataBlock;
156   bool a_activateStrip;
157   char** a_items;
158   int a_maxItem;
159
160   static char* strip(char* str) ;
161
162   void indexException(const int index, const char* fromFile, const int fromLine) const noexcept(false);
163 };
164
165 }
166
167 #endif