b5db77308ceef49785b7088e9e9db78e377f5dd4
[occt.git] / src / Materials / Materials_MaterialsDictionary.cxx
1 // Created on: 1993-01-18
2 // Created by: Gilles DEBARBOUILLE
3 // Copyright (c) 1993-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
21 // Historique :
22 // CRD : 03/07/97 : Portage Windows NT.
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <Materials_MaterialsDictionary.ixx>
28
29 #ifdef HAVE_SYS_TYPES_H
30 # include <sys/types.h>
31 #endif
32 #if defined (HAVE_SYS_STAT_H) || defined (WNT)
33 # include <sys/stat.h>
34 #endif
35 #ifdef HAVE_STDLIB_H
36 # include <stdlib.h>
37 #endif
38 #ifdef HAVE_STRINGS_H
39 # include <strings.h>
40 #endif
41
42 #include <Standard_Stream.hxx>
43
44 #include <Materials.hxx>
45 #include <Materials_MaterialsSequence.hxx>
46 #include <Materials_Material.hxx>
47 #include <Materials_Color.hxx>
48 #include <Quantity_Color.hxx>
49 #include <TCollection_AsciiString.hxx>
50
51 #ifdef WNT
52 #define stat _stat
53 #endif
54 //#define strcasecmp _stricoll
55 #include <stdio.h>
56 //#endif
57
58 //=======================================================================
59 //function : Materials_MaterialsDictionary
60 //purpose  : 
61 //=======================================================================
62
63 Materials_MaterialsDictionary::Materials_MaterialsDictionary()
64 {
65   Standard_Integer i,fr,begin,end,lengthname;
66   //char* filename;
67   
68   char line[255];
69   char name[80];
70   char type[80];
71   char value1[80],value2[80],value3[80];
72   Handle(Materials_MaterialsSequence) materialssequence;
73   Handle(Materials_Material) material;
74   Handle(Materials_Color) pcolor;
75   
76   struct stat buf;
77
78   Standard_CString filename = Materials::MaterialsFile();
79
80   ifstream file(filename);
81   if(!file)
82     {
83       cout<<"unable to open "<<filename<<" for input"<<endl;
84       return;
85     }
86   
87   thefilename = new TCollection_HAsciiString(filename);
88
89   if(!stat(filename, &buf)) thetime = buf.st_ctime;
90
91   thematerialssequence = new Materials_MaterialsSequence();
92   
93   for(;;)
94     {
95       for(i=0; i<255; i++) line[i]=0;
96       file.getline(line,255);
97       if(!file)break;
98
99       i = 254;
100       while( i >= 0 && ( line[i] == ' ' || !line[i]))line[i--] = 0;
101       fr = i+1;
102       if(fr <= 1)continue;
103
104       if(line[0] != ' ')
105         {
106           material = new Materials_Material(line);
107           thematerialssequence->Append(material);
108         }
109       else
110         {
111           begin = end = 0;
112           for(i=0; i<fr; i++)
113             {
114               if(line[i] == '"')
115                 {
116                   if(begin)
117                     {
118                       end = i;
119                       break;
120                     }
121                   else
122                     {
123                       begin = i;
124                     }
125                 }
126             }
127
128           for(i=0; i<80; i++)name[i]=0;
129
130           lengthname = 0;
131           for(i=begin+1; i<=end-1; i++)name[lengthname++] = line[i];
132
133           for(i=0; i<80; i++)type   [i] = 0;
134           for(i=0; i<80; i++)value1 [i] = 0;
135           for(i=0; i<80; i++)value2 [i] = 0;
136           for(i=0; i<80; i++)value3 [i] = 0;
137
138           //          fr = sscanf(&line[end+1],"%s%s%s%s",&type,&value1,&value2,&value3);
139           fr = sscanf(&line[end+1],"%s%s%s%s",type,value1,value2,value3);
140
141           if(fr == -1) continue;
142
143           if     (!strcasecmp(type,"Materials_Color"))
144             {
145               Quantity_Color color
146                 (Atof(value1),Atof(value2),Atof(value3),Quantity_TOC_RGB);
147               pcolor = new Materials_Color(color);
148               material->Parameter(name,pcolor);
149             }
150           else if(!strcasecmp(type,"Standard_Real"))
151             {
152               material->Parameter(name,Atof(value1));
153             }
154           else if(!strcasecmp(type,"Standard_CString"))
155             {
156               material->Parameter(name,value1);
157             }
158         }
159     }
160   file.close();
161 }
162
163 //=======================================================================
164 //function : Material
165 //purpose  : 
166 //=======================================================================
167
168 Handle(Materials_Material) Materials_MaterialsDictionary::Material
169        (const Standard_CString amaterial) const
170 {
171   Handle(Materials_Material) material;
172
173   for(Standard_Integer index=1;index<=thematerialssequence->Length();index++)
174     {
175       material = thematerialssequence->Value(index);
176       if(material->Name() == amaterial) return material;
177     }
178   Standard_NoSuchObject::Raise("Material not in the dictionary");
179 // Pour compil sur NT ....
180   return material;
181 }
182
183 Standard_Boolean  Materials_MaterialsDictionary::ExistMaterial(const Standard_CString amaterial) const
184 {
185   Handle(Materials_Material) material;
186
187   for(Standard_Integer index=1;index<=thematerialssequence->Length();index++)
188     {
189       material = thematerialssequence->Value(index);
190       if(material->Name() == amaterial) return Standard_True;
191     }
192   return Standard_False;
193 }
194
195 //=======================================================================
196 //function : NumberOfMaterials
197 //purpose  : 
198 //=======================================================================
199
200 Standard_Integer Materials_MaterialsDictionary::NumberOfMaterials() const
201 {
202   return thematerialssequence->Length();
203 }
204
205 //=======================================================================
206 //function : Material
207 //purpose  : 
208 //=======================================================================
209
210 Handle(Materials_Material) Materials_MaterialsDictionary::Material
211        (const Standard_Integer anindex) const
212 {
213   return thematerialssequence->Value(anindex);
214 }
215
216 //=======================================================================
217 //function : UpToDate
218 //purpose  : 
219 //=======================================================================
220
221 Standard_Boolean Materials_MaterialsDictionary::UpToDate() const
222 {
223   struct stat buf;
224
225   TCollection_AsciiString string = thefilename->String();
226   if(!stat(string.ToCString(),&buf))
227     {
228       if(thetime == buf.st_ctime) return Standard_True;
229     }
230
231   return Standard_False;
232 }
233
234 //=======================================================================
235 //function : Dump
236 //purpose  : 
237 //=======================================================================
238
239 void Materials_MaterialsDictionary::Dump(Standard_OStream& astream) const
240 {
241   Standard_Integer index;
242   Handle(Materials_Material) material;
243
244   for(index=1;index<=thematerialssequence->Length();index++)
245     {
246       material = thematerialssequence->Value(index);
247       material->Dump(astream);
248     }
249 }
250