CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/CalibCalorimetry/EcalSRTools/src/PasswordReader.cc

Go to the documentation of this file.
00001 #include "PasswordReader.h"
00002 
00003 #include <iostream>
00004 #include <fstream>
00005 #include <algorithm>
00006 
00007 #include "FWCore/Utilities/interface/Exception.h"
00008 
00009 using namespace std;
00010 
00011 void PasswordReader::readPassword(const std::string& fileName,
00012                                   const std::string& user,
00013                                   std::string& password){
00014   ifstream f(fileName.c_str());
00015   if(!f.good()){
00016     throw cms::Exception("File")
00017       << "Failed to open file " << fileName << " for reading condition "
00018       "database password\n";
00019   }
00020   string line;
00021   bool found = false;
00022   int nstatements = 0; //number of lines other than empty and comment lines
00023   while(f.good() && !found){
00024     size_t pos = 0;
00025     getline(f, line);
00026     trim(line, " \t");
00027     if(line[0]=='#' || line.size()==0){//comment line
00028       continue;
00029     }
00030     ++nstatements;
00031     string u = tokenize(line, ":/ \t", pos);
00032     if(u == user){//user found
00033       password = tokenize(line, ":/ \t", pos);
00034       found = true;
00035     }
00036   }
00037   if(!found && nstatements==1){//login not found in the file
00038     //let's check if file does not contain a single password, w/o login
00039     f.clear();
00040     f.seekg(0, ios::beg);
00041     getline(f,line);
00042     trim(line, " \t");
00043     if(line.find_first_of(": \t")==string::npos){//no login/password delimiter
00044       //looks like a single password file
00045       password = line;
00046       found = true;
00047     }
00048   }
00049   if(!found){
00050     throw cms::Exception("Database")
00051       << " Password for condition database user '" << user << "' not found in"
00052       << " password file " << fileName << "\n";
00053   }
00054 }
00055 
00056 std::string PasswordReader::tokenize(const string& s,
00057                                      const string& delim,
00058                                      size_t& pos) const{
00059   size_t pos0 = pos;
00060   size_t len = s.size();
00061   //eats delimeters at beginning of the string
00062   while(pos0<s.size() && find(delim.begin(), delim.end(), s[pos0])!=delim.end()){
00063     ++pos0;
00064   }
00065   if(pos0>=len || pos0==string::npos) return "";
00066   pos = s.find_first_of(delim, pos0);
00067   return s.substr(pos0, (pos>0?pos:s.size())-pos0);
00068 }
00069 
00070 std::string PasswordReader::trim(const std::string& s,
00071                                  const std::string& chars) const{
00072   std::string::size_type pos0 = s.find_first_not_of(chars);
00073   if(pos0==std::string::npos){
00074     pos0=0;
00075   }
00076   string::size_type pos1 = s.find_last_not_of(chars)+1;
00077   if(pos1==std::string::npos){
00078     pos1 = pos0;
00079   }
00080   return s.substr(pos0, pos1-pos0);
00081 }