First commit
[anna.git] / include / anna / core / Configuration.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_Configuration_hpp
38 #define anna_core_Configuration_hpp
39
40 #include <stdlib.h>
41
42 #include <vector>
43 #include <map>
44 #include <string>
45
46 #include <anna/core/functions.hpp>
47 #include <anna/core/util/Variable.hpp>
48 #include <anna/core/RuntimeException.hpp>
49
50 namespace anna {
51
52 /**
53    Clase para recoger parametros de un determinado archivo de configuracion.
54 */
55 class Configuration {
56 public:
57   static const char* defaultSection;
58
59   /**
60      Constructor.
61      \param className Nombre de la clase usado para registrar en la lista de componentes.
62   */
63   Configuration(const char* className) {;}
64
65   /**
66      Constructor.
67   */
68   Configuration() {;}
69
70   /**
71   * Destructor.
72   */
73   ~Configuration() { removeAll(); }
74
75   /**
76      Carga en memoria el archivo indicado como argumento.
77
78      @param configFile Ruta completa con el nombre del archivo de configuracion a cargar.
79      Cualquier otro archivo procesado anteriormente con esta instancia se perdera.
80   */
81   void load(const char* configFile) throw(RuntimeException);
82
83   /**
84     Establece el valor por defecto para una determinada variable, es decir, en caso de que
85     la variable no exista en el fichero de configuracion cargado (ver #load) devolvera
86     el valor establecido mediante este metodo.
87
88     @param sectionName Nombre de la seccion a la que pertenece la variable.
89     @param variableName Nombre de la variable de configuracion.
90     @param defaultValue Valor por defecto de la variable. Ă‰ste valor solo sera devuelto en caso
91     de que la variable indicada por la seccion y el nombre de variable no este contenido en
92     el archivo de configuracion cargado.
93   */
94   void setDefaultValue(const char* sectionName, const char* variableName, const char* defaultValue)
95   throw(RuntimeException);
96
97   /**
98      Devuelve el valor asociada a la variable indicada.
99
100     @param sectionName Nombre de la seccion a la que pertenece la variable.
101     @param variableName Nombre de la variable de configuracion.
102     @param strict Si es true indica que debe devolver el valor estricto de la variable, de forma,
103     que si esta variable no esta contenida en el archivo de configuracion devolvera NULL, en otro
104     caso, devolvera el posible valor por defecto que tenga asociado esta variable (ver #setDefaultValue).
105
106     @return El valor asociado a la variable. Puede ser NULL.
107   */
108   const char* getValue(const char* sectionName, const char* variableName, const bool strict = false) const
109   throw(RuntimeException);
110
111   /**
112      Devuelve el valor asociada a la variable indicada.
113     @param sectionName Nombre de la seccion a la que pertenece la variable.
114     @param variableName Nombre de la variable de configuracion.
115     @param strict Si es true indica que debe devolver el valor estricto de la variable, de forma,
116     que si esta variable no esta contenida en el archivo de configuracion devolvera NULL, en otro
117     caso, devolvera el posible valor por defecto que tenga asociado esta variable (ver #setDefaultValue).
118
119     @return El valor asociado a la variable.
120   */
121   int getIntegerValue(const char* sectionName, const char* variableName, const bool strict = false) const
122   throw(RuntimeException);
123
124   /**
125      Devuelve el estado de existencia o no de la variable indicada.
126     @param sectionName Nombre de la seccion a la que pertenece la variable.
127     @param variableName Nombre de la variable de configuracion.
128
129     @return true si la variable existe, y false en otro caso. Solo deberia invocarse despues de
130     invocar al metodo #load.
131   */
132   bool exists(const char* sectionName, const char* variableName) const throw();
133
134   /**
135      Devuelve la cadena por la que podemos buscar el componente.
136      \return La cadena por la que podemos buscar el componente.
137      \see Application::find
138   */
139   static const char* getClassName() { return "anna::Configuration"; }
140
141 private:
142   class VariableEx : public Variable {
143   public:
144     typedef std::vector <VariableEx*> Vector;
145
146     VariableEx(const char* variableName) :
147       Variable(variableName, Variable::Type::String),
148       a_defaultValue(NULL) {}
149
150     void setDefaultValue(const char* defaultValue) { a_defaultValue = defaultValue; }
151
152     const char* getDefaultValue() const { return a_defaultValue; }
153
154   private:
155     const char* a_defaultValue;
156   };
157
158   std::map <std::string, VariableEx::Vector*> a_sections;
159
160   Configuration(const Configuration& other);  // No implementado
161
162   void initialize() throw(RuntimeException) {;}
163   void stop() throw() {;}
164
165   void removeAll() throw();
166   bool processSection(const int nline, char* buffer, std::string& currentSection);
167   void processVariable(const int nline, char* buffer, const std::string& currentSection) throw(RuntimeException);
168   VariableEx* createVariable(const std::string& section, const char* variableName) throw();
169   VariableEx* find(const std::string& section, const char* variableName) throw();
170   const VariableEx* find(const std::string& section, const char* variableName) const
171   throw() {
172     return const_cast <Configuration*>(this)->find(section, variableName);
173   }
174
175   static char* strip(char* buffer);
176 };
177
178 }
179
180 #endif
181