b311480e |
1 | // Copyright (c) 1998-1999 Matra Datavision |
2 | // Copyright (c) 1999-2012 OPEN CASCADE SAS |
3 | // |
4 | // The content of this file is subject to the Open CASCADE Technology Public |
5 | // License Version 6.5 (the "License"). You may not use the content of this file |
6 | // except in compliance with the License. Please obtain a copy of the License |
7 | // at http://www.opencascade.org and read it completely before using this file. |
8 | // |
9 | // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its |
10 | // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France. |
11 | // |
12 | // The Original Code and all software distributed under the License is |
13 | // distributed on an "AS IS" basis, without warranty of any kind, and the |
14 | // Initial Developer hereby disclaims all such warranties, including without |
15 | // limitation, any warranties of merchantability, fitness for a particular |
16 | // purpose or non-infringement. Please see the License for the specific terms |
17 | // and conditions governing the rights and limitations under the License. |
18 | |
7fd59977 |
19 | |
20 | #ifndef WNT |
21 | |
22 | #include <Standard_NullObject.hxx> |
23 | #include <Standard_ConstructionError.hxx> |
24 | #include <Standard_Failure.hxx> |
25 | #include <Standard_Mutex.hxx> |
26 | #include <OSD_Environment.ixx> |
27 | #include <OSD_WhoAmI.hxx> |
28 | |
29 | #include <stdlib.h> |
30 | #include <stdio.h> |
31 | #include <string.h> |
32 | |
33 | #include <errno.h> |
34 | |
35 | static const OSD_WhoAmI Iam = OSD_WEnvironment; |
36 | |
37 | // ---------------------------------------------------------------------- |
38 | // |
39 | // Updated by : JPT Dec,7 1992 |
40 | // What : OSD_Environment::OSD_Environment |
41 | // (const TCollection_AsciiString& Name, |
42 | // const TCollection_AsciiString& Value) |
43 | // Value could contain the character $ (Ex setenv a = $c) |
44 | // |
45 | // ---------------------------------------------------------------------- |
46 | // Create object |
47 | |
48 | OSD_Environment::OSD_Environment() |
49 | { |
50 | } |
51 | |
52 | |
53 | // ---------------------------------------------------------------------- |
54 | // Constructor |
55 | // ---------------------------------------------------------------------- |
56 | |
57 | OSD_Environment::OSD_Environment(const TCollection_AsciiString& Name) |
58 | { |
59 | |
60 | if (!Name.IsAscii() || Name.Search("$") != -1 ) |
61 | Standard_ConstructionError::Raise("OSD_Environment::OSD_Environment: bad argument"); |
62 | |
63 | myName = Name; |
64 | } |
65 | |
66 | |
67 | // ---------------------------------------------------------------------- |
68 | // Create an environment variable and initialize it |
69 | // ---------------------------------------------------------------------- |
70 | |
71 | OSD_Environment::OSD_Environment(const TCollection_AsciiString& Name, |
72 | const TCollection_AsciiString& Value) |
73 | { |
74 | |
75 | if (!Name.IsAscii() || !Value.IsAscii() || |
76 | // JPT : Dec-7-1992 Name.Search("$") != -1 || Value.Search("$") != -1) |
77 | Name.Search("$") != -1 ) |
78 | Standard_ConstructionError::Raise("OSD_Environment::OSD_Environment: bad argument"); |
79 | |
80 | myName = Name; |
81 | myValue = Value; |
82 | } |
83 | |
84 | |
85 | // ---------------------------------------------------------------------- |
86 | // Returns the name of the symbol |
87 | // ---------------------------------------------------------------------- |
88 | |
89 | TCollection_AsciiString OSD_Environment::Name () const |
90 | { |
91 | return myName; |
92 | } |
93 | |
94 | // ---------------------------------------------------------------------- |
95 | // Set new value for environment variable |
96 | // ---------------------------------------------------------------------- |
97 | |
98 | void OSD_Environment::SetName (const TCollection_AsciiString& Name) |
99 | { |
100 | myError.Reset(); |
101 | if (!Name.IsAscii() || Name.Search("$") != -1 ) |
102 | Standard_ConstructionError::Raise("OSD_Environment::SetName: bad argument"); |
103 | |
104 | myName = Name; |
105 | } |
106 | |
107 | // ---------------------------------------------------------------------- |
108 | // Change value |
109 | // ---------------------------------------------------------------------- |
110 | |
111 | void OSD_Environment::SetValue (const TCollection_AsciiString& Value) |
112 | { |
113 | if (!Value.IsAscii() || Value.Search("$") != -1) |
114 | Standard_ConstructionError::Raise("OSD_Environment::Change: bad argument"); |
115 | |
116 | myValue = Value; |
117 | } |
118 | |
119 | // ---------------------------------------------------------------------- |
120 | // Get environment variable physically |
121 | // ---------------------------------------------------------------------- |
122 | |
123 | TCollection_AsciiString OSD_Environment::Value() |
124 | { |
125 | char *result = getenv(myName.ToCString()); |
126 | if (result == NULL) myValue.Clear(); |
127 | else myValue = result; |
128 | return myValue; |
129 | } |
130 | |
131 | // ---------------------------------------------------------------------- |
132 | // Sets physically the environment variable |
133 | // ---------------------------------------------------------------------- |
134 | |
135 | void OSD_Environment::Build () |
136 | { |
137 | // Static buffer to hold definitions of new variables for the environment. |
138 | // Note that they need to be static since putenv does not make a copy |
139 | // of the string, but just adds its pointer to the environment. |
140 | static char **buffer = 0 ; // JPT: |
141 | static int Ibuffer = 0 ; // Tout ca pour putenv,getenv |
142 | |
143 | // Use mutex to avoid concurrent access to the buffer |
144 | static Standard_Mutex theMutex; |
145 | Standard_Mutex::Sentry aSentry ( theMutex ); |
146 | |
147 | // check if such variable has already been created in the buffer |
148 | int index = -1, len = myName.Length(); |
149 | for ( int i=0; i < Ibuffer; i++ ) { |
150 | if ( ! strncmp ( buffer[i], myName.ToCString(), len ) && buffer[i][len] == '=' ) { |
151 | index = i; |
152 | break; |
153 | } |
154 | } |
155 | |
156 | // and either add a new entry, or remember the old entry for a while |
157 | char *old_value = 0; |
158 | if ( index >=0 ) { |
159 | old_value = buffer[index]; |
160 | } |
161 | else { |
162 | // Allocation memoire. Surtout tout la heap! |
163 | index = Ibuffer++; |
8413a813 |
164 | char **aTmp; |
165 | aTmp = (char **) realloc ( buffer, Ibuffer * sizeof(char*) ); |
166 | if (aTmp) |
167 | { |
168 | buffer = aTmp; |
169 | } |
170 | else |
171 | { |
172 | myError.SetValue(errno, Iam, "Memory realloc failure"); |
173 | return; |
174 | } |
7fd59977 |
175 | } |
176 | |
177 | // create a new entry in the buffer and add it to environment |
178 | buffer[index] = (char *) malloc ( len + myValue.Length() + 2 ); |
179 | sprintf(buffer[index], "%s=%s", myName.ToCString(), myValue.ToCString()); |
180 | putenv(buffer[index]); |
181 | |
182 | // then (and only then!) free old entry, if existed |
183 | if ( old_value ) |
184 | free ( old_value ); |
185 | |
186 | // check the result |
187 | char *result = getenv(myName.ToCString()); |
188 | if (result == NULL) |
189 | myError.SetValue(errno, Iam, "Set Environment"); |
190 | } |
191 | |
192 | // ---------------------------------------------------------------------- |
193 | // Remove physically the environment variable |
194 | // ---------------------------------------------------------------------- |
195 | |
196 | void OSD_Environment::Remove () |
197 | { |
198 | myValue.Clear(); |
199 | Build(); |
200 | } |
201 | |
202 | |
203 | |
204 | // ---------------------------------------------------------------------- |
205 | // ---------------------------------------------------------------------- |
206 | void OSD_Environment::Reset() |
207 | { |
208 | myError.Reset(); |
209 | } |
210 | |
211 | // ---------------------------------------------------------------------- |
212 | // ---------------------------------------------------------------------- |
213 | Standard_Boolean OSD_Environment::Failed() const |
214 | { |
215 | return myError.Failed(); |
216 | } |
217 | |
218 | // ---------------------------------------------------------------------- |
219 | // ---------------------------------------------------------------------- |
220 | void OSD_Environment::Perror() |
221 | { |
222 | myError.Perror(); |
223 | } |
224 | |
225 | |
226 | // ---------------------------------------------------------------------- |
227 | // ---------------------------------------------------------------------- |
228 | Standard_Integer OSD_Environment::Error() const |
229 | { |
230 | return myError.Error(); |
231 | } |
232 | |
233 | #else |
234 | |
235 | //------------------------------------------------------------------------ |
236 | //------------------- WNT Sources of OSD_Environment -------------------- |
237 | //------------------------------------------------------------------------ |
238 | |
239 | #define STRICT |
240 | #include <OSD_Environment.hxx> |
241 | |
242 | #include <OSD_WNT.hxx> |
243 | |
244 | #include <windows.h> |
245 | |
246 | #pragma warning( disable : 4700 ) |
247 | |
248 | static void __fastcall _set_error ( OSD_Error&, DWORD ); |
249 | |
250 | OSD_Environment :: OSD_Environment () { |
251 | |
252 | } // end constructor ( 1 ) |
253 | |
254 | OSD_Environment :: OSD_Environment ( const TCollection_AsciiString& Name ) { |
255 | |
256 | myName = Name; |
257 | |
258 | } // end constructor ( 2 ) |
259 | |
260 | OSD_Environment :: OSD_Environment ( |
261 | const TCollection_AsciiString& Name, |
262 | const TCollection_AsciiString& Value |
263 | ) { |
264 | |
265 | myName = Name; |
266 | myValue = Value; |
267 | |
268 | } // end constructor ( 3 ) |
269 | |
270 | void OSD_Environment :: SetValue ( const TCollection_AsciiString& Value ) { |
271 | |
272 | myValue = Value; |
273 | |
274 | } // end OSD_Environment :: SetValue |
275 | |
276 | TCollection_AsciiString OSD_Environment :: Value () { |
277 | |
278 | Standard_PCharacter pBuff; |
279 | DWORD dwSize = 0; |
280 | char* envVal = NULL; |
281 | |
282 | myValue.Clear (); |
283 | |
284 | SetLastError ( ERROR_SUCCESS ); |
285 | dwSize = GetEnvironmentVariable ( myName.ToCString (), pBuff, dwSize ); |
286 | |
287 | if ( ( dwSize == 0 && GetLastError () != ERROR_SUCCESS ) || |
288 | ( envVal = getenv ( myName.ToCString () ) ) == NULL |
289 | ) |
290 | |
291 | _set_error ( myError, ERROR_ENVVAR_NOT_FOUND ); |
292 | |
293 | else if ( envVal != NULL ) |
294 | |
295 | myValue = envVal; |
296 | |
297 | else { |
298 | |
299 | ++dwSize; |
300 | pBuff = new Standard_Character[ dwSize ]; |
301 | GetEnvironmentVariable ( (char *)myName.ToCString (), pBuff, dwSize ); |
302 | myValue = pBuff; |
303 | delete [] pBuff; |
304 | Reset (); |
305 | |
306 | } // end else |
307 | |
308 | return myValue; |
309 | |
310 | } // end OSD_Environment :: Value |
311 | |
312 | void OSD_Environment :: SetName ( const TCollection_AsciiString& name ) { |
313 | |
314 | myName = name; |
315 | |
316 | } // end OSD_Environment :: SetName |
317 | |
318 | TCollection_AsciiString OSD_Environment :: Name () const { |
319 | |
320 | return myName; |
321 | |
322 | } // end OSD_Environment :: Name |
323 | |
324 | void OSD_Environment :: Build () { |
325 | |
326 | TCollection_AsciiString str; |
327 | |
328 | str = myName + TEXT( "=" ) + myValue; |
329 | |
330 | putenv ( str.ToCString () ); |
331 | |
332 | } // end OSD_Environment :: Build |
333 | |
334 | void OSD_Environment :: Remove () { |
335 | |
336 | TCollection_AsciiString str; |
337 | |
338 | str = myName + TEXT( "=" ); |
339 | |
340 | putenv ( str.ToCString () ); |
341 | |
342 | } // end OSD_Environment :: Remove |
343 | |
344 | Standard_Boolean OSD_Environment :: Failed () const { |
345 | |
346 | return myError.Failed (); |
347 | |
348 | } // end OSD_Environment :: Failed |
349 | |
350 | void OSD_Environment :: Reset () { |
351 | |
352 | myError.Reset (); |
353 | |
354 | } // end OSD_Environment :: Reset |
355 | |
356 | void OSD_Environment :: Perror () { |
357 | |
358 | if ( ErrorPrefix () ) |
359 | |
360 | ( *ErrorStream () ) << TEXT( '\'' ) << myName.ToCString () << TEXT( "' - " ); |
361 | |
362 | myError.Perror (); |
363 | |
364 | } // end OSD_Environment :: Perror |
365 | |
366 | Standard_Integer OSD_Environment :: Error () const { |
367 | |
368 | return myError.Error (); |
369 | |
370 | } // end OSD_Environment :: Error |
371 | |
372 | static void __fastcall _set_error ( OSD_Error& err, DWORD code ) { |
373 | |
374 | DWORD errCode; |
375 | Standard_Character buffer[ 2048 ]; |
376 | |
377 | errCode = code ? code : GetLastError (); |
378 | |
379 | if ( !FormatMessage ( |
380 | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY, |
381 | 0, errCode, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ), |
382 | buffer, 2048, NULL |
383 | ) |
384 | ) { |
385 | |
386 | sprintf ( buffer, "error code %d", (Standard_Integer)errCode ); |
387 | SetLastError ( errCode ); |
388 | |
389 | } // end if |
390 | |
391 | err.SetValue ( errCode, OSD_WEnvironment, buffer ); |
392 | |
393 | } // end _set_error |
394 | |
395 | #endif |