CMS 3D CMS Logo

Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | Friends

IO Class Reference

General option file parser. More...

#include <IO.h>

List of all members.

Public Member Functions

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

Static Public Attributes

static const unsigned sLinesize = 1000
 maximum line size

Private Member Functions

bool ParseFile (const char *filename)
 parse one file

Private Attributes

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

Friends

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

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, electronDbsDiscovery::files, Utils::Glob(), i, and ParseFile().

                              :  fCurline(0) {
  // loop on all files matching pattern, store each non empty line in 
  // fAllLines for efficient searches later.
  cout<<endl;
  cout<<"------ Reading User Parameters : "<<filepattern<<endl;

  vector<string> files = Utils::Glob(filepattern);
  
  if( files.empty() ) {
    string err = "IO::IO : no files verify pattern ";
    err += filepattern;
    throw err;
  }

  for(unsigned i=0; i<files.size(); i++) {
    ParseFile(files[i].c_str());
  }
  cout<<"------ Reading User Parameters : DONE ---------"<<endl;
  cout<<endl;

}
IO::~IO ( ) [inline]

Definition at line 52 of file IO.h.

References fAllLines.

{fAllLines.clear();}

Member Function Documentation

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

dumps fAllLines

Referenced by operator<<().

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.

                                                             {
  std::string data = GetNextLineData(tag, key);
  if(data.empty()) return false;
  
  std::istringstream in(data.c_str());  
  in>>value;
  if(in) {
    return true;
  }
  else {
    return false;  
  }
}
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.

                                                                           {
  return false;
} 
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().

                                                             {
  // if tag matches several option lines, data is the data corresponding
  // to the last tag

  char data[sLinesize];
  bool found = false;
  for(unsigned i=0; i<fAllLines.size(); i++) {
    if( !fnmatch(fAllLines[i].first.c_str(), tag, 0) ) { 
      istringstream in(fAllLines[i].second.c_str());
      string readkey; in>>readkey;
      
      if(readkey == key) {
        //      data.erase();
        //      string skey = key;
        //      int start = pos+skey.size();
        //      data.assign(fAllLines[i].second, start, data.size()-start);
        found=true;
        in.get(data,sLinesize);
      }
    }
  }
  if(found) return string(data);
  else return string();
}
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().

                                                            {

  if(fCurtag != tag || fCurkey != key) {
    // not the same request
    fCurline = 0;
    fCurtag = tag;
    fCurkey = key;
  }
  // cout<<fCurline<<" "<<fCurtag<<" "<<fCurkey<<endl;

  char data[sLinesize];
  bool found = false;
  for(unsigned i=fCurline; i<fAllLines.size(); i++) {
    if( !fnmatch(fAllLines[i].first.c_str(), tag, 0) ) { 
      istringstream in(fAllLines[i].second.c_str());
      string readkey; in>>readkey;
      
      if(readkey == key) {
        found=true;
        in.get(data,sLinesize);
        fCurline=i+1;
        break;
      }
    }
  }
  if(found) return string(data);
  else return string();
}
bool IO::GetOpt ( const char *  tag,
const char *  key,
std::string &  value 
) const

reads a string

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.

                                                                {
  std::string data = GetLineData(tag,key);
  
  std::istringstream in(data.c_str());  
  in>>value;
  if(in.good()) return true;
  else return false;
}
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(), PFRootEventManager::readOptions(), DisplayManager::readOptions(), and PFRootEventManagerColin::readSpecificOptions().

                                                                              {
  std::string data = GetLineData(tag,key);
 
  std::istringstream in(data.c_str());  
  while(1) {
    T tmp;
    in>>tmp;
    if(!in) break;
    values.push_back(tmp);
  } 

  return true;
}
bool IO::IsZombie ( ) const [inline]

true if constructor went wrong

Definition at line 61 of file IO.h.

References fAllLines.

{return !fAllLines.size();}
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().

                                       {
  cout<<"file : "<<filename<<"\t\t";

  ifstream in(filename);
  if( !in.good() ) {
    cout<<"unreadable"<<endl;
    return false;
  }

  char data[sLinesize];
  char s[sLinesize];
  int pos=0;

  do { 
    in.seekg(pos);
    in.getline(s,sLinesize);
    
    pos = in.tellg();     
 
    if(string(s).empty()) {
      continue; // remove empty lines
    }

    istringstream lin(s);  

    string tag;
    lin>>tag;

    if(!strncmp(tag.c_str(),"//",2)) continue; // remove commented lines can be done better ...

    lin.get(data,sLinesize);
    
    fAllLines.push_back(pair<string, string>(tag, data));
  } while(in.good());
  
  if(in.eof()) {
    cout<<"ok"<<endl;
    return true;
  }
  else {
    cout<<"error"<<endl;
    return false;
  }
}

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 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().