Integration of OCCT 6.5.0 from SVN
[occt.git] / src / Units / Units_Lexicon.cxx
1 // File:        Units_Lexicon.cxx
2 // Created:     Wed Jun 24 12:47:21 1992
3 // Author:      Gilles DEBARBOUILLE
4 //              <gde@phobox>
5
6
7 #include <Units_Lexicon.ixx>
8 #include <Units_Token.hxx>
9 #include <TCollection_HAsciiString.hxx>
10 #include <TCollection_AsciiString.hxx>
11
12 #include <sys/types.h>
13 #include <sys/stat.h>
14
15 #ifdef WNT
16 # include <stdio.h>
17 #else
18 #include <Standard_Stream.hxx>
19 #endif  // WNT
20
21 //=======================================================================
22 //function : Units_Lexicon
23 //purpose  : 
24 //=======================================================================
25
26 Units_Lexicon::Units_Lexicon()
27 {
28 }
29
30
31 //=======================================================================
32 //function : Creates
33 //purpose  : 
34 //=======================================================================
35
36 void Units_Lexicon::Creates(const Standard_CString afilename)
37 {
38   char chain[256],line[256];
39   char oper[11],coeff[31];
40 #if 0
41   char *Chain = chain ;
42   char *Line = line ;
43   char *Oper = oper ;
44   char *Coeff = coeff ;
45 #endif
46   Standard_Integer fr,i;
47   Standard_Real value;
48   Handle(Units_Token) token;
49   struct stat buf;
50
51 #if 0
52   chain[255] = '\0' ;
53   oper[10] = '\0' ;
54   coeff[30] = '\0' ;
55 #endif
56
57   //for(i=0; i<=255; i++)chain[i]=0;
58   ifstream file(afilename, ios::in);
59   if(!file) {
60     cout<<"unable to open "<<afilename<<" for input"<<endl;
61     return;
62   }
63
64   thefilename = new TCollection_HAsciiString(afilename);
65   thesequenceoftokens = new Units_TokensSequence();
66
67   if(!stat(afilename,&buf)) thetime = buf.st_ctime;
68
69   //for(i=0; i<=255; i++)line[i]=0;
70
71   while(file.getline(line,255)) {
72     int len = strlen( line ) ;
73     if(len == 1) continue; //skl - ???
74     for ( i = 0 ; i < 30 ; i++ ) {
75       if ( i < len )
76         fr=sscanf(&line[i],"%c",&chain[i]);
77       else
78         chain[i] = 0 ;
79     }
80     for ( i = 0 ; i < 10 ; i++ ) {
81       if ( 30+i < len )
82         fr=sscanf(&line[30+i],"%c",&oper[i]);
83       else
84         oper[i] = 0 ;
85     }
86     for ( i = 0 ; i < 30 ; i++ ) {
87       if ( 40+i < len )
88         fr=sscanf(&line[40+i],"%c",&coeff[i]);
89       else
90         coeff[i] = 0 ;
91     }
92 #if 0
93     cout << "Lexiconsscanf(%c          )" << endl << "Chain" << Chain << endl
94          << "Oper" << Oper << endl
95          << "Coeff" << Coeff << endl ;
96 #endif
97     i=29;
98     while( i>=0 && ( chain[i] == ' ' || !chain[i] ))
99       chain[i--]=0;
100     if(i<0) continue;
101     i=9;
102     while( i>=0 && ( oper [i] == ' ' || !oper [i] ))
103       oper[i--]=0;
104     i=29;
105     while( i>=0 && ( coeff[i] == ' ' || !coeff[i] ))
106       coeff[i--]=0;
107
108     if(coeff[0])
109       value = atof(coeff);
110     else
111       value = 0.;
112
113     if(thesequenceoftokens->IsEmpty()) {
114       token = new Units_Token(chain,oper,value);
115       thesequenceoftokens->Prepend(token);
116     }
117     else {
118       AddToken(chain,oper,value);
119     }
120
121     for(i=0; i<255; i++)
122       line[i]=0;
123   }
124   file.close();
125 }
126
127
128 //=======================================================================
129 //function : UpToDate
130 //purpose  : 
131 //=======================================================================
132
133 Standard_Boolean Units_Lexicon::UpToDate() const
134 {
135   struct stat buf;
136   TCollection_AsciiString string = FileName();
137
138   if(!stat(string.ToCString(),&buf)) {
139     if(thetime >= buf.st_ctime)
140       return Standard_True;
141   }
142
143   return Standard_False;
144 }
145
146
147 //=======================================================================
148 //function : FileName
149 //purpose  : 
150 //=======================================================================
151
152 TCollection_AsciiString Units_Lexicon::FileName() const
153 {
154   return thefilename->String();
155 }
156
157
158 //=======================================================================
159 //function : AddToken
160 //purpose  : 
161 //=======================================================================
162
163 void Units_Lexicon::AddToken(const Standard_CString aword,
164                              const Standard_CString amean,
165                              const Standard_Real avalue)
166 {
167   Handle(Units_Token) token;
168   Handle(Units_Token) referencetoken;
169   Standard_Boolean found = Standard_False;
170   Standard_Integer index;
171
172   for(index=1;index<=thesequenceoftokens->Length();index++) {
173     referencetoken = thesequenceoftokens->Value(index);
174     if( referencetoken->Word() == aword ) {
175       referencetoken->Update(amean);
176       found = Standard_True;
177       break;
178     }
179     else if( !( referencetoken->Word()>aword ) ) {
180       token = new Units_Token(aword,amean,avalue);
181       thesequenceoftokens->InsertBefore(index,token);
182       found = Standard_True;
183       break;
184     }
185   }
186   if(!found) {
187     token = new Units_Token(aword,amean,avalue);
188     thesequenceoftokens->Append(token);
189   }
190 }