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.
25 #include <OSD_DirectoryIterator.ixx>
26 #include <OSD_WhoAmI.hxx>
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
42 //const OSD_WhoAmI Iam = OSD_WDirectoryIterator;
44 OSD_DirectoryIterator::OSD_DirectoryIterator() {
49 OSD_DirectoryIterator::OSD_DirectoryIterator(const OSD_Path& where,
50 const TCollection_AsciiString& Mask){
52 Initialize(where, Mask) ;
55 // For Windows NT compatibility -----------
56 void OSD_DirectoryIterator :: Destroy () {}
57 //-----------------------------------------
59 void OSD_DirectoryIterator::Initialize(const OSD_Path& where,
60 const TCollection_AsciiString& Mask){
62 myFlag = Standard_False;
63 where.SystemName(myPlace);
64 if (myPlace.Length()==0) myPlace = ".";
67 closedir((DIR *)myDescr) ;
73 // Is there another directory entry ?
75 Standard_Boolean OSD_DirectoryIterator::More(){
78 myDescr = (Standard_Address) opendir(myPlace.ToCString());
79 if (myDescr) { // LD : Si repertoire inaccessible retourner False
80 myFlag = Standard_True;
82 Next(); // Now find first entry
88 // Private : See if directory name matches with a mask (like "*.c")
89 // LD : reecrit (original dans OSD_FileIterator.cxx)
92 static int strcmp_joker(const char *Mask,const char *Name)
96 for(p = Mask,s = Name ; *p && *p != '*' ; p++,s++)
97 if (*p != *s) return 0 ;
98 if (!*p) return !(*s) ;
99 while (*p == '*') p++ ;
102 if (strcmp_joker(p,s)) return 1 ;
106 // Find next directory entry in current directory
108 void OSD_DirectoryIterator::Next(){
110 struct stat stat_buf;
113 myFlag = 0; // Initialize to nothing found
116 myEntry = readdir((DIR *)myDescr);
118 if (!myEntry){ // No file found
119 myEntry = NULL; // Keep pointer clean
120 myFlag = Standard_False; // No more files/directory
121 closedir((DIR *)myDescr) ; // so close directory
126 // if (!strcmp(entry->d_name,".")) continue; LD : on prend ces
127 // if (!strcmp(entry->d_name,"..")) continue; 2 directories.
129 // Is it a directory ?
131 sprintf(full_name,"%s/%s",myPlace.ToCString(),
132 ((struct dirent *)myEntry)->d_name); // LD debug
133 stat(full_name, &stat_buf);
134 if (S_ISDIR(stat_buf.st_mode)) // Ensure me it's not a file
135 if (strcmp_joker(myMask.ToCString(), ((struct dirent *)myEntry)->d_name)){
136 // Does it follow mask ?
137 myFlag = Standard_True;
147 // Get Name of selected directory
149 OSD_Directory OSD_DirectoryIterator::Values(){
151 TCollection_AsciiString Name;
152 TCollection_AsciiString Ext;
153 Standard_Integer position;
155 if (myEntry) Name = ((struct dirent *)myEntry)->d_name ;
157 position = Name.Search(".");
160 Ext = Name.Split(position - 1) ; // Debug LD
161 // Ext.Remove(1,position);
162 // Name.Remove( position,Ext.Length()+1);
165 thisvalue.SetValues("", "", "", "", "", Name,Ext);
166 TheIterator.SetPath (thisvalue);
168 return (TheIterator);
172 void OSD_DirectoryIterator::Reset(){
176 Standard_Boolean OSD_DirectoryIterator::Failed()const{
177 return( myError.Failed());
180 void OSD_DirectoryIterator::Perror() {
185 Standard_Integer OSD_DirectoryIterator::Error()const{
186 return( myError.Error());
191 //------------------------------------------------------------------------
192 //------------------- Windows NT sources for OSD_DirectoryIterator ------
193 //------------------------------------------------------------------------
199 #include <OSD_DirectoryIterator.ixx>
201 #define _FD ( ( PWIN32_FIND_DATA )myData )
203 void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
205 OSD_DirectoryIterator :: OSD_DirectoryIterator (
206 const OSD_Path& where,
207 const TCollection_AsciiString& Mask
210 myFlag = Standard_False;
211 myHandle = ( Standard_Integer )INVALID_HANDLE_VALUE;
213 where.SystemName ( myPlace );
215 if ( myPlace.Length () == 0 ) myPlace = TEXT( "." );
222 void OSD_DirectoryIterator :: Destroy () {
224 if ( myData != NULL ) HeapFree ( GetProcessHeap (), 0, myData );
226 if ( myHandle != ( Standard_Integer )INVALID_HANDLE_VALUE )
228 FindClose ( ( HANDLE )myHandle );
230 } // end OSD_DirectoryIterator :: Destroy
232 Standard_Boolean OSD_DirectoryIterator :: More () {
234 if ( myHandle == ( Standard_Integer )INVALID_HANDLE_VALUE ) {
236 TCollection_AsciiString wc = myPlace + TEXT( "/" ) + myMask;
239 GetProcessHeap (), HEAP_GENERATE_EXCEPTIONS, sizeof ( WIN32_FIND_DATA )
242 myHandle = ( Standard_Integer )FindFirstFile (
243 wc.ToCString (), ( PWIN32_FIND_DATA )myData
246 if ( myHandle == ( Standard_Integer )INVALID_HANDLE_VALUE )
248 _osd_wnt_set_error ( myError, OSD_WDirectoryIterator );
252 myFlag = Standard_True;
253 myFirstCall = Standard_True;
259 } else if ( !myFlag ) {
261 FindClose ( ( HANDLE )myHandle );
262 myHandle = ( Standard_Integer )INVALID_HANDLE_VALUE;
268 } // end OSD_DirectoryIterator :: More
270 void OSD_DirectoryIterator :: Next () {
272 if ( myFirstCall && !( _FD -> dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) ||
278 if ( !FindNextFile ( ( HANDLE )myHandle, _FD ) ) {
280 myFlag = Standard_False;
286 } while ( !( _FD -> dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) );
290 myFirstCall = Standard_False;
292 } // end OSD_DirectoryIterator :: Next
294 OSD_Directory OSD_DirectoryIterator :: Values () {
296 TheIterator.SetPath ( OSD_Path ( _FD -> cFileName ) );
300 } // end OSD_DirectoryIterator :: Values
302 Standard_Boolean OSD_DirectoryIterator :: Failed () const {
304 return myError.Failed ();
306 } // end OSD_DirectoryIterator :: Failed
308 void OSD_DirectoryIterator :: Reset () {
312 } // end OSD_DirectoryIterator :: Reset
314 void OSD_DirectoryIterator :: Perror () {
318 } // end OSD_DirectoryIterator :: Perror
320 Standard_Integer OSD_DirectoryIterator :: Error () const {
322 return myError.Error ();
324 } // end OSD_DirectoryIterator :: Error
326 // For compatibility with UNIX version
327 OSD_DirectoryIterator::OSD_DirectoryIterator() {}
329 void OSD_DirectoryIterator::Initialize(
330 const OSD_Path& where,
331 const TCollection_AsciiString& Mask){}