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>
29 OSD_DirectoryIterator::OSD_DirectoryIterator()
37 OSD_DirectoryIterator::OSD_DirectoryIterator(const OSD_Path& where,
38 const TCollection_AsciiString& Mask)
44 Initialize(where, Mask) ;
47 // For Windows NT compatibility -----------
48 void OSD_DirectoryIterator :: Destroy () {}
49 //-----------------------------------------
51 void OSD_DirectoryIterator::Initialize(const OSD_Path& where,
52 const TCollection_AsciiString& Mask){
54 myFlag = Standard_False;
55 where.SystemName(myPlace);
56 if (myPlace.Length()==0) myPlace = ".";
59 closedir((DIR *)myDescr) ;
65 // Is there another directory entry ?
67 Standard_Boolean OSD_DirectoryIterator::More(){
70 myDescr = (Standard_Address) opendir(myPlace.ToCString());
71 if (myDescr) { // LD : Si repertoire inaccessible retourner False
72 myFlag = Standard_True;
74 Next(); // Now find first entry
80 // Private : See if directory name matches with a mask (like "*.c")
81 // LD : reecrit (original dans OSD_FileIterator.cxx)
84 static int strcmp_joker(const char *Mask,const char *Name)
88 for(p = Mask,s = Name ; *p && *p != '*' ; p++,s++)
89 if (*p != *s) return 0 ;
90 if (!*p) return !(*s) ;
91 while (*p == '*') p++ ;
94 if (strcmp_joker(p,s)) return 1 ;
98 // Find next directory entry in current directory
100 void OSD_DirectoryIterator::Next(){
102 struct stat stat_buf;
103 myFlag = false; // Initialize to nothing found
106 myEntry = readdir((DIR *)myDescr);
108 if (!myEntry){ // No file found
109 myEntry = NULL; // Keep pointer clean
110 myFlag = Standard_False; // No more files/directory
111 closedir((DIR *)myDescr) ; // so close directory
116 // if (!strcmp(entry->d_name,".")) continue; LD : on prend ces
117 // if (!strcmp(entry->d_name,"..")) continue; 2 directories.
119 // Is it a directory ?
120 const TCollection_AsciiString aFullName = myPlace + "/" + ((struct dirent* )myEntry)->d_name;
121 stat(aFullName.ToCString(), &stat_buf);
122 if (S_ISDIR(stat_buf.st_mode)) // Ensure me it's not a file
123 if (strcmp_joker(myMask.ToCString(), ((struct dirent *)myEntry)->d_name)){
124 // Does it follow mask ?
125 myFlag = Standard_True;
135 // Get Name of selected directory
137 OSD_Directory OSD_DirectoryIterator::Values(){
139 TCollection_AsciiString Name;
140 TCollection_AsciiString Ext;
141 Standard_Integer position;
143 if (myEntry) Name = ((struct dirent *)myEntry)->d_name ;
145 position = Name.Search(".");
148 Ext = Name.Split(position - 1) ; // Debug LD
149 // Ext.Remove(1,position);
150 // Name.Remove( position,Ext.Length()+1);
153 thisvalue.SetValues("", "", "", "", "", Name,Ext);
154 TheIterator.SetPath (thisvalue);
156 return (TheIterator);
160 void OSD_DirectoryIterator::Reset(){
164 Standard_Boolean OSD_DirectoryIterator::Failed()const{
165 return( myError.Failed());
168 void OSD_DirectoryIterator::Perror() {
173 Standard_Integer OSD_DirectoryIterator::Error()const{
174 return( myError.Error());
179 //------------------------------------------------------------------------
180 //------------------- Windows NT sources for OSD_DirectoryIterator ------
181 //------------------------------------------------------------------------
187 #include <OSD_Directory.hxx>
188 #include <OSD_DirectoryIterator.hxx>
189 #include <OSD_OSDError.hxx>
190 #include <OSD_Path.hxx>
191 #include <TCollection_AsciiString.hxx>
192 #include <TCollection_ExtendedString.hxx>
194 #define _FD ( ( PWIN32_FIND_DATAW )myData )
196 void _osd_wnt_set_error ( OSD_Error&, Standard_Integer, ... );
198 OSD_DirectoryIterator :: OSD_DirectoryIterator (
199 const OSD_Path& where,
200 const TCollection_AsciiString& Mask
203 myFlag = Standard_False;
204 myHandle = INVALID_HANDLE_VALUE;
206 where.SystemName ( myPlace );
208 if ( myPlace.Length () == 0 ) myPlace = ".";
215 void OSD_DirectoryIterator :: Destroy () {
217 if ( myData != NULL ) HeapFree ( GetProcessHeap (), 0, myData );
219 if ( myHandle != INVALID_HANDLE_VALUE )
221 FindClose ( ( HANDLE )myHandle );
223 } // end OSD_DirectoryIterator :: Destroy
225 Standard_Boolean OSD_DirectoryIterator :: More () {
227 if ( myHandle == INVALID_HANDLE_VALUE ) {
229 TCollection_AsciiString wc = myPlace + "/" + myMask;
232 GetProcessHeap (), HEAP_GENERATE_EXCEPTIONS, sizeof ( WIN32_FIND_DATAW )
235 // make wchar_t string from UTF-8
236 TCollection_ExtendedString wcW(wc);
237 myHandle = FindFirstFileExW (wcW.ToWideString(), FindExInfoStandard, (PWIN32_FIND_DATAW)myData, FindExSearchNameMatch, NULL, 0);
239 if ( myHandle == INVALID_HANDLE_VALUE )
241 _osd_wnt_set_error ( myError, OSD_WDirectoryIterator );
245 myFlag = Standard_True;
246 myFirstCall = Standard_True;
252 } else if ( !myFlag ) {
254 FindClose ( ( HANDLE )myHandle );
255 myHandle = INVALID_HANDLE_VALUE;
261 } // end OSD_DirectoryIterator :: More
263 void OSD_DirectoryIterator :: Next () {
265 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()
325 myFirstCall(Standard_False)
328 void OSD_DirectoryIterator::Initialize(
330 const TCollection_AsciiString&){}