CMS 3D CMS Logo

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

pftools::IO Class Reference

#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

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, linker::files, pftools::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;

}
pftools::IO::~IO ( ) [inline]

Definition at line 53 of file IO.h.

References fAllLines.

{fAllLines.clear();}

Member Function Documentation

void pftools::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 138 of file IO.h.

References data, recoMuon::in, AlCaHLTBitMon_QueryRunRegistry::string, 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 133 of file IO.h.

                                                                                  {
  return false;
} 
string IO::GetLineData ( const char *  tag,
const char *  key 
) const

Definition at line 90 of file IO.cc.

References data, fAllLines, first, newFWLiteAna::found, i, recoMuon::in, edm::second(), sLinesize, AlCaHLTBitMon_QueryRunRegistry::string, 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 115 of file IO.cc.

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

                                                            {

  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 pftools::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 123 of file IO.h.

References data, recoMuon::in, AlCaHLTBitMon_QueryRunRegistry::string, 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
bool pftools::IO::IsZombie ( ) const [inline]

true if constructor went wrong

Definition at line 62 of file IO.h.

References fAllLines.

{return !fAllLines.size();}
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, pos, alignCSCRings::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> > pftools::IO::fAllLines [private]

all non empty, uncommented lines

Definition at line 32 of file IO.h.

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