remove theme-specific names from RC.in files, substitute them during build
[ardour2.git] / libs / pbd / epa.cc
blob3d3f7477d7dfe56151a809b52398fed86698b1b1
1 /*
2 Copyright (C) 2010 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <cstdlib>
21 #include <iostream>
23 #include "pbd/epa.h"
24 #include "pbd/strsplit.h"
26 extern char** environ;
28 using namespace PBD;
29 using namespace std;
31 EnvironmentalProtectionAgency* EnvironmentalProtectionAgency::_global_epa = 0;
33 EnvironmentalProtectionAgency::EnvironmentalProtectionAgency (bool arm, const std::string& envname)
34 : _armed (arm)
35 , _envname (envname)
37 if (_armed) {
38 save ();
42 EnvironmentalProtectionAgency::~EnvironmentalProtectionAgency()
44 if (_armed) {
45 restore ();
49 void
50 EnvironmentalProtectionAgency::arm ()
52 _armed = true;
55 void
56 EnvironmentalProtectionAgency::save ()
58 e.clear ();
60 if (!_envname.empty()) {
62 /* fetch environment from named environment variable, rather than "environ"
65 const char* estr = getenv (_envname.c_str());
67 if (!estr) {
68 return;
71 /* parse line by line, and save into "e"
74 vector<string> lines;
75 split (estr, lines, '\n');
77 for (vector<string>::iterator i = lines.begin(); i != lines.end(); ++i) {
79 string estring = *i;
80 string::size_type equal = estring.find_first_of ('=');
82 if (equal == string::npos) {
83 /* say what? an environ value without = ? */
84 continue;
87 string before = estring.substr (0, equal);
88 string after = estring.substr (equal+1);
90 e.insert (pair<string,string>(before,after));
93 } else {
95 /* fetch environment from "environ"
98 for (size_t i = 0; environ[i]; ++i) {
100 string estring = environ[i];
101 string::size_type equal = estring.find_first_of ('=');
103 if (equal == string::npos) {
104 /* say what? an environ value without = ? */
105 continue;
108 string before = estring.substr (0, equal);
109 string after = estring.substr (equal+1);
111 e.insert (pair<string,string>(before,after));
115 void
116 EnvironmentalProtectionAgency::restore () const
118 for (map<string,string>::const_iterator i = e.begin(); i != e.end(); ++i) {
119 setenv (i->first.c_str(), i->second.c_str(), 1);