0020716: Eliminate usage of "config.h" header file
[occt.git] / src / OSD / OSD_DirectoryIterator.cxx
CommitLineData
b311480e 1// Copyright (c) 1998-1999 Matra Datavision
973c2be1 2// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 3//
973c2be1 4// This file is part of Open CASCADE Technology software library.
b311480e 5//
d5f74e42 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
973c2be1 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.
b311480e 11//
973c2be1 12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
b311480e 14
7fd59977 15#ifndef WNT
16
17#include <OSD_DirectoryIterator.ixx>
18#include <OSD_WhoAmI.hxx>
19
20#include <stdio.h>
03155c18 21#include <dirent.h>
22#include <sys/stat.h>
7fd59977 23
24//const OSD_WhoAmI Iam = OSD_WDirectoryIterator;
25
26OSD_DirectoryIterator::OSD_DirectoryIterator() {
27
28 myDescr = NULL ;
29}
30
31OSD_DirectoryIterator::OSD_DirectoryIterator(const OSD_Path& where,
32 const TCollection_AsciiString& Mask){
33 myDescr = NULL ;
34 Initialize(where, Mask) ;
35}
36
37// For Windows NT compatibility -----------
38void OSD_DirectoryIterator :: Destroy () {}
39//-----------------------------------------
40
41void OSD_DirectoryIterator::Initialize(const OSD_Path& where,
42 const TCollection_AsciiString& Mask){
43
44 myFlag = Standard_False;
45 where.SystemName(myPlace);
46 if (myPlace.Length()==0) myPlace = ".";
47 myMask = Mask;
48 if (myDescr) {
49 closedir((DIR *)myDescr) ;
50 myDescr = NULL ;
51 }
52 myInit = 1 ;
53}
54
55// Is there another directory entry ?
56
57Standard_Boolean OSD_DirectoryIterator::More(){
58 if (myInit) {
59 myInit = 0 ;
60 myDescr = (Standard_Address) opendir(myPlace.ToCString());
61 if (myDescr) { // LD : Si repertoire inaccessible retourner False
62 myFlag = Standard_True;
63 myInit = 0 ;
64 Next(); // Now find first entry
65 }
66 }
67 return (myFlag);
68}
69
70// Private : See if directory name matches with a mask (like "*.c")
71// LD : reecrit (original dans OSD_FileIterator.cxx)
72
73
74static int strcmp_joker(const char *Mask,const char *Name)
75{
76 const char *p, *s ;
77
78 for(p = Mask,s = Name ; *p && *p != '*' ; p++,s++)
79 if (*p != *s) return 0 ;
80 if (!*p) return !(*s) ;
81 while (*p == '*') p++ ;
82 if (!*p) return 1 ;
83 for (;*s; s++)
84 if (strcmp_joker(p,s)) return 1 ;
85 return 0 ;
86}
87
88// Find next directory entry in current directory
89
90void OSD_DirectoryIterator::Next(){
91int again = 1;
92struct stat stat_buf;
93char full_name[255];
94
95 myFlag = 0; // Initialize to nothing found
96
97 do{
98 myEntry = readdir((DIR *)myDescr);
99
100 if (!myEntry){ // No file found
101 myEntry = NULL; // Keep pointer clean
102 myFlag = Standard_False; // No more files/directory
103 closedir((DIR *)myDescr) ; // so close directory
104 myDescr = NULL;
105 again = 0;
106 }
107 else {
108// if (!strcmp(entry->d_name,".")) continue; LD : on prend ces
109// if (!strcmp(entry->d_name,"..")) continue; 2 directories.
110
111 // Is it a directory ?
112
113 sprintf(full_name,"%s/%s",myPlace.ToCString(),
114 ((struct dirent *)myEntry)->d_name); // LD debug
115 stat(full_name, &stat_buf);
116 if (S_ISDIR(stat_buf.st_mode)) // Ensure me it's not a file
117 if (strcmp_joker(myMask.ToCString(), ((struct dirent *)myEntry)->d_name)){
118 // Does it follow mask ?
119 myFlag = Standard_True;
120 again = 0;
121 }
122 }
123
124 } while (again);
125
126}
127
128
129// Get Name of selected directory
130
131OSD_Directory OSD_DirectoryIterator::Values(){
132OSD_Path thisvalue;
133TCollection_AsciiString Name;
134TCollection_AsciiString Ext;
135Standard_Integer position;
136
137 if (myEntry) Name = ((struct dirent *)myEntry)->d_name ;
138
139 position = Name.Search(".");
140
141 if (position != -1){
142 Ext = Name.Split(position - 1) ; // Debug LD
143// Ext.Remove(1,position);
144// Name.Remove( position,Ext.Length()+1);
145 }
146
147 thisvalue.SetValues("", "", "", "", "", Name,Ext);
148 TheIterator.SetPath (thisvalue);
149
150 return (TheIterator);
151}
152
153
154void OSD_DirectoryIterator::Reset(){
155 myError.Reset();
156}
157
158Standard_Boolean OSD_DirectoryIterator::Failed()const{
159 return( myError.Failed());
160}
161
162void OSD_DirectoryIterator::Perror() {
163 myError.Perror();
164}
165
166
167Standard_Integer OSD_DirectoryIterator::Error()const{
168 return( myError.Error());
169}
170
171#else
172
173//------------------------------------------------------------------------
174//------------------- Windows NT sources for OSD_DirectoryIterator ------
175//------------------------------------------------------------------------
176
177
178#define STRICT
179#include <windows.h>
180
181#include <OSD_DirectoryIterator.ixx>
182
183#define _FD ( ( PWIN32_FIND_DATA )myData )
184
185void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... );
186
187OSD_DirectoryIterator :: OSD_DirectoryIterator (
188 const OSD_Path& where,
189 const TCollection_AsciiString& Mask
190 ) {
191
192 myFlag = Standard_False;
7dc9e047 193 myHandle = INVALID_HANDLE_VALUE;
7fd59977 194
195 where.SystemName ( myPlace );
196
197 if ( myPlace.Length () == 0 ) myPlace = TEXT( "." );
198
199 myMask = Mask;
200 myData = NULL;
201
202} // end constructor
203
204void OSD_DirectoryIterator :: Destroy () {
205
206 if ( myData != NULL ) HeapFree ( GetProcessHeap (), 0, myData );
207
7dc9e047 208 if ( myHandle != INVALID_HANDLE_VALUE )
7fd59977 209
210 FindClose ( ( HANDLE )myHandle );
211
212} // end OSD_DirectoryIterator :: Destroy
213
214Standard_Boolean OSD_DirectoryIterator :: More () {
215
7dc9e047 216 if ( myHandle == INVALID_HANDLE_VALUE ) {
7fd59977 217
218 TCollection_AsciiString wc = myPlace + TEXT( "/" ) + myMask;
219
220 myData = HeapAlloc (
221 GetProcessHeap (), HEAP_GENERATE_EXCEPTIONS, sizeof ( WIN32_FIND_DATA )
222 );
223
7dc9e047 224 myHandle = FindFirstFile (wc.ToCString (), (PWIN32_FIND_DATA)myData);
7fd59977 225
7dc9e047 226 if ( myHandle == INVALID_HANDLE_VALUE )
227
228 _osd_wnt_set_error ( myError, OSD_WDirectoryIterator );
7fd59977 229
230 else {
231
232 myFlag = Standard_True;
233 myFirstCall = Standard_True;
234
235 Next ();
236
237 } // end else
238
239 } else if ( !myFlag ) {
240
241 FindClose ( ( HANDLE )myHandle );
7dc9e047 242 myHandle = INVALID_HANDLE_VALUE;
7fd59977 243
244 } // end if
245
246 return myFlag;
247
248} // end OSD_DirectoryIterator :: More
249
250void OSD_DirectoryIterator :: Next () {
251
252 if ( myFirstCall && !( _FD -> dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) ||
253 !myFirstCall
254 ) {
255
256 do {
257
258 if ( !FindNextFile ( ( HANDLE )myHandle, _FD ) ) {
259
260 myFlag = Standard_False;
261
262 break;
263
264 } // end if
265
266 } while ( !( _FD -> dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) );
267
268 } // end if
269
270 myFirstCall = Standard_False;
271
272} // end OSD_DirectoryIterator :: Next
273
274OSD_Directory OSD_DirectoryIterator :: Values () {
275
276 TheIterator.SetPath ( OSD_Path ( _FD -> cFileName ) );
277
278 return TheIterator;
279
280} // end OSD_DirectoryIterator :: Values
281
282Standard_Boolean OSD_DirectoryIterator :: Failed () const {
283
284 return myError.Failed ();
285
286} // end OSD_DirectoryIterator :: Failed
287
288void OSD_DirectoryIterator :: Reset () {
289
290 myError.Reset ();
291
292} // end OSD_DirectoryIterator :: Reset
293
294void OSD_DirectoryIterator :: Perror () {
295
296 myError.Perror ();
297
298} // end OSD_DirectoryIterator :: Perror
299
300Standard_Integer OSD_DirectoryIterator :: Error () const {
301
302 return myError.Error ();
303
304} // end OSD_DirectoryIterator :: Error
305
306// For compatibility with UNIX version
307OSD_DirectoryIterator::OSD_DirectoryIterator() {}
308
309void OSD_DirectoryIterator::Initialize(
35e08fe8 310 const OSD_Path&,
311 const TCollection_AsciiString&){}
7fd59977 312
313#endif