156598dc712dfecb5877627120172b7ece9e79ab
[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 <OSD.hxx>
20 #include <OSD_OpenFile.hxx>
21 #include <Standard_Type.hxx>
22 #include <TCollection_AsciiString.hxx>
23 #include <TCollection_HAsciiString.hxx>
24 #include <Units_Token.hxx>
25
26 IMPLEMENT_STANDARD_RTTIEXT(Units_Lexicon,MMgt_TShared)
27
28 #ifdef _MSC_VER
29 # include <stdio.h>
30 #else
31 #include <Standard_Stream.hxx>
32 #endif  // _MSC_VER
33
34 //=======================================================================
35 //function : Units_Lexicon
36 //purpose  : 
37 //=======================================================================
38
39 Units_Lexicon::Units_Lexicon()
40 {
41 }
42
43
44 //=======================================================================
45 //function : Creates
46 //purpose  : 
47 //=======================================================================
48
49 static inline bool strrightadjust (char *str)
50 {
51   for (size_t len = strlen(str); len > 0 && IsSpace (str[len-1]); len--)
52     str[len-1] = '\0';
53   return str[0] != '\0';
54 }
55
56 void Units_Lexicon::Creates(const Standard_CString afilename)
57 {
58   std::ifstream file;
59   OSD_OpenStream (file, afilename, std::ios::in);
60   if(!file) {
61 #ifdef OCCT_DEBUG
62     cout<<"unable to open "<<afilename<<" for input"<<endl;
63 #endif
64     return;
65   }
66
67   thefilename = new TCollection_HAsciiString(afilename);
68   thesequenceoftokens = new Units_TokensSequence();
69   thetime = OSD_FileStatCTime (afilename);
70
71   // read file line-by-line; each line has fixed format:
72   // first 30 symbols for prefix or symbol (e.g. "k" for kilo)
73   // then 10 symbols for operation
74   // then 30 symbols for numeric parameter (e.g. multiplier)
75   // line can be shorter if last fields are empty
76   Handle(Units_Token) token;
77   for (int nline = 0; ; nline++) {
78     char line[256];
79     memset (line, 0, sizeof(line));
80     if (! file.getline (line, 255))
81       break;
82
83     // trim trailing white space
84     if (! strrightadjust (line)) // empty line
85       continue;
86
87     // split line to parts
88     char chain[31], oper[11], coeff[31];
89     memset(chain,0x00,sizeof(chain));
90     memset(oper,0x00,sizeof(oper));
91     memset(coeff,0x00,sizeof(coeff));
92
93     sscanf (line, "%30c%10c%30c", chain, oper, coeff);
94
95     // remove trailing spaces and check values
96     if (! strrightadjust (chain))
97       continue;
98     strrightadjust (oper);
99     double value = 0;
100     if (strrightadjust (coeff))
101       OSD::CStringToReal (coeff, value);
102
103     // add token
104     if(thesequenceoftokens->IsEmpty()) {
105       token = new Units_Token(chain,oper,value);
106       thesequenceoftokens->Prepend(token);
107     }
108     else {
109       AddToken(chain,oper,value);
110     }
111   }
112   file.close();
113 }
114
115
116 //=======================================================================
117 //function : UpToDate
118 //purpose  : 
119 //=======================================================================
120
121 Standard_Boolean Units_Lexicon::UpToDate() const
122 {
123   TCollection_AsciiString aPath = FileName();
124   Standard_Time aTime = OSD_FileStatCTime (aPath.ToCString());
125   return aTime != 0
126       && aTime <= thetime;
127 }
128
129
130 //=======================================================================
131 //function : FileName
132 //purpose  : 
133 //=======================================================================
134
135 TCollection_AsciiString Units_Lexicon::FileName() const
136 {
137   return thefilename->String();
138 }
139
140
141 //=======================================================================
142 //function : AddToken
143 //purpose  : 
144 //=======================================================================
145
146 void Units_Lexicon::AddToken(const Standard_CString aword,
147                              const Standard_CString amean,
148                              const Standard_Real avalue)
149 {
150   Handle(Units_Token) token;
151   Handle(Units_Token) referencetoken;
152   Standard_Boolean found = Standard_False;
153   Standard_Integer index;
154
155   for(index=1;index<=thesequenceoftokens->Length();index++) {
156     referencetoken = thesequenceoftokens->Value(index);
157     if( referencetoken->Word() == aword ) {
158       referencetoken->Update(amean);
159       found = Standard_True;
160       break;
161     }
162     else if( !( referencetoken->Word()>aword ) ) {
163       token = new Units_Token(aword,amean,avalue);
164       thesequenceoftokens->InsertBefore(index,token);
165       found = Standard_True;
166       break;
167     }
168   }
169   if(!found) {
170     token = new Units_Token(aword,amean,avalue);
171     thesequenceoftokens->Append(token);
172   }
173 }