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