1 // File: Units_Lexicon.cxx
2 // Created: Wed Jun 24 12:47:21 1992
3 // Author: Gilles DEBARBOUILLE
7 #include <Units_Lexicon.ixx>
8 #include <Units_Token.hxx>
9 #include <TCollection_HAsciiString.hxx>
10 #include <TCollection_AsciiString.hxx>
12 #include <sys/types.h>
18 #include <Standard_Stream.hxx>
21 //=======================================================================
22 //function : Units_Lexicon
24 //=======================================================================
26 Units_Lexicon::Units_Lexicon()
31 //=======================================================================
34 //=======================================================================
36 void Units_Lexicon::Creates(const Standard_CString afilename)
38 char chain[256],line[256];
39 char oper[11],coeff[31];
46 Standard_Integer fr,i;
48 Handle(Units_Token) token;
57 //for(i=0; i<=255; i++)chain[i]=0;
58 ifstream file(afilename, ios::in);
60 cout<<"unable to open "<<afilename<<" for input"<<endl;
64 thefilename = new TCollection_HAsciiString(afilename);
65 thesequenceoftokens = new Units_TokensSequence();
67 if(!stat(afilename,&buf)) thetime = buf.st_ctime;
69 //for(i=0; i<=255; i++)line[i]=0;
71 while(file.getline(line,255)) {
72 int len = strlen( line ) ;
73 if(len == 1) continue; //skl - ???
74 for ( i = 0 ; i < 30 ; i++ ) {
76 fr=sscanf(&line[i],"%c",&chain[i]);
80 for ( i = 0 ; i < 10 ; i++ ) {
82 fr=sscanf(&line[30+i],"%c",&oper[i]);
86 for ( i = 0 ; i < 30 ; i++ ) {
88 fr=sscanf(&line[40+i],"%c",&coeff[i]);
93 cout << "Lexiconsscanf(%c )" << endl << "Chain" << Chain << endl
94 << "Oper" << Oper << endl
95 << "Coeff" << Coeff << endl ;
98 while( i>=0 && ( chain[i] == ' ' || !chain[i] ))
102 while( i>=0 && ( oper [i] == ' ' || !oper [i] ))
105 while( i>=0 && ( coeff[i] == ' ' || !coeff[i] ))
113 if(thesequenceoftokens->IsEmpty()) {
114 token = new Units_Token(chain,oper,value);
115 thesequenceoftokens->Prepend(token);
118 AddToken(chain,oper,value);
128 //=======================================================================
129 //function : UpToDate
131 //=======================================================================
133 Standard_Boolean Units_Lexicon::UpToDate() const
136 TCollection_AsciiString string = FileName();
138 if(!stat(string.ToCString(),&buf)) {
139 if(thetime >= buf.st_ctime)
140 return Standard_True;
143 return Standard_False;
147 //=======================================================================
148 //function : FileName
150 //=======================================================================
152 TCollection_AsciiString Units_Lexicon::FileName() const
154 return thefilename->String();
158 //=======================================================================
159 //function : AddToken
161 //=======================================================================
163 void Units_Lexicon::AddToken(const Standard_CString aword,
164 const Standard_CString amean,
165 const Standard_Real avalue)
167 Handle(Units_Token) token;
168 Handle(Units_Token) referencetoken;
169 Standard_Boolean found = Standard_False;
170 Standard_Integer index;
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;
179 else if( !( referencetoken->Word()>aword ) ) {
180 token = new Units_Token(aword,amean,avalue);
181 thesequenceoftokens->InsertBefore(index,token);
182 found = Standard_True;
187 token = new Units_Token(aword,amean,avalue);
188 thesequenceoftokens->Append(token);