1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #include <Resource_Manager.hxx>
17 #include <OSD_Directory.hxx>
18 #include <OSD_Environment.hxx>
19 #include <OSD_File.hxx>
20 #include <OSD_Path.hxx>
21 #include <OSD_Protection.hxx>
22 #include <Resource_LexicalCompare.hxx>
23 #include <Resource_NoSuchResource.hxx>
24 #include <Resource_Unicode.hxx>
25 #include <Standard_ErrorHandler.hxx>
26 #include <Standard_OutOfRange.hxx>
27 #include <Standard_Type.hxx>
28 #include <Standard_TypeMismatch.hxx>
29 #include <TCollection_AsciiString.hxx>
30 #include <TCollection_ExtendedString.hxx>
31 #include <TColStd_Array1OfAsciiString.hxx>
36 IMPLEMENT_STANDARD_RTTIEXT(Resource_Manager,Standard_Transient)
38 //! Auxiliary enumeration for function WhatKindOfLine().
39 enum Resource_KindOfLine
44 Resource_KOL_Resource,
48 static Resource_KindOfLine WhatKindOfLine(OSD_File& aFile,
49 TCollection_AsciiString& aToken1,
50 TCollection_AsciiString& aToken2);
52 static Standard_Integer GetLine(OSD_File& aFile,TCollection_AsciiString& aLine);
54 static Standard_Boolean Debug;
56 // =======================================================================
57 // function : Resource_Manager
59 // =======================================================================
60 Resource_Manager::Resource_Manager (const TCollection_AsciiString& theName,
61 const TCollection_AsciiString& theDefaultsDirectory,
62 const TCollection_AsciiString& theUserDefaultsDirectory,
63 const Standard_Boolean theIsVerbose)
65 myVerbose (theIsVerbose)
67 if (!theDefaultsDirectory.IsEmpty())
69 OSD_Path anOSDPath (theDefaultsDirectory);
70 if (!anOSDPath.Name().IsEmpty())
72 anOSDPath.DownTrek (anOSDPath.Name() + anOSDPath.Extension());
74 anOSDPath.SetName (theName);
75 anOSDPath.SetExtension ("");
76 TCollection_AsciiString aPath;
77 anOSDPath.SystemName (aPath);
78 Load (aPath, myRefMap);
82 std::cout << "Resource Manager Warning: aDefaultsDirectory is empty." << std::endl;
85 if (!theUserDefaultsDirectory.IsEmpty())
87 OSD_Path anOSDPath (theUserDefaultsDirectory);
88 if (!anOSDPath.Name().IsEmpty())
90 anOSDPath.DownTrek (anOSDPath.Name() + anOSDPath.Extension());
92 anOSDPath.SetName (theName);
93 anOSDPath.SetExtension ("");
94 TCollection_AsciiString aPath;
95 anOSDPath.SystemName (aPath);
96 Load (aPath, myRefMap);
100 std::cout << "Resource Manager Warning: anUserDefaultsDirectory is empty." << std::endl;
104 Resource_Manager::Resource_Manager(const Standard_CString aName,
105 const Standard_Boolean Verbose) : myName(aName), myVerbose(Verbose)
107 OSD_Environment envDebug("ResourceDebug");
108 Debug = (!envDebug.Value().IsEmpty()) ;
110 TCollection_AsciiString Directory ;
112 OSD_Environment envVerbose("CSF_ResourceVerbose");
113 if (!envVerbose.Value().IsEmpty())
114 myVerbose = Standard_True;
116 TCollection_AsciiString aPath,aUserPath;
117 GetResourcePath(aPath,aName,Standard_False);
118 GetResourcePath(aUserPath,aName,Standard_True);
120 if (!aPath.IsEmpty())
121 Load(aPath,myRefMap);
123 std::cout << "Resource Manager Warning: Environment variable \"CSF_" << aName << "Defaults\" not set." << std::endl;
125 if (!aUserPath.IsEmpty())
126 Load(aUserPath,myRefMap);
128 std::cout << "Resource Manager Warning: Environment variable \"CSF_" << aName << "UserDefaults\" not set." << std::endl;
131 // =======================================================================
134 // =======================================================================
135 void Resource_Manager::Load(const TCollection_AsciiString& thePath,
136 Resource_DataMapOfAsciiStringAsciiString& aMap)
138 Resource_KindOfLine aKind;
139 TCollection_AsciiString Token1, Token2;
140 OSD_Path Path (thePath);
141 OSD_File File = Path;
142 TCollection_AsciiString FileName = Path.Name();
143 File.Open(OSD_ReadOnly,OSD_Protection());
146 std::cout << "Resource Manager Warning: Cannot read file \"" << FileName
147 << "\". File not found or permission denied." << std::endl;
150 Standard_Integer LineNumber = 1;
151 while ((aKind = WhatKindOfLine(File, Token1, Token2)) != Resource_KOL_End) {
153 case Resource_KOL_End:
154 case Resource_KOL_Comment:
155 case Resource_KOL_Empty:
157 case Resource_KOL_Resource:
158 if (!aMap.Bind(Token1,Token2))
159 aMap(Token1) = Token2;
161 case Resource_KOL_Error:
163 std::cout << "Resource Manager: Syntax error at line "
164 << LineNumber << " in file : " << FileName << std::endl;
171 std::cout << "Resource Manager: " << ((&aMap == &myUserMap) ? "User" : "Reference")
172 << " file \"" << FileName << "\" loaded" << std::endl;
175 static Resource_KindOfLine WhatKindOfLine(OSD_File& aFile,
176 TCollection_AsciiString& aToken1,
177 TCollection_AsciiString& aToken2)
179 TCollection_AsciiString WhiteSpace = " \t" ;
180 Standard_Integer Pos1,Pos2,Pos ;
181 TCollection_AsciiString Line ;
183 if (!GetLine(aFile,Line))
184 return Resource_KOL_End;
186 if (Line.Value(1) == '!')
187 return Resource_KOL_Comment;
189 Pos1 = Line.FirstLocationNotInSet(WhiteSpace, 1, Line.Length());
190 if (Line.Value(Pos1) == '\n')
191 return Resource_KOL_Empty;
193 Pos2 = Line.Location(1,':',Pos1,Line.Length());
194 if (!Pos2 || Pos1 == Pos2)
195 return Resource_KOL_Error;
197 for (Pos = Pos2-1; Line.Value(Pos) == '\t' || Line.Value(Pos) == ' ' ; Pos--);
198 aToken1 = Line.SubString(Pos1, Pos);
201 std::cout << "Key = '" << aToken1 << std::flush ;
203 Pos = Line.FirstLocationNotInSet(WhiteSpace, Pos2+1, Line.Length());
205 if (Line.Value(Pos) == '\\')
206 switch(Line.Value(Pos+1)) {
214 if (Pos == Line.Length())
217 Line.Remove(1,Pos-1);
218 Line.Remove(Line.Length());
222 std::cout << "'\t Value = '" << aToken2 << "'" << std::endl << std::flush;
223 return Resource_KOL_Resource;
226 // Retourne 0 (EOF) ou une ligne toujours terminee par <NL>.
228 static Standard_Integer GetLine(OSD_File& aFile,TCollection_AsciiString& aLine)
230 TCollection_AsciiString Buffer;
231 Standard_Integer BufSize = 10;
232 Standard_Integer Len;
236 aFile.ReadLine(Buffer,BufSize,Len);
238 if (aFile.IsAtEnd()) {
239 if (!aLine.Length()) return 0;
242 } while (aLine.Value(aLine.Length()) != '\n');
247 //=======================================================================
249 //purpose : Sort and save the user resources in the user file.
250 // Creates the file if it does not exist.
251 //=======================================================================
252 Standard_Boolean Resource_Manager::Save() const
254 TCollection_AsciiString anEnvVar("CSF_");
256 anEnvVar += "UserDefaults";
258 TCollection_AsciiString dir;
259 OSD_Environment anEnv(anEnvVar);
263 std::cout << "Resource Manager Warning: environment variable \""
264 << anEnvVar << "\" not set. Cannot save resources." << std::endl ;
265 return Standard_False;
268 TCollection_AsciiString aFilePath(dir);
269 OSD_Path anOSDPath(aFilePath);
270 OSD_Directory Dir = anOSDPath;
271 Standard_Boolean aStatus = Standard_True;
272 if ( !Dir.Exists() ) {
276 Dir.Build(OSD_Protection(OSD_RX, OSD_RWXD, OSD_RX, OSD_RX));
278 catch (Standard_Failure const&) {
279 aStatus = Standard_False;
282 aStatus = aStatus && !Dir.Failed();
285 std::cout << "Resource Manager: Error opening or creating directory \"" << aFilePath
286 << "\". Permission denied. Cannot save resources." << std::endl;
287 return Standard_False;
291 if (!anOSDPath.Name().IsEmpty())
293 anOSDPath.DownTrek (anOSDPath.Name () + anOSDPath.Extension ());
295 anOSDPath.SetName(myName);
296 anOSDPath.SetExtension("");
297 anOSDPath.SystemName(aFilePath);
299 OSD_File File = anOSDPath;
300 OSD_Protection theProt;
301 aStatus = Standard_True;
305 File.Build(OSD_ReadWrite, theProt);
307 catch (Standard_Failure const&) {
308 aStatus = Standard_False;
311 aStatus = aStatus && !File.Failed();
314 std::cout << "Resource Manager: Error opening or creating file \"" << aFilePath
315 << "\". Permission denied. Cannot save resources." << std::endl;
316 return Standard_False;
319 const Standard_Integer NbKey = myUserMap.Extent();
322 TColStd_Array1OfAsciiString KeyArray(1,NbKey);
323 Resource_DataMapIteratorOfDataMapOfAsciiStringAsciiString Iter(myUserMap);
325 Standard_Integer Index;
326 for ( Index = 1; Iter.More(); Iter.Next())
327 KeyArray(Index++)= Iter.Key();
329 std::sort (KeyArray.begin(), KeyArray.end());
331 TCollection_AsciiString Line, Value;
332 for (Index = 1 ; Index <= NbKey ; Index++) {
333 Value = myUserMap(KeyArray(Index));
334 if (!Value.IsEmpty())
335 switch(Value.Value(1)) {
339 Value.Insert(1,'\\');
342 Line = KeyArray(Index) + ":\t" + Value + "\n";
345 std::cout << "Line = '" << Line << "'" << std::endl;
347 File.Write(Line, Line.Length());
350 std::cout << "Resource Manager: Resources saved in file " << aFilePath << std::endl;
353 return Standard_True;
356 //=======================================================================
358 //purpose : Gets the value of an integer resource
359 //=======================================================================
361 Standard_Integer Resource_Manager::Integer(const Standard_CString aResourceName) const
363 TCollection_AsciiString Result = Value(aResourceName) ;
364 if (!Result.IsIntegerValue()) {
365 TCollection_AsciiString n("Value of resource `");
367 n+= "` is not an integer";
368 throw Standard_TypeMismatch(n.ToCString());
370 return Result.IntegerValue();
373 //=======================================================================
375 //purpose : Gets the value of a real resource
376 //=======================================================================
378 Standard_Real Resource_Manager::Real(const Standard_CString aResourceName) const
380 TCollection_AsciiString Result = Value(aResourceName) ;
381 if (!Result.IsRealValue()) {
382 TCollection_AsciiString n("Value of resource `");
384 n+= "` is not a real";
385 throw Standard_TypeMismatch(n.ToCString());
387 return Result.RealValue();
390 //=======================================================================
392 //purpose : Gets the value of a CString resource
393 //=======================================================================
395 Standard_CString Resource_Manager::Value(const Standard_CString aResource) const
397 TCollection_AsciiString Resource(aResource);
398 if (myUserMap.IsBound(Resource))
399 return myUserMap(Resource).ToCString();
400 if (myRefMap.IsBound(Resource))
401 return myRefMap(Resource).ToCString();
402 throw Resource_NoSuchResource(aResource);
405 //=======================================================================
406 //function : ExtValue
407 //purpose : Gets the value of a ExtString resource
408 //=======================================================================
410 Standard_ExtString Resource_Manager::ExtValue(const Standard_CString aResource)
412 TCollection_AsciiString Resource(aResource);
413 if (myExtStrMap.IsBound(Resource))
414 return myExtStrMap(Resource).ToExtString();
416 TCollection_AsciiString Result = Value(aResource);
417 TCollection_ExtendedString ExtResult;
419 Resource_Unicode::ConvertFormatToUnicode(Result.ToCString(),ExtResult);
421 myExtStrMap.Bind(Resource, ExtResult);
422 return myExtStrMap(Resource).ToExtString();
425 //=======================================================================
426 //function : SetResource
427 //purpose : Sets the new value of an integer resource.
428 // If the resource does not exist, it is created.
429 //=======================================================================
430 void Resource_Manager::SetResource(const Standard_CString aResourceName,
431 const Standard_Integer aValue)
433 SetResource(aResourceName,TCollection_AsciiString(aValue).ToCString());
436 //=======================================================================
437 //function : SetResource
438 //purpose : Sets the new value of a real resource.
439 // If the resource does not exist, it is created.
440 //=======================================================================
441 void Resource_Manager::SetResource(const Standard_CString aResourceName,
442 const Standard_Real aValue)
444 SetResource(aResourceName,TCollection_AsciiString(aValue).ToCString());
447 //=======================================================================
448 //function : SetResource
449 //purpose : Sets the new value of ExtString resource.
450 // If the resource does not exist, it is created.
451 //=======================================================================
452 void Resource_Manager::SetResource(const Standard_CString aResource,
453 const Standard_ExtString aValue)
455 Standard_PCharacter pStr;
456 TCollection_AsciiString Resource = aResource;
457 TCollection_ExtendedString ExtValue = aValue;
458 TCollection_AsciiString FormatStr(ExtValue.Length()*3+10, ' ');
460 if (!myExtStrMap.Bind(Resource,ExtValue)) {
461 myExtStrMap(Resource) = ExtValue;
464 pStr=(Standard_PCharacter)FormatStr.ToCString();
466 Resource_Unicode::ConvertUnicodeToFormat(ExtValue,
467 pStr,//FormatStr.ToCString(),
468 FormatStr.Length()) ;
469 SetResource(aResource,FormatStr.ToCString());
472 //=======================================================================
473 //function : SetResource
474 //purpose : Sets the new value of an enum resource.
475 // If the resource does not exist, it is created.
476 //=======================================================================
477 void Resource_Manager::SetResource(const Standard_CString aResource,
478 const Standard_CString aValue)
480 TCollection_AsciiString Resource = aResource;
481 TCollection_AsciiString Value = aValue;
482 if (!myUserMap.Bind(Resource, Value))
483 myUserMap(Resource) = Value;
486 //=======================================================================
488 //purpose : Tells if a resource exits.
489 //=======================================================================
490 Standard_Boolean Resource_Manager::Find(const Standard_CString aResource) const
492 TCollection_AsciiString Resource(aResource);
493 if (myUserMap.IsBound(Resource) || myRefMap.IsBound(Resource))
494 return Standard_True;
495 return Standard_False;
498 //=======================================================================
501 //=======================================================================
502 Standard_Boolean Resource_Manager::Find (const TCollection_AsciiString& theResource,
503 TCollection_AsciiString& theValue) const
505 return myUserMap.Find (theResource, theValue)
506 || myRefMap .Find (theResource, theValue);
509 //=======================================================================
510 //function : GetResourcePath
512 //=======================================================================
514 void Resource_Manager::GetResourcePath (TCollection_AsciiString& aPath, const Standard_CString aName, const Standard_Boolean isUserDefaults)
518 TCollection_AsciiString anEnvVar("CSF_");
520 anEnvVar += isUserDefaults?"UserDefaults":"Defaults";
522 TCollection_AsciiString dir;
523 OSD_Environment anEnv(anEnvVar);
528 TCollection_AsciiString aResPath(dir);
530 OSD_Path anOSDPath(aResPath);
532 if (!anOSDPath.Name().IsEmpty())
534 anOSDPath.DownTrek (anOSDPath.Name () + anOSDPath.Extension ());
536 anOSDPath.SetName (aName);
537 anOSDPath.SetExtension ("");
539 anOSDPath.SystemName(aPath);