0024814: Avoid using explicit names of Handle classes
[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-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 // Historique :
18 // CRD : 15/04/97 : Passage WOK++ : Replace TYPE by STANDARD_TYPE
19 // CRD : 03/07/97 : Porting Windows NT.
20
21 #include <Standard_Stream.hxx>
22
23 #include <Dynamic_MethodDefinitionsDictionary.ixx>
24 #include <Dynamic.hxx>
25 #include <Dynamic_CompiledMethod.hxx>
26 #include <Dynamic_BooleanParameter.hxx>
27 #include <Dynamic_IntegerParameter.hxx>
28 #include <Dynamic_RealParameter.hxx>
29 #include <Dynamic_StringParameter.hxx>
30 #include <Dynamic_ObjectParameter.hxx>
31 #include <Dynamic_InstanceParameter.hxx>
32 #include <TCollection_AsciiString.hxx>
33
34 #ifdef HAVE_CONFIG_H
35 # include <config.h>
36 #endif
37
38 #include <stdio.h>
39 #ifdef HAVE_SYS_TYPES_H
40 # include <sys/types.h>
41 #endif
42
43 #if defined (HAVE_SYS_STAT_H) || defined (WNT)
44 # include <sys/stat.h>
45 #endif
46
47 #ifdef HAVE_STRINGS_H
48 # include <strings.h>
49 #endif
50 #ifdef WNT
51 #define stat _stat
52 //#define strcasecmp _stricoll
53 #endif
54
55 //=======================================================================
56 //function : Dynamic_MethodDefinitionsDictionary
57 //purpose  : 
58 //=======================================================================
59
60 Dynamic_MethodDefinitionsDictionary::Dynamic_MethodDefinitionsDictionary()
61 {
62 }
63
64 //=======================================================================
65 //function : Creates
66 //purpose  : 
67 //=======================================================================
68
69 void Dynamic_MethodDefinitionsDictionary::Creates(const Standard_CString afilename)
70 {
71   Standard_Boolean group;
72   Standard_Integer fr,i,begin,end,endline;
73   char line[256];
74   char name[81];
75   char mode[81];
76   char type[81];
77   char value[81],value1[81],value2[81],value3[81];
78   Handle(Dynamic_CompiledMethod) methoddefinition;
79   Handle(Dynamic_Parameter) parameter;
80   
81   struct stat buf;
82
83   ifstream file(afilename);
84   if(!file)
85     {
86       cout<<"unable to open "<<afilename<<" for input"<<endl;
87       return;
88     }
89   
90   thefilename = new TCollection_HAsciiString(afilename);
91
92   if(!stat(afilename,&buf)) thetime = buf.st_ctime;
93
94   thesequenceofmethoddefinitions = new Dynamic_SequenceOfMethodDefinitions();
95   
96   for(;;)
97     {
98       memset(line,0,sizeof(line));
99
100       file.getline(line,255);
101       if(!file)break;
102
103       i = 254;
104       while( i >= 0 && ( line[i] == ' ' || !line[i]))line[i--] = 0;
105       fr = i+1;
106       if(fr <= 1)continue;
107
108       if(line[0] != ' ')
109         {
110           methoddefinition = new Dynamic_CompiledMethod(line,line);
111           thesequenceofmethoddefinitions->Append(methoddefinition);
112         }
113       else
114         {
115           begin = end = 0;
116           for(i=0; i<fr; i++)
117             {
118               if(line[i] == '"')
119                 {
120                   if(begin)
121                     {
122                       end = i;
123                       break;
124                     }
125                   else
126                     {
127                       begin = i;
128                     }
129                 }
130             }
131
132       memset(name,0,sizeof(name));
133
134           endline = 0;
135           for(i=begin+1; i<=end-1; i++)name[endline++] = line[i];
136
137       memset(mode,  0x00,sizeof(mode));
138       memset(type,  0x00,sizeof(type));
139       memset(value, 0x00,sizeof(value));
140       memset(value1,0x00,sizeof(value1));
141       memset(value2,0x00,sizeof(value2));
142       memset(value3,0x00,sizeof(value3));
143
144           fr = sscanf(&line[end+1],"%80s%80s%80c",mode,type,value);
145           if(fr == -1) continue;
146
147           group = Standard_False;
148           if(type[0] == '[')
149             {
150               group = Standard_True;
151               for(i=1; i<80; i++)type[i-1] = type[i];
152             }
153
154           begin = 0;
155           for(i=0; i<80; i++)
156             {
157               if(value[i] != ' ')
158                 {
159                   begin = i;
160                   break;
161                 }
162             }
163           for(i=begin; i<80; i++) value[i-begin] = value[i];
164           for(i=80-begin; i<80; i++) value[i] = 0;
165
166           if     (!strcasecmp(type,"Standard_Boolean"))
167             methoddefinition->AddVariable(new Dynamic_BooleanParameter(name,value),Dynamic::Mode(mode),group);
168
169           else if(!strcasecmp(type,"Standard_Integer"))
170             methoddefinition->AddVariable(new Dynamic_IntegerParameter(name,atoi(value)),Dynamic::Mode(mode),group);
171
172           else if(!strcasecmp(type,"Standard_Real"))
173             methoddefinition->AddVariable(new Dynamic_RealParameter(name,Atof(value)),Dynamic::Mode(mode),group);
174
175           else if(!strcasecmp(type,"Standard_CString"))
176             methoddefinition->AddVariable(new Dynamic_StringParameter(name,value),Dynamic::Mode(mode),group);
177
178           else
179             {
180               parameter = Switch(name,type,value);
181               if(!parameter.IsNull())methoddefinition->AddVariable(parameter,Dynamic::Mode(mode),group);
182             }
183
184         }
185     }
186   file.close();
187 }
188
189 //=======================================================================
190 //function : Definition
191 //purpose  : 
192 //=======================================================================
193
194 Standard_Boolean Dynamic_MethodDefinitionsDictionary::Definition(const Handle(Dynamic_Method)& adefinition)
195 {
196   Handle(Dynamic_MethodDefinition) methoddefinition;
197   if(adefinition->IsKind(STANDARD_TYPE(Dynamic_MethodDefinition)))
198     {
199       methoddefinition = *(Handle(Dynamic_MethodDefinition)*)&adefinition;
200       thesequenceofmethoddefinitions->Append(methoddefinition);
201       return Standard_True;
202     }
203   else
204     {
205       cout<<"the definition is not a method definition"<<endl;
206       return Standard_False;
207     }
208 }
209
210 //=======================================================================
211 //function : Definition
212 //purpose  : 
213 //=======================================================================
214
215 Standard_Boolean Dynamic_MethodDefinitionsDictionary::Definition
216     (const Standard_CString atype,
217      Handle(Dynamic_Method)& adefinition) const
218 {
219   Handle(Dynamic_Method) definition;
220
221   for(Standard_Integer index=1; index<=thesequenceofmethoddefinitions->Length(); index++)
222     {
223       definition = thesequenceofmethoddefinitions->Value(index);
224       if(definition->Type() == atype)
225         {
226           adefinition = definition;
227           return Standard_True;
228         }
229     }
230   return Standard_False;
231 }
232
233 //=======================================================================
234 //function : Switch
235 //purpose  : 
236 //=======================================================================
237
238 Handle(Dynamic_Parameter) Dynamic_MethodDefinitionsDictionary::Switch(
239   const Standard_CString aname,
240   const Standard_CString atype,
241   const Standard_CString avalue) const
242 {
243   Handle(Dynamic_ObjectParameter) objectparameter;
244   cout<<"Parameter "<<aname<<" of type "<<atype<<" with "<<avalue<<" does not exist."<<endl;
245   return objectparameter;
246 }
247
248 //=======================================================================
249 //function : UpToDate
250 //purpose  : 
251 //=======================================================================
252
253 Standard_Boolean Dynamic_MethodDefinitionsDictionary::UpToDate() const
254 {
255   struct stat buf;
256
257   TCollection_AsciiString string = thefilename->String();
258   if(!stat(string.ToCString(),&buf))
259     {
260       if(thetime == buf.st_ctime) return Standard_True;
261     }
262
263   return Standard_False;
264 }
265
266 //=======================================================================
267 //function : NumberOfDefinitions
268 //purpose  : 
269 //=======================================================================
270
271 Standard_Integer Dynamic_MethodDefinitionsDictionary::NumberOfDefinitions() const
272 {
273   return thesequenceofmethoddefinitions->Length();
274 }
275
276 //=======================================================================
277 //function : Definition
278 //purpose  : 
279 //=======================================================================
280
281 Handle(Dynamic_Method) Dynamic_MethodDefinitionsDictionary::Definition
282       (const Standard_Integer anindex) const
283 {
284   return thesequenceofmethoddefinitions->Value(anindex);
285 }
286
287 //=======================================================================
288 //function : Dump
289 //purpose  : 
290 //=======================================================================
291
292 void Dynamic_MethodDefinitionsDictionary::Dump(Standard_OStream& astream) const
293 {
294   Standard_Integer index;
295   astream<<" DICTIONARY : \n\n";
296   for(index=1;index<=thesequenceofmethoddefinitions->Length();index++)
297     thesequenceofmethoddefinitions->Value(index)->Dump(astream);
298 }