Gaudi Framework, version v20r3

Generated: 24 Nov 2008

ParserUtils.cpp

Go to the documentation of this file.
00001 // $Id: ParserUtils.cpp,v 1.7 2007/05/24 14:41:22 hmd Exp $
00002 // ============================================================================
00003 // CVS tag $Name: v17r2 $, version $Revision: 1.7 $ 
00004 // ============================================================================
00005 // Include files
00006 // ============================================================================
00007 // STD & STL
00008 // ============================================================================
00009 #include <iostream>
00010 #include <fstream>
00011 // ============================================================================
00012 // Boost
00013 // ============================================================================
00014 #include <boost/filesystem/operations.hpp>
00015 #include <boost/tokenizer.hpp>
00016 #include <boost/regex.hpp>
00017 #include <boost/algorithm/string.hpp>
00018 #include <boost/format.hpp>
00019 // ============================================================================
00020 // local
00021 // ============================================================================
00022 #include "ParserUtils.h"
00023 #include "ParserGrammar.h"
00024 // ============================================================================
00025 namespace fs = boost::filesystem;
00026 using namespace std;
00027 
00028 // ============================================================================
00039 // ============================================================================
00040 StatusCode Gaudi::Parsers::parse
00041 ( Gaudi::Parsers::Parser                parser    ,
00042   const std::string&                    fileName  ,
00043   std::vector<Gaudi::Parsers::Message>& msgs      )
00044 {
00045   StatusCode res = parser.parse(fileName);
00046   msgs = parser.messages();
00047   return res;  
00048 }
00049 // ============================================================================
00050 StatusCode Gaudi::Parsers::parse
00051 ( const string& filename,
00052   const vector<string>& searchPath,
00053   Gaudi::Parsers::Catalogue& catalogue,
00054   std::vector<std::string>& included,
00055   vector<Gaudi::Parsers::Message>& msgs)
00056 {
00057   return parse
00058     ( Gaudi::Parsers::Parser(catalogue, included, searchPath) , 
00059       filename , msgs ) ;  
00060 }
00061 // ============================================================================
00062 StatusCode Gaudi::Parsers::parse
00063 ( const string& filename,
00064   const string& searchPath,
00065   Gaudi::Parsers::Catalogue& catalogue,
00066   std::vector<std::string>& included,
00067   vector<Gaudi::Parsers::Message>& msgs)
00068 {
00069   return parse
00070     ( Gaudi::Parsers::Parser (catalogue , included, searchPath ) , 
00071       filename ,  msgs ) ;  
00072 }
00073 // ============================================================================
00074 StatusCode Gaudi::Parsers::parse
00075 ( const string& filename,
00076   Gaudi::Parsers::Catalogue& catalogue,
00077   std::vector<std::string>& included,
00078   vector<Gaudi::Parsers::Message>& msgs){
00079   return parse
00080     (Gaudi::Parsers::Parser(catalogue,included),filename,msgs);
00081 }
00082 // ============================================================================
00083 StatusCode Gaudi::Parsers::Utils::getEnv
00084 ( const std::string& envName , std::string& envValue )
00085 {
00086   char* envch = getenv(envName.c_str());
00087         if(envch==NULL) { return StatusCode::FAILURE; }
00088         envValue = envch;
00089   return StatusCode::SUCCESS;
00090 }
00091 // ============================================================================
00092 std::string 
00093 Gaudi::Parsers::Utils::removeEnvironment(const std::string& input){
00094   std::string result=input;// result
00095   
00096   const char* re = "\\$(([A-Za-z0-9_]+)|\\(([A-Za-z0-9_]+)\\))";
00097   std::string::const_iterator start, end;
00098   boost::regex expression(re);
00099   start = input.begin();
00100   end = input.end();   
00101   boost::match_results<std::string::const_iterator> what;
00102   boost::match_flag_type flags = boost::match_default;
00103   while ( boost::regex_search(start, end, what, expression, flags ) )   
00104   {
00105     std::string var,env;
00106     std::string matched(what[0].first,what[0].second);
00107     std::string v1(what[2].first,what[2].second);
00108     std::string v2(what[3].first,what[3].second);      
00109     
00110     if ( v1.length()>0){ var = v1; }
00111     else { var = v2; }
00112     
00113     StatusCode ok = getEnv(var, env);
00114     if(ok.isSuccess())
00115     { boost::algorithm::replace_first(result,matched, env); }
00116     start = what[0].second;
00117     // update flags:
00118     flags |= boost::match_prev_avail;
00119     flags |= boost::match_not_bob;
00120   }
00121   return result;
00122 }
00123 // ============================================================================
00124 StatusCode Gaudi::Parsers::Utils::searchFile
00125 ( const std::string&              fileInput  , 
00126   bool                            addCurrent ,
00127   const std::vector<std::string>& dirs       , 
00128   std::string&                    fileOutput )
00129 {
00130         std::string result;
00131   try {
00132     fs::path givenPath(removeEnvironment(fileInput),fs::native);
00133     //std::cout<<"Given path="<<givenPath.string()<<std::endl;
00134     fs::path currentPath = givenPath;
00135     std::vector<std::string> sdirs = dirs;
00136     if(addCurrent){
00137       sdirs.insert(sdirs.begin(), 
00138                    fs::initial_path().native_directory_string());
00139     }
00140     std::vector<std::string>::const_iterator current = 
00141       sdirs.begin(),end=sdirs.end();
00142     while(1) {
00143       if(fs::exists(currentPath)) {
00144         fileOutput = currentPath.native_directory_string();
00145         return StatusCode::SUCCESS;
00146       }
00147       if(current!=end) {
00148         std::string _n = *current;
00149         currentPath = fs::path(_n, fs::native)/givenPath;
00150       }else{
00151         return StatusCode::FAILURE;
00152       }
00153       ++current;
00154     }
00155   }catch(...) {}
00156   return StatusCode::FAILURE;
00157 }
00158 // ============================================================================
00159 StatusCode Gaudi::Parsers::Utils::readFile
00160 (const std::string& name ,  std::string& result)
00161 {
00162   std::ifstream in(name.c_str());
00163   if (!in.is_open()) { return StatusCode::FAILURE; }
00164   char c;
00165   while (!in.get(c).eof()) { result += c; }
00166   return StatusCode::SUCCESS;  
00167 }
00168 // ============================================================================
00169 bool Gaudi::Parsers::Utils::isWin()
00170 {
00171 #ifdef _WIN32
00172   return true;
00173 #else
00174   return false;
00175 #endif  
00176 }
00177 // ============================================================================
00178 std::string Gaudi::Parsers::Utils::pathSeparator()
00179 { return Gaudi::Parsers::Utils::isWin()?";":":"; }
00180 // ============================================================================
00181 std::vector<std::string> Gaudi::Parsers::Utils::extractPath
00182 ( const std::string& input, bool removeEnv )
00183 {
00184   std::string in = removeEnv?removeEnvironment(input):input;
00185   std::string separator = ","+pathSeparator();
00186   typedef boost::tokenizer<boost::char_separator<char> > Tokenizer;
00187   std::vector<std::string> result;
00188   Tokenizer tok(in, boost::char_separator<char>(separator.c_str()));
00189   Tokenizer::iterator it =tok.begin();
00190   while(it!=tok.end())
00191   {
00192     if(it->length()>0){ result.push_back(*it); }
00193     it++;
00194   }
00195   return result;
00196 }
00197 // ============================================================================
00198 StatusCode Gaudi::Parsers::Utils::parseValue
00199 ( const string&             input        , 
00200   std::string&              stringResult ,
00201   std::vector<std::string>& vectorResult )
00202 {
00203   
00204   typedef 
00205     boost::spirit::position_iterator<std::string::const_iterator> IteratorT;
00206   
00207   Gaudi::Parsers::ValueGrammar   grValue;
00208   Gaudi::Parsers::SkipperGrammar grSkipper;
00209   IteratorT beginpos(input.begin(), input.end(), "");
00210   IteratorT endpos;
00211   boost::tuple<std::string, std::vector<std::string> > result;
00212   boost::spirit::parse_info<IteratorT> info = 
00213     boost::spirit::parse
00214     (beginpos, endpos, grValue[var(result)=arg1] ,grSkipper);
00215   if (!info.full){ return StatusCode::FAILURE; }
00216   stringResult = result.get<0>();
00217   vectorResult = result.get<1>();
00218   return StatusCode::SUCCESS;
00219 }
00220 // ============================================================================
00221 
00222 // ============================================================================
00223 // The END 
00224 // ============================================================================
00225 

Generated at Mon Nov 24 14:38:49 2008 for Gaudi Framework, version v20r3 by Doxygen version 1.5.6 written by Dimitri van Heesch, © 1997-2004