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