1f1ec0461c300e2b31f8f6d3fa8c09a624bcab14
[occt.git] / src / Dynamic / Dynamic_FuzzyDefinitionsDictionary.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 // CRD : 03/07/97 : Porting Windows NT.
22
23 #include <Standard_Stream.hxx>
24
25 #include <Dynamic_FuzzyDefinitionsDictionary.ixx>
26 #include <Dynamic_FuzzyDefinition.hxx>
27 #include <Dynamic_BooleanParameter.hxx>
28 #include <Dynamic_IntegerParameter.hxx>
29 #include <Dynamic_RealParameter.hxx>
30 #include <Dynamic_StringParameter.hxx>
31 #include <Dynamic_ObjectParameter.hxx>
32 #include <Dynamic_InstanceParameter.hxx>
33 #include <TCollection_AsciiString.hxx>
34
35 #ifdef HAVE_CONFIG_H
36 # include <config.h>
37 #endif
38
39 #include <stdio.h>
40
41 #ifdef HAVE_SYS_TYPES_H
42 # include <sys/types.h>
43 #endif
44
45 #if defined (HAVE_SYS_STAT_H) || defined (WNT)
46 # include <sys/stat.h>
47 #endif
48
49 #ifdef HAVE_STRINGS_H
50 # include <strings.h>
51 #endif
52
53 #ifdef WNT
54 //#define strcasecmp _stricoll
55 #define stat _stat
56 #endif
57
58 //=======================================================================
59 //function : Dynamic_FuzzyDefinitionsDictionary
60 //purpose  : 
61 //=======================================================================
62
63 Dynamic_FuzzyDefinitionsDictionary::Dynamic_FuzzyDefinitionsDictionary()
64 {
65 }
66
67 //=======================================================================
68 //function : Creates
69 //purpose  : 
70 //=======================================================================
71
72 void Dynamic_FuzzyDefinitionsDictionary::Creates(const Standard_CString afilename)
73 {
74   Standard_Integer fr,i,begin,end,endline;
75   char line[255];
76   char name[80];
77   char type[80];
78   char value[80],value1[80],value2[80],value3[80];
79   Handle(Dynamic_FuzzyDefinition) fuzzydefinition;
80   Handle(Dynamic_Parameter) parameter;
81   
82  struct stat buf;
83
84   ifstream file(afilename);
85   if(!file)
86     {
87       cout<<"unable to open "<<afilename<<" for input"<<endl;
88       return;
89     }
90   
91   thefilename = new TCollection_HAsciiString(afilename);
92
93   if(!stat(afilename,&buf)) thetime = buf.st_ctime;
94
95   thesequenceoffuzzydefinitions = new Dynamic_SequenceOfFuzzyDefinitions();
96   
97   for(;;)
98     {
99       for(i=0; i<255; i++) line[i] = 0;
100
101       file.getline(line,255);
102       if(!file)break;
103
104       i = 254;
105       while( i >= 0 && ( line[i] == ' ' || !line[i]))line[i--] = 0;
106       fr = i+1;
107       if(fr <= 1)continue;
108
109       if(line[0] != ' ')
110         {
111           fuzzydefinition = new Dynamic_FuzzyDefinition(line);
112           thesequenceoffuzzydefinitions->Append(fuzzydefinition);
113         }
114       else
115         {
116           begin = end = 0;
117           for(i=0; i<fr; i++)
118             {
119               if(line[i] == '"')
120                 {
121                   if(begin)
122                     {
123                       end = i;
124                       break;
125                     }
126                   else
127                     {
128                       begin = i;
129                     }
130                 }
131             }
132
133           for(i=0; i<80; i++)name[i]=0;
134
135           endline = 0;
136           for(i=begin+1; i<=end-1; i++)name[endline++] = line[i];
137
138           for(i=0; i<80; i++)type   [i] = 0;
139           for(i=0; i<80; i++)value  [i] = 0;
140           for(i=0; i<80; i++)value1 [i] = 0;
141           for(i=0; i<80; i++)value2 [i] = 0;
142           for(i=0; i<80; i++)value3 [i] = 0;
143
144 //        fr = sscanf(&line[end+1],"%s%80c",&type,&value);
145           fr = sscanf(&line[end+1],"%s%80c",type,value);
146           if(fr == -1) continue;
147
148           begin = 0;
149           for(i=0; i<80; i++)
150             {
151               if(value[i] != ' ')
152                 {
153                   begin = i;
154                   break;
155                 }
156             }
157           for(i=begin; i<80; i++) value[i-begin] = value[i];
158           for(i=80-begin; i<80; i++) value[i] = 0;
159
160           if     (!strcasecmp(type,"Standard_Boolean"))
161             fuzzydefinition->Parameter(new Dynamic_BooleanParameter(name,value));
162
163           else if(!strcasecmp(type,"Standard_Integer"))
164             fuzzydefinition->Parameter(new Dynamic_IntegerParameter(name,atoi(value)));
165
166           else if(!strcasecmp(type,"Standard_Real"))
167             fuzzydefinition->Parameter(new Dynamic_RealParameter(name,Atof(value)));
168
169           else if(!strcasecmp(type,"Standard_CString"))
170             fuzzydefinition->Parameter(new Dynamic_StringParameter(name,value));
171
172           else
173             {
174               parameter = Switch(name,type,value);
175               if(!parameter.IsNull())fuzzydefinition->Parameter(parameter);
176             }
177
178         }
179     }
180   file.close();
181 }
182
183 //=======================================================================
184 //function : Definition
185 //purpose  : 
186 //=======================================================================
187
188 Standard_Boolean Dynamic_FuzzyDefinitionsDictionary::Definition
189     (const Standard_CString atype,
190      Handle(Dynamic_FuzzyClass)& adefinition) const
191 {
192   Handle(Dynamic_FuzzyClass) definition;
193
194   for(Standard_Integer index=1; index<=thesequenceoffuzzydefinitions->Length(); index++)
195     {
196       definition = thesequenceoffuzzydefinitions->Value(index);
197       if(definition->Type() == atype)
198         {
199           adefinition = definition;
200           return Standard_True;
201         }
202     }
203   return Standard_False;
204 }
205
206 //=======================================================================
207 //function : Switch
208 //purpose  : 
209 //=======================================================================
210
211 Handle(Dynamic_Parameter) Dynamic_FuzzyDefinitionsDictionary::Switch(
212   const Standard_CString aname,
213   const Standard_CString atype,
214   const Standard_CString avalue) const
215 {
216   Handle(Dynamic_ObjectParameter) objectparameter;
217   cout<<"Parameter "<<aname<<" of type "<<atype<<" with "<<avalue<<" does not exist."<<endl;
218   return objectparameter;
219 }
220
221 //=======================================================================
222 //function : UpToDate
223 //purpose  : 
224 //=======================================================================
225
226 Standard_Boolean Dynamic_FuzzyDefinitionsDictionary::UpToDate() const
227 {
228   struct stat buf;
229
230   TCollection_AsciiString string = thefilename->String();
231   if(!stat(string.ToCString(),&buf))
232     {
233         if(thetime == buf.st_ctime) return Standard_True;
234     }
235
236   return Standard_False;
237 }
238
239 //=======================================================================
240 //function : NumberOfDefinitions
241 //purpose  : 
242 //=======================================================================
243
244 Standard_Integer Dynamic_FuzzyDefinitionsDictionary::NumberOfDefinitions() const
245 {
246   return thesequenceoffuzzydefinitions->Length();
247 }
248
249 //=======================================================================
250 //function : Definition
251 //purpose  : 
252 //=======================================================================
253
254 Handle(Dynamic_FuzzyClass) Dynamic_FuzzyDefinitionsDictionary::Definition
255       (const Standard_Integer anindex) const
256 {
257   return thesequenceoffuzzydefinitions->Value(anindex);
258 }
259
260 //=======================================================================
261 //function : Dump
262 //purpose  : 
263 //=======================================================================
264
265 void Dynamic_FuzzyDefinitionsDictionary::Dump(Standard_OStream& astream) const
266 {
267   Standard_Integer index;
268   astream<<" DICTIONARY : /n";
269   for(index=1;index<=thesequenceoffuzzydefinitions->Length();index++)
270     thesequenceoffuzzydefinitions->Value(index)->Dump(astream);
271 }