First commit
[anna.git] / include / anna / core / util / String.hpp
1 // ANNA - Anna is Not 'N' Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
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
16 // distribution.
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.
20 //
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.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #ifndef anna_core_util_String_hpp
38 #define anna_core_util_String_hpp
39
40 #include <string>
41
42 #include <stdio.h>
43
44 #include <anna/config/defines.hpp>
45
46 namespace anna {
47
48 class DataBlock;
49
50 /**
51  * Clase String que optimiza el uso y la conversi�n de tipos usados habitualmente.
52  */
53 class String : public std::string {
54 public:
55   /**
56    * Indicadores que permiten ajustar el funcionamiento de la clase anna::String.
57    *
58    * \see anna::String
59    */
60   struct Flag { enum _v { None, ShowNull }; };
61
62   /**
63    * Constructor.
64    * \param flag Indicadores que permiten ajustar el funcionamiento de la clase anna::String.
65    */
66   String(const Flag::_v flag = Flag::None) : a_flags(flag) {;}
67
68   /**
69    * Constructor.
70    * \param str Cadena con la que iniciar el contenido de esta instancia. Puede ser NULL.
71    * \param flag Indicadores que permiten ajustar el funcionamiento de la clase anna::String.
72    */
73   explicit String(const char* str, const Flag::_v flag = Flag::None) :
74     std::string((str == NULL) ? ((flag & Flag::ShowNull) ? "<null>" : "") : str),
75     a_flags(flag)
76   {;}
77
78   /**
79    * Constructor copia.
80    * \param other Instancia con la iniciar el contenido de esta instancia.
81    */
82   String(const String &other) : std::string(other), a_flags(other.a_flags) {;}
83
84
85   /**
86    * Constructor copia.
87    * \param other Instancia con la iniciar el contenido de esta instancia.
88    * \param flag Indicadores que permiten ajustar el funcionamiento de la clase anna::String.
89    */
90   explicit String(const std::string& other, const Flag::_v flag = Flag::None) : std::string(other), a_flags(flag) {;}
91
92   /**
93    * Convierte a may�sculas el contenido de esta instancia.
94    */
95   void toUpper() throw();
96
97   /**
98    * Convierte a min�sculas el contenido de esta instancia.
99    */
100   void toLower() throw();
101
102   String& operator = (const char* vv) throw() { std::string::clear(); return operator<< (vv); }
103   String& operator = (const int vv) throw() { std::string::clear(); return operator<< (vv); }
104   String& operator = (const unsigned int vv) throw() { std::string::clear(); return operator<< (vv); }
105   String& operator = (const bool vv) throw() { std::string::clear(); return operator<< (vv); }
106   String& operator = (const Integer64 vv) throw() { std::string::clear(); return operator<< (vv); }
107   String& operator = (const Unsigned64 vv) throw() { std::string::clear(); return operator<< (vv); }
108   String& operator = (const float vv) throw() { std::string::clear(); return operator<< (vv); }
109   String& operator = (const double vv) throw() { std::string::clear(); return operator<< (vv); }
110   String& operator = (const std::string& vv) throw() { std::string::operator= (vv); return *this; }
111   String& operator = (const DataBlock& vv) throw() { std::string::clear(); return operator<< (vv); }
112
113   String& operator += (const char* vv) throw() { return operator<< (vv); }
114   String& operator += (const int vv) throw() { return operator<< (vv); }
115   String& operator += (const unsigned int vv) throw() { return operator<< (vv); }
116   String& operator += (const bool vv) throw() { return operator<< (vv); }
117   String& operator += (const Integer64 vv) throw() { return operator<< (vv); }
118   String& operator += (const Unsigned64 vv) throw() { return operator<< (vv); }
119   String& operator += (const float vv) throw() { return operator<< (vv); }
120   String& operator += (const double vv) throw() { return operator<< (vv); }
121   String& operator += (const std::string& vv) throw() { return *this << vv; }
122   String& operator += (const DataBlock& vv) throw() { return *this << vv; }
123
124   String& operator << (const char* vv) throw();
125   String& operator << (const int vv) throw();
126   String& operator << (const unsigned int vv) throw();
127   String& operator << (const bool vv) throw() { std::string::operator+= ((vv == true) ? "true" : "false"); return *this; }
128   String& operator << (const Integer64 vv) throw();
129   String& operator << (const Unsigned64 vv) throw();
130   String& operator << (const float vv) throw();
131   String& operator << (const double vv) throw();
132   String& operator << (const std::string& vv) throw() { std::string::operator+= (vv); return *this; }
133   String& operator << (const DataBlock& vv) throw();
134
135   /**
136    * Formatea el contenido del \em float con la cadena recibida como par�metro.
137    * La cadena de forma se interpreta de la misma forma que en el m�todo \em 'sprintf'
138    * \param vv Valor a interpretar.
139    * \param format Cadena usada para interpretar el valor (ver sprintf).
140    * \return La cadena con el contenido interpretado.
141    */
142   static String format(const float vv, const char* format) throw() { return __format(vv, format); }
143
144   /**
145    * Formatea el contenido del \em double con la cadena recibida como par�metro.
146    * La cadena de forma se interpreta de la misma forma que en el m�todo \em 'sprintf'
147    * \param vv Valor a interpretar.
148    * \param format Cadena usada para interpretar el valor (ver sprintf).
149    * \return La cadena con el contenido interpretado.
150    */
151   static String format(const double vv, const char* format) throw() { return __format(vv, format); }
152
153   /**
154    * Formatea el contenido del DataBlock con el n�mero de caracteres por l�nea recibida como par�metro.
155    * \param vv Valor a interpretar.
156    * \param characterByLine N�mero de caracters por l�nea.
157    * \return La cadena con el contenido interpretado.
158    */
159   static String format(const DataBlock& vv, const int characterByLine) throw();
160
161   /**
162    * Formatea el valor del par�metro como un n�mero en hexadecimal.
163    * \param vv Valor a interpretar.
164    * \return La cadena con el contenido interpretado.
165    */
166   static String hex(const int vv) { return __format(vv, "0x%x"); }
167
168   /**
169    * Formatea el valor del par�metro como un n�mero en hexadecimal.
170    * \param vv Valor a interpretar.
171    * \return La cadena con el contenido interpretado.
172    */
173   static String hex(const unsigned int vv) { return __format(vv, "0x%x"); }
174
175   /**
176    * Formatea el valor del par�metro como un n�mero en hexadecimal.
177    * \param vv Valor a interpretar.
178    * \return La cadena con el contenido interpretado.
179    */
180   static String hex(const Integer64 vv) {
181     return __format(vv, "0x%llx");
182     /*#ifdef __anna64__
183        return __format (vv, "0x%lx");
184     #else
185        return __format (vv, "0x%llx");
186     #endif
187     */
188   }
189
190 private:
191   Flag::_v a_flags;
192
193   template <typename T> static String __format(const T vv, const char* format) {
194     char aux [64];
195     sprintf(aux, format, vv);
196     return String(aux);
197   }
198 };
199
200 }
201
202 #endif