CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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  const std::string& user,
13  std::string& password){
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 }
55 
57  const string& delim,
58  size_t& pos) const{
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 }
69 
71  const std::string& chars) const{
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 }
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
uint16_t size_type
double f[11][100]
void readPassword(const std::string &fileName, const std::string &user, std::string &password)
std::vector< std::string > tokenize(std::string const &input, std::string const &separator)
breaks the input string into tokens, delimited by the separator
Definition: Parse.cc:57
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