From 58dfc538adf7752d9038454292932354051fee99 Mon Sep 17 00:00:00 2001 From: Miles Stoudenmire Date: Thu, 24 Apr 2014 13:54:27 -0400 Subject: [PATCH] Added more convenient interface to InputGroup class which allows defining parameters on a single line and specifying defaults. --- sample/dmrg_table.cc | 13 ++++---- utilities/input.cc | 89 +++++++++++++++++++++++++++++++++++++++------------- utilities/input.h | 13 ++++++++ 3 files changed, 87 insertions(+), 28 deletions(-) diff --git a/sample/dmrg_table.cc b/sample/dmrg_table.cc index b62eaa43..cc5c89b0 100644 --- a/sample/dmrg_table.cc +++ b/sample/dmrg_table.cc @@ -27,14 +27,15 @@ int main(int argc, char* argv[]) InputGroup basic(infile,"basic"); //Read in individual parameters from the input file - int N = 0; - basic.GetIntM("N",N); //the 'M' stands for mandatory - int nsweeps = 0; - basic.GetIntM("nsweeps",nsweeps); - int quiet = 1; - basic.GetYesNo("quiet",quiet); + const int N = basic.getInt("N"); + const int nsweeps = basic.getInt("nsweeps"); + //second argument to getXXX methods is a default + //in case parameter not provided in input file + const bool quiet = basic.getYesNo("quiet",true); + // // Read the sweeps parameters from a table. + // //Read in the sweeps table itself InputGroup table(basic,"sweeps"); diff --git a/utilities/input.cc b/utilities/input.cc index d72534a2..217ba755 100644 --- a/utilities/input.cc +++ b/utilities/input.cc @@ -277,35 +277,44 @@ int InputGroup::GetYesNo(string s, int& yes,const char* c) { string res; if(!GotoToken(s) || !(infile.file >> res)) - { - if(!quiet) - { - cout << "Couldnt get " << name << "." << s; - if(c) cout << ": " << c; - cout << endl; - } - return 0; - } + { + if(!quiet) + { + cout << "Couldnt get " << name << "." << s; + if(c) cout << ": " << c; + cout << endl; + } + return 0; + } if(!quiet) - { - cout << "Got " << name << "." << s << " = " << res; - if(c) cout << ": " << c; - cout << endl; - } + { + cout << "Got " << name << "." << s << " = " << res; + if(c) cout << ": " << c; + cout << endl; + } transform(res.begin(),res.end(),res.begin(),mydolower); if(res == "yes" || res == "y") - { - yes = 1; - return 1; - } + { + yes = 1; + return 1; + } if(res == "no" || res == "n") - { - yes = 0; - return 1; - } + { + yes = 0; + return 1; + } return 0; } +int InputGroup:: +GetYesNo(string s, bool& yes,const char* c) + { + int resi = 0; + int got = GetYesNo(s,resi,c); + yes = (resi==1); + return got; + } + void InputGroup::SkipLine() { char c = '\0'; @@ -314,6 +323,42 @@ void InputGroup::SkipLine() eatwhite(infile.file); } +int InputGroup:: +getInt(std::string s, int def, const char* c) + { + int res = 0; + int got = GetInt(s,res,c); + if(!got) return def; + return res; + } + +Real InputGroup:: +getReal(std::string s, Real def, const char* c) + { + Real res = 0; + int got = GetReal(s,res,c); + if(!got) return def; + return res; + } + +std::string InputGroup:: +getString(std::string s, std::string def, const char* c) + { + std::string res; + int got = GetString(s,res,c); + if(!got) return def; + return res; + } + +bool InputGroup:: +getYesNo(std::string s, bool def, const char* c) + { + bool res = false; + int got = GetYesNo(s,res,c); + if(!got) return def; + return res; + } + void InputGroup::GetIntM(string s, int& res,const char* c) { if(!GetInt(s,res,c)) diff --git a/utilities/input.h b/utilities/input.h index 431a373a..8298a042 100644 --- a/utilities/input.h +++ b/utilities/input.h @@ -13,10 +13,16 @@ #include //#include "String.h" #include +#include "math.h" +#include #define SP << " " << typedef double Real; void error(const std::string& s); +#ifndef NAN +#define NAN (std::numeric_limits::quiet_NaN()) +#endif + class InputFile { public: @@ -63,6 +69,13 @@ class InputGroup int GetReal(std::string s, Real& r,const char* c = 0); int GetString(std::string s, std::string& t,const char* c = 0); int GetYesNo(std::string s, int& yes,const char* c = 0); // understands yes/no + int GetYesNo(std::string s, bool& yes,const char* c = 0); // understands yes/no + + //These versions return their value + int getInt(std::string s, int def = 0, const char* c = 0); + Real getReal(std::string s, Real def = NAN, const char* c = 0); + std::string getString(std::string s, std::string def = "", const char* c = 0); + bool getYesNo(std::string s, bool def = false, const char* c = 0); // The following are mandatory versions; if they doesn't get it, we quit void GetIntM(std::string s, int& i,const char* c = 0);