0031682: Visualization - Prs3d_ShadingAspect::SetTransparency() has no effect with...
[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-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 #include <Units_Lexicon.hxx>
18
19 #include <Standard_Type.hxx>
20 #include <TCollection_AsciiString.hxx>
21 #include <TCollection_HAsciiString.hxx>
22 #include <Units_Token.hxx>
23
24 IMPLEMENT_STANDARD_RTTIEXT(Units_Lexicon,Standard_Transient)
25
26 namespace
27 {
28
29   //! Lexicon item
30   struct LexiconItem
31   {
32     char   Prefix[10];   //!< prefix or symbol (e.g. "k" for kilo)
33     char   Operation[2]; //!< operation
34     double Value;        //!< numeric parameter (e.g. multiplier)
35   };
36
37   //! Lexicon table.
38   //!
39   //! Original table (UnitsAPI/Lexi_Expr.dat) used symbols from extended ASCII,
40   //! which should not be used within UTF-8 text.
41   //!
42   //! This table preserves these codes for compatibility.
43   //! UTF-8 items might be uncommented after updating UnitsAPI/Units.dat
44   //! and analysis of further consequences.
45   static const LexiconItem THE_LEXICON[] =
46   {
47     // scope
48     {         "(", "S", 0.0 },
49     {         ")", "S", 0.0 },
50     // operators
51     {         "+", "O", 0.0 },
52     {         "-", "O", 0.0 },
53     {         "*", "O", 0.0 },
54     {         ".", "O", 0.0 },
55     {         "/", "O", 0.0 },
56     {        "**", "O", 0.0 },
57     // ^2, power of two
58     {      "\xB2", "P", 2.0 }, // ISO 8859-1/ISO Latin-1 (extended ASCII)
59     //{  "\xC2\xB2", "P", 2.0 }, // UTF-8
60     {        "p2", "P", 2.0 },
61     {       "sq.", "P", 2.0 },
62     // ^3, power of three
63     {      "\xB3", "P", 3.0 }, // ISO 8859-1/ISO Latin-1 (extended ASCII)
64     //{  "\xC2\xB3", "P", 3.0 }, // UTF-8
65     {       "cu.", "P", 3.0 },
66     // multipliers
67     {         "y", "M", 1.E-24 }, // yocto
68     {         "z", "M", 1.E-21 }, // zepto
69     {         "a", "M", 1.E-18 }, // atto
70     {         "f", "M", 1.E-15 }, // femto
71     {         "p", "M", 1.E-12 }, // pico
72     {         "n", "M", 1.E-09 }, // nano
73     {      "\xB5", "M", 1.E-06 }, // micro, ISO 8859-1/ISO Latin-1 (extended ASCII)
74     //{  "\xC2\xB5", "M", 1.E-06 }, // micro, UTF-8
75     {         "m", "M", 1.E-03 }, // milli
76     {         "c", "M", 1.E-02 }, // centi
77     {         "d", "M", 1.E-01 }, // deci
78     {        "da", "M", 1.E+01 }, // deca
79     {         "h", "M", 1.E+02 }, // hecto
80     {         "k", "M", 1.E+03 }, // kilo
81     {         "M", "M", 1.E+06 }, // mega
82     {         "G", "M", 1.E+09 }, // giga
83     {         "T", "M", 1.E+12 }, // tera
84     {         "P", "M", 1.E+15 }, // peta
85     {         "E", "M", 1.E+18 }, // exa
86     {         "Z", "M", 1.E+21 }, // zetta
87     {         "Y", "M", 1.E+24 }, // yotta
88     // Pi constant
89     {       "\xB6", "",  M_PI }, // Pilcrow sign, ISO 8859-1/ISO Latin-1 (extended ASCII)
90     //{   "\xCF\x80", "",  M_PI }, // UTF-8
91     {         "Pi", "",  M_PI },
92   };
93
94 }
95
96 //=======================================================================
97 //function : Units_Lexicon
98 //purpose  : 
99 //=======================================================================
100
101 Units_Lexicon::Units_Lexicon()
102 {
103 }
104
105
106 //=======================================================================
107 //function : Creates
108 //purpose  : 
109 //=======================================================================
110
111 void Units_Lexicon::Creates()
112 {
113   thesequenceoftokens = new Units_TokensSequence();
114
115   const Standard_Integer aNbLexiItems = sizeof(THE_LEXICON) / sizeof(LexiconItem);
116   for (Standard_Integer anItemIter = 0; anItemIter < aNbLexiItems; ++anItemIter)
117   {
118     const LexiconItem& anItem = THE_LEXICON[anItemIter];
119     if (thesequenceoftokens->IsEmpty())
120     {
121       Handle(Units_Token) aToken = new Units_Token (anItem.Prefix, anItem.Operation, anItem.Value);
122       thesequenceoftokens->Prepend (aToken);
123     }
124     else
125     {
126       AddToken (anItem.Prefix, anItem.Operation, anItem.Value);
127     }
128   }
129 }
130
131 //=======================================================================
132 //function : AddToken
133 //purpose  : 
134 //=======================================================================
135
136 void Units_Lexicon::AddToken(const Standard_CString aword,
137                              const Standard_CString amean,
138                              const Standard_Real avalue)
139 {
140   Handle(Units_Token) token;
141   Handle(Units_Token) referencetoken;
142   Standard_Boolean found = Standard_False;
143   Standard_Integer index;
144
145   for(index=1;index<=thesequenceoftokens->Length();index++) {
146     referencetoken = thesequenceoftokens->Value(index);
147     if( referencetoken->Word() == aword ) {
148       referencetoken->Update(amean);
149       found = Standard_True;
150       break;
151     }
152     else if( !( referencetoken->Word()>aword ) ) {
153       token = new Units_Token(aword,amean,avalue);
154       thesequenceoftokens->InsertBefore(index,token);
155       found = Standard_True;
156       break;
157     }
158   }
159   if(!found) {
160     token = new Units_Token(aword,amean,avalue);
161     thesequenceoftokens->Append(token);
162   }
163 }