0024510: Remove unused local variables
[occt.git] / src / Units / Units_Sentence.cxx
CommitLineData
b311480e 1// Created on: 1992-06-24
2// Created by: Gilles DEBARBOUILLE
3// Copyright (c) 1992-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
973c2be1 8// This library is free software; you can redistribute it and / or modify it
9// under the terms of the GNU Lesser General Public 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.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
17#include <Units_NoSuchType.hxx>
18#include <Units_Sentence.ixx>
19#include <Units_Token.hxx>
20#include <Units_ShiftedToken.hxx>
21#include <Units_TokensSequence.hxx>
22#include <Units_Operators.hxx>
23
24
25static Handle(Units_Token) CreateTokenForNumber(const Standard_CString str)
26{
27 TCollection_AsciiString tstr = str[0];
28 Standard_Boolean IsPoint = Standard_False;
60be1f9b 29 Standard_Size len = strlen(str);
30 for(Standard_Size in=1; in < len; in++) {
7fd59977 31 if( str[in]=='0' || str[in]=='1' || str[in]=='2' || str[in]=='3' ||
32 str[in]=='4' || str[in]=='5' || str[in]=='6' || str[in]=='7' ||
33 str[in]=='8' || str[in]=='9' ) {
34 tstr.AssignCat(str[in]);
35 }
36 else if( str[in]=='.' && !IsPoint ) {
37 tstr.AssignCat(str[in]);
38 IsPoint = Standard_True;
39 }
40 else
41 break;
42 }
43 return new Units_Token(tstr.ToCString(), "0");
44}
45
46
47//=======================================================================
48//function : Units_Sentence
49//purpose :
50//=======================================================================
51
52Units_Sentence::Units_Sentence(const Handle(Units_Lexicon)& alexicon,
53 const Standard_CString astring)
54{
55 Standard_Integer index;
fb8a7358 56 Standard_Size i,limchain;
7fd59977 57 Handle(Units_Token) token;
58 Handle(Units_Token) referencetoken;
59
60 thesequenceoftokens = new Units_TokensSequence();
61 Handle(Units_TokensSequence) lstlexicon=alexicon->Sequence();
62 Units_NoSuchType_Raise_if(lstlexicon.IsNull(),"BAD LEXICON descriptor");
63 limchain=strlen(astring);
7fd59977 64 i=0;
65
66 TCollection_AsciiString tmpstr = astring;
67 //Handle(Units_Token) tmptoken;
68 TCollection_AsciiString PrevMean;
69 TCollection_AsciiString PrevWord;
70 while(i < limchain) {
71 Standard_Boolean IsFound = Standard_False;
72 TCollection_AsciiString LastWord = "";
73 for(index=1; index<=lstlexicon->Length(); index++) {
74 referencetoken = lstlexicon->Value(index);
75 TCollection_AsciiString aword = referencetoken->Word();
76 int num = tmpstr.Search(aword);
77 if( num==1 && aword.Length()>=LastWord.Length() ) {
78 token = referencetoken->Creates();
79 LastWord = aword;
80 IsFound = Standard_True;
81 }
82 }
83 if(!IsFound) {
84 // may be it is a number(degree)?
85 LastWord = tmpstr.SubString(1,1);
86 if(!LastWord.IsIntegerValue()) {
87 // unknown expression - not create sentene
88 thesequenceoftokens->Clear();
89#ifdef DEB
90 cout<<"Warning: can not create correct sentence from string: "<<astring<<endl;
91#endif
92 return;
93 }
94 else {
95 // create token for number
96 token = CreateTokenForNumber(tmpstr.ToCString());
97 LastWord = token->Word();
98 }
99 }
100 if(i>0) {
101 // make additional checking
102 if( ( token->Mean()=="M" && ( PrevMean=="M" || PrevMean=="MU" || PrevMean=="0" ) ) ||
103 ( token->Mean()=="U" && ( PrevMean=="U" || PrevMean=="0" ) ) ||
104 ( token->Mean()=="O" && ( PrevMean=="M" || PrevMean=="O" ) ) ||
105 ( ( token->Word()=="+" || token->Word()=="-" ) && PrevWord!="(" ) ||
106 ( token->Mean()=="S" && ( PrevMean=="M" ) ) ||
107 ( token->Mean()=="0" && ( PrevMean=="M" || PrevMean=="U" || PrevMean=="MU" ) ) ) {
108 // incorrect situation
109 thesequenceoftokens->Clear();
110#ifdef DEB
111 cout<<"Warning: can not create correct sentence from string: "<<astring<<endl;
112#endif
113 return;
114 }
115 }
116 thesequenceoftokens->Append(token);
117 PrevMean = token->Mean();
118 PrevWord = token->Word();
119 i = i + LastWord.Length();
120 tmpstr.Remove(1,LastWord.Length());
121 }
7fd59977 122}
123
124
125//=======================================================================
126//function : SetConstants
127//purpose :
128//=======================================================================
129
130void Units_Sentence::SetConstants()
131{
132 Standard_Integer index;
133 Standard_Real value;
134 Handle(Units_Token) token;
135 TCollection_AsciiString string;
136
137 for(index=1;index<=thesequenceoftokens->Length();index++) {
138 token = thesequenceoftokens->Value(index);
139 if(token->Value()==0.) {
140 string = token->Word();
141 if(string.IsRealValue()) {
142 value = string.RealValue();
143 token->Mean("K");
144 token->Value(value);
145 }
146 }
147 }
148}
149
150
151//=======================================================================
152//function : CalculateLocal
153//purpose : auxilary
154//=======================================================================
155static Handle(Units_Token) CalculateLocal(const Handle(Units_TokensSequence)& aSeq)
156{
157 //cout<<endl;
158 //for(int index=1; index<=aSeq->Length(); index++) {
159 // Handle(Units_Token) tok = aSeq->Value(index);
160 // cout<<tok->Word()<<" ";
161 //}
162 //cout<<endl;
163 Handle(Units_Token) tok1,tok2;
164 Standard_Integer i,j;
165
166 if(aSeq->Length()==1) {
167 return aSeq->Value(1);
168 }
169
170 // case of unar sign
171 if(aSeq->Length()==2) {
172 if(aSeq->Value(1)->Word()=="+")
173 aSeq->Remove(1);
174 if(aSeq->Value(1)->Word()=="-") {
175 tok2 = aSeq->Value(2);
176 TCollection_AsciiString aword = "-";
177 aword.AssignCat(tok2->Word());
178 tok1 = new Units_Token(aword.ToCString(), tok2->Mean().ToCString(),
179 tok2->Value()*(-1.0), tok2->Dimensions());
180 aSeq->Remove(1);
181 aSeq->SetValue(1,tok1);
182 }
183 return aSeq->Value(1);
184 }
185
186 Standard_Boolean IsBracket = Standard_True;
187 while(IsBracket) {
188 for(i=1; i<=aSeq->Length(); i++) {
189 if(aSeq->Value(i)->Word()=="(") {
190 Handle(Units_TokensSequence) TmpSeq = new Units_TokensSequence;
191 Standard_Integer NbBrackets = 1;
192 for(j=i+1; j<=aSeq->Length(); j++) {
193 if(aSeq->Value(j)->Word()==")")
194 NbBrackets--;
195 if(aSeq->Value(j)->Word()=="(")
196 NbBrackets++;
197 if(NbBrackets>0)
198 TmpSeq->Append(aSeq->Value(j));
199 else
200 break;
201 }
202 tok1 = CalculateLocal(TmpSeq);
203 aSeq->Remove(i+1,j);
204 aSeq->SetValue(i,tok1);
205 break;
206 }
207 }
208 if(i>aSeq->Length()) {
209 IsBracket = Standard_False;
210 // there are not brackets => make calculations
211 // step 1: operation '**'
212 for(i=1; i<=aSeq->Length(); i++) {
213 if(aSeq->Value(i)->Word()=="**") {
214 tok1 = aSeq->Value(i-1);
215 tok2 = aSeq->Value(i+1);
216 tok1 = pow(tok1,tok2);
217 aSeq->Remove(i);
218 aSeq->Remove(i);
219 aSeq->SetValue(i-1,tok1);
220 i--;
221 }
222 }
223 // step 2: operation '*', '.' and '/'
224 for(i=1; i<=aSeq->Length(); i++) {
225 if(aSeq->Value(i)->Mean()=="O") {
226 tok1 = aSeq->Value(i-1);
227 tok2 = aSeq->Value(i+1);
228 if(aSeq->Value(i)->Word()=="/")
229 tok1 = tok1 / tok2;
230 else
231 tok1 = tok1 * tok2;
232 aSeq->Remove(i);
233 aSeq->Remove(i);
234 aSeq->SetValue(i-1,tok1);
235 i--;
236 }
237 }
238 // now aSeq have to include only one result token
239 }
240 }
241 return tok1;
242}
243
244
245//=======================================================================
246//function : Evaluate
247//purpose :
248//=======================================================================
249
250Handle(Units_Token) Units_Sentence::Evaluate()
251{
252 Handle(Units_Token) rtoken,ktoken;
253 if(thesequenceoftokens->Length()==0)
254 return rtoken;
255
256/* old variant
257 Standard_Integer index;
258 static char *ooper[6] = { "+", "-", "*", ".", "/", "**" };
259 Standard_Integer numterm,i,j,k,maxlevel,curlevel,level[255];
260 Handle(Units_Token) currenttoken;
261 TCollection_AsciiString string;
262 TCollection_AsciiString mean;
263 TCollection_AsciiString oper;
264
265 numterm=curlevel=i=0;
266 for(index=1;index<=thesequenceoftokens->Length();index++) {
267 string = thesequenceoftokens->Value(index)->Word();
268
269 if( string=="(" ) {
270 level[numterm]=curlevel;
271 curlevel++;
272 }
273 else if( string==")" ) {
274 curlevel--;
275 level[numterm]=curlevel;
276 }
277 else {
278 level[numterm]=curlevel;
279 }
280 numterm++;
281 }
282
283 //if(IPrint==1) {
284 // cout<<endl;
285 // for(index=1; index<=thesequenceoftokens->Length(); index++) {
286 // Handle(Units_Token) tok = thesequenceoftokens->Value(index);
287 // cout<<tok->Word()<<" ";
288 // }
289 // cout<<endl;
290 // for(index=1; index<=thesequenceoftokens->Length(); index++) {
291 // cout<<level[index-1]<<" ";
292 // }
293 // cout<<endl;
294 //}
295
296 if(curlevel) {
297 cout<<" Incorrect number of brackets"<<endl;
298 return new Units_Token();
299 }
300
301 for(k=0; k<6; k++) {
302 for(index=1,i=0; index<=thesequenceoftokens->Length(); index++,i++) {
303 if( thesequenceoftokens->Value(index)->Word() == ooper[k] ) {
304 j=i+1;
305 curlevel=level[j];
306 while(j < numterm && level[j] >= curlevel)
307 level[j++]++;
308 j=i-1;
309 curlevel=level[j];
310 while(j >= 0 && level[j] >= curlevel)
311 level[j--]++;
312 }
313 }
314 }
315
316 Handle(Units_Dimensions) aDim;
317
318 numterm--;
319 while(numterm>0) {
320
321 maxlevel=0;
322 for(i=0; i<=numterm; i++)if(level[i] > maxlevel)
323 maxlevel=level[i];
324
325 i=0;
326 while(level[i] != maxlevel)
327 i++;
328
329 oper.Clear();
330
331 k=i;
332 while( k<=numterm && level[k]==maxlevel ) {
333 ktoken=thesequenceoftokens->Value(k+1);
334 mean=ktoken->Mean();
335 if(mean=="O") {
336 oper = ktoken->Word();
337 }
338 else if(mean!="S") {
339 if (oper == "/") rtoken = rtoken / ktoken;
340 else if(oper == "+") rtoken = rtoken + ktoken;
341 else if(oper == "-") rtoken = rtoken - ktoken;
342 else if(oper == "*") rtoken = rtoken * ktoken;
343 else if(oper == "**") rtoken = pow(rtoken,ktoken);
344 else if(oper == ".") rtoken = rtoken * ktoken;
345 else rtoken = ktoken->Creates();
346 }
347 k++;
348 }
349 thesequenceoftokens->SetValue(i+1,rtoken);
350 level[i]=maxlevel-1;
351
352 if( (i+1) != k ) {
353 thesequenceoftokens->Remove(i+2,k);
354 j=i;
355 while(k <= numterm)
356 level[++j]=level[k++];
357 numterm=j;
358 }
359 }
360
361 rtoken = thesequenceoftokens->Value(1)->Creates();
362
363 if( rtoken->Value()==0. ) {
364 cout << "*BAD token value from unit '" << rtoken->Word() << "'" << endl;
365 rtoken->Value(1.);
366 }
367*/ // end old variant
368
369 // variant skl 15.09.2005
370 rtoken = CalculateLocal(thesequenceoftokens);
371
372 //rtoken->Dump(0,1);
373
374 return rtoken;
375}