shell_expand.cc
Go to the documentation of this file.
00001 #include "utilmm/configfile/shell_expand.hh"
00002 
00003 #include <boost/regex.hpp>
00004 #include <iterator>
00005 #include <sstream>
00006 
00007 using namespace std;
00008 using boost::regex;
00009 using boost::smatch;
00010 
00011 
00012 #include <iostream>
00013 
00014 string utilmm::shell_expand(string const& text)
00015 {
00016     static regex const 
00017         rx_shellvalue("\\$(\\w+)");
00018 
00019     smatch result;
00020     if (! regex_search(text, result, rx_shellvalue))
00021         return text;
00022     
00023     string::const_iterator last_match;
00024     string::const_iterator const end = text.end();
00025 
00026     ostringstream expanded;
00027     do
00028     {
00029         expanded << result.prefix();
00030 
00031         std::string env_key = result[1];
00032         char const* env_value = getenv(env_key.c_str());
00033         if (env_value)
00034             expanded << env_value;
00035 
00036         last_match = result.suffix().first;
00037     } while(regex_search(last_match, end, result, rx_shellvalue));
00038 
00039     copy(last_match, end, ostream_iterator<string::value_type>(expanded));
00040     return expanded.str();
00041 }
00042 


utilmm
Author(s): Sylvain Joyeux/sylvain.joyeux@m4x.org
autogenerated on Thu Jan 2 2014 11:38:31