Permalink
Browse files

Added more convenient interface to InputGroup class which allows defi…

…ning parameters on a single line and specifying defaults.
  • Loading branch information...
emstoudenmire committed Apr 24, 2014
1 parent c45466c commit 58dfc538adf7752d9038454292932354051fee99
Showing with 87 additions and 28 deletions.
  1. +7 −6 sample/dmrg_table.cc
  2. +67 −22 utilities/input.cc
  3. +13 −0 utilities/input.h
View
@@ -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");
View
@@ -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))
View
@@ -13,10 +13,16 @@
#include <string>
//#include "String.h"
#include <cctype>
+#include "math.h"
+#include <cmath>
#define SP << " " <<
typedef double Real;
void error(const std::string& s);
+#ifndef NAN
+#define NAN (std::numeric_limits<Real>::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);

0 comments on commit 58dfc53

Please sign in to comment.