JDFTx  1.2.0
ParamList.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------
2 Copyright 2011 Ravishankar Sundararaman
3 
4 This file is part of JDFTx.
5 
6 JDFTx is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 JDFTx is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with JDFTx. If not, see <http://www.gnu.org/licenses/>.
18 -------------------------------------------------------------------*/
19 
20 #ifndef JDFTX_COMMAND_PARAMLIST_H
21 #define JDFTX_COMMAND_PARAMLIST_H
22 
23 #include <core/Util.h>
24 
29 class ParamList
31 {
32  istringstream iss;
33 public:
35  ParamList(string params) : iss(params) {}
36 
38  void rewind() { iss.seekg(0, std::ios_base::beg); }
39 
47  template<typename T>
48  void get(T& t, T tDefault, string paramName, bool required=false)
49  { iss.clear(); //clear previous errors
50  iss >> t;
51  if(iss.bad()) throw string("I/O error while reading parameter <"+paramName+">.");
52  if(iss.eof())
53  { t = tDefault;
54  if(required) throw string("Parameter <"+paramName+"> must be specified.");
55  else return;
56  }
57  if(iss.fail()) { t = tDefault; throw string("Conversion of parameter <"+paramName+"> failed."); }
58  }
59 
67  template<typename T>
68  void get(T& t, T tDefault, const EnumStringMap<T>& tMap, string paramName, bool required=false)
69  { iss.clear(); //clear previous errors
70  string key;
71  iss >> key;
72  if(iss.bad()) throw string("I/O error while reading parameter <"+paramName+">.");
73  if(iss.eof())
74  { t = tDefault;
75  if(required) throw string("Parameter <"+paramName+"> must be specified.");
76  else return;
77  }
78  if(!tMap.getEnum(key.c_str(), t))
79  { t = tDefault;
80  throw string("Parameter <"+paramName+"> must be one of "+tMap.optionList());
81  }
82  }
83 
85  string getRemainder()
86  { if(iss.eof()) return string();
87  size_t curPos = iss.tellg();
88  iss.seekg(0, std::ios_base::end);
89  size_t endPos = iss.tellg();
90  if(endPos > curPos)
91  { string buf(endPos-curPos, 0);
92  iss.seekg(curPos);
93  iss.read(&buf.at(0), buf.length());
94  trim(buf);
95  return buf;
96  }
97  else return string();
98  }
99 };
100 
101 #endif //JDFTX_COMMAND_PARAMLIST_H
A template to ease option parsing (maps enums <–> strings)
Definition: Util.h:179
Definition: string.h:77
void rewind()
Rewind to beginning of stream (useful for commands with multiple alternate syntaxes) ...
Definition: ParamList.h:38
string getRemainder()
Get the section of the input string not yet parsed.
Definition: ParamList.h:85
Miscellaneous utilities.
void trim(string &s)
Remove leading and trailing spaces from a string.
Definition: string.h:45
Wrapper to std::istringstream that eases parsing of input file command lines.
Definition: ParamList.h:30
ParamList(string params)
Construct given the string with all the parameters.
Definition: ParamList.h:35
std::basic_string< char, ichar_traits > string
Case-insensitive string.
Definition: string.h:42