00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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
00127
00128
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 }