CMS 3D CMS Logo

PasswordReader.cc
Go to the documentation of this file.
1 #include "PasswordReader.h"
2 
3 #include <iostream>
4 #include <fstream>
5 #include <algorithm>
6 
8 
9 using namespace std;
10 
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 }
52 
53 std::string PasswordReader::tokenize(const string& s, const string& delim, size_t& pos) const {
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 }
65 
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 }
static void trim(std::string &s)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
uint16_t size_type
double f[11][100]
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
std::vector< std::string > tokenize(std::string const &input, std::string const &separator)
breaks the input string into tokens, delimited by the separator