Adjusting testing cases for current state of OCCT
[occt.git] / src / Units / Units_Lexicon.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
22
23 #include <Units_Lexicon.ixx>
24 #include <Units_Token.hxx>
25 #include <TCollection_HAsciiString.hxx>
26 #include <TCollection_AsciiString.hxx>
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30
31 #ifdef WNT
32 # include <stdio.h>
33 #else
34 #include <Standard_Stream.hxx>
35 #endif  // WNT
36
37 //=======================================================================
38 //function : Units_Lexicon
39 //purpose  : 
40 //=======================================================================
41
42 Units_Lexicon::Units_Lexicon()
43 {
44 }
45
46
47 //=======================================================================
48 //function : Creates
49 //purpose  : 
50 //=======================================================================
51
52 void Units_Lexicon::Creates(const Standard_CString afilename)
53 {
54   char chain[256],line[256];
55   char oper[11],coeff[31];
56 #if 0
57   char *Chain = chain ;
58   char *Line = line ;
59   char *Oper = oper ;
60   char *Coeff = coeff ;
61 #endif
62   Standard_Integer fr;
63   Standard_Size i;
64   Standard_Real value;
65   Handle(Units_Token) token;
66   struct stat buf;
67
68 #if 0
69   chain[255] = '\0' ;
70   oper[10] = '\0' ;
71   coeff[30] = '\0' ;
72 #endif
73
74   //for(i=0; i<=255; i++)chain[i]=0;
75   ifstream file(afilename, ios::in);
76   if(!file) {
77     cout<<"unable to open "<<afilename<<" for input"<<endl;
78     return;
79   }
80
81   thefilename = new TCollection_HAsciiString(afilename);
82   thesequenceoftokens = new Units_TokensSequence();
83
84   if(!stat(afilename,&buf)) thetime = buf.st_ctime;
85
86   //for(i=0; i<=255; i++)line[i]=0;
87
88   while(file.getline(line,255)) {
89     Standard_Size len = strlen( line ) ;
90     if(len == 1) continue; //skl - ???
91     for ( i = 0 ; i < 30 ; i++ ) {
92       if ( i < len )
93         fr=sscanf(&line[i],"%c",&chain[i]);
94       else
95         chain[i] = 0 ;
96     }
97     for ( i = 0 ; i < 10 ; i++ ) {
98       if ( 30+i < len )
99         fr=sscanf(&line[30+i],"%c",&oper[i]);
100       else
101         oper[i] = 0 ;
102     }
103     for ( i = 0 ; i < 30 ; i++ ) {
104       if ( 40+i < len )
105         fr=sscanf(&line[40+i],"%c",&coeff[i]);
106       else
107         coeff[i] = 0 ;
108     }
109 #if 0
110     cout << "Lexiconsscanf(%c          )" << endl << "Chain" << Chain << endl
111          << "Oper" << Oper << endl
112          << "Coeff" << Coeff << endl ;
113 #endif
114     i=29;
115     while( i>=0 && ( chain[i] == ' ' || !chain[i] ))
116       chain[i--]=0;
117     if(i<0) continue;
118     i=9;
119     while( i>=0 && ( oper [i] == ' ' || !oper [i] ))
120       oper[i--]=0;
121     i=29;
122     while( i>=0 && ( coeff[i] == ' ' || !coeff[i] ))
123       coeff[i--]=0;
124
125     if(coeff[0])
126       value = atof(coeff);
127     else
128       value = 0.;
129
130     if(thesequenceoftokens->IsEmpty()) {
131       token = new Units_Token(chain,oper,value);
132       thesequenceoftokens->Prepend(token);
133     }
134     else {
135       AddToken(chain,oper,value);
136     }
137
138     for(i=0; i<255; i++)
139       line[i]=0;
140   }
141   file.close();
142 }
143
144
145 //=======================================================================
146 //function : UpToDate
147 //purpose  : 
148 //=======================================================================
149
150 Standard_Boolean Units_Lexicon::UpToDate() const
151 {
152   struct stat buf;
153   TCollection_AsciiString string = FileName();
154
155   if(!stat(string.ToCString(),&buf)) {
156     if(thetime >= buf.st_ctime)
157       return Standard_True;
158   }
159
160   return Standard_False;
161 }
162
163
164 //=======================================================================
165 //function : FileName
166 //purpose  : 
167 //=======================================================================
168
169 TCollection_AsciiString Units_Lexicon::FileName() const
170 {
171   return thefilename->String();
172 }
173
174
175 //=======================================================================
176 //function : AddToken
177 //purpose  : 
178 //=======================================================================
179
180 void Units_Lexicon::AddToken(const Standard_CString aword,
181                              const Standard_CString amean,
182                              const Standard_Real avalue)
183 {
184   Handle(Units_Token) token;
185   Handle(Units_Token) referencetoken;
186   Standard_Boolean found = Standard_False;
187   Standard_Integer index;
188
189   for(index=1;index<=thesequenceoftokens->Length();index++) {
190     referencetoken = thesequenceoftokens->Value(index);
191     if( referencetoken->Word() == aword ) {
192       referencetoken->Update(amean);
193       found = Standard_True;
194       break;
195     }
196     else if( !( referencetoken->Word()>aword ) ) {
197       token = new Units_Token(aword,amean,avalue);
198       thesequenceoftokens->InsertBefore(index,token);
199       found = Standard_True;
200       break;
201     }
202   }
203   if(!found) {
204     token = new Units_Token(aword,amean,avalue);
205     thesequenceoftokens->Append(token);
206   }
207 }