Fix libpbd tests.
[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 #include <iostream>
22
23 #include "pbd/epa.h"
24 #include "pbd/strsplit.h"
25
26 extern char** environ;
27
28 using namespace PBD;
29 using namespace std;
30
31 EnvironmentalProtectionAgency* EnvironmentalProtectionAgency::_global_epa = 0;
32
33 EnvironmentalProtectionAgency::EnvironmentalProtectionAgency (bool arm, const std::string& envname)
34         : _armed (arm)
35         , _envname (envname)
36 {
37         if (_armed) {
38                 save ();
39         }
40 }
41
42 EnvironmentalProtectionAgency::~EnvironmentalProtectionAgency()
43 {
44         if (_armed) {
45                 restore ();
46         }
47 }
48
49 void
50 EnvironmentalProtectionAgency::arm ()
51 {
52         _armed = true;
53 }
54
55 void
56 EnvironmentalProtectionAgency::save ()
57 {
58         e.clear ();
59
60         if (!_envname.empty()) {
61                 
62                 /* fetch environment from named environment variable, rather than "environ"
63                  */
64
65                 const char* estr = getenv (_envname.c_str());
66
67                 if (!estr) {
68                         return;
69                 }
70                 
71                 /* parse line by line, and save into "e" 
72                  */
73
74                 vector<string> lines;
75                 split (estr, lines, '\n');
76
77                 for (vector<string>::iterator i = lines.begin(); i != lines.end(); ++i) {
78
79                         string estring = *i;
80                         string::size_type equal = estring.find_first_of ('=');
81                         
82                         if (equal == string::npos) {
83                                 /* say what? an environ value without = ? */
84                                 continue;
85                         }
86                         
87                         string before = estring.substr (0, equal);
88                         string after = estring.substr (equal+1);
89                         
90                         e.insert (pair<string,string>(before,after));
91                 }
92                 
93         } else {
94
95                 /* fetch environment from "environ"
96                  */
97
98                 for (size_t i = 0; environ[i]; ++i) {
99                         
100                         string estring = environ[i];
101                         string::size_type equal = estring.find_first_of ('=');
102                         
103                         if (equal == string::npos) {
104                                 /* say what? an environ value without = ? */
105                                 continue;
106                         }
107                         
108                         string before = estring.substr (0, equal);
109                         string after = estring.substr (equal+1);
110                         
111                         e.insert (pair<string,string>(before,after));
112                 }
113         }
114 }                         
115 void
116 EnvironmentalProtectionAgency::restore () const
117 {
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);
120         }
121 }