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.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>
26 IMPLEMENT_STANDARD_RTTIEXT(Units_Lexicon,MMgt_TShared)
31 #include <Standard_Stream.hxx>
34 //=======================================================================
35 //function : Units_Lexicon
37 //=======================================================================
39 Units_Lexicon::Units_Lexicon()
44 //=======================================================================
47 //=======================================================================
49 static inline bool strrightadjust (char *str)
51 for (size_t len = strlen(str); len > 0 && IsSpace (str[len-1]); len--)
53 return str[0] != '\0';
56 void Units_Lexicon::Creates(const Standard_CString afilename)
59 OSD_OpenStream (file, afilename, std::ios::in);
62 cout<<"unable to open "<<afilename<<" for input"<<endl;
67 thefilename = new TCollection_HAsciiString(afilename);
68 thesequenceoftokens = new Units_TokensSequence();
69 thetime = OSD_FileStatCTime (afilename);
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++) {
79 memset (line, 0, sizeof(line));
80 if (! file.getline (line, 255))
83 // trim trailing white space
84 if (! strrightadjust (line)) // empty line
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));
93 sscanf (line, "%30c%10c%30c", chain, oper, coeff);
95 // remove trailing spaces and check values
96 if (! strrightadjust (chain))
98 strrightadjust (oper);
100 if (strrightadjust (coeff))
101 OSD::CStringToReal (coeff, value);
104 if(thesequenceoftokens->IsEmpty()) {
105 token = new Units_Token(chain,oper,value);
106 thesequenceoftokens->Prepend(token);
109 AddToken(chain,oper,value);
116 //=======================================================================
117 //function : UpToDate
119 //=======================================================================
121 Standard_Boolean Units_Lexicon::UpToDate() const
123 TCollection_AsciiString aPath = FileName();
124 Standard_Time aTime = OSD_FileStatCTime (aPath.ToCString());
130 //=======================================================================
131 //function : FileName
133 //=======================================================================
135 TCollection_AsciiString Units_Lexicon::FileName() const
137 return thefilename->String();
141 //=======================================================================
142 //function : AddToken
144 //=======================================================================
146 void Units_Lexicon::AddToken(const Standard_CString aword,
147 const Standard_CString amean,
148 const Standard_Real avalue)
150 Handle(Units_Token) token;
151 Handle(Units_Token) referencetoken;
152 Standard_Boolean found = Standard_False;
153 Standard_Integer index;
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;
162 else if( !( referencetoken->Word()>aword ) ) {
163 token = new Units_Token(aword,amean,avalue);
164 thesequenceoftokens->InsertBefore(index,token);
165 found = Standard_True;
170 token = new Units_Token(aword,amean,avalue);
171 thesequenceoftokens->Append(token);