diff options
author | Paul Davis <paul@linuxaudiosystems.com> | 2011-01-07 16:25:57 +0000 |
---|---|---|
committer | Paul Davis <paul@linuxaudiosystems.com> | 2011-01-07 16:25:57 +0000 |
commit | 603d07a80bb293cb7819e50397111674a96b142c (patch) | |
tree | 187719cf2027a205c6d7d7977839b18341c03471 /libs/pbd | |
parent | 5bff882ce1f324dc54e7ec71a33923439f50f865 (diff) |
forward port EPA changes from 2.X
git-svn-id: svn://localhost/ardour2/branches/3.0@8473 d708f5d6-7413-0410-9779-e7cbd77b26cf
Diffstat (limited to 'libs/pbd')
-rw-r--r-- | libs/pbd/epa.cc | 68 | ||||
-rw-r--r-- | libs/pbd/pbd/epa.h | 27 |
2 files changed, 67 insertions, 28 deletions
diff --git a/libs/pbd/epa.cc b/libs/pbd/epa.cc index 8665823d77..3d3f7477d7 100644 --- a/libs/pbd/epa.cc +++ b/libs/pbd/epa.cc @@ -21,6 +21,7 @@ #include <iostream> #include "pbd/epa.h" +#include "pbd/strsplit.h" extern char** environ; @@ -29,8 +30,9 @@ using namespace std; EnvironmentalProtectionAgency* EnvironmentalProtectionAgency::_global_epa = 0; -EnvironmentalProtectionAgency::EnvironmentalProtectionAgency (bool arm) +EnvironmentalProtectionAgency::EnvironmentalProtectionAgency (bool arm, const std::string& envname) : _armed (arm) + , _envname (envname) { if (_armed) { save (); @@ -55,29 +57,65 @@ EnvironmentalProtectionAgency::save () { e.clear (); - for (size_t i = 0; environ[i]; ++i) { + if (!_envname.empty()) { + + /* fetch environment from named environment variable, rather than "environ" + */ - string estring = environ[i]; - string::size_type equal = estring.find_first_of ('='); + const char* estr = getenv (_envname.c_str()); - if (equal == string::npos) { - /* say what? an environ value without = ? */ - continue; + if (!estr) { + return; + } + + /* parse line by line, and save into "e" + */ + + vector<string> lines; + split (estr, lines, '\n'); + + for (vector<string>::iterator i = lines.begin(); i != lines.end(); ++i) { + + string estring = *i; + string::size_type equal = estring.find_first_of ('='); + + if (equal == string::npos) { + /* say what? an environ value without = ? */ + continue; + } + + string before = estring.substr (0, equal); + string after = estring.substr (equal+1); + + e.insert (pair<string,string>(before,after)); + } + + } else { + + /* fetch environment from "environ" + */ + + for (size_t i = 0; environ[i]; ++i) { + + string estring = environ[i]; + string::size_type equal = estring.find_first_of ('='); + + if (equal == string::npos) { + /* say what? an environ value without = ? */ + continue; + } + + string before = estring.substr (0, equal); + string after = estring.substr (equal+1); + + e.insert (pair<string,string>(before,after)); } - - string before = estring.substr (0, equal); - string after = estring.substr (equal+1); - - cerr << "Save [" << before << "] = " << after << endl; - - e.insert (pair<string,string>(before,after)); } } void EnvironmentalProtectionAgency::restore () const { for (map<string,string>::const_iterator i = e.begin(); i != e.end(); ++i) { - cerr << "Restore [" << i->first << "] = " << i->second << endl; setenv (i->first.c_str(), i->second.c_str(), 1); } } diff --git a/libs/pbd/pbd/epa.h b/libs/pbd/pbd/epa.h index b2708fbd4c..ffcd78504a 100644 --- a/libs/pbd/pbd/epa.h +++ b/libs/pbd/pbd/epa.h @@ -27,20 +27,21 @@ namespace PBD { class EnvironmentalProtectionAgency { public: - EnvironmentalProtectionAgency (bool arm=true); - ~EnvironmentalProtectionAgency (); - - void arm (); - void save (); - void restore () const; - - static EnvironmentalProtectionAgency* get_global_epa () { return _global_epa; } - static void set_global_epa (EnvironmentalProtectionAgency* epa) { _global_epa = epa; } - + EnvironmentalProtectionAgency (bool arm = true, const std::string& envname = std::string()); + ~EnvironmentalProtectionAgency (); + + void arm (); + void save (); + void restore () const; + + static EnvironmentalProtectionAgency* get_global_epa () { return _global_epa; } + static void set_global_epa (EnvironmentalProtectionAgency* epa) { _global_epa = epa; } + private: - bool _armed; - std::map<std::string,std::string> e; - static EnvironmentalProtectionAgency* _global_epa; + bool _armed; + std::string _envname; + std::map<std::string,std::string> e; + static EnvironmentalProtectionAgency* _global_epa; }; } |