CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | Friends
IO Class Reference

General option file parser. More...

#include <IO.h>

Public Member Functions

void Dump (std::ostream &out=std::cout) const
 dumps fAllLines More...
 
template<class T >
bool GetAllOpt (const char *tag, const char *key, std::vector< T > &values)
 reads a vector of T More...
 
template<class T >
bool GetAllOpt (const char *tag, const char *key, T &value)
 reads a T More...
 
std::string GetLineData (const char *tag, const char *key) const
 
std::string GetNextLineData (const char *tag, const char *key)
 
template<class T >
bool GetOpt (const char *tag, const char *key, std::vector< T > &values) const
 reads a vector of T More...
 
template<class T >
bool GetOpt (const char *tag, const char *key, T &value) const
 reads a T More...
 
bool GetOpt (const char *tag, const char *key, std::string &value) const
 reads a string More...
 
 IO (const char *filepattern)
 builds IO from files matching filepattern More...
 
bool IsZombie () const
 true if constructor went wrong More...
 
 ~IO ()
 

Static Public Attributes

static const unsigned sLinesize = 1000
 maximum line size More...
 

Private Member Functions

bool ParseFile (const char *filename)
 parse one file More...
 

Private Attributes

std::vector< std::pair
< std::string, std::string > > 
fAllLines
 all non empty, uncommented lines More...
 
std::string fCurkey
 current key More...
 
int fCurline
 counter More...
 
std::string fCurtag
 current tag More...
 

Friends

std::ostream & operator<< (std::ostream &out, IO &io)
 dumps fAllLines More...
 

Detailed Description

General option file parser.

* IO io("myfile.opt");
* fECALthresh = 0.05;
* io.GetOpt("ECAL", "threshold", fECALthresh);
*
Author
Colin Bernet
Date
January 2002

Definition at line 28 of file IO.h.

Constructor & Destructor Documentation

IO::IO ( const char *  filepattern)

builds IO from files matching filepattern

Definition at line 9 of file IO.cc.

References gather_cfg::cout, linker::files, Utils::Glob(), i, and ParseFile().

9  : fCurline(0) {
10  // loop on all files matching pattern, store each non empty line in
11  // fAllLines for efficient searches later.
12  cout<<endl;
13  cout<<"------ Reading User Parameters : "<<filepattern<<endl;
14 
15  vector<string> files = Utils::Glob(filepattern);
16 
17  if( files.empty() ) {
18  string err = "IO::IO : no files verify pattern ";
19  err += filepattern;
20  throw err;
21  }
22 
23  for(unsigned i=0; i<files.size(); i++) {
24  ParseFile(files[i].c_str());
25  }
26  cout<<"------ Reading User Parameters : DONE ---------"<<endl;
27  cout<<endl;
28 
29 }
int i
Definition: DBlmapReader.cc:9
static std::vector< std::string > Glob(const char *pattern)
get all files matching pattern
Definition: Utils.cc:46
int fCurline
counter
Definition: IO.h:37
tuple files
Definition: linker.py:146
bool ParseFile(const char *filename)
parse one file
Definition: IO.cc:31
tuple cout
Definition: gather_cfg.py:41
IO::~IO ( )
inline

Definition at line 52 of file IO.h.

References fAllLines.

52 {fAllLines.clear();}
std::vector< std::pair< std::string, std::string > > fAllLines
all non empty, uncommented lines
Definition: IO.h:31

Member Function Documentation

void IO::Dump ( std::ostream &  out = std::cout) const

dumps fAllLines

Definition at line 76 of file IO.cc.

References fAllLines, and i.

Referenced by operator<<().

76  {
77  for (unsigned i=0; i<fAllLines.size(); i++) {
78  out<<fAllLines[i].first<< "\t" << fAllLines[i].second << endl;
79  }
80 }
int i
Definition: DBlmapReader.cc:9
std::vector< std::pair< std::string, std::string > > fAllLines
all non empty, uncommented lines
Definition: IO.h:31
tuple out
Definition: dbtoconf.py:99
template<class T >
bool IO::GetAllOpt ( const char *  tag,
const char *  key,
std::vector< T > &  values 
)

reads a vector of T

this function allows to read several lines with the same tag/key:

* while ( GetAllOpt(tag, key, values) )
* // do something...
*

Definition at line 131 of file IO.h.

131  {
132  return false;
133 }
template<class T >
bool IO::GetAllOpt ( const char *  tag,
const char *  key,
T &  value 
)

reads a T

this function allows to read several lines with the same tag/key:

* while ( GetAllOpt(tag, key, value) )
* // do something...
*

Definition at line 136 of file IO.h.

References runTheMatrix::data, GetNextLineData(), recoMuon::in, and relativeConstraints::value.

136  {
137  std::string data = GetNextLineData(tag, key);
138  if(data.empty()) return false;
139 
140  std::istringstream in(data.c_str());
141  in>>value;
142  if(in) {
143  return true;
144  }
145  else {
146  return false;
147  }
148 }
std::string GetNextLineData(const char *tag, const char *key)
Definition: IO.cc:114
list key
Definition: combine.py:13
string IO::GetLineData ( const char *  tag,
const char *  key 
) const

Definition at line 89 of file IO.cc.

References runTheMatrix::data, fAllLines, first, newFWLiteAna::found, i, recoMuon::in, edm::second(), sLinesize, and GlobalPosition_Frontier_DevDB_cff::tag.

Referenced by GetOpt().

89  {
90  // if tag matches several option lines, data is the data corresponding
91  // to the last tag
92 
93  char data[sLinesize];
94  bool found = false;
95  for(unsigned i=0; i<fAllLines.size(); i++) {
96  if( !fnmatch(fAllLines[i].first.c_str(), tag, 0) ) {
97  istringstream in(fAllLines[i].second.c_str());
98  string readkey; in>>readkey;
99 
100  if(readkey == key) {
101  // data.erase();
102  // string skey = key;
103  // int start = pos+skey.size();
104  // data.assign(fAllLines[i].second, start, data.size()-start);
105  found=true;
106  in.get(data,sLinesize);
107  }
108  }
109  }
110  if(found) return string(data);
111  else return string();
112 }
int i
Definition: DBlmapReader.cc:9
static const unsigned sLinesize
maximum line size
Definition: IO.h:48
U second(std::pair< T, U > const &p)
bool first
Definition: L1TdeRCT.cc:79
std::vector< std::pair< std::string, std::string > > fAllLines
all non empty, uncommented lines
Definition: IO.h:31
list key
Definition: combine.py:13
string IO::GetNextLineData ( const char *  tag,
const char *  key 
)

Definition at line 114 of file IO.cc.

References runTheMatrix::data, fAllLines, fCurkey, fCurline, fCurtag, first, newFWLiteAna::found, i, recoMuon::in, combine::key, edm::second(), sLinesize, and GlobalPosition_Frontier_DevDB_cff::tag.

Referenced by GetAllOpt().

114  {
115 
116  if(fCurtag != tag || fCurkey != key) {
117  // not the same request
118  fCurline = 0;
119  fCurtag = tag;
120  fCurkey = key;
121  }
122  // cout<<fCurline<<" "<<fCurtag<<" "<<fCurkey<<endl;
123 
124  char data[sLinesize];
125  bool found = false;
126  for(unsigned i=fCurline; i<fAllLines.size(); i++) {
127  if( !fnmatch(fAllLines[i].first.c_str(), tag, 0) ) {
128  istringstream in(fAllLines[i].second.c_str());
129  string readkey; in>>readkey;
130 
131  if(readkey == key) {
132  found=true;
133  in.get(data,sLinesize);
134  fCurline=i+1;
135  break;
136  }
137  }
138  }
139  if(found) return string(data);
140  else return string();
141 }
int i
Definition: DBlmapReader.cc:9
static const unsigned sLinesize
maximum line size
Definition: IO.h:48
int fCurline
counter
Definition: IO.h:37
U second(std::pair< T, U > const &p)
bool first
Definition: L1TdeRCT.cc:79
std::vector< std::pair< std::string, std::string > > fAllLines
all non empty, uncommented lines
Definition: IO.h:31
std::string fCurtag
current tag
Definition: IO.h:43
std::string fCurkey
current key
Definition: IO.h:40
list key
Definition: combine.py:13
template<class T >
bool IO::GetOpt ( const char *  tag,
const char *  key,
std::vector< T > &  values 
) const

reads a vector of T

Definition at line 106 of file IO.h.

References runTheMatrix::data, GetLineData(), recoMuon::in, and tmp.

Referenced by PFRootEventManager::connect(), DisplayManager::readOptions(), PFRootEventManager::readOptions(), and PFRootEventManagerColin::readSpecificOptions().

106  {
107  std::string data = GetLineData(tag,key);
108 
109  std::istringstream in(data.c_str());
110  while(1) {
111  T tmp;
112  in>>tmp;
113  if(!in) break;
114  values.push_back(tmp);
115  }
116 
117  return true;
118 }
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
std::string GetLineData(const char *tag, const char *key) const
Definition: IO.cc:89
list key
Definition: combine.py:13
template<class T >
bool IO::GetOpt ( const char *  tag,
const char *  key,
T &  value 
) const

reads a T

Definition at line 121 of file IO.h.

References runTheMatrix::data, GetLineData(), recoMuon::in, and relativeConstraints::value.

121  {
122  std::string data = GetLineData(tag,key);
123 
124  std::istringstream in(data.c_str());
125  in>>value;
126  if(in.good()) return true;
127  else return false;
128 }
std::string GetLineData(const char *tag, const char *key) const
Definition: IO.cc:89
list key
Definition: combine.py:13
bool IO::GetOpt ( const char *  tag,
const char *  key,
std::string &  value 
) const

reads a string

bool IO::IsZombie ( ) const
inline

true if constructor went wrong

Definition at line 61 of file IO.h.

References fAllLines.

61 {return !fAllLines.size();}
std::vector< std::pair< std::string, std::string > > fAllLines
all non empty, uncommented lines
Definition: IO.h:31
bool IO::ParseFile ( const char *  filename)
private

parse one file

Definition at line 31 of file IO.cc.

References gather_cfg::cout, runTheMatrix::data, relativeConstraints::empty, fAllLines, recoMuon::in, pos, asciidump::s, sLinesize, and GlobalPosition_Frontier_DevDB_cff::tag.

Referenced by IO().

31  {
32  cout<<"file : "<<filename<<"\t\t";
33 
34  ifstream in(filename);
35  if( !in.good() ) {
36  cout<<"unreadable"<<endl;
37  return false;
38  }
39 
40  char data[sLinesize];
41  char s[sLinesize];
42  int pos=0;
43 
44  do {
45  in.seekg(pos);
46  in.getline(s,sLinesize);
47 
48  pos = in.tellg();
49 
50  if(string(s).empty()) {
51  continue; // remove empty lines
52  }
53 
54  istringstream lin(s);
55 
56  string tag;
57  lin>>tag;
58 
59  if(!strncmp(tag.c_str(),"//",2)) continue; // remove commented lines can be done better ...
60 
61  lin.get(data,sLinesize);
62 
63  fAllLines.push_back(pair<string, string>(tag, data));
64  } while(in.good());
65 
66  if(in.eof()) {
67  cout<<"ok"<<endl;
68  return true;
69  }
70  else {
71  cout<<"error"<<endl;
72  return false;
73  }
74 }
static const unsigned sLinesize
maximum line size
Definition: IO.h:48
std::vector< std::pair< std::string, std::string > > fAllLines
all non empty, uncommented lines
Definition: IO.h:31
tuple filename
Definition: lut2db_cfg.py:20
tuple cout
Definition: gather_cfg.py:41
string s
Definition: asciidump.py:422

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  out,
IO io 
)
friend

dumps fAllLines

Member Data Documentation

std::vector< std::pair< std::string, std::string> > IO::fAllLines
private

all non empty, uncommented lines

Definition at line 31 of file IO.h.

Referenced by Dump(), GetLineData(), GetNextLineData(), IsZombie(), ParseFile(), and ~IO().

std::string IO::fCurkey
private

current key

Definition at line 40 of file IO.h.

Referenced by GetNextLineData().

int IO::fCurline
private

counter

Definition at line 37 of file IO.h.

Referenced by GetNextLineData().

std::string IO::fCurtag
private

current tag

Definition at line 43 of file IO.h.

Referenced by GetNextLineData().

const unsigned IO::sLinesize = 1000
static

maximum line size

Definition at line 48 of file IO.h.

Referenced by GetLineData(), GetNextLineData(), and ParseFile().