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