00001 /* 00002 * This file is part of libcxxsupport. 00003 * 00004 * libcxxsupport is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation; either version 2 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * libcxxsupport is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with libcxxsupport; if not, write to the Free Software 00016 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00017 */ 00018 00019 /* 00020 * libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik 00021 * and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt 00022 * (DLR). 00023 */ 00024 00025 /*! \file paramfile.h 00026 * Class for parsing parameter files 00027 * 00028 * Copyright (C) 2003-2014 Max-Planck-Society 00029 * Authors: Martin Reinecke 00030 */ 00031 00032 #ifndef PLANCK_PARAMFILE_H 00033 #define PLANCK_PARAMFILE_H 00034 00035 #include <map> 00036 #include <set> 00037 #include <string> 00038 #include "datatypes.h" 00039 #include "string_utils.h" 00040 00041 /*! Class for storing and querying key/value pairs. The name is historical; 00042 the parameters can actually be obtained from othersources as well 00043 (e.g. the command line). */ 00044 class paramfile 00045 { 00046 private: 00047 typedef std::map<std::string,std::string> params_type; 00048 params_type params; 00049 mutable std::set<std::string> read_params; 00050 bool verbose; 00051 00052 std::string get_valstr(const std::string &key) const; 00053 bool param_unread (const std::string &key) const; 00054 void findhelper (const std::string &key, const std::string &value, NDT type, 00055 bool deflt) const; 00056 void setParamString (const std::string &key, const std::string &value); 00057 00058 public: 00059 paramfile() : verbose(true) {} 00060 /*! Constructs a paramfile object from the contents of \a filename. 00061 If \a verbose_==true, diagnostic output is generated when calling 00062 methods on this object, otherwise not. */ 00063 paramfile (const std::string &filename, bool verbose_=true); 00064 /*! Constructs a paramfile object from the contents of \a par. 00065 If \a verbose_==true, diagnostic output is generated when calling 00066 methods on this object, otherwise not. */ 00067 paramfile (const params_type &par, bool verbose_=true); 00068 ~paramfile(); 00069 00070 /*! Allows adjusting the verbosity. */ 00071 void setVerbosity (bool verbose_) 00072 { verbose = verbose_; } 00073 00074 /*! Returns the verbosity setting of the object. */ 00075 bool getVerbosity () const 00076 { return verbose; } 00077 00078 /*! Returns \c true, if a paremeter called \a key is stored in the object, 00079 else \c false. */ 00080 bool param_present(const std::string &key) const; 00081 00082 /*! Returns the value stored for the parameter name \a key, after converting 00083 it to the requested type. If \a key is not present, an exception is 00084 thrown. */ 00085 template<typename T> T find (const std::string &key) const; 00086 /*! Returns the value stored for the parameter name \a key, after converting 00087 it to the requested type. If \a key is not present, \a deflt is returned 00088 instead, and is also entered into the parameter set. */ 00089 template<typename T> T find 00090 (const std::string &key, const T &deflt); 00091 00092 /*! Returns the entire set of currently stored parameters. */ 00093 const params_type &getParams() const 00094 { return params; } 00095 00096 /*! Sets the parameter with the name \a key to \a value. */ 00097 template<typename T> void setParam (const std::string &key, const T &value) 00098 { setParamString(key,dataToString(value)); } 00099 }; 00100 00101 /*! Tries to build a \a paramfile object from the contents of a command line. 00102 If \a argc==2 and \a argv[1] does not contain the character "=", the 00103 function tries to input parameters from the file \a argv[1]. Otherwise 00104 the function interprets each command line argument as a "key=value" 00105 statement. */ 00106 paramfile getParamsFromCmdline (int argc, const char **argv, 00107 bool verbose=true); 00108 00109 #endif