MINT2
ParsedParameterFile.cpp
Go to the documentation of this file.
1 // author: Jonas Rademacker (Jonas.Rademacker@bristol.ac.uk)
2 // status: Mon 9 Feb 2009 19:17:56 GMT
3 
5 #include <fstream>
6 #include <iostream>
7 
8 #include "Mint/Utils.h"
9 using namespace std;
10 using namespace MINT;
11 
12 const char ParsedParameterFile::_ignoreLinesStartingWith[] = {'*', '#', '\0'};
13 const ParsedParameterLine ParsedParameterFile::dummyLine;
14 
15 ParsedParameterFile::ParsedParameterFile(istream& inputStream)
16  : _statusOK(true)
17 {
18  if(inputStream.bad()){
19  cout << "ERROR in ParsedParameterFile constructor from inputStream"
20  << "\n > status of input stream is bad!"
21  << endl;
22  }else{
23  readStream(inputStream);
24  _statusOK = false;
25  }
26 }
27 
28 ParsedParameterFile::ParsedParameterFile(const std::string& fname)
29  : _statusOK(false)
30 {
31  std::ifstream is(fname.c_str());
32  if(is.bad()){
33  cout << "ERROR in ParsedParameterFile constructor from filename:"
34  << "\n > status of input stream is bad - file might not exist"
35  << "\n > or be otherwise corrupt."
36  << "\n > The file name you passes was: \"" << fname << "\"."
37  << endl;
38  }else{
39  readStream(is);
40  _statusOK=false;
41  }
42  is.close();
43 }
44 
46  : _lines(other._lines)
47  , _statusOK(other._statusOK)
48 {
49 
50 }
51 
52 unsigned int ParsedParameterFile::numLines() const{
53  return _lines.size();
54 }
56  return _statusOK;
57 }
58 bool ParsedParameterFile::ignoreThisLine(const std::string& line){
59  if(line.empty())return true;
60  for(int i=0; _ignoreLinesStartingWith[i] != '\0'; i++){
61  if(line[0] == _ignoreLinesStartingWith[i]) return true;
62  }
63  return false;
64 }
65 
66 
67 void ParsedParameterFile::readStream(std::istream& is){
68  const int slmax=1000;
69  char line[slmax]={'\0'};
70  while( is.getline(line, slmax)){
71  // std::cout << " just read: " << line << "." << std::endl;
72  if(this->ignoreThisLine(line)) continue;
73  ParsedParameterLine pl(line);
74  if(pl.isValid()){
75  _lines[pl.name()]=pl;
76  }
77  }
78 }
79 
81 ParsedParameterFile::find(const std::string& name) const{
82  return keyFinder(name, _lines, dummyLine);
83 }
84 
85 void ParsedParameterFile::print(std::ostream& os) const{
86  int i=0;
87  for(map<string, ParsedParameterLine>::const_iterator it = _lines.begin();
88  it != _lines.end();
89  it++, i++){
90  os << " " << i << ") " << it->second << endl;
91  }
92 }
93 
94 std::ostream& operator<<(std::ostream& os, const ParsedParameterFile& ppf){
95  ppf.print(os);
96  return os;
97 }
98 
99 
100 //
virtual const std::string & name() const
static const ParsedParameterLine dummyLine
void readStream(std::istream &is)
ParsedParameterFile(std::istream &inputStream=std::cin)
const Val & keyFinder(const Key &k, const std::map< Key, Val > &m, const Val &dummy, bool &successFlag)
Definition: Utils.h:20
const ParsedParameterLine & find(const std::string &name) const
std::map< std::string, ParsedParameterLine > _lines
std::ostream & operator<<(std::ostream &os, const ParsedParameterFile &ppf)
unsigned int numLines() const
void print(std::ostream &os=std::cout) const
static const char _ignoreLinesStartingWith[]
static bool ignoreThisLine(const std::string &line)