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

cxxutils.cc

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  *  This file contains the implementation of various convenience functions
00029  *  used by the Planck LevelS package.
00030  *
00031  *  Copyright (C) 2002, 2003, 2004, 2005 Max-Planck-Society
00032  *  Authors: Martin Reinecke, Reinhard Hell
00033  */
00034 
00035 // if we are using g++, check for version 3.0 or higher
00036 #ifdef __GNUC__
00037 #if (__GNUC__<3)
00038 #error your C++ compiler is too old. g++ version 3.0 or higher is required.
00039 #endif
00040 #endif
00041 
00042 #include <fstream>
00043 #include <sstream>
00044 #include <iostream>
00045 #include <iomanip>
00046 #include <string>
00047 #include <cstdio>
00048 #include "cxxutils.h"
00049 
00050 using namespace std;
00051 
00052 bool file_present (const string &filename)
00053   {
00054   ifstream dummy(filename.c_str());
00055   return dummy;
00056   }
00057 
00058 void assert_present (const string &filename)
00059   {
00060   if (file_present(filename)) return;
00061   throw Message_error ("Error: file " + filename + " does not exist!");
00062   }
00063 
00064 void assert_not_present (const string &filename)
00065   {
00066   if (!file_present(filename)) return;
00067   throw Message_error ("Error: file " + filename + " already exists!");
00068   }
00069 
00070 void remove_file (const string &filename)
00071   {
00072   remove (filename.c_str());
00073   }
00074 
00075 string trim (const string &orig)
00076   {
00077   string::size_type p1=orig.find_first_not_of(" \t");
00078   if (p1==string::npos) return "";
00079   string::size_type p2=orig.find_last_not_of(" \t");
00080   return orig.substr(p1,p2-p1+1);
00081   }
00082 
00083 template<typename T> string dataToString (const T &x)
00084   {
00085   ostringstream strstrm;
00086   strstrm << x;
00087   return trim(strstrm.str());
00088   }
00089 
00090 template<> string dataToString (const bool &x)
00091   { return x ? "T" : "F"; }
00092 template<> string dataToString (const string &x)
00093   { return trim(x); }
00094 template<> string dataToString (const float &x)
00095   {
00096   ostringstream strstrm;
00097   strstrm << setprecision(8) << x;
00098   return trim(strstrm.str());
00099   }
00100 template<> string dataToString (const double &x)
00101   {
00102   ostringstream strstrm;
00103   strstrm << setprecision(16) << x;
00104   return trim(strstrm.str());
00105   }
00106 
00107 template string dataToString (const int &x);
00108 template string dataToString (const unsigned int &x);
00109 template string dataToString (const long &x);
00110 template string dataToString (const unsigned long long &x);
00111 template string dataToString (const long long &x);
00112 template string dataToString (const unsigned long &x);
00113 
00114 string intToString(int x, int width)
00115   {
00116   ostringstream strstrm;
00117   strstrm << setw(width) << setfill('0') << x;
00118   return trim(strstrm.str());
00119   }
00120 
00121 template<typename T> void stringToData (const string &x, T &value)
00122   {
00123   istringstream strstrm(x);
00124   strstrm >> value;
00125   planck_assert(strstrm, "conversion error in stringToData()");
00126 // FIXME: disable the test below for now, since some compilers choke on it
00127 //  planck_assert(strstrm.tellg()==streampos(x.length()),
00128 //    "parse error in stringToData()");
00129   }
00130 
00131 template<> void stringToData (const string &x, string &value)
00132   { value = trim(x); }
00133 
00134 template<> void stringToData (const string &x, bool &value)
00135   {
00136   if ( x=="F" || x=="f" || x=="n" || x=="N" || x=="false" || x==".false."
00137        || x=="FALSE" || x==".FALSE.")
00138     value=false;
00139   else if (x=="T" || x=="t" || x=="y" || x=="Y" || x=="true" || x==".true."
00140        || x=="TRUE" || x==".TRUE.")
00141     value=true;
00142   else throw Message_error ("stringToData<bool>: error parsing argument");
00143   }
00144 
00145 template void stringToData (const string &x, int &value);
00146 template void stringToData (const string &x, long &value);
00147 template void stringToData (const string &x, float &value);
00148 template void stringToData (const string &x, double &value);
00149 template void stringToData (const string &x, unsigned long long &value);
00150 template void stringToData (const string &x, long long &value);
00151 template void stringToData (const string &x, unsigned long &value);
00152 template void stringToData (const string &x, unsigned int &value);
00153 
00154 void announce_progress (int now, int total)
00155   {
00156   if ((now%(max(total/100,1)))==0)
00157     cout << "\r " << setw(3) << planck_nint ((now*100.)/total)
00158          << "% done\r" << flush;
00159   }
00160 
00161 void announce_progress (double now, double last, double total)
00162   {
00163   int lastpercent = int((last/total)*100),
00164       nowpercent  = int(( now/total)*100);
00165   if (nowpercent>lastpercent)
00166     cout << "\r " << setw(3) << nowpercent << "% done\r" << flush;
00167   }
00168 
00169 void announce (const string &name)
00170   {
00171   cout << endl << "+-";
00172   for (unsigned int m=0; m<name.length(); ++m) cout << "-";
00173   cout << "-+" << endl;
00174   cout << "| " << name << " |" << endl;
00175   cout << "+-";
00176   for (unsigned int m=0; m<name.length(); ++m) cout << "-";
00177   cout << "-+" << endl;
00178   }
00179 
00180 void module_startup (const std::string &name, int argc, const char **,
00181   int argc_expected, const std::string &argv_expected)
00182   {
00183   announce (name);
00184   if (argc==argc_expected) return;
00185   cerr << "Usage: " << name << " " << argv_expected << endl;
00186   throw Message_error();
00187   }
00188 
00189 void parse_file (const string &filename, map<string,string> &dict)
00190   {
00191   string line;
00192   int lineno=0;
00193   dict.clear();
00194   ifstream inp(filename.c_str());
00195   planck_assert (inp,"Could not open parameter file "+filename);
00196   while (inp)
00197     {
00198     getline(inp, line);
00199     ++lineno;
00200     line=line.substr(0,line.find_first_of("#"));
00201     line=trim(line);
00202     if (line.size()>0)
00203       {
00204       string::size_type eqpos=line.find("=");
00205       if (eqpos!=string::npos)
00206         {
00207         string key=trim(line.substr(0,eqpos)),
00208                value=trim(line.substr(eqpos+1,string::npos));
00209         if (key=="")
00210           cerr << "Warning: empty key in " << filename << ", line "
00211                << lineno << endl;
00212         else
00213           {
00214           if (dict.find(key)!=dict.end())
00215             cerr << "Warning: key " << key << " multiply defined in "
00216                  << filename << ", line " << lineno << endl;
00217           dict[key]=value;
00218           }
00219         }
00220       else
00221         cerr << "Warning: unrecognized format in " << filename << ", line "
00222              << lineno << ":\n" << line << endl;
00223       }
00224     }
00225   }

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