b311480e |
1 | // Created on: 2003-10-09 |
2 | // Created by: Mikhail KUZMITCHEV |
973c2be1 |
3 | // Copyright (c) 2003-2014 OPEN CASCADE SAS |
b311480e |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
b311480e |
6 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
b311480e |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
7fd59977 |
15 | |
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 <Resource_Manager.hxx> |
22 | #include <Draw_Interpretor.hxx> |
23 | #include <Draw_MapOfAsciiString.hxx> |
24 | #include <Draw.hxx> |
25 | |
26 | static Handle(Resource_Manager) myResources; |
27 | |
28 | //======================================================================= |
29 | //function : FindPluginFile |
30 | //purpose : Searches for the existence of the plugin file according to its name thePluginName: |
31 | // - if thePluginName is empty then it defaults to DrawPlugin |
32 | // - the search directory is defined according to the variable |
33 | // CSF_<filename>Defaults (if it is omitted then it defaults to |
34 | // $CASROOT/src/DrawResources) |
35 | // - finally existence of the file is verified in the search directory |
36 | // - if the file exists but corresponding variable (CSF_...) has not been |
37 | // explicitly set, it is forced to (for further reuse by Resource_Manager) |
38 | // Returns True if the file exists, otherwise - False. |
39 | //======================================================================= |
40 | |
41 | #define FAILSTR "Failed to load plugin: " |
42 | |
43 | //static Standard_Boolean FindPluginFile (TCollection_AsciiString& thePluginName) |
44 | static Standard_Boolean FindPluginFile (TCollection_AsciiString& thePluginName, TCollection_AsciiString& aPluginDir) |
45 | { |
46 | Standard_Boolean aResult = Standard_True; |
47 | |
48 | // check if the file name has been specified and use default value if not |
49 | if (thePluginName.IsEmpty()) { |
50 | thePluginName += "DrawPlugin"; |
51 | #ifdef DEB |
52 | cout << "Plugin file name has not been specified. Defaults to " << thePluginName.ToCString() << endl; |
53 | #endif |
54 | } |
55 | |
56 | //TCollection_AsciiString aPluginDir; // the search directory |
57 | Standard_Boolean aDirFound = Standard_True, aToSetCSFVariable = Standard_False; |
58 | |
59 | // the order of search : by CSF_<PluginFileName>Defaults and then by CASROOT |
60 | TCollection_AsciiString aCSFVariable = TCollection_AsciiString ("CSF_") + thePluginName + "Defaults"; |
61 | aPluginDir = getenv (aCSFVariable.ToCString()); |
62 | |
63 | if (aPluginDir.IsEmpty()) { |
64 | // now try by CASROOT |
65 | aPluginDir = getenv("CASROOT"); |
66 | |
67 | if ( !aPluginDir.IsEmpty() ) { |
68 | aPluginDir +="/src/DrawResources" ; |
69 | aToSetCSFVariable = Standard_True; //CSF variable to be set later |
70 | } else { |
71 | aResult = aDirFound = Standard_False; |
72 | cout << FAILSTR "Neither " << aCSFVariable.ToCString() << ", nor CASROOT variables have been set" << endl; |
73 | } |
74 | } |
75 | |
76 | if (aDirFound) { |
77 | // search directory name has been constructed, now check whether it and the file exist |
78 | |
79 | TCollection_AsciiString aPluginFileName = aPluginDir + "/" + thePluginName; |
80 | OSD_File PluginFile ( aPluginFileName ); |
81 | if ( PluginFile.Exists() ) { |
82 | if (aToSetCSFVariable) { |
83 | OSD_Environment aCSFVarEnv ( aCSFVariable, aPluginDir ); |
84 | aCSFVarEnv.Build(); |
85 | #ifdef DEB |
86 | cout << "Variable " << aCSFVariable.ToCString() << " has not been explicitly defined. Set to " << aPluginDir.ToCString() << endl; |
87 | #endif |
88 | if ( aCSFVarEnv.Failed() ) { |
89 | aResult = Standard_False; |
90 | cout << FAILSTR "Failed to initialize " << aCSFVariable.ToCString() << " with " << aPluginDir.ToCString() << endl; |
91 | } |
92 | } |
93 | } else { |
94 | aResult = Standard_False; |
95 | cout << FAILSTR "File " << aPluginFileName.ToCString() << " not found" << endl; |
96 | } |
97 | } |
98 | |
99 | return aResult; |
100 | } |
101 | |
102 | //======================================================================= |
103 | //function : Parse |
104 | //purpose : Parse the input keys to atomic keys (<key> --> <akey>[<akey> ..]) |
105 | //======================================================================= |
106 | |
107 | static void Parse (Draw_MapOfAsciiString& theMap) |
108 | { |
109 | Draw_MapOfAsciiString aMap, aMap2; |
110 | Standard_Integer j, k; |
111 | Standard_Integer aMapExtent, aMap2Extent; |
112 | aMapExtent = theMap.Extent(); |
113 | for(j = 1; j <= aMapExtent; j++) { |
114 | if (!myResources.IsNull()) { |
115 | const TCollection_AsciiString& aKey = theMap.FindKey(j); |
116 | TCollection_AsciiString aResource = aKey; |
117 | if(myResources->Find(aResource.ToCString())) { |
118 | #ifdef DEB |
119 | cout << "Parse Value ==> " << myResources->Value(aResource.ToCString()) << endl; |
120 | #endif |
121 | TCollection_AsciiString aValue(myResources->Value(aResource.ToCString())); |
122 | // parse aValue string |
123 | Standard_Integer i=1; |
124 | for(;;) { |
125 | TCollection_AsciiString aCurKey = aValue.Token(" \t,", i++); |
126 | #ifdef DEB |
127 | cout << "Parse aCurKey = " << aCurKey.ToCString() << endl; |
128 | #endif |
129 | if(aCurKey.IsEmpty()) break; |
130 | if(!myResources->Find(aCurKey.ToCString())) { |
131 | // It is toolkit |
132 | aMap.Add(aResource); |
133 | } |
134 | else |
135 | aMap2.Add(aCurKey); |
136 | } |
137 | } else |
138 | cout <<"Pload : Resource = " << aResource << " is not found" << endl; |
139 | if(!aMap2.IsEmpty()) |
140 | Parse(aMap2); |
141 | // |
142 | aMap2Extent = aMap2.Extent(); |
143 | for(k = 1; k <= aMap2Extent; k++) { |
144 | aMap.Add(aMap2.FindKey(k)); |
145 | } |
146 | |
147 | } |
148 | } |
149 | |
150 | theMap.Assign(aMap); |
151 | } |
152 | |
153 | //======================================================================= |
154 | //function : Pload |
155 | //purpose : |
156 | //======================================================================= |
157 | |
158 | static Standard_Integer Pload (Draw_Interpretor& di, |
159 | Standard_Integer n, |
160 | const char** argv) |
161 | { |
162 | char adef[] = "-"; |
163 | TCollection_AsciiString aPluginFileName(""); |
164 | TCollection_AsciiString aPluginDir(""), aPluginDir2(""); |
165 | Standard_Integer aStart = 0; |
166 | Standard_Integer aFinish = n - 1; |
167 | |
168 | if (n == 1) { |
169 | // Load DEFAULT key |
170 | aStart = 0; |
171 | } else { |
172 | if(argv[1][0] == adef[0]) { |
173 | aPluginFileName = argv[1]; |
174 | aPluginFileName.Remove(1,1); |
175 | if (n == 2) { |
176 | // Load DEFAULT key from aPluginFileName file |
177 | aStart = 0; |
178 | aFinish = n - 2; |
179 | } else { |
180 | aStart = 2; |
181 | } |
182 | } else { |
183 | aStart = 1; |
184 | } |
185 | } |
186 | |
187 | //if ( !FindPluginFile (aPluginFileName) ) { |
188 | if ( !FindPluginFile (aPluginFileName, aPluginDir) ) { |
189 | return 1; |
190 | } |
191 | |
192 | Draw_MapOfAsciiString aMap; |
193 | TCollection_AsciiString aDEFAULT("DEFAULT"); |
194 | //for(Standard_Integer i = aStart; i < n; i++) |
195 | for(Standard_Integer i = aStart; i <= aFinish; i++) |
196 | if (i == 0) { |
197 | // Load DEFAULT key |
198 | aMap.Add(aDEFAULT); |
199 | } else { |
200 | TCollection_AsciiString aTK(argv[i]); |
201 | aMap.Add(aTK); |
202 | } |
203 | |
204 | //myResources = new Resource_Manager(aPluginFileName.ToCString()); |
205 | myResources = new Resource_Manager(aPluginFileName.ToCString(), aPluginDir, aPluginDir2, Standard_False); |
206 | |
207 | Parse(aMap); |
208 | Standard_Integer j; |
209 | Standard_Integer aMapExtent; |
210 | aMapExtent = aMap.Extent(); |
211 | for(j = 1; j <= aMapExtent; j++) { |
212 | const TCollection_AsciiString& aKey = aMap.FindKey(j); |
213 | TCollection_AsciiString aResource = aKey; |
214 | #ifdef DEB |
215 | cout << "aResource = " << aResource << endl; |
216 | #endif |
217 | if(myResources->Find(aResource.ToCString())) { |
218 | const TCollection_AsciiString& aValue = myResources->Value(aResource.ToCString()); |
219 | #ifdef DEB |
220 | cout << "Value ==> " << aValue << endl; |
221 | #endif |
222 | |
223 | //Draw::Load(di, aKey, aPluginFileName); |
224 | Draw::Load(di, aKey, aPluginFileName, aPluginDir, aPluginDir2, Standard_False); |
225 | |
226 | // Load TclScript |
227 | TCollection_AsciiString aCSFVariable ("CSF_DrawPluginTclDir"); |
228 | TCollection_AsciiString aTclScriptDir; |
229 | aTclScriptDir = getenv (aCSFVariable.ToCString()); |
230 | TCollection_AsciiString aTclScriptFileName; |
231 | TCollection_AsciiString aTclScriptFileNameDefaults; |
232 | aTclScriptFileName = aTclScriptDir + "/" + aValue + ".tcl"; |
233 | aTclScriptFileNameDefaults = aPluginDir + "/" + aValue + ".tcl"; |
234 | OSD_File aTclScriptFile ( aTclScriptFileName ); |
235 | OSD_File aTclScriptFileDefaults ( aTclScriptFileNameDefaults ); |
236 | if (!aTclScriptDir.IsEmpty() && aTclScriptFile.Exists()) { |
237 | #ifdef DEB |
238 | cout << "Load " << aTclScriptFileName << " TclScript" << endl; |
239 | #endif |
240 | di.EvalFile( aTclScriptFileName.ToCString() ); |
241 | } else if (!aPluginDir.IsEmpty() && aTclScriptFileDefaults.Exists()) { |
242 | #ifdef DEB |
243 | cout << "Load " << aTclScriptFileNameDefaults << " TclScript" << endl; |
244 | #endif |
245 | di.EvalFile( aTclScriptFileNameDefaults.ToCString() ); |
246 | } |
247 | |
248 | } else |
249 | cout <<"Pload : Resource = " << aResource << " is not found" << endl; |
250 | } |
251 | return 0; |
252 | } |
253 | |
254 | //======================================================================= |
255 | //function : PloadCommands |
256 | //purpose : |
257 | //======================================================================= |
258 | |
259 | void Draw::PloadCommands(Draw_Interpretor& theCommands) |
260 | { |
261 | static Standard_Boolean Done = Standard_False; |
262 | if (Done) return; |
263 | Done = Standard_True; |
264 | |
265 | const char* g = "Draw Plugin"; |
266 | |
267 | theCommands.Add("pload" , "pload [-PluginFilename] [[Key1] [Key2] ...]: Loads Draw plugins " , |
268 | __FILE__, Pload, g); |
269 | } |