Changed LICENSE. Now referenced to web site and file on project root directory
[anna.git] / include / anna / core / util / Configuration.hpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
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 //
7
8
9 #ifndef anna_core_util_Configuration_hpp
10 #define anna_core_util_Configuration_hpp
11
12 #include <stdlib.h>
13
14 #include <vector>
15 #include <map>
16 #include <string>
17
18 #include <anna/core/functions.hpp>
19 #include <anna/core/util/Variable.hpp>
20 #include <anna/core/RuntimeException.hpp>
21
22 namespace anna {
23
24 /**
25    Clase para recoger parametros de un determinado archivo de configuracion.
26 */
27 class Configuration {
28 public:
29   static const char* defaultSection;
30
31   /**
32      Constructor.
33      \param className Nombre de la clase usado para registrar en la lista de componentes.
34   */
35   Configuration(const char* className) {;}
36
37   /**
38      Constructor.
39   */
40   Configuration() {;}
41
42   /**
43   * Destructor.
44   */
45   ~Configuration() { removeAll(); }
46
47   /**
48      Carga en memoria el archivo indicado como argumento.
49
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.
52   */
53   void load(const char* configFile) throw(RuntimeException);
54
55   /**
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.
59
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.
65   */
66   void setDefaultValue(const char* sectionName, const char* variableName, const char* defaultValue)
67   throw(RuntimeException);
68
69   /**
70      Devuelve el valor asociada a la variable indicada.
71
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.
77
78     @return El valor asociado a la variable. Puede ser NULL.
79   */
80   const char* getValue(const char* sectionName, const char* variableName, const bool strict = false) const
81   throw(RuntimeException);
82
83   /**
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.
90
91     @return El valor asociado a la variable.
92   */
93   int getIntegerValue(const char* sectionName, const char* variableName, const bool strict = false) const
94   throw(RuntimeException);
95
96   /**
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.
100
101     @return true si la variable existe, y false en otro caso. Solo deberia invocarse despues de
102     invocar al metodo #load.
103   */
104   bool exists(const char* sectionName, const char* variableName) const throw();
105
106   /**
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
110   */
111   static const char* getClassName() { return "anna::Configuration"; }
112
113 private:
114   class VariableEx : public Variable {
115   public:
116     typedef std::vector <VariableEx*> Vector;
117
118     VariableEx(const char* variableName) :
119       Variable(variableName, Variable::Type::String),
120       a_defaultValue(NULL) {}
121
122     void setDefaultValue(const char* defaultValue) { a_defaultValue = defaultValue; }
123
124     const char* getDefaultValue() const { return a_defaultValue; }
125
126   private:
127     const char* a_defaultValue;
128   };
129
130   std::map <std::string, VariableEx::Vector*> a_sections;
131
132   Configuration(const Configuration& other);  // No implementado
133
134   void initialize() throw(RuntimeException) {;}
135   void stop() throw() {;}
136
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
143   throw() {
144     return const_cast <Configuration*>(this)->find(section, variableName);
145   }
146
147   static char* strip(char* buffer);
148 };
149
150 }
151
152 #endif
153