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_util_Configuration_hpp
10 #define anna_core_util_Configuration_hpp
18 #include <anna/core/functions.hpp>
19 #include <anna/core/util/Variable.hpp>
20 #include <anna/core/RuntimeException.hpp>
25 Clase para recoger parametros de un determinado archivo de configuracion.
29 static const char* defaultSection;
33 \param className Nombre de la clase usado para registrar en la lista de componentes.
35 Configuration(const char* className) {;}
45 ~Configuration() { removeAll(); }
48 Carga en memoria el archivo indicado como argumento.
50 @param configFile Ruta completa con el nombre del archivo de configuracion a cargar.
51 Cualquier otro archivo procesado anteriormente con esta instancia se perdera.
53 void load(const char* configFile) throw(RuntimeException);
56 Establece el valor por defecto para una determinada variable, es decir, en caso de que
57 la variable no exista en el fichero de configuracion cargado (ver #load) devolvera
58 el valor establecido mediante este metodo.
60 @param sectionName Nombre de la seccion a la que pertenece la variable.
61 @param variableName Nombre de la variable de configuracion.
62 @param defaultValue Valor por defecto de la variable. Éste valor solo sera devuelto en caso
63 de que la variable indicada por la seccion y el nombre de variable no este contenido en
64 el archivo de configuracion cargado.
66 void setDefaultValue(const char* sectionName, const char* variableName, const char* defaultValue)
67 throw(RuntimeException);
70 Devuelve el valor asociada a la variable indicada.
72 @param sectionName Nombre de la seccion a la que pertenece la variable.
73 @param variableName Nombre de la variable de configuracion.
74 @param strict Si es true indica que debe devolver el valor estricto de la variable, de forma,
75 que si esta variable no esta contenida en el archivo de configuracion aun habiendo asociado
76 un valor por defecto (ver #setDefaultValue) devolvera NULL.
78 @return El valor asociado a la variable. Puede ser NULL.
80 const char* getValue(const char* sectionName, const char* variableName, const bool strict = false) const
81 throw(RuntimeException);
84 Devuelve el valor asociada a la variable indicada.
85 @param sectionName Nombre de la seccion a la que pertenece la variable.
86 @param variableName Nombre de la variable de configuracion.
87 @param strict Si es true indica que debe devolver el valor estricto de la variable, de forma,
88 que si esta variable no esta contenida en el archivo de configuracion aun habiendo asociado
89 un valor por defecto (ver #setDefaultValue) devolvera NULL.
91 @return El valor asociado a la variable.
93 int getIntegerValue(const char* sectionName, const char* variableName, const bool strict = false) const
94 throw(RuntimeException);
97 Devuelve el estado de existencia o no de la variable indicada.
98 @param sectionName Nombre de la seccion a la que pertenece la variable.
99 @param variableName Nombre de la variable de configuracion.
101 @return true si la variable existe, y false en otro caso. Solo deberia invocarse despues de
102 invocar al metodo #load.
104 bool exists(const char* sectionName, const char* variableName) const throw();
107 Devuelve la cadena por la que podemos buscar el componente.
108 \return La cadena por la que podemos buscar el componente.
109 \see Application::find
111 static const char* getClassName() { return "anna::Configuration"; }
114 class VariableEx : public Variable {
116 typedef std::vector <VariableEx*> Vector;
118 VariableEx(const char* variableName) :
119 Variable(variableName, Variable::Type::String),
120 a_defaultValue(NULL) {}
122 void setDefaultValue(const char* defaultValue) { a_defaultValue = defaultValue; }
124 const char* getDefaultValue() const { return a_defaultValue; }
127 const char* a_defaultValue;
130 std::map <std::string, VariableEx::Vector*> a_sections;
132 Configuration(const Configuration& other); // No implementado
134 void initialize() throw(RuntimeException) {;}
135 void stop() throw() {;}
137 void removeAll() throw();
138 bool processSection(const int nline, char* buffer, std::string& currentSection);
139 void processVariable(const int nline, char* buffer, const std::string& currentSection) throw(RuntimeException);
140 VariableEx* createVariable(const std::string& section, const char* variableName) throw();
141 VariableEx* find(const std::string& section, const char* variableName) throw();
142 const VariableEx* find(const std::string& section, const char* variableName) const
144 return const_cast <Configuration*>(this)->find(section, variableName);
147 static char* strip(char* buffer);