CMS 3D CMS Logo

Public Member Functions

PasswordReader Class Reference

#include <PasswordReader.h>

List of all members.

Public Member Functions

void readPassword (const std::string &fileName, const std::string &user, std::string &password)
std::string tokenize (const std::string &s, const std::string &delim, size_t &pos) const
std::string trim (const std::string &s, const std::string &chars) const

Detailed Description

Definition at line 6 of file PasswordReader.h.


Member Function Documentation

void PasswordReader::readPassword ( const std::string &  fileName,
const std::string &  user,
std::string &  password 
)

Read data password from a local file. File can contain password for several user. The format is one record by line. Line must start with the user login followed by the password. Login and password can be separated by space(s), tab(s), a colon or a slash.

Parameters:
fileNamefile with password list
loginof user to look for
password[out] password read from the file
Exceptions:
cms::Exceptionif file cannot be read or if the password was not found

Definition at line 11 of file PasswordReader.cc.

References Exception, f, newFWLiteAna::found, geometryCSVtoXML::line, pos, and edm::tokenize().

Referenced by EcalDccWeightBuilder::writeWeightToDB().

                                                      {
  ifstream f(fileName.c_str());
  if(!f.good()){
    throw cms::Exception("File")
      << "Failed to open file " << fileName << " for reading condition "
      "database password\n";
  }
  string line;
  bool found = false;
  int nstatements = 0; //number of lines other than empty and comment lines
  while(f.good() && !found){
    size_t pos = 0;
    getline(f, line);
    trim(line, " \t");
    if(line[0]=='#' || line.size()==0){//comment line
      continue;
    }
    ++nstatements;
    string u = tokenize(line, ":/ \t", pos);
    if(u == user){//user found
      password = tokenize(line, ":/ \t", pos);
      found = true;
    }
  }
  if(!found && nstatements==1){//login not found in the file
    //let's check if file does not contain a single password, w/o login
    f.clear();
    f.seekg(0, ios::beg);
    getline(f,line);
    trim(line, " \t");
    if(line.find_first_of(": \t")==string::npos){//no login/password delimiter
      //looks like a single password file
      password = line;
      found = true;
    }
  }
  if(!found){
    throw cms::Exception("Database")
      << " Password for condition database user '" << user << "' not found in"
      << " password file " << fileName << "\n";
  }
}
std::string PasswordReader::tokenize ( const std::string &  s,
const std::string &  delim,
size_t &  pos 
) const

Function to split a string into tokens. Usage:

 int pos = 0;
 string tok;
 const string s = .....;     //string to tokenize
 const string delim = " \t"; //list of token delimiters
 while((tok = tokenize(s, delim, pos))!=string::npos){
   .... code using tok ...
 }
 
Parameters:
sstring to tokenize
listof delimiters
[in,out]poscurrent scan position in the string
std::string PasswordReader::trim ( const std::string &  s,
const std::string &  chars 
) const

Trims unwanted characters (e.g. spaces) at start and end of a string.

Parameters:
s[in,out] input string
charsset of characters to trim

Definition at line 70 of file PasswordReader.cc.

                                                              {
  std::string::size_type pos0 = s.find_first_not_of(chars);
  if(pos0==std::string::npos){
    pos0=0;
  }
  string::size_type pos1 = s.find_last_not_of(chars)+1;
  if(pos1==std::string::npos){
    pos1 = pos0;
  }
  return s.substr(pos0, pos1-pos0);
}