bes  Updated for version 3.20.10
TheBESKeys.h
1 // TheBESKeys.h
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #ifndef TheBESKeys_h_
34 #define TheBESKeys_h_ 1
35 
36 #include <fstream>
37 #include <map>
38 #include <set>
39 #include <vector>
40 #include <string>
41 
42 #include "BESObj.h"
43 
44 #define DYNAMIC_CONFIG_KEY "DynamicConfig"
45 #define DC_REGEX_KEY "regex"
46 #define DC_CONFIG_KEY "config"
47 #define DYNAMIC_CONFIG_ENABLED 0
48 
85 class TheBESKeys: public BESObj {
86 
87  friend class keysT;
88 
89 private:
90 
91  // TODO I don't think this needs to be a pointer - the code could be
92  // redesigned. jhrg 3/7/18
93  // Dropping this member variable because as far as I can tell it never serves a purpose. - ndp 08/05/2020
94  // std::ifstream * _keys_file;
95 
96  std::string d_keys_file_name;
97  std::map<std::string, std::vector<std::string> > *d_the_keys;
98  std::map<std::string, std::vector<std::string> > *d_the_original_keys;
99  bool d_dynamic_config_in_use;
100  bool d_own_keys;
101 
102  static std::set<std::string> d_ingested_key_files;
103 
104  static bool LoadedKeys(const std::string &key_file);
105 
106  void clean();
107 
108  void initialize_keys();
109  static void initialize_instance();
110  static void delete_instance();
111 
112 
113 
114 #if 0
115  //void load_keys();
116  //bool break_pair(const char* b, std::string& key, std::string &value, bool &addto);
117  //bool only_blanks(const char *line);
118  //void load_include_files(const std::string &files);
119  //void load_include_file(const std::string &file);
120 #endif
121 
122  TheBESKeys() : d_keys_file_name(""), d_the_keys(0), d_dynamic_config_in_use(false), d_own_keys(false) {
123  }
124 
125 #if 0
126  TheBESKeys(const std::string &keys_file_name, std::map<std::string, std::vector<std::string> > *keys);
127 #endif
128 
129 protected:
130  TheBESKeys(const std::string &keys_file_name);
131 
132 public:
133  static TheBESKeys *d_instance;
134 
135  virtual ~TheBESKeys();
136 
137  std::string keys_file_name() const {
138  return d_keys_file_name;
139  }
140 
141  void set_key(const std::string &key, const std::string &val, bool addto = false);
142 
143  void set_key(const std::string &pair);
144 
145  void set_keys(const std::string &key, const std::vector<std::string> &values, bool addto);
146 
147  void set_keys(const std::string &key, const std::map<std::string, std::string> &values,
148  const bool case_insensitive_map_keys, bool addto);
149 
150  void get_value(const std::string &s, std::string &val, bool &found);
151 
152  void get_values(const std::string &s, std::vector<std::string> &vals, bool &found);
153 
154  void get_values(const std::string &, std::map<std::string, std::string> &map_values,
155  const bool &case_insensitive_map_keys, bool &found);
156 
157  void get_values(const std::string &, std::map<std::string, std::map<std::string, std::vector<std::string> > > &map,
158  const bool &case_insensitive_map_keys, bool &found);
159 
160 
161  bool read_bool_key(const std::string &key, bool default_value);
162 
163  std::string read_string_key(const std::string &key, const std::string &default_value);
164 
165  int read_int_key(const std::string &key, int default_value);
166 
167  typedef std::map<std::string, std::vector<std::string> >::const_iterator Keys_citer;
168 
169  Keys_citer keys_begin() {
170  return d_the_keys->begin();
171  }
172 
173  Keys_citer keys_end() {
174  return d_the_keys->end();
175  }
176 
177  virtual void dump(std::ostream &strm) const;
178  virtual std::string dump() const;
179 
180  std::string get_as_config() const;
185  static std::string ConfigFile;
186 
190  static TheBESKeys *TheKeys();
191 
192  void load_dynamic_config(std::string name);
193  bool using_dynamic_config();
194 };
195 
196 #endif // TheBESKeys_h_
197 
top level BES object to house generic methods
Definition: BESObj.h:50
mapping of key/value pairs defining different behaviors of an application.
Definition: TheBESKeys.h:85
void get_value(const std::string &s, std::string &val, bool &found)
Retrieve the value of a given key, if set.
Definition: TheBESKeys.cc:340
static TheBESKeys * TheKeys()
Definition: TheBESKeys.cc:71
void set_key(const std::string &key, const std::string &val, bool addto=false)
allows the user to set key/value pairs from within the application.
Definition: TheBESKeys.cc:206
int read_int_key(const std::string &key, int default_value)
Read an integer-valued key from the bes.conf file.
Definition: TheBESKeys.cc:448
void get_values(const std::string &s, std::vector< std::string > &vals, bool &found)
Retrieve the values of a given key, if set.
Definition: TheBESKeys.cc:371
void load_dynamic_config(std::string name)
Loads the the applicable dynamic configuration or nothing if no configuration is applicable.
Definition: TheBESKeys.cc:681
virtual std::string dump() const
dumps information about this object
Definition: TheBESKeys.cc:484
static std::string ConfigFile
Definition: TheBESKeys.h:185
bool read_bool_key(const std::string &key, bool default_value)
Read a boolean-valued key from the bes.conf file.
Definition: TheBESKeys.cc:398
std::string read_string_key(const std::string &key, const std::string &default_value)
Read a string-valued key from the bes.conf file.
Definition: TheBESKeys.cc:423
void set_keys(const std::string &key, const std::vector< std::string > &values, bool addto)
allows the user to set key/value pairs from within the application.
Definition: TheBESKeys.cc:236
virtual ~TheBESKeys()
cleans up the key/value pair mapping
Definition: TheBESKeys.cc:140