1 // Created on: 2003-10-09
2 // Created by: Mikhail KUZMITCHEV
3 // Copyright (c) 2003-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <TCollection_AsciiString.hxx>
17 #include <OSD_Path.hxx>
18 #include <OSD_Directory.hxx>
19 #include <OSD_File.hxx>
20 #include <OSD_Environment.hxx>
21 #include <OSD_SharedLibrary.hxx>
22 #include <Resource_Manager.hxx>
23 #include <Draw_Interpretor.hxx>
24 #include <Draw_MapOfAsciiString.hxx>
27 static Handle(Resource_Manager) myResources;
29 //=======================================================================
30 //function : FindPluginFile
31 //purpose : Searches for the existence of the plugin file according to its name thePluginName:
32 // - if thePluginName is empty then it defaults to DrawPlugin
33 // - the search directory is defined according to the variable
34 // CSF_<filename>Defaults (if it is omitted then it defaults to
35 // $CASROOT/src/DrawResources)
36 // - finally existence of the file is verified in the search directory
37 // - if the file exists but corresponding variable (CSF_...) has not been
38 // explicitly set, it is forced to (for further reuse by Resource_Manager)
39 // Returns True if the file exists, otherwise - False.
40 //=======================================================================
42 #define FAILSTR "Failed to load plugin: "
44 //static Standard_Boolean FindPluginFile (TCollection_AsciiString& thePluginName)
45 static Standard_Boolean FindPluginFile (TCollection_AsciiString& thePluginName, TCollection_AsciiString& aPluginDir)
47 Standard_Boolean aResult = Standard_True;
49 // check if the file name has been specified and use default value if not
50 if (thePluginName.IsEmpty()) {
51 thePluginName += "DrawPlugin";
53 cout << "Plugin file name has not been specified. Defaults to " << thePluginName.ToCString() << endl;
57 //TCollection_AsciiString aPluginDir; // the search directory
58 Standard_Boolean aDirFound = Standard_True, aToSetCSFVariable = Standard_False;
60 // the order of search : by CSF_<PluginFileName>Defaults and then by CASROOT
61 TCollection_AsciiString aCSFVariable = TCollection_AsciiString ("CSF_") + thePluginName + "Defaults";
62 aPluginDir = getenv (aCSFVariable.ToCString());
64 if (aPluginDir.IsEmpty()) {
66 aPluginDir = getenv("CASROOT");
68 if ( !aPluginDir.IsEmpty() ) {
69 aPluginDir +="/src/DrawResources" ;
70 aToSetCSFVariable = Standard_True; //CSF variable to be set later
72 aResult = aDirFound = Standard_False;
73 cout << FAILSTR "Neither " << aCSFVariable.ToCString() << ", nor CASROOT variables have been set" << endl;
78 // search directory name has been constructed, now check whether it and the file exist
80 TCollection_AsciiString aPluginFileName = aPluginDir + "/" + thePluginName;
81 OSD_File PluginFile ( aPluginFileName );
82 if ( PluginFile.Exists() ) {
83 if (aToSetCSFVariable) {
84 OSD_Environment aCSFVarEnv ( aCSFVariable, aPluginDir );
87 cout << "Variable " << aCSFVariable.ToCString() << " has not been explicitly defined. Set to " << aPluginDir.ToCString() << endl;
89 if ( aCSFVarEnv.Failed() ) {
90 aResult = Standard_False;
91 cout << FAILSTR "Failed to initialize " << aCSFVariable.ToCString() << " with " << aPluginDir.ToCString() << endl;
95 aResult = Standard_False;
96 cout << FAILSTR "File " << aPluginFileName.ToCString() << " not found" << endl;
103 //=======================================================================
105 //purpose : Parse the input keys to atomic keys (<key> --> <akey>[<akey> ..])
106 //=======================================================================
108 static void Parse (Draw_MapOfAsciiString& theMap)
110 Draw_MapOfAsciiString aMap, aMap2;
111 Standard_Integer j, k;
112 Standard_Integer aMapExtent, aMap2Extent;
113 aMapExtent = theMap.Extent();
114 for(j = 1; j <= aMapExtent; j++) {
115 if (!myResources.IsNull()) {
116 const TCollection_AsciiString& aKey = theMap.FindKey(j);
117 TCollection_AsciiString aResource = aKey;
118 if(myResources->Find(aResource.ToCString())) {
120 cout << "Parse Value ==> " << myResources->Value(aResource.ToCString()) << endl;
122 TCollection_AsciiString aValue(myResources->Value(aResource.ToCString()));
123 // parse aValue string
124 Standard_Integer i=1;
126 TCollection_AsciiString aCurKey = aValue.Token(" \t,", i++);
128 cout << "Parse aCurKey = " << aCurKey.ToCString() << endl;
130 if(aCurKey.IsEmpty()) break;
131 if(!myResources->Find(aCurKey.ToCString())) {
139 cout <<"Pload : Resource = " << aResource << " is not found" << endl;
143 aMap2Extent = aMap2.Extent();
144 for(k = 1; k <= aMap2Extent; k++) {
145 aMap.Add(aMap2.FindKey(k));
154 //=======================================================================
157 //=======================================================================
159 static Standard_Integer Pload (Draw_Interpretor& di,
164 TCollection_AsciiString aPluginFileName("");
165 TCollection_AsciiString aPluginDir(""), aPluginDir2("");
166 Standard_Integer aStart = 0;
167 Standard_Integer aFinish = n - 1;
173 if(argv[1][0] == adef[0]) {
174 aPluginFileName = argv[1];
175 aPluginFileName.Remove(1,1);
177 // Load DEFAULT key from aPluginFileName file
188 //if ( !FindPluginFile (aPluginFileName) ) {
189 if ( !FindPluginFile (aPluginFileName, aPluginDir) ) {
193 Draw_MapOfAsciiString aMap;
194 TCollection_AsciiString aDEFAULT("DEFAULT");
195 //for(Standard_Integer i = aStart; i < n; i++)
196 for(Standard_Integer i = aStart; i <= aFinish; i++)
201 TCollection_AsciiString aTK(argv[i]);
205 //myResources = new Resource_Manager(aPluginFileName.ToCString());
206 myResources = new Resource_Manager(aPluginFileName.ToCString(), aPluginDir, aPluginDir2, Standard_False);
210 Standard_Integer aMapExtent;
211 aMapExtent = aMap.Extent();
212 for(j = 1; j <= aMapExtent; j++) {
213 const TCollection_AsciiString& aKey = aMap.FindKey(j);
214 TCollection_AsciiString aResource = aKey;
216 cout << "aResource = " << aResource << endl;
218 if(myResources->Find(aResource.ToCString())) {
219 const TCollection_AsciiString& aValue = myResources->Value(aResource.ToCString());
221 cout << "Value ==> " << aValue << endl;
224 //Draw::Load(di, aKey, aPluginFileName);
225 Draw::Load(di, aKey, aPluginFileName, aPluginDir, aPluginDir2, Standard_False);
228 TCollection_AsciiString aCSFVariable ("CSF_DrawPluginTclDir");
229 TCollection_AsciiString aTclScriptDir;
230 aTclScriptDir = getenv (aCSFVariable.ToCString());
231 TCollection_AsciiString aTclScriptFileName;
232 TCollection_AsciiString aTclScriptFileNameDefaults;
233 aTclScriptFileName = aTclScriptDir + "/" + aValue + ".tcl";
234 aTclScriptFileNameDefaults = aPluginDir + "/" + aValue + ".tcl";
235 OSD_File aTclScriptFile ( aTclScriptFileName );
236 OSD_File aTclScriptFileDefaults ( aTclScriptFileNameDefaults );
237 if (!aTclScriptDir.IsEmpty() && aTclScriptFile.Exists()) {
239 cout << "Load " << aTclScriptFileName << " TclScript" << endl;
241 di.EvalFile( aTclScriptFileName.ToCString() );
242 } else if (!aPluginDir.IsEmpty() && aTclScriptFileDefaults.Exists()) {
244 cout << "Load " << aTclScriptFileNameDefaults << " TclScript" << endl;
246 di.EvalFile( aTclScriptFileNameDefaults.ToCString() );
250 cout <<"Pload : Resource = " << aResource << " is not found" << endl;
255 //=======================================================================
256 //function : dtryload
258 //=======================================================================
260 static Standard_Integer dtryload (Draw_Interpretor& di, Standard_Integer n, const char** argv)
264 cout << "Error: specify path to library to be loaded" << endl;
268 OSD_SharedLibrary aLib(argv[1]);
269 if (aLib.DlOpen(OSD_RTLD_NOW))
271 di << "Loading " << argv[1] << " successful";
276 di << "Loading " << argv[1] << " failed: " << aLib.DlError();
281 //=======================================================================
282 //function : PloadCommands
284 //=======================================================================
286 void Draw::PloadCommands(Draw_Interpretor& theCommands)
288 static Standard_Boolean Done = Standard_False;
290 Done = Standard_True;
292 const char* g = "Draw Plugin";
294 theCommands.Add("pload" , "pload [-PluginFilename] [[Key1] [Key2] ...]: Loads Draw plugins " ,
296 theCommands.Add("dtryload" , "dtryload path : load and unload specified dynamic loaded library" ,
297 __FILE__, dtryload, g);