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
6 // This file is part of Open CASCADE Technology software library.
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <Units_Lexicon.ixx>
18 #include <Units_Token.hxx>
19 #include <TCollection_HAsciiString.hxx>
20 #include <TCollection_AsciiString.hxx>
23 #include <sys/types.h>
29 #include <Standard_Stream.hxx>
32 //=======================================================================
33 //function : Units_Lexicon
35 //=======================================================================
37 Units_Lexicon::Units_Lexicon()
42 //=======================================================================
45 //=======================================================================
47 static inline bool strrightadjust (char *str)
49 for (size_t len = strlen(str); len > 0 && IsSpace (str[len-1]); len--)
51 return str[0] != '\0';
54 void Units_Lexicon::Creates(const Standard_CString afilename)
56 ifstream file(afilename, ios::in);
59 cout<<"unable to open "<<afilename<<" for input"<<endl;
64 thefilename = new TCollection_HAsciiString(afilename);
65 thesequenceoftokens = new Units_TokensSequence();
68 if(!stat(afilename,&buf)) thetime = buf.st_ctime;
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++) {
78 memset (line, 0, sizeof(line));
79 if (! file.getline (line, 255))
82 // trim trailing white space
83 if (! strrightadjust (line)) // empty line
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));
92 sscanf (line, "%30c%10c%30c", chain, oper, coeff);
94 // remove trailing spaces and check values
95 if (! strrightadjust (chain))
97 strrightadjust (oper);
99 if (strrightadjust (coeff))
100 OSD::CStringToReal (coeff, value);
103 if(thesequenceoftokens->IsEmpty()) {
104 token = new Units_Token(chain,oper,value);
105 thesequenceoftokens->Prepend(token);
108 AddToken(chain,oper,value);
115 //=======================================================================
116 //function : UpToDate
118 //=======================================================================
120 Standard_Boolean Units_Lexicon::UpToDate() const
123 TCollection_AsciiString string = FileName();
125 if(!stat(string.ToCString(),&buf)) {
126 if(thetime >= buf.st_ctime)
127 return Standard_True;
130 return Standard_False;
134 //=======================================================================
135 //function : FileName
137 //=======================================================================
139 TCollection_AsciiString Units_Lexicon::FileName() const
141 return thefilename->String();
145 //=======================================================================
146 //function : AddToken
148 //=======================================================================
150 void Units_Lexicon::AddToken(const Standard_CString aword,
151 const Standard_CString amean,
152 const Standard_Real avalue)
154 Handle(Units_Token) token;
155 Handle(Units_Token) referencetoken;
156 Standard_Boolean found = Standard_False;
157 Standard_Integer index;
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;
166 else if( !( referencetoken->Word()>aword ) ) {
167 token = new Units_Token(aword,amean,avalue);
168 thesequenceoftokens->InsertBefore(index,token);
169 found = Standard_True;
174 token = new Units_Token(aword,amean,avalue);
175 thesequenceoftokens->Append(token);