1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
18 #include <OSD_File.hxx>
19 #include <OSD_FileIterator.hxx>
20 #include <OSD_OSDError.hxx>
21 #include <OSD_Path.hxx>
22 #include <OSD_WhoAmI.hxx>
23 #include <TCollection_AsciiString.hxx>
28 #include <sys/types.h>
29 # define NAMLEN(dirent) strlen((dirent)->d_name)
31 extern char *vmsify PARAMS ((char *name, int type));
34 /* In GNU systems, <dirent.h> defines this macro for us. */
37 # define NAMLEN(d) _D_NAMLEN(d)
40 #if (defined (POSIX) || defined (VMS) || defined (WINDOWS32)) && !defined (__GNU_LIBRARY__)
41 /* Posix does not require that the d_ino field be present, and some
42 systems do not provide it. */
43 # define REAL_DIR_ENTRY(dp) 1
44 # define FAKE_DIR_ENTRY(dp)
46 # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
47 # define FAKE_DIR_ENTRY(dp) (dp->d_ino = 1)
50 //const OSD_WhoAmI Iam = OSD_WFileIterator;
53 OSD_FileIterator::OSD_FileIterator()
61 OSD_FileIterator::OSD_FileIterator(const OSD_Path& where,
62 const TCollection_AsciiString& Mask)
68 Initialize(where, Mask) ;
71 // For Windows NT compatibility
72 void OSD_FileIterator :: Destroy () {}
74 void OSD_FileIterator::Initialize(const OSD_Path& where,
75 const TCollection_AsciiString& Mask){
76 myFlag = Standard_False;
77 where.SystemName(myPlace);
78 if (myPlace.Length()==0) myPlace = ".";
81 closedir((DIR *)myDescr) ;
87 // Is there another file entry ?
89 Standard_Boolean OSD_FileIterator::More(){
92 myDescr = (Standard_Address) opendir(myPlace.ToCString());
93 if (myDescr) { // LD : Si repertoire inaccessible retourner False
94 myFlag = Standard_True;
96 Next(); // Now find first entry
102 // Private : See if file name matches with a mask (like "*.c")
104 static int strcmp_joker(const char *Mask,const char *Name)
108 for(p = Mask,s = Name ; *p && *p != '*' ; p++,s++)
109 if (*p != *s) return 0 ;
111 if (!*p) return !(*s) ;
112 while (*p == '*') p++ ;
115 if (strcmp_joker(p,s)) return 1 ;
120 // LD : ancienne version.
126 static int strcmp_joker(char *fileMask,char *fileName)
128 char *sauve_fileMask,*sauve_fileName;
130 int beginning = 1; // 0 if first character is a joker, otherwise 1
132 while (*fileName) { // Test end of AsciiString
134 if (*fileMask == NO_MASK) {
137 while(*fileMask == NO_MASK) fileMask++;
141 *fileName != *fileMask)
144 sauve_fileMask = fileMask; // Save strings
145 sauve_fileName = fileName;
147 else return(0); // fileMask ends with a joker
150 else { // Compare two characters
151 compare_result = *fileMask++ - *fileName++;
153 if (compare_result != 0)
155 return (compare_result); /* 1ere chaine pas de joker au debut */
156 else { // Look ahead for same string
157 fileMask = sauve_fileMask;
158 fileName = ++sauve_fileName;
160 *fileName != *fileMask)
162 sauve_fileName = fileName;
169 while(*fileMask == NO_MASK) fileMask++;
170 return(*fileMask - *fileName);
174 // Find next file entry in current directory
176 void OSD_FileIterator::Next(){
178 struct stat stat_buf;
181 myFlag = false; // Initialize to nothing found
184 myEntry = readdir((DIR *)myDescr);
186 if (!myEntry){ // No file found
187 myEntry = NULL; // Keep pointer clean
188 myFlag = Standard_False; // No more files/directory
189 closedir((DIR *)myDescr) ; // so close directory
194 if (!strcmp(((struct dirent *)myEntry)->d_name,".")) continue;
195 if (!strcmp(((struct dirent *)myEntry)->d_name,"..")) continue;
199 sprintf(full_name,"%s/%s",myPlace.ToCString(),
200 ((struct dirent *)myEntry)->d_name); // LD debug
202 cout << "Place : " << myPlace << endl;
203 cout << "FName : " << full_name << endl;
205 stat(full_name, &stat_buf);
206 if (S_ISREG(stat_buf.st_mode)) // LD : Ensure me it's a regular file
207 if (strcmp_joker(myMask.ToCString(), ((struct dirent *)myEntry)->d_name)){
208 // Does it follow mask ?
209 myFlag = Standard_True;
218 // Get Name of selected file
220 OSD_File OSD_FileIterator::Values(){
222 TCollection_AsciiString Name;
223 TCollection_AsciiString Ext;
224 Standard_Integer position;
226 if (myEntry) Name = ((struct dirent *)myEntry)->d_name ;
228 position = Name.Search(".");
232 Ext.Remove(1,position-1);
233 Name.Remove( position,Ext.Length());
236 thisvalue.SetValues("", "", "", "", "", Name,Ext);
237 TheIterator.SetPath (thisvalue);
239 return (TheIterator);
243 void OSD_FileIterator::Reset(){
247 Standard_Boolean OSD_FileIterator::Failed()const{
248 return( myError.Failed());
251 void OSD_FileIterator::Perror() {
256 Standard_Integer OSD_FileIterator::Error()const{
257 return( myError.Error());
262 //------------------------------------------------------------------------
263 //------------------- Windows NT sources for OSD_FileIterator -----------
264 //------------------------------------------------------------------------
269 #include <OSD_File.hxx>
270 #include <OSD_FileIterator.hxx>
271 #include <OSD_OSDError.hxx>
272 #include <OSD_Path.hxx>
273 #include <TCollection_AsciiString.hxx>
274 #include <TCollection_ExtendedString.hxx>
276 #define _FD ( ( PWIN32_FIND_DATAW )myData )
278 void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
280 OSD_FileIterator :: OSD_FileIterator (
281 const OSD_Path& where,
282 const TCollection_AsciiString& Mask
285 myFlag = Standard_False;
286 myHandle = INVALID_HANDLE_VALUE;
288 where.SystemName ( myPlace );
290 if ( myPlace.Length () == 0 ) myPlace = ".";
297 void OSD_FileIterator :: Destroy () {
299 if ( myData != NULL ) HeapFree ( GetProcessHeap (), 0, myData );
301 if ( myHandle != INVALID_HANDLE_VALUE )
303 FindClose ( ( HANDLE )myHandle );
305 } // end OSD_DirectoryIterator :: Destroy
307 Standard_Boolean OSD_FileIterator :: More () {
309 if ( myHandle == INVALID_HANDLE_VALUE ) {
311 TCollection_AsciiString wc = myPlace + "/" + myMask;
314 GetProcessHeap (), HEAP_GENERATE_EXCEPTIONS, sizeof ( WIN32_FIND_DATAW )
317 // make wchar_t string from UTF-8
318 TCollection_ExtendedString wcW(wc);
319 myHandle = FindFirstFileExW (wcW.ToWideString(), FindExInfoStandard, (PWIN32_FIND_DATAW)myData, FindExSearchNameMatch, NULL, 0);
321 if ( myHandle == INVALID_HANDLE_VALUE )
323 _osd_wnt_set_error ( myError, OSD_WDirectoryIterator );
327 myFlag = Standard_True;
328 myFirstCall = Standard_True;
334 } else if ( !myFlag ) {
336 FindClose ( ( HANDLE )myHandle );
337 myHandle = INVALID_HANDLE_VALUE;
343 } // end OSD_FileIterator :: More
345 void OSD_FileIterator :: Next () {
347 if ( ! myFirstCall || ( _FD -> dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) ) {
351 if ( !FindNextFileW ( ( HANDLE )myHandle, _FD ) ) {
353 myFlag = Standard_False;
359 } while ( ( _FD -> dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) );
363 myFirstCall = Standard_False;
365 } // end OSD_FileIterator :: Next
367 OSD_File OSD_FileIterator :: Values () {
370 TCollection_AsciiString aFileName
371 (TCollection_ExtendedString( (Standard_ExtString) _FD -> cFileName) );
372 TheIterator.SetPath ( OSD_Path ( aFileName ) );
376 } // end OSD_FileIterator :: Values
378 Standard_Boolean OSD_FileIterator :: Failed () const {
380 return myError.Failed ();
382 } // end OSD_FileIterator :: Failed
384 void OSD_FileIterator :: Reset () {
388 } // end OSD_FileIterator :: Reset
390 void OSD_FileIterator :: Perror () {
394 } // end OSD_FileIterator :: Perror
396 Standard_Integer OSD_FileIterator :: Error () const {
398 return myError.Error ();
400 } // end OSD_FileIterator :: Error
402 // For compatibility with UNIX version
403 OSD_FileIterator::OSD_FileIterator() {}
405 void OSD_FileIterator::Initialize(
407 const TCollection_AsciiString&){}