1 // Created on: 1992-06-24
2 // Created by: Gilles DEBARBOUILLE
3 // Copyright (c) 1992-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
23 #include <Units_Lexicon.ixx>
24 #include <Units_Token.hxx>
25 #include <TCollection_HAsciiString.hxx>
26 #include <TCollection_AsciiString.hxx>
29 #include <sys/types.h>
35 #include <Standard_Stream.hxx>
38 //=======================================================================
39 //function : Units_Lexicon
41 //=======================================================================
43 Units_Lexicon::Units_Lexicon()
48 //=======================================================================
51 //=======================================================================
53 static inline bool strrightadjust (char *str)
55 for (size_t len = strlen(str); len > 0 && IsSpace (str[len-1]); len--)
57 return str[0] != '\0';
60 void Units_Lexicon::Creates(const Standard_CString afilename)
62 ifstream file(afilename, ios::in);
64 cout<<"unable to open "<<afilename<<" for input"<<endl;
68 thefilename = new TCollection_HAsciiString(afilename);
69 thesequenceoftokens = new Units_TokensSequence();
72 if(!stat(afilename,&buf)) thetime = buf.st_ctime;
74 // read file line-by-line; each line has fixed format:
75 // first 30 symbols for prefix or symbol (e.g. "k" for kilo)
76 // then 10 symbols for operation
77 // then 30 symbols for numeric parameter (e.g. multiplier)
78 // line can be shorter if last fields are empty
79 Handle(Units_Token) token;
80 for (int nline = 0; ; nline++) {
82 memset (line, 0, sizeof(line));
83 if (! file.getline (line, 255))
86 // trim trailing white space
87 if (! strrightadjust (line)) // empty line
90 // split line to parts
91 char chain[31], oper[11], coeff[31];
92 for (int i=0; i < 31; i++) chain[i] = '\0';
93 for (int i=0; i < 11; i++) oper[i] = '\0';
94 for (int i=0; i < 31; i++) coeff[i] = '\0';
96 sscanf (line, "%30c%10c%30c", chain, oper, coeff);
98 // remove trailing spaces and check values
99 if (! strrightadjust (chain))
101 strrightadjust (oper);
103 if (strrightadjust (coeff))
104 OSD::CStringToReal (coeff, value);
107 if(thesequenceoftokens->IsEmpty()) {
108 token = new Units_Token(chain,oper,value);
109 thesequenceoftokens->Prepend(token);
112 AddToken(chain,oper,value);
119 //=======================================================================
120 //function : UpToDate
122 //=======================================================================
124 Standard_Boolean Units_Lexicon::UpToDate() const
127 TCollection_AsciiString string = FileName();
129 if(!stat(string.ToCString(),&buf)) {
130 if(thetime >= buf.st_ctime)
131 return Standard_True;
134 return Standard_False;
138 //=======================================================================
139 //function : FileName
141 //=======================================================================
143 TCollection_AsciiString Units_Lexicon::FileName() const
145 return thefilename->String();
149 //=======================================================================
150 //function : AddToken
152 //=======================================================================
154 void Units_Lexicon::AddToken(const Standard_CString aword,
155 const Standard_CString amean,
156 const Standard_Real avalue)
158 Handle(Units_Token) token;
159 Handle(Units_Token) referencetoken;
160 Standard_Boolean found = Standard_False;
161 Standard_Integer index;
163 for(index=1;index<=thesequenceoftokens->Length();index++) {
164 referencetoken = thesequenceoftokens->Value(index);
165 if( referencetoken->Word() == aword ) {
166 referencetoken->Update(amean);
167 found = Standard_True;
170 else if( !( referencetoken->Word()>aword ) ) {
171 token = new Units_Token(aword,amean,avalue);
172 thesequenceoftokens->InsertBefore(index,token);
173 found = Standard_True;
178 token = new Units_Token(aword,amean,avalue);
179 thesequenceoftokens->Append(token);