add EPA stuff from 2.X
[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
25 extern char** environ;
26
27 using namespace PBD;
28 using namespace std;
29
30 EnvironmentalProtectionAgency* EnvironmentalProtectionAgency::_global_epa = 0;
31
32 EnvironmentalProtectionAgency::EnvironmentalProtectionAgency ()
33 {
34         save ();
35 }
36
37 EnvironmentalProtectionAgency::~EnvironmentalProtectionAgency()
38 {
39         restore ();
40 }
41
42 void
43 EnvironmentalProtectionAgency::save ()
44 {
45         e.clear ();
46
47         for (size_t i = 0; environ[i]; ++i) {
48
49                 string estring = environ[i];
50                 string::size_type equal = estring.find_first_of ('=');
51
52                 if (equal == string::npos) {
53                         /* say what? an environ value without = ? */
54                         continue;
55                 }
56
57                 string before = estring.substr (0, equal);
58                 string after = estring.substr (equal+1);
59
60                 cerr << "Save [" << before << "] = " << after << endl;
61
62                 e.insert (pair<string,string>(before,after));
63         }
64 }                         
65 void
66 EnvironmentalProtectionAgency::restore ()
67 {
68         for (map<string,string>::iterator i = e.begin(); i != e.end(); ++i) {
69                 cerr << "Restore [" << i->first << "] = " << i->second << endl;
70                 setenv (i->first.c_str(), i->second.c_str(), 1);
71         }
72 }