CMS 3D CMS Logo

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

◆ readPassword()

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.

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 }

References Exception, f, MillePedeFileConverter_cfg::fileName, newFWLiteAna::found, mps_splice::line, EcalCondDBWriter_cfi::password, Ph1PMapSummaryHelper::tokenize(), trim(), and EnviromentSettings::user.

◆ tokenize()

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.

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 }

References spr::find(), and alignCSCRings::s.

◆ trim()

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 }

References alignCSCRings::s.

PasswordReader::tokenize
std::string tokenize(const std::string &s, const std::string &delim, size_t &pos) const
Definition: PasswordReader.cc:53
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
pos
Definition: PixelAliasList.h:18
MillePedeFileConverter_cfg.fileName
fileName
Definition: MillePedeFileConverter_cfg.py:32
spr::find
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
newFWLiteAna.found
found
Definition: newFWLiteAna.py:118
alignCSCRings.s
s
Definition: alignCSCRings.py:92
EcalCondDBWriter_cfi.password
password
Definition: EcalCondDBWriter_cfi.py:62
trigger::size_type
uint16_t size_type
Definition: TriggerTypeDefs.h:18
PasswordReader::trim
std::string trim(const std::string &s, const std::string &chars) const
Definition: PasswordReader.cc:66
EnviromentSettings.user
user
Definition: EnviromentSettings.py:30
Exception
Definition: hltDiff.cc:245
mps_splice.line
line
Definition: mps_splice.py:76