libeblearntools
|
00001 /*************************************************************************** 00002 * Copyright (C) 2009 by Pierre Sermanet * 00003 * pierre.sermanet@gmail.com * 00004 * All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions are met: 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Redistribution under a license not approved by the Open Source 00014 * Initiative (http://www.opensource.org) must display the 00015 * following acknowledgement in all advertising material: 00016 * This product includes software developed at the Courant 00017 * Institute of Mathematical Sciences (http://cims.nyu.edu). 00018 * * The names of the authors may not be used to endorse or promote products 00019 * derived from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 00022 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00023 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00024 * DISCLAIMED. IN NO EVENT SHALL ThE AUTHORS BE LIABLE FOR ANY 00025 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00026 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00027 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00028 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00029 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 ***************************************************************************/ 00032 00033 #ifndef CONFIGURATION_H_ 00034 #define CONFIGURATION_H_ 00035 00036 #include <map> 00037 #include <vector> 00038 #include "defines.h" 00039 #include "utils.h" 00040 #include <list> 00041 00042 using namespace std; 00043 00044 namespace ebl { 00045 00047 00048 typedef map<string, string, less<string> > string_map_t; 00049 typedef map<string, vector<string>, less<string> > string_list_map_t; 00050 00055 class textlist : public list< pair<string,string> > { 00056 public: 00058 textlist(); 00059 00061 textlist(const textlist &txt); 00062 00064 virtual ~textlist(); 00065 00069 void update(const string &varname, const string &value); 00070 00072 void print(ostream &out); 00073 }; 00074 00076 // utility functions 00077 00078 string timestamp(); 00079 00082 class EXPORT configuration { 00083 public: 00085 configuration(); 00087 configuration(const configuration &other); 00090 configuration(const char *filename, bool replquotes = false, 00091 bool silent = false, bool resolve = true); 00094 configuration(const string &filename, bool replquotes = false, 00095 bool silent = false, bool resolve = true); 00099 configuration(string_map_t &smap, textlist &txt, string &name, 00100 string &output_dir); 00102 virtual ~configuration(); 00103 00104 // input/output //////////////////////////////////////////////////////////// 00105 00108 bool read(const char *fname, bool resolve = true, bool replquotes = true, 00109 bool silent = false, const char *outdir = NULL); 00111 bool write(const char *fname); 00114 void resolve(bool replquotes = false); 00116 void resolve_bq(); 00117 00118 // accessors /////////////////////////////////////////////////////////////// 00119 00121 const string &get_name(); 00123 void set_name(const string &name); 00125 const string &get_output_dir(); 00127 void set_output_dir(const string &d); 00128 00131 template <typename T> 00132 void get(T &v, const char *varname); 00133 00136 template <typename T> 00137 void get(T &v, const std::string &varname); 00138 00140 void get(intg &v, const char *varname); 00142 void get(uint &v, const char *varname); 00144 void get(double &v, const char *varname); 00146 void get(float &v, const char *varname); 00148 void get(string &v, const char *varname); 00150 void get(bool &v, const char *varname); 00151 00154 const string &get_string(const char *varname); 00155 00158 const string &get_string(const string &varname); 00159 00162 const char *get_cstring(const char *varname); 00163 00168 double get_double(const char *varname); 00169 00172 uint try_get_uint(const char *varname, uint default_val); 00175 int try_get_int(const char *varname, int default_val); 00178 intg try_get_intg(const char *varname, intg default_val); 00181 float try_get_float(const char *varname, float default_val); 00184 double try_get_double(const char *varname, double default_val); 00187 string try_get_string(const char *varname, const char *default_val = ""); 00188 00193 float get_float(const char *varname); 00194 00199 uint get_uint(const char *varname); 00200 00205 int get_int(const char *varname); 00206 00211 intg get_intg(const char *varname); 00212 00217 bool get_bool(const char *varname); 00218 00223 char get_char(const char *varname); 00224 00226 bool exists_bool(const char *varname); 00227 00229 bool exists_true(const char *varname); 00230 00232 bool exists_true(const string &varname); 00233 00235 bool exists_false(const char *varname); 00236 00239 void exists_throw(const char *varname); 00240 00242 void set(const char *varname, const char *value); 00243 00246 const char* get_cstr(const char *varname, bool silent = false); 00248 vector<string> get_all_strings(const string &s); 00249 00251 bool exists(const char *varname); 00253 bool exists(const std::string &varname); 00254 00256 virtual void pretty(); 00258 virtual void pretty_match(const string &s); 00259 00261 protected: 00262 00264 void resolve_variables(string_map_t &m, bool replquotes = false); 00265 00267 string resolve0(string_map_t &m, const string &variable, 00268 const string &v, bool firstonly = false); 00269 00271 string resolve_backquotes(string_map_t &m, 00272 const string &variable, const string &v, 00273 bool firstonly = false); 00274 00276 string resolve_string(string_map_t &m, const string &variable, 00277 const string &v, bool firstonly = false); 00278 00279 // open file fname and put variables assignments in smap. 00280 // e.g. " i = 42 # comment " will yield a entry in smap 00281 // with "i" as first value and "42" as second value. 00282 bool extract_variables(const char *fname, string_map_t &smap, textlist &txt, 00283 string_map_t *meta_smap = NULL, bool bresolve = true, 00284 bool replquotes = false, bool silent = false); 00285 00286 protected: 00287 string_map_t smap; 00288 string_map_t tmp_smap; 00289 string name; 00290 string output_dir; 00291 textlist otxt; 00292 bool silent; 00293 }; 00294 00297 class EXPORT meta_configuration : public configuration { 00298 private: 00299 string_list_map_t lmap; 00300 vector<size_t> conf_indices; 00301 int conf_combinations; 00302 vector<configuration> confs; 00303 00304 public: 00305 meta_configuration(); 00306 virtual ~meta_configuration(); 00307 00310 bool read(const char *fname, bool bresolve = true, 00311 const string *tstamp = NULL, 00312 bool replace_quotes = false, 00313 const char *resume_name = NULL, 00314 bool silent = false); 00315 00316 // accessors 00317 00319 vector<configuration>& configurations(); 00320 00322 virtual void pretty(); 00323 00325 virtual void pretty_combinations(); 00326 }; 00327 00328 } // end namespace ebl 00329 00330 #include "configuration.hpp" 00331 00332 #endif /* CONFIGURATION_H_ */