Ensures normalization on waitfe/fc-xml operations
[anna.git] / include / anna / core / functions.hpp
index 512499d..78fe13b 100644 (file)
@@ -1,37 +1,9 @@
-// ANNA - Anna is Not Nothingness Anymore
-//
-// (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
-//
-// https://bitbucket.org/testillano/anna
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions
-// are met:
-//
-//     * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-//     * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following disclaimer
-// in the documentation and/or other materials provided with the
-// distribution.
-//     * Neither the name of Google Inc. nor the names of its
-// contributors may be used to endorse or promote products derived from
-// this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-//
-// Authors: eduardo.ramos.testillano@gmail.com
-//          cisco.tierra@gmail.com
+// ANNA - Anna is Not Nothingness Anymore                                                         //
+//                                                                                                //
+// (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
+//                                                                                                //
+// See project site at http://redmine.teslayout.com/projects/anna-suite                           //
+// See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
 
 
 #ifndef anna_core_functions_hpp
@@ -125,12 +97,6 @@ struct functions {
   */
   static std::string asString(const int number) throw();
 
-  /**
-     \param number Numero a convertir.
-     @return Un literal con el numero convertido a cadena decimal.
-  */
-  static std::string asString(const unsigned long number) throw();
-
   /**
      \param number Numero a convertir.
      @return Un literal con el numero sin signo convertido a cadena decimal.
@@ -260,12 +226,6 @@ struct functions {
   */
   static std::string asHexString(const S64 number) throw();
 
-  /**
-     \param number Numero a convertir.
-     @return Un literal con el numero convertido a cadena hexadecimal.
-  */
-  static std::string asHexString(const long number) throw();
-
   /**
      \param number Numero a convertir.
      @return Un literal con el numero convertido a cadena hexadecimal.
@@ -344,18 +304,6 @@ struct functions {
   */
   static S64 hash(const std::string& str) throw() { return hash(str.c_str()); }
 
-  /**
-     Calcula la funcion hash exclusive de la cadena recibida como parametro.
-     \param str Cadena a la que aplicar la funcion hash exclusiva.
-  */
-  static unsigned long exclusiveHash(const std::string& str) throw() { return st_stringExclusiveHash.calcule(str); }
-
-  /**
-     Calcula la funcion hash exclusive de la cadena recibida como parametro.
-     \param str Cadena a la que aplicar la funcion hash exclusiva.
-  */
-  static unsigned long exclusiveHash(const char* str) throw() { return st_stringExclusiveHash.calcule(std::string(str)); }
-
   /**
      Devuelve la cadena que contiene el resultado de aplicar la especificacion \em format
      sobre el resto de los parametros.
@@ -441,7 +389,14 @@ struct functions {
   */
   static Microsecond hardwareClock() throw() {
     timespec ts;
-    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
+    //clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); // DONT works (original)
+    //clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); // DONT works
+    //clock_gettime(CLOCK_MONOTONIC, &ts); // works
+      // Note that CLOCK_MONOTONIC is subject to discontinuities from system time
+      //  adjustment in Linux. CLOCK_MONOTONIC_RAW was defined to get around this
+      //  (gets hardware time not adjusted by NTP).
+    clock_gettime(CLOCK_MONOTONIC, &ts); // works
+
     Microsecond result((Microsecond::type_t)1000000 * ts.tv_sec + ts.tv_nsec / 1000);
     return result;
   }
@@ -510,7 +465,7 @@ struct functions {
    * \return el nĂºmero de bits necesarios para representar el valor recibido como parĂ¡metro.
    */
   static int bitsize(const S64 n) throw() {
-    register int aux = n >> intBitSize;
+    int aux = n >> intBitSize;
     return (aux != 0) ? (bitsize(aux) + intBitSize) : bitsize((int) n);
   }
 
@@ -637,28 +592,24 @@ struct functions {
   //////////////////////////////////////////////////////////////////////////////////////////////////
 
   /**
-     Pattern to obtain a component instance easily.
+     Pattern to obtain a multi named component instance easily.
      Parameters are usually replaced by the macro C <b>FILE_LOCATION</b>.
 
+     \param className Component class name
      \param fromFile File which called the method
      \param fromLine Line number within the file from where the method is called.
 
      \return Component instance for the class provided at the pattern
-     \warning The class T must define:
-     \code
-         static const char* getClassName () throw ();
-     \endcode
      \see Component
   */
-  template <typename T> static T* component(const char* fromFile, const int fromLine)
+  template <typename T> static T* componentByName(const char *className, const char* fromFile, const int fromLine)
   throw(RuntimeException) {
     ComponentManager &cm = ComponentManager::instantiate();
-    const char *className = T::getClassName();
     T* result = static_cast <T*>(cm.find(className));
 
     if(result == NULL) {
       std::string msg(className);
-      msg += " | Componente no registrado";
+      msg += " | Unregistered component";
       throw RuntimeException(msg, fromFile, fromLine);
     }
 
@@ -666,22 +617,23 @@ struct functions {
   }
 
   /**
-   * Gets exclusive hash for string provided on integer range
-   *
-   * @param str String hashed
-   *
-   * @return Hash unique value
-   */
-  static int exclusiveHashInt(const std::string& str) throw() { return st_string2intExclusiveHash.calcule(str); }
+     Pattern to obtain a single named component instance easily.
+     Parameters are usually replaced by the macro C <b>FILE_LOCATION</b>.
 
-  /**
-   * Gets exclusive hash for string (char pointer) provided on integer range
-   *
-   * @param str String hashed
-   *
-   * @return Hash unique value
-   */
-  static int exclusiveHashInt(const char* str) throw() { return st_string2intExclusiveHash.calcule(std::string(str)); }
+     \param fromFile File which called the method
+     \param fromLine Line number within the file from where the method is called.
+
+     \return Component instance for the class provided at the pattern
+     \warning T class must implement a method in the form:
+     \code
+         static const char* getClassName () throw ();
+     \endcode
+     \see Component
+  */
+  template <typename T> static T* component(const char* fromFile, const int fromLine)
+  throw(RuntimeException) {
+    return functions::componentByName<T> (T::getClassName(), fromFile, fromLine);
+  }
 
   /**
      Finds string at the end of another
@@ -1146,9 +1098,38 @@ struct functions {
   */
   static void codeIsupNumber(const isup_number_t & isupNumber, bool calledOrCalling, std::string & target) throw(RuntimeException);
 
-private:
-  static ExclusiveHash <std::string> st_stringExclusiveHash;
-  static ExclusiveHash <std::string, int> st_string2intExclusiveHash;
+  /**
+  * Base64 encoding
+  *
+  * @param str String to be encoded
+  *
+  * @return Returns encoded representation
+  */
+  static std::string encodeBase64(const U8* buf, unsigned int bufLen);
+  static std::string encodeBase64(const std::string & str)
+  {
+    return encodeBase64((const U8 *)str.c_str(), str.size());
+  }
+
+  /**
+  * Base64 decoding
+  *
+  * @param encodedString Encoded base64 representation
+  *
+  * @return Returns decoded representation
+  */
+  static std::string decodeBase64(const std::string & encodedString);
+
+
+  /*
+  * Reads a file into passed string
+  *
+  * @param pathfile Path file to read
+  * @param content String where file content is dump
+  *
+  * @return success for read operation
+  */
+  static bool getContentFromFile(const std::string &pathfile, std::string &content) throw(anna::RuntimeException);
 };
 
 }