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