0030895: Coding Rules - specify std namespace explicitly for std::cout and streams
[occt.git] / src / Units / Units_UnitSentence.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
18 #include <TCollection_AsciiString.hxx>
19 #include <Units.hxx>
20 #include <Units_Operators.hxx>
21 #include <Units_QuantitiesSequence.hxx>
22 #include <Units_Quantity.hxx>
23 #include <Units_Token.hxx>
24 #include <Units_TokensSequence.hxx>
25 #include <Units_Unit.hxx>
26 #include <Units_UnitsDictionary.hxx>
27 #include <Units_UnitSentence.hxx>
28 #include <Units_UnitsSequence.hxx>
29
30 //=======================================================================
31 //function : Units_UnitSentence
32 //purpose  : 
33 //=======================================================================
34 Units_UnitSentence::Units_UnitSentence (const Standard_CString astring)
35  : Units_Sentence(Units::LexiconUnits(), astring)
36
37   Analyse();
38   SetConstants();
39   SetUnits(Units::DictionaryOfUnits()->Sequence());
40
41 }
42
43
44 //=======================================================================
45 //function : Units_UnitSentence
46 //purpose  : 
47 //=======================================================================
48
49 Units_UnitSentence::Units_UnitSentence
50   (const Standard_CString astring,
51    const Handle(Units_QuantitiesSequence)& aquantitiessequence)
52  : Units_Sentence(Units::LexiconUnits(Standard_False),astring)
53
54   Analyse();
55   SetConstants();
56   SetUnits(aquantitiessequence);
57 }
58
59
60 //=======================================================================
61 //function : Analyse
62 //purpose  : 
63 //=======================================================================
64
65 void Units_UnitSentence::Analyse()
66 {
67   if(Sequence()->Length()==0)
68     return;
69
70   Standard_Integer index;
71   TCollection_AsciiString s;
72   Handle(Units_Token) token;
73   Handle(Units_Token) previoustoken;
74   Handle(Units_Token) currenttoken;
75   Handle(Units_Token) nexttoken;
76   Handle(Units_Token) lasttoken;
77   Handle(Units_TokensSequence) sequence = Sequence();
78
79   currenttoken = sequence->Value(1);
80
81   //std::cout<<std::endl;
82   //for(int ind=1; ind<=sequence->Length(); ind++) {
83   //  Handle(Units_Token) tok = sequence->Value(ind);
84   //  std::cout<<tok->Word()<<" ";
85   //}
86   //std::cout<<std::endl;
87   //for(ind=1; ind<=sequence->Length(); ind++) {
88   //  Handle(Units_Token) tok = sequence->Value(ind);
89   //  std::cout<<tok->Mean()<<" ";
90   //}
91   //std::cout<<std::endl;
92
93   for(index=2;index<=sequence->Length();index++) {
94     previoustoken = currenttoken;
95     currenttoken = sequence->Value(index);
96     s = currenttoken->Mean();
97     if( s=="MU" || s=="U" ) {
98       if( previoustoken->Mean()=="M" || previoustoken->Mean()=="MU" ) {
99         previoustoken->Mean("M");
100         currenttoken->Mean("U");
101         currenttoken->Value(0.);
102       }
103     }
104   }
105
106   for(index=1;index<=sequence->Length();index++) {
107     currenttoken = sequence->Value(index);
108     s=currenttoken->Mean();
109     if(s=="MU") {
110       currenttoken->Mean("U");
111       currenttoken->Value(0.);
112     }
113   }
114
115   currenttoken = sequence->Value(1);
116   index = 2;
117
118   while(index<=sequence->Length()) {
119     previoustoken = currenttoken;
120     currenttoken = sequence->Value(index);
121     if( previoustoken->Mean()=="M" && currenttoken->Mean()=="U") {
122       token=new Units_Token("(", "S");
123       sequence->InsertBefore(index-1,token);
124       token = new Units_Token("*", "O");
125       sequence->InsertBefore(++index,token);
126       token = new Units_Token(")", "S");
127       sequence->InsertAfter(++index,token);
128       index++;
129     }
130     index++;
131   }
132
133   index = 1;
134   while(index <= sequence->Length()) {
135     currenttoken = sequence->Value(index);
136     if(currenttoken->Mean()=="P") {
137       if( currenttoken->Word()=="sq." || currenttoken->Word()=="cu." ) {
138         sequence->Exchange(index,index+1);
139         index++;
140       }
141       token = new Units_Token("**", "O");
142       sequence->InsertBefore(index,token);
143       index++;
144     }
145     index++;
146   }
147
148   currenttoken = sequence->Value(1);
149   index = 2;
150
151   while(index<=sequence->Length()) {
152     previoustoken = currenttoken;
153     currenttoken=sequence->Value(index);
154     if(currenttoken->Word()=="(") {
155       if( !( previoustoken->Mean()=="O" || previoustoken->Word()=="(" ) ) {
156         token=new Units_Token("*", "O");
157         sequence->InsertBefore(index,token);
158         index++;
159       }
160     }
161     else if(previoustoken->Word()==")") {
162       if( !( currenttoken->Mean()=="O" || currenttoken->Word()==")" ) ) {
163         token = new Units_Token("*", "O");
164         sequence->InsertBefore(index,token);
165         index++;
166       }
167     }
168     index++;
169   }
170
171 }
172
173
174 //=======================================================================
175 //function : SetUnits
176 //purpose  : 
177 //=======================================================================
178
179 void Units_UnitSentence::SetUnits
180   (const Handle(Units_QuantitiesSequence)& aquantitiessequence)
181 {
182   Standard_Integer index,jindex,kindex;
183   Standard_Boolean istheend=0;
184   Handle(Units_Quantity) quantity;
185   Handle(Units_TokensSequence) sequenceoftokens;
186   Handle(Units_Token) currenttoken;
187   Handle(Units_UnitsSequence) unitssequence;
188   Handle(Units_Unit) unit;
189   TCollection_AsciiString symbol;
190
191   Handle(Units_QuantitiesSequence) quantitiessequence = aquantitiessequence;
192
193   for(index=1; index<=quantitiessequence->Length(); index++) {
194     quantity = quantitiessequence->Value(index);
195     unitssequence=quantity->Sequence();
196     for(jindex=1; jindex<=unitssequence->Length(); jindex++) {
197       unit = unitssequence->Value(jindex);
198       sequenceoftokens = Sequence();
199       istheend = Standard_True;
200       for(kindex=1; kindex<=sequenceoftokens->Length(); kindex++) {
201         currenttoken = sequenceoftokens->Value(kindex);
202         if(currenttoken->Mean()=="U") {
203           if(currenttoken->Value()==0.) {
204             symbol = currenttoken->Word();
205             if(unit == symbol.ToCString())
206               sequenceoftokens->SetValue(kindex,unit->Token());
207             else
208               istheend = Standard_False;
209           }
210         }
211       }
212       if(istheend)
213         break;
214     }
215     if(istheend)
216       break;
217   }
218 }