CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_6/src/FWCore/Utilities/src/Parse.cc

Go to the documentation of this file.
00001 #include "FWCore/Utilities/interface/Parse.h"
00002 #include "FWCore/Utilities/interface/EDMException.h"
00003 #include "FWCore/Utilities/interface/Algorithms.h"
00004 #include <boost/tokenizer.hpp>
00005 #include <fstream>
00006 #include <iostream>
00007 
00008 namespace edm {
00009 
00010     std::string  read_whole_file(std::string const& filename) {
00011       std::string result;
00012       std::ifstream input(filename.c_str());
00013       if (!input) {
00014        throw edm::Exception(errors::Configuration,"MissingFile")
00015          << "Cannot read file " << filename;
00016       }
00017       std::string buffer;
00018       while (getline(input, buffer)) {
00019           // getline strips newlines; we have to put them back by hand.
00020           result += buffer;
00021           result += '\n';
00022       }
00023       return result; 
00024     }
00025 
00026 
00027     void read_from_cin(std::string & output) {
00028       std::string line;
00029       while (getline(std::cin, line)) {
00030         output += line;
00031         output += '\n';
00032       }
00033     }
00034 
00035 
00036     std::string withoutQuotes(const std::string& from) {
00037       std::string result = from;
00038       if(!result.empty()) {
00039       // get rid of leading quotes
00040         if(result[0] == '"' || result[0] == '\'') {
00041           result.erase(0,1);
00042         }
00043       }
00044 
00045       if(!result.empty()) {
00046        // and trailing quotes
00047         int lastpos = result.size()-1;
00048         if(result[lastpos] == '"' || result[lastpos] == '\'') {
00049           result.erase(lastpos, 1);
00050         }
00051       }
00052      return result;
00053     }
00054 
00055 
00056     std::vector<std::string> 
00057     tokenize(const std::string & input, const std::string &separator) {
00058       typedef boost::char_separator<char>   separator_t;
00059       typedef boost::tokenizer<separator_t> tokenizer_t;
00060 
00061       std::vector<std::string> result;
00062       separator_t  sep(separator.c_str(), "", boost::keep_empty_tokens); // separator for elements in path
00063       tokenizer_t  tokens(input, sep);
00064       copy_all(tokens, std::back_inserter<std::vector<std::string> >(result));
00065       return result;
00066     }
00067 
00068 } // namespace edm
00069