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 #ifndef anna_core_functions_hpp
10 #define anna_core_functions_hpp
14 #include <anna/core/util/ComponentManager.hpp>
15 #include <anna/core/util/defines.hpp>
16 #include <anna/core/RuntimeException.hpp>
18 #include <anna/core/util/ExclusiveHash.hpp>
19 #include <anna/core/util/Second.hpp>
20 #include <anna/core/util/Millisecond.hpp>
21 #include <anna/core/util/Microsecond.hpp>
23 #include <anna/core/DataBlock.hpp>
32 //------------------------------------------------------------------------------
33 //---------------------------------------------------------------------- #define
34 //------------------------------------------------------------------------------
35 #define s_REGEXP_IPv4_ADDRESSES "\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b"
36 //#define s_REGEXP_IPv6_ADDRESSES "s*((([0-9A-Fa-f]{1,4}:){7}(([0-9A-Fa-f]{1,4})|:))|(([0-9A-Fa-f]{1,4}:){6}(:|((25[0-5]|2[0-4]\\d|[01]?\\d{1,2})(\\.(25[0-5]|2[0-4]\\d|[01]?\\d{1,2})){3})|(:[0-9A-Fa-f]{1,4})))|(([0-9A-Fa-f]{1,4}:){5}((:((25[0-5]|2[0-4]\\d|[01]?\\d{1,2})(\\.(25[0-5]|2[0-4]\\d|[01]?\\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}){0,1}((:((25[0-5]|2[0-4]\\d|[01]?\\d{1,2})(\\.(25[0-5]|2[0-4]\\d|[01]?\\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}){0,2}((:((25[0-5]|2[0-4]\\d|[01]?\\d{1,2})(\\.(25[0-5]|2[0-4]\\d|[01]?\\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}){0,3}((:((25[0-5]|2[0-4]\\d|[01]?\\d{1,2})(\\.(25[0-5]|2[0-4]\\d|[01]?\\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:)(:[0-9A-Fa-f]{1,4}){0,4}((:((25[0-5]|2[0-4]\\d|[01]?\\d{1,2})(\\.(25[0-5]|2[0-4]\\d|[01]?\\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(:(:[0-9A-Fa-f]{1,4}){0,5}((:((25[0-5]|2[0-4]\\d|[01]?\\d{1,2})(\\.(25[0-5]|2[0-4]\\d|[01]?\\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(((25[0-5]|2[0-4]\\d|[01]?\\d{1,2})(\\.(25[0-5]|2[0-4]\\d|[01]?\\d{1,2})){3})))(%.+)?\\s*"
37 #define s_REGEXP_IPv6_ADDRESSES "s*((([0-9A-Fa-f]{1,4}:){7}(([0-9A-Fa-f]{1,4})|:))|(([0-9A-Fa-f]{1,4}:){6}(:|((25[0-5]|2[0-4]\\d|[01]?\\d{1,2})\
38 (\\.(25[0-5]|2[0-4]\\d|[01]?\\d{1,2})){3})|(:[0-9A-Fa-f]{1,4})))|(([0-9A-Fa-f]{1,4}:){5}((:((25[0-5]|2[0-4]\\d|[01]?\\d{1,2})(\\.(25[0-5]|2[0-4]\
39 \\d|[01]?\\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}){0,1}((:((25[0-5]|2[0-4]\\d|[01]?\\d{1,2})(\\.(\
40 25[0-5]|2[0-4]\\d|[01]?\\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}){0,2}((:((25[0-5]|2[0-4]\\d|[01]?\
41 \\d{1,2})(\\.(25[0-5]|2[0-4]\\d|[01]?\\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}){0,3}((:((25[0-5]|2\
42 [0-4]\\d|[01]?\\d{1,2})(\\.(25[0-5]|2[0-4]\\d|[01]?\\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:)(:[0-9A-Fa-f]{1,4}){0,4}((:\
43 ((25[0-5]|2[0-4]\\d|[01]?\\d{1,2})(\\.(25[0-5]|2[0-4]\\d|[01]?\\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(:(:[0-9A-Fa-f]{1,4}){0,5}((:((25[0-\
44 5]|2[0-4]\\d|[01]?\\d{1,2})(\\.(25[0-5]|2[0-4]\\d|[01]?\\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(((25[0-5]|2[0-4]\\d|[01]?\\d{1,2})(\\.(25[\
45 0-5]|2[0-4]\\d|[01]?\\d{1,2})){3})))(%.+)?\\s*"
47 // Helpers for alarm parsing parameters:
48 #define STRING_WITH_QUOTATION_MARKS__C_STR(x) ((anna::functions::addQuotationMarks(x)).c_str())
49 #define ANNA_AS_STRING__C_STR(x) ((anna::functions::asString(x)).c_str())
58 #define CLOCK_PROCESS_CPUTIME_ID CLOCK_PROCESS_CPUTIME
63 functions - Métodos y variables
67 Tamao de la memoria reservada que debe tener la variable usada para guardar
68 el resultado de convertir el 'time' en texto.
72 static const int DateTimeSizeString = 21;
75 @return La versin de functions con la que hemos linkado nuestra aplicacion.
77 static std::string getVersion() ;
80 @return Un literal con la arquitectura sobre la que hemos compilado nuestra aplicacion.
82 static std::string getArchitecture() ;
85 Indica el número de bits de un entero.
87 static const int intBitSize = sizeof(int) * 8;
90 * Indica el número de bits de un entero largo.
92 static const int int64BitSize = sizeof(S64) * 8;
95 \param number Numero a convertir.
96 @return Un literal con el numero convertido a cadena decimal.
98 static std::string asString(const int number) ;
101 \param number Numero a convertir.
102 @return Un literal con el numero sin signo convertido a cadena decimal.
104 static std::string asString(const unsigned int number) ;
107 Devuelve un literal con tel numero convertido a cadena decimal
108 @return Un literal con el numero signo convertido a cadena decimal.
110 static std::string asString(const S64 number) ;
113 Devuelve un literal con tel numero convertido a cadena decimal
114 @return Un literal con el numero signo convertido a cadena decimal.
116 static std::string asString(const U64 number) ;
119 \param _bool Booleano a convertir.
120 \return Un literal con el boolean convertido a cadena.
122 static const char* asString(const bool _bool) { return (_bool == true) ? "true" : "false"; }
125 Devuelve una cadena con el bloque de datos decodificado en grupos de 16 bytes.
126 @param dataBlock Bloque de datos a interpretar.
127 \param characterByLine Número de caracteres en cada línea.
128 @return Devuelve una cadena con el bloque de datos decodificado en grupos de 16 bytes.
130 static std::string asString(const DataBlock& dataBlock, const int characterByLine = 16) ;
133 Devuelve una cadena con el numero en coma flotante.
134 \param v Numero a tratar.
135 \param format Formato aplicado para convertir el numero a cadena. Ver \em man printf.
136 \return una cadena con el numero en coma flotante.
138 static std::string asString(const double v, const char* format = "%e") ;
141 Devuelve una cadena con el numero en coma flotante.
142 \param v Numero a tratar.
143 \param format Formato aplicado para convertir el numero a cadena. Ver \em man printf.
144 \return una cadena con el numero en coma flotante.
146 static std::string asString(const float v, const char* format = "%f") ;
149 \param comment Comentario que precede al valor.
150 \param number Numero a convertir.
151 @return Un literal con el numero convertido a cadena decimal.
153 static std::string asText(const char* comment, const int number)
155 std::string result(comment);
156 return result += asString(number);
160 \param comment Comentario que precede al valor.
161 \param number Numero a convertir.
162 @return Un literal con el numero convertido a cadena decimal.
164 static std::string asText(const char* comment, const S64 number)
166 std::string result(comment);
167 return result += asString(number);
171 \param comment Comentario que precede al valor.
172 \param _bool Booleano a convertir.
173 @return Un literal con el numero convertido a cadena decimal.
175 static std::string asText(const char* comment, const bool _bool)
177 std::string result(comment);
178 return result += asString(_bool);
182 \param comment Comentario que precede al valor.
183 \param dataBlock Bloque de datos a interpretar.
184 \param characterByLine Número de caracteres en cada línea.
185 @return Un literal con el numero convertido a cadena decimal.
187 static std::string asText(const char* comment, const DataBlock& dataBlock, const int characterByLine = 16)
189 std::string result(comment);
190 return result += asString(dataBlock, characterByLine);
194 \param comment Comentario que precede al valor.
195 \param value Numero a tratar.
196 \param format Formato aplicado para convertir el numero a cadena. Ver \em man printf.
197 \return Un literal con el numero convertido a cadena.
199 static std::string asText(const char* comment, const float value, const char* format = "%f")
201 std::string result(comment);
202 return result += asString(value, format);
206 \param comment Comentario que precede al valor.
207 \param value Numero a tratar.
208 \param format Formato aplicado para convertir el numero a cadena. Ver \em man printf.
209 \return Un literal con el numero convertido a cadena.
211 static std::string asText(const char* comment, const double value, const char* format = "%e")
213 std::string result(comment);
214 return result += asString(value, format);
218 \param number Numero a convertir.
219 @return Un literal con el numero convertido a cadena hexadecimal.
221 static std::string asHexString(const int number) ;
224 \param number Numero a convertir.
225 @return Un literal con el numero convertido a cadena hexadecimal.
227 static std::string asHexString(const S64 number) ;
230 \param number Numero a convertir.
231 @return Un literal con el numero convertido a cadena hexadecimal.
233 static std::string asHexString(const U64 number) { return asHexString((S64) number); }
236 \param comment Comentario que precede al valor.
237 \param number Numero a convertir.
238 @return Un literal con el numero convertido a cadena decimal.
240 static std::string asHexText(const char* comment, const int number)
242 std::string result(comment);
243 return result += asHexString(number);
247 \param comment Comentario que precede al valor.
248 \param number Numero a convertir.
249 @return Un literal con el numero convertido a cadena decimal.
251 static std::string asHexText(const char* comment, const S64 number)
253 std::string result(comment);
254 return result += asHexString(number);
258 * Devuelve un cadena con el contenido del bloque de datos interpretado como BCD, pero pasa
259 * cada valor binario a su correspondiente carácter. Por ejemplo, el buffer aa210c quedará como una cadena "AA210C".
261 * \param dataBlock Bloque a codificar.
262 * \return La cadena que contiene el valor literal del buffer de datos.
264 static std::string asHexString(const DataBlock& dataBlock) ;
267 * Obtiene el valor original de una cadena obtenido con #asHexString (const DataBlock&).
268 * \param hexString Cadena que contiene el búfer.
269 * \param target Bloque de datos sobre el que decodificar la cadena.
270 * \return El bloque de datos original correspondiente a la cadena recibida.
272 static DataBlock& fromHexString(const std::string& hexString, DataBlock& target) noexcept(false);
275 Devuelve una cadena con la hora en formato 'dd/mm/yyyy hh:mm:ss'.
277 @param second Hora que deseamos traducir.
279 @return Un literal con la hora en el formato 'dd/mm/yyyy hh:mm:ss'.
281 static std::string asDateTime(const Second &second) ;
284 Devuelve una cadena con la hora en formato 'dd/mm/yyyy hh:mm:ss'.
286 @param second Hora que deseamos traducir.
287 @param result Puntero donde vamos a guardar el resultado de la conversin.
288 Debe tener espacio reservado para contener #TimeSizeAsString caracteres.
290 @return El puntero recibido como parametro conteniendo el literal con la hora
291 en el formato 'dd/mm/yyyy hh:mm:ss'.
293 static const char* asDateTime(const Second &second, char* result) ;
296 Calcula la funcion hash de la cadena recibida como parametro.
297 \param str Cadena a la que aplicar la funcion hash.
299 static S64 hash(const char* str) ;
302 Calcula la funcion hash de la cadena recibida como parametro.
303 \param str Cadena a la que aplicar la funcion hash.
305 static S64 hash(const std::string& str) { return hash(str.c_str()); }
308 Devuelve la cadena que contiene el resultado de aplicar la especificacion \em format
309 sobre el resto de los parametros.
311 \param format especificacion de formato similiar al empleado en las funciones \em printf,
314 \return la cadena que contiene el resultado de aplicar la especificacion \em format
315 sobre el resto de los parametros.
317 static std::string asString(const char* format, ...) ;
320 Devuelve el resultado de invocar a metodo asString de la clase recibida como parametro.
321 Si t es NULL devolvera el texto indicando la sitacion.
322 \param t Instancia de la clase a usar. Puede ser NULL.
323 \return el resultado de invoca a T::asString () si t es distinto de NULL.
324 \warning La clase T debe tener un metodo estatico con la signatura:
326 static const char* className () ;
329 template <typename T> static std::string asString(const T* t)
332 std::string result(T::className());
333 result += " { <null> }";
337 return t->asString();
341 Metodo identididad. Facilita la implementacion de patrones donde no se conoce el tipo de dato recibido.
343 \param str Instancia de la cadena.
344 \return La misma instancia recibida como parametro.
346 static const std::string& asString(const std::string& str) { return str; }
349 Detiene la ejecucion del thread durante el numero de milisegundos indicados.
351 \param millisecond Numero de milisegundos que vamos a detener la ejecucion de este thread.
353 static void sleep(const Millisecond &millisecond) ;
356 Obtiene el numero de segundos transcurridos desde el 1 del 1 de 1970.
357 \return El numero de segundos transcurridos desde el 1 del 1 de 1970.
359 static Second second() {
360 Second result(::time(NULL));
365 Obtiene el numero de microsegundos transcurridos desde el 1 del 1 de 1970.
366 \return El numero de microsegundos transcurridos desde el 1 del 1 de 1970.
368 static Microsecond microsecond() {
370 gettimeofday(&tv, NULL);
371 Microsecond result((Microsecond::type_t)1000000 * tv.tv_sec + tv.tv_usec);
376 Obtiene el numero de milisegundos transcurridos desde el 1 del 1 de 1970.
377 \return El numero de milisegundos transcurridos desde el 1 del 1 de 1970.
379 static Millisecond millisecond() {
381 gettimeofday(&tv, NULL);
382 Millisecond result((Millisecond::type_t)1000 * tv.tv_sec + tv.tv_usec / 1000);
387 Devuelve la referencia interna de los microsegundos transcurrido en el procesador.
388 \return la referencia interna de los microsegundos transcurrido en el procesador.
390 static Microsecond hardwareClock() {
392 //clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); // DONT works (original)
393 //clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); // DONT works
394 //clock_gettime(CLOCK_MONOTONIC, &ts); // works
395 // Note that CLOCK_MONOTONIC is subject to discontinuities from system time
396 // adjustment in Linux. CLOCK_MONOTONIC_RAW was defined to get around this
397 // (gets hardware time not adjusted by NTP).
398 clock_gettime(CLOCK_MONOTONIC, &ts); // works
400 Microsecond result((Microsecond::type_t)1000000 * ts.tv_sec + ts.tv_nsec / 1000);
405 Interpreta la cadena recibida como parametro como un dato de tipo boolean.
407 Si la cadena vale NULL, o contiene los literales "false" o "0" devolvera \em false,
408 si contiene los literales "true" o "1" devolvera \em true, en otro caso devolvera un excepcion.
410 \param str Cadena a interpretar.
412 \return El valor booleano correspondiente a la cadena recibida.
414 static bool asBool(const char* str) noexcept(false);
417 Interpreta la cadena recibida como parametro como un entero de 32 bits.
420 static int asInteger(const char* str) { return atoi(str); }
423 Interpreta la cadena recibida como parametro como un entero de 32 bits.
426 static S64 asInteger64(const char* str) ;
429 Devuelve el identificador de thread desde el que es invocado este metodo.
430 Si el programa no tuviera soporta para MT siempre devolvera 0.
431 \return el identificador de thread desde el que es invocado este metodo.
433 static pthread_t getCurrentThread() ;
436 Devuelve \em true si la version de nucleo que estamos ejecutado soporta multithread o \em false en otro
438 \return \em true si la version de nucleo que estamos ejecutado soporta multithread o \em false en otro
440 static bool supportMultithread() {
441 WHEN_SINGLETHREAD(return false);
442 WHEN_MULTITHREAD(return true);
446 Devuelve \em true si el valor recibido cumple el patron establecido o \em false en otro caso.
447 \param pattern Expresion regular que describe el patron a cumplir.
448 \param value Cadena a comparar con el patron.
449 \return \em true si el valor recibido cumple el patron establecido o \em false en otro caso.
451 \see regexec para mas informacion sobre las expresiones regulares.
453 static bool isLike(const char* pattern, const std::string& value) noexcept(false);
456 * Devuelve el número de bits necesarios para representar el valor recibido como parámetro.
457 * \param n Valor a estudiar.
458 * \return el número de bits necesarios para representar el valor recibido como parámetro.
460 static int bitsize(const int n) { return (n == 0) ? 1 : functions::log2(n) + 1; }
463 * Devuelve el número de bits necesarios para representar el valor recibido como parámetro.
464 * \param n Valor a estudiar.
465 * \return el número de bits necesarios para representar el valor recibido como parámetro.
467 static int bitsize(const S64 n) {
468 int aux = n >> intBitSize;
469 return (aux != 0) ? (bitsize(aux) + intBitSize) : bitsize((int) n);
473 * Calcula la operación (n1 << bitShit) | n2. Establece las comprobaciones necesarias para verificar
474 * que la operación se realiza correctamente, teniendo especial cuidado de que no se puedan solapar
475 * ninguno de los valores.
477 * \param whatis Literal que debería identificar el punto de invocación en caso de que haya algún error.
478 * \param n1 Número a desplazar el nº de bits indicado por \em bitShift.
479 * \param bitShift Número de bits a desplazar.
480 * \param n2 Número a combinar con el resultado de la operación (n1 << bitShift).
482 static S64 merge(const char* whatis, const int n1, const int n2, const int bitShift) noexcept(false);
485 * Calcula el logaritmo en base 2 del número recibo como parámetro.
486 * \param v Valor a calcular.
487 * \return El algoritmo en base 2 del número recibido como parámetro o -1 si el parámetro recibido es 0.
489 static int log2(const unsigned int v) ;
494 //////////////////////////////////////////////////////////////////////////////////////////////////
495 // Text format resources /////////////////////////////////////////////////////////////////////////
496 //////////////////////////////////////////////////////////////////////////////////////////////////
497 struct TextHighlightMode {
499 None = -1, // Initialized
509 struct TextJustifyMode {
511 None = -1, // Initialized
519 Solve singular/plural literal expression for any number.
521 Provide (0): returns "no entries"
522 Provide (1): returns "1 entry"
523 Provide (2): returns "2 entries"
525 Provide (0, 'table'): returns "no tables"
526 Provide (1, 'table'): returns "1 table"
527 Provide (2, 'table'): returns "2 tables"
529 Provide (0, 'query', 'queries'): returns "no queries"
530 Provide (1, 'query', 'queries'): returns "1 query"
531 Provide (2, 'query', 'queries'): returns "2 queries"
534 @param number Amount processed
535 @param wordForSingular Word used as singular, 'entry' by default.
536 @param wordForPlural Word used as plural, 'entries' by default.
538 @return Coherent literal as '%d <singular word/plural word>'
540 static std::string entriesAsString(int number, const char * wordForSingular = NULL, const char * wordForPlural = NULL) ;
543 Justify text (traces and output improvement)
545 @param title Title processed
546 @param mode Justify mode: Left (default), Center, Right
547 @param filler Filler character used (space by default)
549 @return Processed text
551 static std::string justify(const std::string & title, TextJustifyMode::_v mode = TextJustifyMode::Left, char filler = ' ') ;
554 Highligth text (traces and output improvement)
556 @param title Title processed
557 @param mode Highlight mode: Overline, Underline(default), OverAndUnderline, Leftline, Rightline, LeftAndRightline
558 @param filler Filler character used (dash by default)
559 @param appendCR Carriage return inclusion (true by default)
561 @return Processed text
563 static std::string highlight(const std::string & title, TextHighlightMode::_v mode = TextHighlightMode::Underline, char filler = '-', bool appendCR = true) ;
566 Highligth and justify text (traces and output improvement)
568 @param title Title processed
569 @param hMode Highlight mode: Overline, Underline(default), OverAndUnderline, Leftline, Rightline, LeftAndRightline
570 @param jMode Justify mode: Left (default), Center, Right
571 @param highlightFiller Filler character used (double dash ('=') by default)
572 @param justifyFiller Filler character used when justify (space by default)
573 @param appendCR Carriage return inclusion (true by default)
575 @return Processed text
577 static std::string highlightJustify(const std::string & title, TextHighlightMode::_v hMode = TextHighlightMode::OverAndUnderline, TextJustifyMode::_v jMode = TextJustifyMode::Center, char highlightFiller = '=', char justifyFiller = ' ', bool appendCR = true) {
578 return(highlight(justify(title, jMode, justifyFiller), hMode, highlightFiller, appendCR));
582 Tabulate text (traces and output improvement)
584 @param text Text processed
585 @param tabSpaces Tab spaces (three by default)
587 static std::string tab(const std::string & text, int tabSpaces = 3) ;
590 //////////////////////////////////////////////////////////////////////////////////////////////////
591 // Conversions and helpers ///////////////////////////////////////////////////////////////////////
592 //////////////////////////////////////////////////////////////////////////////////////////////////
595 Pattern to obtain a multi named component instance easily.
596 Parameters are usually replaced by the macro C <b>FILE_LOCATION</b>.
598 \param className Component class name
599 \param fromFile File which called the method
600 \param fromLine Line number within the file from where the method is called.
602 \return Component instance for the class provided at the pattern
605 template <typename T> static T* componentByName(const char *className, const char* fromFile, const int fromLine)
607 ComponentManager &cm = ComponentManager::instantiate();
608 T* result = static_cast <T*>(cm.find(className));
611 std::string msg(className);
612 msg += " | Unregistered component";
613 throw RuntimeException(msg, fromFile, fromLine);
620 Pattern to obtain a single named component instance easily.
621 Parameters are usually replaced by the macro C <b>FILE_LOCATION</b>.
623 \param fromFile File which called the method
624 \param fromLine Line number within the file from where the method is called.
626 \return Component instance for the class provided at the pattern
627 \warning T class must implement a method in the form:
629 static const char* getClassName () ;
633 template <typename T> static T* component(const char* fromFile, const int fromLine)
635 return functions::componentByName<T> (T::getClassName(), fromFile, fromLine);
639 Finds string at the end of another
641 @param pattern String where we find
642 @param suffix Searched string
644 @return Boolean about ocurrency
646 static bool endsWith(const std::string & pattern, const std::string & suffix) {
648 return endsWith(pattern, suffix, dummy);
652 Similar to #endsWith but returning additional preffix string by reference (pattern without suffix)
654 static bool endsWith(const std::string & pattern, const std::string & suffix, std::string & preffix) ;
657 Finds string at the begining of another
659 @param pattern String where we find
660 @param preffix Searched string
662 @return Boolean about ocurrency
664 static bool startsWith(const std::string & pattern, const std::string & preffix) {
666 return startsWith(pattern, preffix, dummy);
670 Similar to #startsWith but returning additional suffix string by reference (pattern without preffix)
672 static bool startsWith(const std::string & pattern, const std::string & preffix, std::string & suffix) ;
675 Finds 'item' and replaces it with 'target' within the string provided ('text').
676 The result is returned.
678 @param text Original string
679 @param item Searched string
680 @param target String which replaces the item
681 @param all Boolean about replace all items or only the first found. True by default.
683 @return Modified string
685 static std::string replace(const std::string & text, const char *item, const char *target, bool all = true) ;
688 * Coverts original string without quotation into quoted one: '\%s'
690 static std::string addQuotationMarks(const std::string & str) ;
691 static std::string addQuotationMarks(const char * str) ;
692 static std::string addQuotationMarks(const int & integer) ;
695 * Generates space-separated string lists based on integer elements
696 * Also, another separator could be used.
698 static std::string vectorToStringRepresentation(const std::vector<int> & v, const char separator = ' ') ;
701 * Generates space-separated string lists based on string elements.
702 * Also, another separator could be used.
704 static std::string vectorToStringRepresentation(const std::vector<std::string> & v, const char separator = ' ') ;
707 Returns socket notation 'Address:Port'
709 static std::string socketLiteralAsString(const std::string & address, int port) ;
712 Ascii string for buffer/size data block
714 @param buffer Octet string buffer
715 @param size Buffer size
716 @param isFullyPrintable Returned by reference
718 @return Ascii string representation, and dots for non-printable cheracters
720 static std::string asAsciiString(const char * buffer, int size, bool & isFullyPrintable) ;
723 Same as #asAsciiString but without interest about if is printable or not
725 static std::string asAsciiString(const char * buffer, int size) {
726 bool isFullyPrintable;
727 return asAsciiString(buffer, size, isFullyPrintable);
731 Same as #asAsciiString providing anna::DataBlock
733 static std::string asAsciiString(const DataBlock & db, bool & isFullyPrintable) {
734 return asAsciiString(db.getData(), db.getSize(), isFullyPrintable);
738 Same as #asAsciiString providing DataBlock and without interest about if is printable or not
740 static std::string asAsciiString(const DataBlock & db) {
741 bool isFullyPrintable;
742 return asAsciiString(db.getData(), db.getSize(), isFullyPrintable);
747 * IP Address enconding based on common database 'human-readable raw presentation':
749 * Example for IPv4: 'AABBCCDD' will be DataBlock for '170.187.204.221'
750 * Example for IPv6: '20010DB885A3000000008A2E03707334' will be DataBlock for '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
752 * '000000000000000000000000AABBCCDD' will be encoded as 16-sized Datablock, not IPv4 (4 bytes). Is not recommended to
753 * put IPv4 on this way because of ambiguity regarding IPv4-compatible format. It is application responsability to trim
754 * leading zeros in order to use this method for IPv4 source.
757 * @param rawPresentation Input IP address as raw presentation. Must be 8 or 32 sized for IPv4 and IPv6 respectively.
759 * @return Encoded DataBlock
761 static DataBlock rawIpPresentationAsRaw(const std::string & rawPresentation) noexcept(false);
765 * IP Address decoding from raw presentation:
767 * Example for IPv4: 'AABBCCDD' will be '170.187.204.221'
768 * Example for IPv6: '20010DB885A3000000008A2E03707334' will be '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
770 * '000000000000000000000000AABBCCDD' will be internally encoded as 16-sized Datablock, not IPv4 (4 bytes).
771 * Is not recommended to put IPv4 on this way because of ambiguity regarding IPv4-compatible format. It is
772 * application responsability to trim leading zeros in order to use this method for IPv4 source.
775 * @param rawPresentation Input IP address as raw presentation. Must be 8 or 32 sized for IPv4 and IPv6 respectively.
776 * @param normalize Normalize returned address representation, 'false' by default (to avoid IPv4 to IPv6 conversion)
778 * @return Decoded IP address
780 static std::string rawIpPresentationToIpAsString(const std::string & rawPresentation, bool normalize = false) noexcept(false) {
781 return rawIpAsString(rawIpPresentationAsRaw(rawPresentation), normalize);
786 * IP Address decoding to 'human-readable raw presentation':
788 * Example for IPv4: DataBlock for '170.187.204.221' will be 'AABBCCDD' (a pure IPv4 will never contain leading zeros outside of its scope (i.e., 24 zeros on a 32-character presentation)
789 * Example for IPv6: DataBlock for '2001:0db8:85a3:0000:0000:8a2e:0370:7334' will be '20010DB885A3000000008A2E03707334'
791 * DataBlock for '::170.187.204.221' will be represented as IPv4 compatible: '000000000000000000000000AABBCCDD'
794 * @param db Encoded DataBlock with 4 or 16 bytes to represent Ipv4 or Ipv6.
796 * @return Human-readable raw IP presentation
798 static std::string rawIpAsRawIpPresentation(const DataBlock & db) noexcept(false);
802 * Gets the host name (system name)
806 static std::string getHostname() ;
809 * Gets the domain name
813 static std::string getDomainname() ;
816 * Gets the FQDN (Fully Qualified Domain Name)
818 * @param hostname Specific provided hostname. Automatically solved if missing. Empty string implies exclusion from FQDN.
819 * @param domainname Specific provided domainname. Automatically solved if missing. Empty string implies exclusion from FQDN.
821 * @return FQDN (<hostname>.<domainname>)
823 static std::string getFQDN(const char *hostname = NULL, const char *domainname = NULL) ;
826 * Gets the IP based on hostname (#getHostname)
828 * @return Hostname-based IP
830 static std::string getHostnameIP() ;
834 //////////////////////////////////////////////////////////////////////////////////////////////////
835 // IP Address resources //////////////////////////////////////////////////////////////////////////
836 //////////////////////////////////////////////////////////////////////////////////////////////////
839 * IPv4 subtype (Estrict/Compatible)
843 Estrict = -1, // Initialized,
850 * IPv4 address family detection
852 * @param ip IP address
853 * @param ipv4Type Check for IPv4-compatible (i.e. '::192.168.0.1'), IPv4-mapped (i.e. '2001:0db8:85a3:0000:0000:8a2e:192.168.0.1') or estrict IPv4 format.
855 * @return Boolean for IPv4 nature
857 static bool isIPv4(const std::string & ip, IPv4Type::_v ipv4Type = IPv4Type::Estrict) ;
860 * IPv6 address family detection
862 * @param ip IP address
864 * @return Boolean for IPv6 nature
866 static bool isIPv6(const std::string & ip) ;
869 * Convert an IPv4 address to IPv6. Also removes dots from IPv4-mapped format.
871 * @param ipv4 IP Address in dot notation (192.168.1.100)
873 * @return string IPv6 formatted address or launch exception if invalid input
875 static std::string IPv4To6(const std::string & ipv4) noexcept(false);
878 * Normalizes an IP address to long notation. Specially used for IPv6, but valid for IPv4 (via IPv4To6 conversion).
881 * ::1 -> 0000:0000:0000:0000:0000:0000:0000:0001
882 * 2001:db8:85a3::8a2e:370:7334 -> 2001:0db8:85a3:0000:0000:8a2e:0370:7334
884 * @param ip Input IP address
886 * @return Normalized IP address
888 static std::string normalizeIP(const std::string & ip) noexcept(false);
891 * Compare two IP addresses by mean normalization
893 * @param ip1 First IP address compared
894 * @param ip2 Second IP address compared
896 * @return Boolean about IP's comparison
898 static bool sameIP(const std::string & ip1, const std::string & ip2) noexcept(false);
901 * Compare two IP addresses by mean internal comparison after ipv6 preffix restriction
903 * @param ipv6 IPv6 address matched
904 * @param preffixedIpv6 Preffixed IPv6 address (<ipv6>/<preffix length>: only values from 0 (always match) to 128 (maximum restriction) are allowed).
906 * @return Boolean about subnet matching
908 static bool matchIPv6(const std::string & ipv6, const std::string & preffixedIpv6) noexcept(false);
911 * IP Address serialization
913 * @param ip Input IP address
915 * @return Encoded DataBlock
917 static DataBlock ipAsRaw(const std::string & ip) noexcept(false);
920 * IP Address decoding
922 * @param db Encoded DataBlock with 4 or 16 bytes to represent Ipv4 or Ipv6.
923 * @param normalize Normalize returned address representation, 'false' by default (to avoid IPv4 to IPv6 conversion)
925 * @return Decoded IP address
927 static std::string rawIpAsString(const DataBlock & db, bool normalize = false) noexcept(false) {
928 return (rawIpAsString(db.getData(), db.getSize(), normalize));
932 * IP Address decoding
934 * @param buffer Encoded buffer with 4 or 16 bytes to represent Ipv4 or Ipv6.
935 * @param bufferLength Encoded buffer length with 4 or 16 bytes to represent Ipv4 or Ipv6.
936 * @param normalize Normalize returned address representation, 'false' by default (to avoid IPv4 to IPv6 conversion)
938 * @return Decoded IP address
940 static std::string rawIpAsString(const char *buffer, int bufferLength, bool normalize = false) noexcept(false);
943 * Abbreviates an IP address. Specially used for IPv6, but valid for IPv4.
946 * 0000:0000:0000:0000:0000:0000:0000:0001 -> ::1
947 * 2001:0db8:85a3:0000:0000:8a2e:0370:7334 -> 2001:db8:85a3::8a2e:370:7334
949 * @param ip Input IP address
951 * @return Abbreviated IP address
953 static std::string abbreviateIP(const std::string & ip) noexcept(false) {
954 return (rawIpAsString(ipAsRaw(ip)));
960 // socket literal description typedef, vectors and conversion tools
963 * Extract ADDRESS (ip or hostname ro resolve) and PORT from socket literal description ('<ip|hostname>:<port>').
965 * @param literal Socket literal in format '<ip|hostname>:<port>'
966 * @param address Address extracted by reference
967 * @param port Port extracted by reference
969 static void getAddressAndPortFromSocketLiteral(const std::string &literal, std::string &address, int &port) ;
972 * Translate pipe-separated socket literal list into Address/Port vector.
974 * @param list Comma-separated Address/Port list. I.e.: '10.95.10.25:4000,10.95.10.25:4001', or 'fed1:4000,fed2:4001'
975 * @return Address/Port socket items vector
977 static socket_v getSocketVectorFromString(const std::string & list) ;
980 * Translate Address/Port vector into comma-separated Address/Port list.
982 * @param socketVector Address/Port vector
984 * @return Comma-separated Address/Port list. I.e.: '10.95.10.25:4000,10.95.10.25:4001', or 'fed1:4000,fed2:4001'
986 static std::string socketVectorAsString(const socket_v & socketVector) ;
990 Endianess of the system
992 @result Returns true when the system is little endian, false if big endian
994 static bool littleEndian() ;
997 Encodes an integer number with 32 bits over a buffer with at least 4 bytes of length.
998 @param result Buffer where the number is encoded.
999 @param n Number to encode.
1000 \return Buffer with the encoded number.
1002 static const char* codeInteger(char* result, const int n) ;
1005 Encodes an integer number with 16 bits over a buffer with at least 2 bytes of length.
1006 @param result Buffer where the number is encoded.
1007 @param n Number to encode.
1008 \return Buffer with the encoded number.
1010 static const char* codeShort(char* result, const short int n) ;
1013 Encodes an integer number with 64 bits over a buffer with at least 8 bytes of length.
1014 @param result Buffer where the number is encoded.
1015 @param n Number to encode.
1016 \return Buffer with the encoded number.
1018 static const char* codeInteger64(char* result, const S64 n) ;
1021 Encodes a floating number with 32 bits (according to the standard IEEE-754) over a buffer with at least 4 bytes of length.
1022 @param result Buffer where the number is encoded.
1023 @param n Number to encode.
1024 \return Buffer with the encoded number.
1026 static const char* codeFloat(char* result, const float n) ;
1029 Encodes a floating number with 64 bits (according to the standard IEEE-754) over a buffer with at least 8 bytes of length.
1030 @param result Buffer where the number is encoded.
1031 @param n Number to encode.
1032 \return Buffer with the encoded number.
1034 static const char* codeDouble(char* result, const double n) ;
1037 Decodes an 32 bits integer number contained in a 4-bytes buffer.
1038 @param data Buffer with the encoded number.
1039 @return Value for the number contained in the buffer.
1041 static int decodeInteger(const char* data) ;
1044 Decodes an 16 bits integer number contained in a 2-bytes buffer.
1045 @param data Buffer with the encoded number.
1046 @return Value for the number contained in the buffer.
1048 static short int decodeShort(const char* data) ;
1051 Decodes an 64 bits integer number contained in a 8-bytes buffer.
1052 @param data Buffer with the encoded number.
1053 @return Value for the number contained in the buffer.
1055 static S64 decodeInteger64(const char* data) ;
1058 Decodes an 32 bits floating number (according to the standard IEEE-754) contained in a 4-bytes buffer.
1059 @param data Buffer with the encoded number.
1060 @return Value for the number contained in the buffer.
1062 static float decodeFloat(const char* data) ;
1065 Decodes an 64 bits floating number (according to the standard IEEE-754) contained in a 8-bytes buffer.
1066 @param data Buffer with the encoded number.
1067 @return Value for the number contained in the buffer.
1069 static double decodeDouble(const char* data) ;
1073 * Decodes an ISUP Number (called or calling party number).
1075 * @param buffer Isup number content buffer.
1076 * @param length Isup number content length.
1077 * @param isupNumber Isup number decoded by reference.
1078 * @param calledOrCalling True for called party number, false for calling
1080 static void decodeIsupNumber(const char *buffer, int length, isup_number_t & isupNumber, bool calledOrCalling) noexcept(false);
1083 * Encodes an ISUP Number (called or calling party number).
1085 * @param isupNumber Isup number.
1086 * @param calledOrCalling True for called party number, false for calling
1087 * @param buffer Isup number content encoded buffer.
1088 * @param length Isup number content encoded length.
1090 static void codeIsupNumber(const isup_number_t & isupNumber, bool calledOrCalling, char * buffer, int & length) noexcept(false);
1093 * Encodes an ISUP Number (called or calling party number).
1095 * @param isupNumber Isup number.
1096 * @param calledOrCalling True for called party number, false for calling
1097 * @param target Isup number octet string.
1099 static void codeIsupNumber(const isup_number_t & isupNumber, bool calledOrCalling, std::string & target) noexcept(false);
1104 * @param str String to be encoded
1106 * @return Returns encoded representation
1108 static std::string encodeBase64(const U8* buf, unsigned int bufLen);
1109 static std::string encodeBase64(const std::string & str)
1111 return encodeBase64((const U8 *)str.c_str(), str.size());
1117 * @param encodedString Encoded base64 representation
1119 * @return Returns decoded representation
1121 static std::string decodeBase64(const std::string & encodedString);
1125 * Reads a file into passed string
1127 * @param pathfile Path file to read
1128 * @param content String where file content is dump
1130 * @return success for read operation
1132 static bool getContentFromFile(const std::string &pathfile, std::string &content) noexcept(false);