1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
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
17 // * Neither the name of the copyright holder 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.
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.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 #ifndef anna_core_util_Configuration_hpp
38 #define anna_core_util_Configuration_hpp
46 #include <anna/core/functions.hpp>
47 #include <anna/core/util/Variable.hpp>
48 #include <anna/core/RuntimeException.hpp>
53 Clase para recoger parametros de un determinado archivo de configuracion.
57 static const char* defaultSection;
61 \param className Nombre de la clase usado para registrar en la lista de componentes.
63 Configuration(const char* className) {;}
73 ~Configuration() { removeAll(); }
76 Carga en memoria el archivo indicado como argumento.
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.
81 void load(const char* configFile) throw(RuntimeException);
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.
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.
94 void setDefaultValue(const char* sectionName, const char* variableName, const char* defaultValue)
95 throw(RuntimeException);
98 Devuelve el valor asociada a la variable indicada.
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 aun habiendo asociado
104 un valor por defecto (ver #setDefaultValue) devolvera NULL.
106 @return El valor asociado a la variable. Puede ser NULL.
108 const char* getValue(const char* sectionName, const char* variableName, const bool strict = false) const
109 throw(RuntimeException);
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 aun habiendo asociado
117 un valor por defecto (ver #setDefaultValue) devolvera NULL.
119 @return El valor asociado a la variable.
121 int getIntegerValue(const char* sectionName, const char* variableName, const bool strict = false) const
122 throw(RuntimeException);
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.
129 @return true si la variable existe, y false en otro caso. Solo deberia invocarse despues de
130 invocar al metodo #load.
132 bool exists(const char* sectionName, const char* variableName) const throw();
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
139 static const char* getClassName() { return "anna::Configuration"; }
142 class VariableEx : public Variable {
144 typedef std::vector <VariableEx*> Vector;
146 VariableEx(const char* variableName) :
147 Variable(variableName, Variable::Type::String),
148 a_defaultValue(NULL) {}
150 void setDefaultValue(const char* defaultValue) { a_defaultValue = defaultValue; }
152 const char* getDefaultValue() const { return a_defaultValue; }
155 const char* a_defaultValue;
158 std::map <std::string, VariableEx::Vector*> a_sections;
160 Configuration(const Configuration& other); // No implementado
162 void initialize() throw(RuntimeException) {;}
163 void stop() throw() {;}
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
172 return const_cast <Configuration*>(this)->find(section, variableName);
175 static char* strip(char* buffer);