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