Main Page | Modules | Alphabetical List | Class List | File List | Class Members | File Members

paramfile.h

00001 /*
00002  *  This file is part of Healpix_cxx.
00003  *
00004  *  Healpix_cxx 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  *  Healpix_cxx 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 Healpix_cxx; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017  *
00018  *  For more information about HEALPix, see http://healpix.jpl.nasa.gov
00019  */
00020 
00021 /*
00022  *  Healpix_cxx is being developed at the Max-Planck-Institut fuer Astrophysik
00023  *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
00024  *  (DLR).
00025  */
00026 
00027 /*
00028  *  Class for parsing parameter files
00029  *
00030  *  Copyright (C) 2003, 2004, 2005 Max-Planck-Society
00031  *  Authors: Martin Reinecke, Reinhard Hell
00032  */
00033 
00034 #ifndef PLANCK_PARAMFILE_H
00035 #define PLANCK_PARAMFILE_H
00036 
00037 #include <map>
00038 #include <string>
00039 #include <iostream>
00040 #include "simparams.h"
00041 #include "cxxutils.h"
00042 
00043 class paramfile
00044   {
00045   private:
00046     std::map<std::string,std::string> params;
00047     bool verbose;
00048 
00049     std::string get_valstr(const std::string &key) const
00050       {
00051       std::map<std::string,std::string>::const_iterator loc=params.find(key);
00052       if (loc!=params.end()) return loc->second;
00053       throw Message_error ("Error: Cannot find the key \"" + key + "\".");
00054       }
00055 
00056   public:
00057     paramfile (const std::string &filename, bool verbose_=true)
00058       : verbose(verbose_)
00059       { parse_file (filename, params); }
00060 
00061     paramfile (const std::map<std::string,std::string> &par)
00062       : params (par), verbose(true)
00063       {}
00064 
00065     bool param_present(const std::string &key) const
00066       { return (params.find(key)!=params.end()); }
00067 
00068     template<typename T> T find (const std::string &key) const
00069       {
00070       T result;
00071       stringToData(get_valstr(key),result);
00072       if (verbose)
00073         std::cout << "Parser: " << key << " = " << dataToString(result)
00074                   << std::endl;
00075       return result;
00076       }
00077     template<typename T> T find
00078       (const std::string &key, const T &deflt)
00079       {
00080       if (param_present(key)) return find<T>(key);
00081       if (verbose)
00082         std::cout << "Parser: " << key << " = " << dataToString(deflt)
00083                   << " <default>" << std::endl;
00084       params[key]=dataToString(deflt);
00085       return deflt;
00086       }
00087 
00088     template<typename T> void findParam
00089       (const std::string &key, T &value) const
00090       { value = find<T>(key); }
00091 
00092     template<typename T> void findHeaderParam(const std::string& key,
00093       T& value, simparams& headerParams, const std::string& headerKey,
00094       const std::string& headerComment) const
00095       {
00096       findParam(key, value);
00097       headerParams.add(key, headerKey, dataToString(value), headerComment);
00098       }
00099     void findSourceParam(const std::string& key, std::string& value,
00100       simparams& headerParams) const
00101       {
00102       findParam(key, value);
00103       headerParams.add_source_file(value);
00104       }
00105   };
00106 
00107 #endif

Generated on Fri Jul 8 09:37:14 2005 for LevelS C++ support library