7a254565d009392b337a2b269ccbb32fc72ec16a
[occt.git] / src / OSD / OSD_DirectoryIterator.cxx
1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 //
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
8 //
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 //
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #ifndef WNT
24
25 #include <OSD_DirectoryIterator.ixx>
26 #include <OSD_WhoAmI.hxx>
27
28 #include <stdio.h>
29
30 #ifdef  HAVE_DIRENT_H
31 # include <dirent.h>
32 #endif
33
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
36 #endif
37
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
40 #endif
41
42 //const OSD_WhoAmI Iam = OSD_WDirectoryIterator;
43
44 OSD_DirectoryIterator::OSD_DirectoryIterator() {
45
46  myDescr = NULL ;
47 }
48
49 OSD_DirectoryIterator::OSD_DirectoryIterator(const OSD_Path& where,
50                                              const TCollection_AsciiString& Mask){
51  myDescr = NULL ;
52  Initialize(where, Mask) ;
53 }
54
55 // For Windows NT compatibility -----------
56 void OSD_DirectoryIterator :: Destroy () {}
57 //-----------------------------------------
58
59 void OSD_DirectoryIterator::Initialize(const OSD_Path& where,
60                                    const TCollection_AsciiString& Mask){
61
62  myFlag = Standard_False;
63  where.SystemName(myPlace);
64  if (myPlace.Length()==0) myPlace = ".";
65  myMask = Mask;
66  if (myDescr) {
67    closedir((DIR *)myDescr) ;
68    myDescr = NULL ;
69  }
70  myInit = 1 ;
71 }
72
73 // Is there another directory entry ?
74
75 Standard_Boolean OSD_DirectoryIterator::More(){
76  if (myInit) {
77    myInit = 0 ;
78    myDescr = (Standard_Address) opendir(myPlace.ToCString()); 
79    if (myDescr) {            // LD : Si repertoire inaccessible retourner False
80      myFlag = Standard_True;
81      myInit = 0 ;
82      Next();          // Now find first entry
83    }
84  }
85  return (myFlag);
86 }
87
88 // Private :  See if directory name matches with a mask (like "*.c")
89 // LD : reecrit (original dans OSD_FileIterator.cxx)
90
91
92 static int strcmp_joker(const char *Mask,const char *Name)
93 {
94   const char *p, *s ;
95
96   for(p = Mask,s = Name ; *p && *p != '*' ; p++,s++)
97     if (*p != *s) return 0 ;
98   if (!*p) return !(*s) ;
99   while (*p == '*') p++ ;
100   if (!*p) return 1 ;
101   for (;*s; s++)
102     if (strcmp_joker(p,s)) return 1 ;
103   return 0 ;
104 }
105
106 // Find next directory entry in current directory
107
108 void OSD_DirectoryIterator::Next(){
109 int again = 1;
110 struct stat stat_buf;
111 char full_name[255];
112
113  myFlag = 0;   // Initialize to nothing found
114
115  do{
116     myEntry = readdir((DIR *)myDescr);
117    
118     if (!myEntry){   // No file found
119      myEntry = NULL;              // Keep pointer clean
120      myFlag = Standard_False;   // No more files/directory
121      closedir((DIR *)myDescr) ;       // so close directory
122      myDescr = NULL;
123      again = 0;
124     }
125     else {
126 //     if (!strcmp(entry->d_name,".")) continue;     LD : on prend ces 
127 //     if (!strcmp(entry->d_name,"..")) continue;         2 directories.
128
129      // Is it a directory ?
130
131      sprintf(full_name,"%s/%s",myPlace.ToCString(),
132              ((struct dirent *)myEntry)->d_name);                // LD debug
133      stat(full_name, &stat_buf);
134      if (S_ISDIR(stat_buf.st_mode))   // Ensure me it's not a file
135       if (strcmp_joker(myMask.ToCString(), ((struct dirent *)myEntry)->d_name)){
136                                                          // Does it follow mask ?
137        myFlag = Standard_True;
138        again = 0;
139      }
140     }
141
142  } while (again);
143
144 }
145
146
147 // Get Name of selected directory
148
149 OSD_Directory OSD_DirectoryIterator::Values(){
150 OSD_Path thisvalue;
151 TCollection_AsciiString Name;
152 TCollection_AsciiString Ext;
153 Standard_Integer position;
154
155  if (myEntry) Name = ((struct dirent *)myEntry)->d_name ;
156
157  position = Name.Search(".");
158
159  if (position != -1){
160   Ext = Name.Split(position - 1) ;   // Debug LD
161 //  Ext.Remove(1,position);
162 //  Name.Remove( position,Ext.Length()+1);
163  }
164
165  thisvalue.SetValues("", "", "", "", "", Name,Ext);
166  TheIterator.SetPath (thisvalue);
167
168  return (TheIterator);
169 }
170
171
172 void OSD_DirectoryIterator::Reset(){
173  myError.Reset();
174 }
175
176 Standard_Boolean OSD_DirectoryIterator::Failed()const{
177  return( myError.Failed());
178 }
179
180 void OSD_DirectoryIterator::Perror() {
181  myError.Perror();
182 }
183
184
185 Standard_Integer OSD_DirectoryIterator::Error()const{
186  return( myError.Error());
187 }
188
189 #else
190
191 //------------------------------------------------------------------------
192 //-------------------  Windows NT sources for OSD_DirectoryIterator ------
193 //------------------------------------------------------------------------
194
195
196 #define STRICT
197 #include <windows.h>
198
199 #include <OSD_DirectoryIterator.ixx>
200
201 #define _FD (  ( PWIN32_FIND_DATA )myData  )
202
203 void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
204
205 OSD_DirectoryIterator :: OSD_DirectoryIterator (
206                           const OSD_Path&                where,
207                           const TCollection_AsciiString& Mask
208                          ) {
209
210  myFlag   = Standard_False;
211  myHandle = ( Standard_Integer )INVALID_HANDLE_VALUE;
212
213  where.SystemName ( myPlace );
214
215  if (  myPlace.Length () == 0  ) myPlace = TEXT( "." );
216
217  myMask = Mask;
218  myData = NULL;
219
220 }  // end constructor
221
222 void OSD_DirectoryIterator :: Destroy () {
223
224  if ( myData != NULL ) HeapFree (  GetProcessHeap (), 0, myData  );
225
226  if (  myHandle != ( Standard_Integer )INVALID_HANDLE_VALUE  )
227
228   FindClose (  ( HANDLE )myHandle  );
229
230 }  // end  OSD_DirectoryIterator :: Destroy
231
232 Standard_Boolean OSD_DirectoryIterator :: More () {
233
234  if (  myHandle == ( Standard_Integer )INVALID_HANDLE_VALUE  ) {
235  
236   TCollection_AsciiString wc = myPlace + TEXT( "/" ) + myMask;
237
238   myData = HeapAlloc (
239             GetProcessHeap (), HEAP_GENERATE_EXCEPTIONS, sizeof ( WIN32_FIND_DATA )
240            );
241
242   myHandle = ( Standard_Integer )FindFirstFile (
243                                   wc.ToCString (), ( PWIN32_FIND_DATA )myData
244                                  );
245
246   if (  myHandle == ( Standard_Integer )INVALID_HANDLE_VALUE  )
247   
248    _osd_wnt_set_error ( myError, OSD_WDirectoryIterator );
249   
250   else {
251   
252    myFlag      = Standard_True;
253    myFirstCall = Standard_True;
254
255    Next ();
256
257   }  // end else
258   
259  } else if ( !myFlag ) {
260  
261   FindClose (  ( HANDLE )myHandle  );
262   myHandle = ( Standard_Integer )INVALID_HANDLE_VALUE;
263  
264  }  // end if
265
266  return myFlag;
267
268 }  // end OSD_DirectoryIterator :: More
269
270 void OSD_DirectoryIterator :: Next () {
271
272  if (  myFirstCall && !( _FD -> dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )  ||
273        !myFirstCall
274  ) {
275  
276   do {
277   
278    if (   !FindNextFile (  ( HANDLE )myHandle, _FD  )   ) {
279    
280     myFlag = Standard_False;
281
282     break;
283    
284    }  // end if
285   
286   } while (  !( _FD -> dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )  );
287  
288  }  // end if
289  
290  myFirstCall = Standard_False;
291
292 }  // end  OSD_DirectoryIterator :: Next
293
294 OSD_Directory OSD_DirectoryIterator :: Values () {
295
296  TheIterator.SetPath (   OSD_Path ( _FD -> cFileName  )   );
297
298  return TheIterator;
299
300 }  // end  OSD_DirectoryIterator :: Values
301
302 Standard_Boolean OSD_DirectoryIterator :: Failed () const {
303
304  return myError.Failed ();
305
306 }  // end OSD_DirectoryIterator :: Failed
307
308 void OSD_DirectoryIterator :: Reset () {
309
310  myError.Reset ();
311
312 }  // end OSD_DirectoryIterator :: Reset
313
314 void OSD_DirectoryIterator :: Perror () {
315
316  myError.Perror ();
317
318 }  // end OSD_DirectoryIterator :: Perror
319
320 Standard_Integer OSD_DirectoryIterator :: Error () const {
321
322  return myError.Error ();
323
324 }  // end OSD_DirectoryIterator :: Error
325
326 // For compatibility with UNIX version
327 OSD_DirectoryIterator::OSD_DirectoryIterator() {}
328
329 void OSD_DirectoryIterator::Initialize(
330                              const OSD_Path& /*where*/,
331                              const TCollection_AsciiString& /*Mask*/){}
332
333 #endif