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;
105 myFlag = 0; // Initialize to nothing found
108 myEntry = readdir((DIR *)myDescr);
110 if (!myEntry){ // No file found
111 myEntry = NULL; // Keep pointer clean
112 myFlag = Standard_False; // No more files/directory
113 closedir((DIR *)myDescr) ; // so close directory
118 // if (!strcmp(entry->d_name,".")) continue; LD : on prend ces
119 // if (!strcmp(entry->d_name,"..")) continue; 2 directories.
121 // Is it a directory ?
123 sprintf(full_name,"%s/%s",myPlace.ToCString(),
124 ((struct dirent *)myEntry)->d_name); // LD debug
125 stat(full_name, &stat_buf);
126 if (S_ISDIR(stat_buf.st_mode)) // Ensure me it's not a file
127 if (strcmp_joker(myMask.ToCString(), ((struct dirent *)myEntry)->d_name)){
128 // Does it follow mask ?
129 myFlag = Standard_True;
139 // Get Name of selected directory
141 OSD_Directory OSD_DirectoryIterator::Values(){
143 TCollection_AsciiString Name;
144 TCollection_AsciiString Ext;
145 Standard_Integer position;
147 if (myEntry) Name = ((struct dirent *)myEntry)->d_name ;
149 position = Name.Search(".");
152 Ext = Name.Split(position - 1) ; // Debug LD
153 // Ext.Remove(1,position);
154 // Name.Remove( position,Ext.Length()+1);
157 thisvalue.SetValues("", "", "", "", "", Name,Ext);
158 TheIterator.SetPath (thisvalue);
160 return (TheIterator);
164 void OSD_DirectoryIterator::Reset(){
168 Standard_Boolean OSD_DirectoryIterator::Failed()const{
169 return( myError.Failed());
172 void OSD_DirectoryIterator::Perror() {
177 Standard_Integer OSD_DirectoryIterator::Error()const{
178 return( myError.Error());
183 //------------------------------------------------------------------------
184 //------------------- Windows NT sources for OSD_DirectoryIterator ------
185 //------------------------------------------------------------------------
192 #include <OSD_Directory.hxx>
193 #include <OSD_DirectoryIterator.hxx>
194 #include <OSD_OSDError.hxx>
195 #include <OSD_Path.hxx>
196 #include <TCollection_AsciiString.hxx>
197 #include <TCollection_ExtendedString.hxx>
199 #define _FD ( ( PWIN32_FIND_DATAW )myData )
201 void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
203 OSD_DirectoryIterator :: OSD_DirectoryIterator (
204 const OSD_Path& where,
205 const TCollection_AsciiString& Mask
208 myFlag = Standard_False;
209 myHandle = INVALID_HANDLE_VALUE;
211 where.SystemName ( myPlace );
213 if ( myPlace.Length () == 0 ) myPlace = ".";
220 void OSD_DirectoryIterator :: Destroy () {
222 if ( myData != NULL ) HeapFree ( GetProcessHeap (), 0, myData );
224 if ( myHandle != INVALID_HANDLE_VALUE )
226 FindClose ( ( HANDLE )myHandle );
228 } // end OSD_DirectoryIterator :: Destroy
230 Standard_Boolean OSD_DirectoryIterator :: More () {
232 if ( myHandle == INVALID_HANDLE_VALUE ) {
234 TCollection_AsciiString wc = myPlace + "/" + myMask;
237 GetProcessHeap (), HEAP_GENERATE_EXCEPTIONS, sizeof ( WIN32_FIND_DATAW )
240 // make wchar_t string from UTF-8
241 TCollection_ExtendedString wcW(wc);
242 myHandle = FindFirstFileW ((const wchar_t*)wcW.ToExtString(), (PWIN32_FIND_DATAW)myData);
244 if ( myHandle == INVALID_HANDLE_VALUE )
246 _osd_wnt_set_error ( myError, OSD_WDirectoryIterator );
250 myFlag = Standard_True;
251 myFirstCall = Standard_True;
257 } else if ( !myFlag ) {
259 FindClose ( ( HANDLE )myHandle );
260 myHandle = INVALID_HANDLE_VALUE;
266 } // end OSD_DirectoryIterator :: More
268 void OSD_DirectoryIterator :: Next () {
270 if ( myFirstCall && !( _FD -> dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) ||
276 if ( !FindNextFileW ( ( HANDLE )myHandle, _FD ) ) {
278 myFlag = Standard_False;
284 } while ( !( _FD -> dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) );
288 myFirstCall = Standard_False;
290 } // end OSD_DirectoryIterator :: Next
292 OSD_Directory OSD_DirectoryIterator :: Values () {
295 TCollection_AsciiString aFileName
296 (TCollection_ExtendedString( (Standard_ExtString) _FD -> cFileName) );
297 TheIterator.SetPath ( OSD_Path ( aFileName ) );
301 } // end OSD_DirectoryIterator :: Values
303 Standard_Boolean OSD_DirectoryIterator :: Failed () const {
305 return myError.Failed ();
307 } // end OSD_DirectoryIterator :: Failed
309 void OSD_DirectoryIterator :: Reset () {
313 } // end OSD_DirectoryIterator :: Reset
315 void OSD_DirectoryIterator :: Perror () {
319 } // end OSD_DirectoryIterator :: Perror
321 Standard_Integer OSD_DirectoryIterator :: Error () const {
323 return myError.Error ();
325 } // end OSD_DirectoryIterator :: Error
327 // For compatibility with UNIX version
328 OSD_DirectoryIterator::OSD_DirectoryIterator() {}
330 void OSD_DirectoryIterator::Initialize(
332 const TCollection_AsciiString&){}