1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
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.
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.
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.
26 #include <OSD_FileIterator.ixx>
27 #include <OSD_WhoAmI.hxx>
30 #include <sys/types.h>
35 # define NAMLEN(dirent) strlen((dirent)->d_name)
37 extern char *vmsify PARAMS ((char *name, int type));
40 # define dirent direct
41 # define NAMLEN(dirent) (dirent)->d_namlen
42 # ifdef HAVE_SYS_NDIR_H
43 # include <sys/ndir.h>
45 # ifdef HAVE_SYS_DIR_H
53 # endif /* HAVE_VMSDIR_H */
56 /* In GNU systems, <dirent.h> defines this macro for us. */
59 # define NAMLEN(d) _D_NAMLEN(d)
62 #if (defined (POSIX) || defined (VMS) || defined (WINDOWS32)) && !defined (__GNU_LIBRARY__)
63 /* Posix does not require that the d_ino field be present, and some
64 systems do not provide it. */
65 # define REAL_DIR_ENTRY(dp) 1
66 # define FAKE_DIR_ENTRY(dp)
68 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
69 # define FAKE_DIR_ENTRY(dp) (dp->d_ino = 1)
72 //const OSD_WhoAmI Iam = OSD_WFileIterator;
75 OSD_FileIterator::OSD_FileIterator() {
79 OSD_FileIterator::OSD_FileIterator(const OSD_Path& where,
80 const TCollection_AsciiString& Mask){
82 Initialize(where, Mask) ;
85 // For Windows NT compatibility
86 void OSD_FileIterator :: Destroy () {}
88 void OSD_FileIterator::Initialize(const OSD_Path& where,
89 const TCollection_AsciiString& Mask){
90 myFlag = Standard_False;
91 where.SystemName(myPlace);
92 if (myPlace.Length()==0) myPlace = ".";
95 closedir((DIR *)myDescr) ;
101 // Is there another file entry ?
103 Standard_Boolean OSD_FileIterator::More(){
106 myDescr = (Standard_Address) opendir(myPlace.ToCString());
107 if (myDescr) { // LD : Si repertoire inaccessible retourner False
108 myFlag = Standard_True;
110 Next(); // Now find first entry
116 // Private : See if file name matches with a mask (like "*.c")
118 static int strcmp_joker(const char *Mask,const char *Name)
122 for(p = Mask,s = Name ; *p && *p != '*' ; p++,s++)
123 if (*p != *s) return 0 ;
125 if (!*p) return !(*s) ;
126 while (*p == '*') p++ ;
129 if (strcmp_joker(p,s)) return 1 ;
134 // LD : ancienne version.
140 static int strcmp_joker(char *fileMask,char *fileName)
142 char *sauve_fileMask,*sauve_fileName;
144 int beginning = 1; // 0 if first character is a joker, otherwise 1
146 while (*fileName) { // Test end of AsciiString
148 if (*fileMask == NO_MASK) {
151 while(*fileMask == NO_MASK) fileMask++;
155 *fileName != *fileMask)
158 sauve_fileMask = fileMask; // Save strings
159 sauve_fileName = fileName;
161 else return(0); // fileMask ends with a joker
164 else { // Compare two characters
165 compare_result = *fileMask++ - *fileName++;
167 if (compare_result != 0)
169 return (compare_result); /* 1ere chaine pas de joker au debut */
170 else { // Look ahead for same string
171 fileMask = sauve_fileMask;
172 fileName = ++sauve_fileName;
174 *fileName != *fileMask)
176 sauve_fileName = fileName;
183 while(*fileMask == NO_MASK) fileMask++;
184 return(*fileMask - *fileName);
188 // Find next file entry in current directory
190 void OSD_FileIterator::Next(){
192 struct stat stat_buf;
195 myFlag = 0; // Initialize to nothing found
198 myEntry = readdir((DIR *)myDescr);
200 if (!myEntry){ // No file found
201 myEntry = NULL; // Keep pointer clean
202 myFlag = Standard_False; // No more files/directory
203 closedir((DIR *)myDescr) ; // so close directory
208 if (!strcmp(((struct dirent *)myEntry)->d_name,".")) continue;
209 if (!strcmp(((struct dirent *)myEntry)->d_name,"..")) continue;
213 sprintf(full_name,"%s/%s",myPlace.ToCString(),
214 ((struct dirent *)myEntry)->d_name); // LD debug
216 cout << "Place : " << myPlace << endl;
217 cout << "FName : " << full_name << endl;
219 stat(full_name, &stat_buf);
220 if (S_ISREG(stat_buf.st_mode)) // LD : Ensure me it's a regular file
221 if (strcmp_joker(myMask.ToCString(), ((struct dirent *)myEntry)->d_name)){
222 // Does it follow mask ?
223 myFlag = Standard_True;
232 // Get Name of selected file
234 OSD_File OSD_FileIterator::Values(){
236 TCollection_AsciiString Name;
237 TCollection_AsciiString Ext;
238 Standard_Integer position;
240 if (myEntry) Name = ((struct dirent *)myEntry)->d_name ;
242 position = Name.Search(".");
246 Ext.Remove(1,position-1);
247 Name.Remove( position,Ext.Length());
250 thisvalue.SetValues("", "", "", "", "", Name,Ext);
251 TheIterator.SetPath (thisvalue);
253 return (TheIterator);
257 void OSD_FileIterator::Reset(){
261 Standard_Boolean OSD_FileIterator::Failed()const{
262 return( myError.Failed());
265 void OSD_FileIterator::Perror() {
270 Standard_Integer OSD_FileIterator::Error()const{
271 return( myError.Error());
276 //------------------------------------------------------------------------
277 //------------------- Windows NT sources for OSD_FileIterator -----------
278 //------------------------------------------------------------------------
283 #include <OSD_FileIterator.ixx>
285 #define _FD ( ( PWIN32_FIND_DATA )myData )
287 void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
289 OSD_FileIterator :: OSD_FileIterator (
290 const OSD_Path& where,
291 const TCollection_AsciiString& Mask
294 myFlag = Standard_False;
295 myHandle = INVALID_HANDLE_VALUE;
297 where.SystemName ( myPlace );
299 if ( myPlace.Length () == 0 ) myPlace = TEXT( "." );
306 void OSD_FileIterator :: Destroy () {
308 if ( myData != NULL ) HeapFree ( GetProcessHeap (), 0, myData );
310 if ( myHandle != INVALID_HANDLE_VALUE )
312 FindClose ( ( HANDLE )myHandle );
314 } // end OSD_DirectoryIterator :: Destroy
316 Standard_Boolean OSD_FileIterator :: More () {
318 if ( myHandle == INVALID_HANDLE_VALUE ) {
320 TCollection_AsciiString wc = myPlace + TEXT( "/" ) + myMask;
323 GetProcessHeap (), HEAP_GENERATE_EXCEPTIONS, sizeof ( WIN32_FIND_DATA )
326 myHandle = FindFirstFile (wc.ToCString (), (PWIN32_FIND_DATA)myData);
328 if ( myHandle == INVALID_HANDLE_VALUE )
330 _osd_wnt_set_error ( myError, OSD_WDirectoryIterator );
334 myFlag = Standard_True;
335 myFirstCall = Standard_True;
341 } else if ( !myFlag ) {
343 FindClose ( ( HANDLE )myHandle );
344 myHandle = INVALID_HANDLE_VALUE;
350 } // end OSD_FileIterator :: More
352 void OSD_FileIterator :: Next () {
354 if ( myFirstCall && ( _FD -> dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) ||
360 if ( !FindNextFile ( ( HANDLE )myHandle, _FD ) ) {
362 myFlag = Standard_False;
368 } while ( ( _FD -> dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) );
372 myFirstCall = Standard_False;
374 } // end OSD_FileIterator :: Next
376 OSD_File OSD_FileIterator :: Values () {
378 TheIterator.SetPath ( OSD_Path ( _FD -> cFileName ) );
382 } // end OSD_FileIterator :: Values
384 Standard_Boolean OSD_FileIterator :: Failed () const {
386 return myError.Failed ();
388 } // end OSD_FileIterator :: Failed
390 void OSD_FileIterator :: Reset () {
394 } // end OSD_FileIterator :: Reset
396 void OSD_FileIterator :: Perror () {
400 } // end OSD_FileIterator :: Perror
402 Standard_Integer OSD_FileIterator :: Error () const {
404 return myError.Error ();
406 } // end OSD_FileIterator :: Error
408 // For compatibility with UNIX version
409 OSD_FileIterator::OSD_FileIterator() {}
411 void OSD_FileIterator::Initialize(
413 const TCollection_AsciiString&){}