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_Directory.hxx>
19 #include <OSD_DirectoryIterator.hxx>
20 #include <OSD_OSDError.hxx>
21 #include <OSD_Path.hxx>
22 #include <OSD_WhoAmI.hxx>
23 #include <TCollection_AsciiString.hxx>
28 //const OSD_WhoAmI Iam = OSD_WDirectoryIterator;
29 OSD_DirectoryIterator::OSD_DirectoryIterator() {
34 OSD_DirectoryIterator::OSD_DirectoryIterator(const OSD_Path& where,
35 const TCollection_AsciiString& Mask){
37 Initialize(where, Mask) ;
40 // For Windows NT compatibility -----------
41 void OSD_DirectoryIterator :: Destroy () {}
42 //-----------------------------------------
44 void OSD_DirectoryIterator::Initialize(const OSD_Path& where,
45 const TCollection_AsciiString& Mask){
47 myFlag = Standard_False;
48 where.SystemName(myPlace);
49 if (myPlace.Length()==0) myPlace = ".";
52 closedir((DIR *)myDescr) ;
58 // Is there another directory entry ?
60 Standard_Boolean OSD_DirectoryIterator::More(){
63 myDescr = (Standard_Address) opendir(myPlace.ToCString());
64 if (myDescr) { // LD : Si repertoire inaccessible retourner False
65 myFlag = Standard_True;
67 Next(); // Now find first entry
73 // Private : See if directory name matches with a mask (like "*.c")
74 // LD : reecrit (original dans OSD_FileIterator.cxx)
77 static int strcmp_joker(const char *Mask,const char *Name)
81 for(p = Mask,s = Name ; *p && *p != '*' ; p++,s++)
82 if (*p != *s) return 0 ;
83 if (!*p) return !(*s) ;
84 while (*p == '*') p++ ;
87 if (strcmp_joker(p,s)) return 1 ;
91 // Find next directory entry in current directory
93 void OSD_DirectoryIterator::Next(){
98 myFlag = 0; // Initialize to nothing found
101 myEntry = readdir((DIR *)myDescr);
103 if (!myEntry){ // No file found
104 myEntry = NULL; // Keep pointer clean
105 myFlag = Standard_False; // No more files/directory
106 closedir((DIR *)myDescr) ; // so close directory
111 // if (!strcmp(entry->d_name,".")) continue; LD : on prend ces
112 // if (!strcmp(entry->d_name,"..")) continue; 2 directories.
114 // Is it a directory ?
116 sprintf(full_name,"%s/%s",myPlace.ToCString(),
117 ((struct dirent *)myEntry)->d_name); // LD debug
118 stat(full_name, &stat_buf);
119 if (S_ISDIR(stat_buf.st_mode)) // Ensure me it's not a file
120 if (strcmp_joker(myMask.ToCString(), ((struct dirent *)myEntry)->d_name)){
121 // Does it follow mask ?
122 myFlag = Standard_True;
132 // Get Name of selected directory
134 OSD_Directory OSD_DirectoryIterator::Values(){
136 TCollection_AsciiString Name;
137 TCollection_AsciiString Ext;
138 Standard_Integer position;
140 if (myEntry) Name = ((struct dirent *)myEntry)->d_name ;
142 position = Name.Search(".");
145 Ext = Name.Split(position - 1) ; // Debug LD
146 // Ext.Remove(1,position);
147 // Name.Remove( position,Ext.Length()+1);
150 thisvalue.SetValues("", "", "", "", "", Name,Ext);
151 TheIterator.SetPath (thisvalue);
153 return (TheIterator);
157 void OSD_DirectoryIterator::Reset(){
161 Standard_Boolean OSD_DirectoryIterator::Failed()const{
162 return( myError.Failed());
165 void OSD_DirectoryIterator::Perror() {
170 Standard_Integer OSD_DirectoryIterator::Error()const{
171 return( myError.Error());
176 //------------------------------------------------------------------------
177 //------------------- Windows NT sources for OSD_DirectoryIterator ------
178 //------------------------------------------------------------------------
185 #include <OSD_Directory.hxx>
186 #include <OSD_DirectoryIterator.hxx>
187 #include <OSD_OSDError.hxx>
188 #include <OSD_Path.hxx>
189 #include <TCollection_AsciiString.hxx>
190 #include <TCollection_ExtendedString.hxx>
192 #define _FD ( ( PWIN32_FIND_DATAW )myData )
194 void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
196 OSD_DirectoryIterator :: OSD_DirectoryIterator (
197 const OSD_Path& where,
198 const TCollection_AsciiString& Mask
201 myFlag = Standard_False;
202 myHandle = INVALID_HANDLE_VALUE;
204 where.SystemName ( myPlace );
206 if ( myPlace.Length () == 0 ) myPlace = ".";
213 void OSD_DirectoryIterator :: Destroy () {
215 if ( myData != NULL ) HeapFree ( GetProcessHeap (), 0, myData );
217 if ( myHandle != INVALID_HANDLE_VALUE )
219 FindClose ( ( HANDLE )myHandle );
221 } // end OSD_DirectoryIterator :: Destroy
223 Standard_Boolean OSD_DirectoryIterator :: More () {
225 if ( myHandle == INVALID_HANDLE_VALUE ) {
227 TCollection_AsciiString wc = myPlace + "/" + myMask;
230 GetProcessHeap (), HEAP_GENERATE_EXCEPTIONS, sizeof ( WIN32_FIND_DATAW )
233 // make wchar_t string from UTF-8
234 TCollection_ExtendedString wcW(wc);
235 myHandle = FindFirstFileW ((const wchar_t*)wcW.ToExtString(), (PWIN32_FIND_DATAW)myData);
237 if ( myHandle == INVALID_HANDLE_VALUE )
239 _osd_wnt_set_error ( myError, OSD_WDirectoryIterator );
243 myFlag = Standard_True;
244 myFirstCall = Standard_True;
250 } else if ( !myFlag ) {
252 FindClose ( ( HANDLE )myHandle );
253 myHandle = INVALID_HANDLE_VALUE;
259 } // end OSD_DirectoryIterator :: More
261 void OSD_DirectoryIterator :: Next () {
263 if ( myFirstCall && !( _FD -> dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) ||
269 if ( !FindNextFileW ( ( HANDLE )myHandle, _FD ) ) {
271 myFlag = Standard_False;
277 } while ( !( _FD -> dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) );
281 myFirstCall = Standard_False;
283 } // end OSD_DirectoryIterator :: Next
285 OSD_Directory OSD_DirectoryIterator :: Values () {
288 TCollection_AsciiString aFileName
289 (TCollection_ExtendedString( (Standard_ExtString) _FD -> cFileName) );
290 TheIterator.SetPath ( OSD_Path ( aFileName ) );
294 } // end OSD_DirectoryIterator :: Values
296 Standard_Boolean OSD_DirectoryIterator :: Failed () const {
298 return myError.Failed ();
300 } // end OSD_DirectoryIterator :: Failed
302 void OSD_DirectoryIterator :: Reset () {
306 } // end OSD_DirectoryIterator :: Reset
308 void OSD_DirectoryIterator :: Perror () {
312 } // end OSD_DirectoryIterator :: Perror
314 Standard_Integer OSD_DirectoryIterator :: Error () const {
316 return myError.Error ();
318 } // end OSD_DirectoryIterator :: Error
320 // For compatibility with UNIX version
321 OSD_DirectoryIterator::OSD_DirectoryIterator() {}
323 void OSD_DirectoryIterator::Initialize(
325 const TCollection_AsciiString&){}