CMS 3D CMS Logo

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

#include <PasswordReader.h>

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 edm::hlt::Exception, f, newFWLiteAna::found, geometryCSVtoXML::line, pos, and edm::tokenize().

Referenced by EcalDccWeightBuilder::writeWeightToDB().

13  {
14  ifstream f(fileName.c_str());
15  if(!f.good()){
16  throw cms::Exception("File")
17  << "Failed to open file " << fileName << " for reading condition "
18  "database password\n";
19  }
20  string line;
21  bool found = false;
22  int nstatements = 0; //number of lines other than empty and comment lines
23  while(f.good() && !found){
24  size_t pos = 0;
25  getline(f, line);
26  trim(line, " \t");
27  if(line[0]=='#' || line.size()==0){//comment line
28  continue;
29  }
30  ++nstatements;
31  string u = tokenize(line, ":/ \t", pos);
32  if(u == user){//user found
33  password = tokenize(line, ":/ \t", pos);
34  found = true;
35  }
36  }
37  if(!found && nstatements==1){//login not found in the file
38  //let's check if file does not contain a single password, w/o login
39  f.clear();
40  f.seekg(0, ios::beg);
41  getline(f,line);
42  trim(line, " \t");
43  if(line.find_first_of(": \t")==string::npos){//no login/password delimiter
44  //looks like a single password file
45  password = line;
46  found = true;
47  }
48  }
49  if(!found){
50  throw cms::Exception("Database")
51  << " Password for condition database user '" << user << "' not found in"
52  << " password file " << fileName << "\n";
53  }
54 }
double f[11][100]
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
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

Definition at line 56 of file PasswordReader.cc.

References spr::find(), and pos.

58  {
59  size_t pos0 = pos;
60  size_t len = s.size();
61  //eats delimeters at beginning of the string
62  while(pos0<s.size() && find(delim.begin(), delim.end(), s[pos0])!=delim.end()){
63  ++pos0;
64  }
65  if(pos0>=len || pos0==string::npos) return "";
66  pos = s.find_first_of(delim, pos0);
67  return s.substr(pos0, (pos>0?pos:s.size())-pos0);
68 }
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
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.

71  {
72  std::string::size_type pos0 = s.find_first_not_of(chars);
73  if(pos0==std::string::npos){
74  pos0=0;
75  }
76  string::size_type pos1 = s.find_last_not_of(chars)+1;
77  if(pos1==std::string::npos){
78  pos1 = pos0;
79  }
80  return s.substr(pos0, pos1-pos0);
81 }
uint16_t size_type