08dcd62ad87dc004594499ae801a944ea2150057
[occt.git] / src / Dynamic / Dynamic_MethodDefinitionsDictionary.cxx
1 // Created on: 1992-06-24
2 // Created by: Gilles DEBARBOUILLE
3 // Copyright (c) 1992-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21 // Historique :
22 // CRD : 15/04/97 : Passage WOK++ : Replace TYPE by STANDARD_TYPE
23 // CRD : 03/07/97 : Porting Windows NT.
24
25 #include <Standard_Stream.hxx>
26
27 #include <Dynamic_MethodDefinitionsDictionary.ixx>
28 #include <Dynamic.hxx>
29 #include <Dynamic_CompiledMethod.hxx>
30 #include <Dynamic_BooleanParameter.hxx>
31 #include <Dynamic_IntegerParameter.hxx>
32 #include <Dynamic_RealParameter.hxx>
33 #include <Dynamic_StringParameter.hxx>
34 #include <Dynamic_ObjectParameter.hxx>
35 #include <Dynamic_InstanceParameter.hxx>
36 #include <TCollection_AsciiString.hxx>
37
38 #ifdef HAVE_CONFIG_H
39 # include <config.h>
40 #endif
41
42 #include <stdio.h>
43 #ifdef HAVE_SYS_TYPES_H
44 # include <sys/types.h>
45 #endif
46
47 #if defined (HAVE_SYS_STAT_H) || defined (WNT)
48 # include <sys/stat.h>
49 #endif
50
51 #ifdef HAVE_STRINGS_H
52 # include <strings.h>
53 #endif
54 #ifdef WNT
55 #define stat _stat
56 //#define strcasecmp _stricoll
57 #endif
58
59 //=======================================================================
60 //function : Dynamic_MethodDefinitionsDictionary
61 //purpose  : 
62 //=======================================================================
63
64 Dynamic_MethodDefinitionsDictionary::Dynamic_MethodDefinitionsDictionary()
65 {
66 }
67
68 //=======================================================================
69 //function : Creates
70 //purpose  : 
71 //=======================================================================
72
73 void Dynamic_MethodDefinitionsDictionary::Creates(const Standard_CString afilename)
74 {
75   Standard_Boolean group;
76   Standard_Integer fr,i,begin,end,endline;
77   char line[255];
78   char name[80];
79   char mode[80];
80   char type[80];
81   char value[80],value1[80],value2[80],value3[80];
82   Handle(Dynamic_CompiledMethod) methoddefinition;
83   Handle(Dynamic_Parameter) parameter;
84   
85   struct stat buf;
86
87   ifstream file(afilename);
88   if(!file)
89     {
90       cout<<"unable to open "<<afilename<<" for input"<<endl;
91       return;
92     }
93   
94   thefilename = new TCollection_HAsciiString(afilename);
95
96   if(!stat(afilename,&buf)) thetime = buf.st_ctime;
97
98   thesequenceofmethoddefinitions = new Dynamic_SequenceOfMethodDefinitions();
99   
100   for(;;)
101     {
102       for(i=0; i<255; i++) line[i] = 0;
103
104       file.getline(line,255);
105       if(!file)break;
106
107       i = 254;
108       while( i >= 0 && ( line[i] == ' ' || !line[i]))line[i--] = 0;
109       fr = i+1;
110       if(fr <= 1)continue;
111
112       if(line[0] != ' ')
113         {
114           methoddefinition = new Dynamic_CompiledMethod(line,line);
115           thesequenceofmethoddefinitions->Append(methoddefinition);
116         }
117       else
118         {
119           begin = end = 0;
120           for(i=0; i<fr; i++)
121             {
122               if(line[i] == '"')
123                 {
124                   if(begin)
125                     {
126                       end = i;
127                       break;
128                     }
129                   else
130                     {
131                       begin = i;
132                     }
133                 }
134             }
135
136           for(i=0; i<80; i++)name[i]=0;
137
138           endline = 0;
139           for(i=begin+1; i<=end-1; i++)name[endline++] = line[i];
140
141           for(i=0; i<80; i++)mode   [i] = 0;
142           for(i=0; i<80; i++)type   [i] = 0;
143           for(i=0; i<80; i++)value  [i] = 0;
144           for(i=0; i<80; i++)value1 [i] = 0;
145           for(i=0; i<80; i++)value2 [i] = 0;
146           for(i=0; i<80; i++)value3 [i] = 0;
147
148           //      fr = sscanf(&line[end+1],"%s%s%80c",&mode,&type,&value);
149           fr = sscanf(&line[end+1],"%s%s%80c",mode,type,value);
150           if(fr == -1) continue;
151
152           group = Standard_False;
153           if(type[0] == '[')
154             {
155               group = Standard_True;
156               for(i=1; i<80; i++)type[i-1] = type[i];
157             }
158
159           begin = 0;
160           for(i=0; i<80; i++)
161             {
162               if(value[i] != ' ')
163                 {
164                   begin = i;
165                   break;
166                 }
167             }
168           for(i=begin; i<80; i++) value[i-begin] = value[i];
169           for(i=80-begin; i<80; i++) value[i] = 0;
170
171           if     (!strcasecmp(type,"Standard_Boolean"))
172             methoddefinition->AddVariable(new Dynamic_BooleanParameter(name,value),Dynamic::Mode(mode),group);
173
174           else if(!strcasecmp(type,"Standard_Integer"))
175             methoddefinition->AddVariable(new Dynamic_IntegerParameter(name,atoi(value)),Dynamic::Mode(mode),group);
176
177           else if(!strcasecmp(type,"Standard_Real"))
178             methoddefinition->AddVariable(new Dynamic_RealParameter(name,atof(value)),Dynamic::Mode(mode),group);
179
180           else if(!strcasecmp(type,"Standard_CString"))
181             methoddefinition->AddVariable(new Dynamic_StringParameter(name,value),Dynamic::Mode(mode),group);
182
183           else
184             {
185               parameter = Switch(name,type,value);
186               if(!parameter.IsNull())methoddefinition->AddVariable(parameter,Dynamic::Mode(mode),group);
187             }
188
189         }
190     }
191   file.close();
192 }
193
194 //=======================================================================
195 //function : Definition
196 //purpose  : 
197 //=======================================================================
198
199 Standard_Boolean Dynamic_MethodDefinitionsDictionary::Definition(const Handle(Dynamic_Method)& adefinition)
200 {
201   Handle(Dynamic_MethodDefinition) methoddefinition;
202   if(adefinition->IsKind(STANDARD_TYPE(Dynamic_MethodDefinition)))
203     {
204       methoddefinition = *(Handle_Dynamic_MethodDefinition*)&adefinition;
205       thesequenceofmethoddefinitions->Append(methoddefinition);
206       return Standard_True;
207     }
208   else
209     {
210       cout<<"the definition is not a method definition"<<endl;
211       return Standard_False;
212     }
213 }
214
215 //=======================================================================
216 //function : Definition
217 //purpose  : 
218 //=======================================================================
219
220 Standard_Boolean Dynamic_MethodDefinitionsDictionary::Definition
221     (const Standard_CString atype,
222      Handle(Dynamic_Method)& adefinition) const
223 {
224   Handle(Dynamic_Method) definition;
225
226   for(Standard_Integer index=1; index<=thesequenceofmethoddefinitions->Length(); index++)
227     {
228       definition = thesequenceofmethoddefinitions->Value(index);
229       if(definition->Type() == atype)
230         {
231           adefinition = definition;
232           return Standard_True;
233         }
234     }
235   return Standard_False;
236 }
237
238 //=======================================================================
239 //function : Switch
240 //purpose  : 
241 //=======================================================================
242
243 Handle(Dynamic_Parameter) Dynamic_MethodDefinitionsDictionary::Switch(
244   const Standard_CString aname,
245   const Standard_CString atype,
246   const Standard_CString avalue) const
247 {
248   Handle(Dynamic_ObjectParameter) objectparameter;
249   cout<<"Parameter "<<aname<<" of type "<<atype<<" with "<<avalue<<" does not exist."<<endl;
250   return objectparameter;
251 }
252
253 //=======================================================================
254 //function : UpToDate
255 //purpose  : 
256 //=======================================================================
257
258 Standard_Boolean Dynamic_MethodDefinitionsDictionary::UpToDate() const
259 {
260   struct stat buf;
261
262   TCollection_AsciiString string = thefilename->String();
263   if(!stat(string.ToCString(),&buf))
264     {
265       if(thetime == buf.st_ctime) return Standard_True;
266     }
267
268   return Standard_False;
269 }
270
271 //=======================================================================
272 //function : NumberOfDefinitions
273 //purpose  : 
274 //=======================================================================
275
276 Standard_Integer Dynamic_MethodDefinitionsDictionary::NumberOfDefinitions() const
277 {
278   return thesequenceofmethoddefinitions->Length();
279 }
280
281 //=======================================================================
282 //function : Definition
283 //purpose  : 
284 //=======================================================================
285
286 Handle(Dynamic_Method) Dynamic_MethodDefinitionsDictionary::Definition
287       (const Standard_Integer anindex) const
288 {
289   return thesequenceofmethoddefinitions->Value(anindex);
290 }
291
292 //=======================================================================
293 //function : Dump
294 //purpose  : 
295 //=======================================================================
296
297 void Dynamic_MethodDefinitionsDictionary::Dump(Standard_OStream& astream) const
298 {
299   Standard_Integer index;
300   astream<<" DICTIONARY : \n\n";
301   for(index=1;index<=thesequenceofmethoddefinitions->Length();index++)
302     thesequenceofmethoddefinitions->Value(index)->Dump(astream);
303 }