CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 Exception, validate-o2o-wbm::f, newFWLiteAna::found, geometryCSVtoXML::line, Ph1PMapSummaryHelper::tokenize(), and trim().

Referenced by EcalDccWeightBuilder::writeWeightToDB().

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

References spr::find().

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

66  {
67  std::string::size_type pos0 = s.find_first_not_of(chars);
68  if (pos0 == std::string::npos) {
69  pos0 = 0;
70  }
71  string::size_type pos1 = s.find_last_not_of(chars) + 1;
72  if (pos1 == std::string::npos) {
73  pos1 = pos0;
74  }
75  return s.substr(pos0, pos1 - pos0);
76 }
uint16_t size_type