Merge branch 'master' into cairocanvas
[ardour.git] / libs / pbd / epa.cc
1 /*
2     Copyright (C) 2010 Paul Davis 
3
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.
8
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.
13
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.
17
18 */
19
20 #include <cstdlib>
21
22 #include "pbd/epa.h"
23 #include "pbd/strsplit.h"
24
25 extern char** environ;
26
27 using namespace PBD;
28 using namespace std;
29
30 EnvironmentalProtectionAgency* EnvironmentalProtectionAgency::_global_epa = 0;
31
32 EnvironmentalProtectionAgency::EnvironmentalProtectionAgency (bool arm, const std::string& envname)
33         : _armed (arm)
34         , _envname (envname)
35 {
36         if (_armed) {
37                 save ();
38         }
39 }
40
41 EnvironmentalProtectionAgency::~EnvironmentalProtectionAgency()
42 {
43         if (_armed) {
44                 restore ();
45         }
46 }
47
48 void
49 EnvironmentalProtectionAgency::arm ()
50 {
51         _armed = true;
52 }
53
54 void
55 EnvironmentalProtectionAgency::save ()
56 {
57         e.clear ();
58
59         if (!_envname.empty()) {
60                 
61                 /* fetch environment from named environment variable, rather than "environ"
62                  */
63
64                 const char* estr = getenv (_envname.c_str());
65
66                 if (!estr) {
67                         return;
68                 }
69                 
70                 /* parse line by line, and save into "e" 
71                  */
72
73                 vector<string> lines;
74                 split (estr, lines, '\n');
75
76                 for (vector<string>::iterator i = lines.begin(); i != lines.end(); ++i) {
77
78                         string estring = *i;
79                         string::size_type equal = estring.find_first_of ('=');
80                         
81                         if (equal == string::npos) {
82                                 /* say what? an environ value without = ? */
83                                 continue;
84                         }
85                         
86                         string before = estring.substr (0, equal);
87                         string after = estring.substr (equal+1);
88                         
89                         e.insert (pair<string,string>(before,after));
90                 }
91                 
92         } else {
93
94                 /* fetch environment from "environ"
95                  */
96
97                 for (size_t i = 0; environ[i]; ++i) {
98                         
99                         string estring = environ[i];
100                         string::size_type equal = estring.find_first_of ('=');
101                         
102                         if (equal == string::npos) {
103                                 /* say what? an environ value without = ? */
104                                 continue;
105                         }
106                         
107                         string before = estring.substr (0, equal);
108                         string after = estring.substr (equal+1);
109                         
110                         e.insert (pair<string,string>(before,after));
111                 }
112         }
113 }                         
114 void
115 EnvironmentalProtectionAgency::restore () const
116 {
117                 clear ();
118
119         for (map<string,string>::const_iterator i = e.begin(); i != e.end(); ++i) {
120                 setenv (i->first.c_str(), i->second.c_str(), 1);
121         }
122
123
124 void
125 EnvironmentalProtectionAgency::clear () const
126 {
127         char** the_environ = environ;
128
129         for (size_t i = 0; the_environ[i]; ++i) {
130                         
131                 string estring = the_environ[i];
132                 string::size_type equal = estring.find_first_of ('=');
133                         
134                 if (equal == string::npos) {
135                         /* say what? an environ value without = ? */
136                         continue;
137                 }
138                         
139                 string before = estring.substr (0, equal);
140                 unsetenv(before.c_str());
141         }
142 }