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
pftools::IO Class Reference

#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

Definition at line 29 of file IO.h.

Constructor & Destructor Documentation

IO::IO ( const char *  filepattern)

builds IO from files matching filepattern

Definition at line 10 of file IO.cc.

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

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

Definition at line 53 of file IO.h.

References fAllLines.

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

Member Function Documentation

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

dumps fAllLines

Definition at line 77 of file IO.cc.

References fAllLines, and i.

Referenced by operator<<().

77  {
78  for (unsigned i=0; i<fAllLines.size(); i++) {
79  out<<fAllLines[i].first<< "\t" << fAllLines[i].second << endl;
80  }
81 }
int i
Definition: DBlmapReader.cc:9
std::vector< std::pair< std::string, std::string > > fAllLines
all non empty, uncommented lines
Definition: IO.h:32
tuple out
Definition: dbtoconf.py:99
template<class T >
bool pftools::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 133 of file IO.h.

133  {
134  return false;
135 }
template<class T >
bool pftools::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 138 of file IO.h.

References data, recoMuon::in, AlCaHLTBitMon_QueryRunRegistry::string, and relativeConstraints::value.

138  {
140  if(data.empty()) return false;
141 
142  std::istringstream in(data.c_str());
143  in>>value;
144  if(in) {
145  return true;
146  }
147  else {
148  return false;
149  }
150 }
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
std::string GetNextLineData(const char *tag, const char *key)
Definition: IO.cc:115
string IO::GetLineData ( const char *  tag,
const char *  key 
) const

Definition at line 90 of file IO.cc.

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

Referenced by GetOpt().

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

Definition at line 115 of file IO.cc.

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

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

reads a vector of T

Definition at line 108 of file IO.h.

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

Referenced by pftools::CalibCompare::CalibCompare(), pftools::Exercises3::calibrateCalibratables(), pftools::Exercises3::evaluateCalibrator(), pftools::Exercises3::evaluateSpaceManager(), and pftools::Exercises3::Exercises3().

108  {
110 
111  std::istringstream in(data.c_str());
112  while(1) {
113  T tmp;
114  in>>tmp;
115  if(!in) break;
116  values.push_back(tmp);
117  }
118 
119  return true;
120 }
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
std::string GetLineData(const char *tag, const char *key) const
Definition: IO.cc:90
long double T
template<class T >
bool pftools::IO::GetOpt ( const char *  tag,
const char *  key,
T value 
) const

reads a T

Definition at line 123 of file IO.h.

References data, recoMuon::in, AlCaHLTBitMon_QueryRunRegistry::string, and relativeConstraints::value.

123  {
125 
126  std::istringstream in(data.c_str());
127  in>>value;
128  if(in.good()) return true;
129  else return false;
130 }
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
std::string GetLineData(const char *tag, const char *key) const
Definition: IO.cc:90
bool pftools::IO::GetOpt ( const char *  tag,
const char *  key,
std::string &  value 
) const

reads a string

bool pftools::IO::IsZombie ( ) const
inline

true if constructor went wrong

Definition at line 62 of file IO.h.

References fAllLines.

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

parse one file

Definition at line 32 of file IO.cc.

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

Referenced by IO().

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

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> > pftools::IO::fAllLines
private

all non empty, uncommented lines

Definition at line 32 of file IO.h.

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

std::string pftools::IO::fCurkey
private

current key

Definition at line 41 of file IO.h.

Referenced by GetNextLineData().

int pftools::IO::fCurline
private

counter

Definition at line 38 of file IO.h.

Referenced by GetNextLineData().

std::string pftools::IO::fCurtag
private

current tag

Definition at line 44 of file IO.h.

Referenced by GetNextLineData().

const unsigned IO::sLinesize = 1000
static

maximum line size

Definition at line 49 of file IO.h.

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