7fd59977 |
1 | |
2 | #ifdef HAVE_CONFIG_H |
3 | # include <config.h> |
4 | #endif |
5 | |
6 | #ifndef WNT |
7 | |
8 | #include <OSD_FileIterator.ixx> |
9 | #include <OSD_WhoAmI.hxx> |
10 | |
11 | #include <stdio.h> |
12 | #include <sys/types.h> |
13 | #include <sys/stat.h> |
14 | |
15 | #ifdef HAVE_DIRENT_H |
16 | # include <dirent.h> |
17 | # define NAMLEN(dirent) strlen((dirent)->d_name) |
18 | # ifdef VMS |
19 | extern char *vmsify PARAMS ((char *name, int type)); |
20 | # endif |
21 | #else |
22 | # define dirent direct |
23 | # define NAMLEN(dirent) (dirent)->d_namlen |
24 | # ifdef HAVE_SYS_NDIR_H |
25 | # include <sys/ndir.h> |
26 | # endif |
27 | # ifdef HAVE_SYS_DIR_H |
28 | # include <sys/dir.h> |
29 | # endif |
30 | # ifdef HAVE_NDIR_H |
31 | # include <ndir.h> |
32 | # endif |
33 | # ifdef HAVE_VMSDIR_H |
34 | # include "vmsdir.h" |
35 | # endif /* HAVE_VMSDIR_H */ |
36 | #endif |
37 | |
38 | /* In GNU systems, <dirent.h> defines this macro for us. */ |
39 | #ifdef _D_NAMLEN |
40 | # undef NAMLEN |
41 | # define NAMLEN(d) _D_NAMLEN(d) |
42 | #endif |
43 | |
44 | #if (defined (POSIX) || defined (VMS) || defined (WINDOWS32)) && !defined (__GNU_LIBRARY__) |
45 | /* Posix does not require that the d_ino field be present, and some |
46 | systems do not provide it. */ |
47 | # define REAL_DIR_ENTRY(dp) 1 |
48 | # define FAKE_DIR_ENTRY(dp) |
49 | #else |
50 | # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0) |
51 | # define FAKE_DIR_ENTRY(dp) (dp->d_ino = 1) |
52 | #endif /* POSIX */ |
53 | |
54 | //const OSD_WhoAmI Iam = OSD_WFileIterator; |
55 | |
56 | |
57 | OSD_FileIterator::OSD_FileIterator() { |
58 | myDescr = NULL ; |
59 | } |
60 | |
61 | OSD_FileIterator::OSD_FileIterator(const OSD_Path& where, |
62 | const TCollection_AsciiString& Mask){ |
63 | myDescr = NULL ; |
64 | Initialize(where, Mask) ; |
65 | } |
66 | |
67 | // For Windows NT compatibility |
68 | void OSD_FileIterator :: Destroy () {} |
69 | |
70 | void OSD_FileIterator::Initialize(const OSD_Path& where, |
71 | const TCollection_AsciiString& Mask){ |
72 | myFlag = Standard_False; |
73 | where.SystemName(myPlace); |
74 | if (myPlace.Length()==0) myPlace = "."; |
75 | myMask = Mask; |
76 | if (myDescr) { |
77 | closedir((DIR *)myDescr) ; |
78 | myDescr = NULL ; |
79 | } |
80 | myInit = 1 ; |
81 | } |
82 | |
83 | // Is there another file entry ? |
84 | |
85 | Standard_Boolean OSD_FileIterator::More(){ |
86 | if (myInit) { |
87 | myInit = 0 ; |
88 | myDescr = (Standard_Address) opendir(myPlace.ToCString()); |
89 | if (myDescr) { // LD : Si repertoire inaccessible retourner False |
90 | myFlag = Standard_True; |
91 | myInit = 0 ; |
92 | Next(); // Now find first entry |
93 | } |
94 | } |
95 | return (myFlag); |
96 | } |
97 | |
98 | // Private : See if file name matches with a mask (like "*.c") |
99 | |
100 | static int strcmp_joker(const char *Mask,const char *Name) |
101 | { |
102 | const char *p, *s ; |
103 | |
104 | for(p = Mask,s = Name ; *p && *p != '*' ; p++,s++) |
105 | if (*p != *s) return 0 ; |
106 | |
107 | if (!*p) return !(*s) ; |
108 | while (*p == '*') p++ ; |
109 | if (!*p) return 1 ; |
110 | for (;*s; s++) |
111 | if (strcmp_joker(p,s)) return 1 ; |
112 | return 0 ; |
113 | } |
114 | |
115 | #if 0 |
116 | // LD : ancienne version. |
117 | |
118 | #define TRUE 1 |
119 | #define FALSE 0 |
120 | #define NO_MASK '*' |
121 | |
122 | static int strcmp_joker(char *fileMask,char *fileName) |
123 | { |
124 | char *sauve_fileMask,*sauve_fileName; |
125 | int compare_result; |
126 | int beginning = 1; // 0 if first character is a joker, otherwise 1 |
127 | |
128 | while (*fileName) { // Test end of AsciiString |
129 | |
130 | if (*fileMask == NO_MASK) { |
131 | beginning = 0; |
132 | |
133 | while(*fileMask == NO_MASK) fileMask++; |
134 | |
135 | if (*fileMask) { |
136 | while(*fileName && |
137 | *fileName != *fileMask) |
138 | fileName++; |
139 | |
140 | sauve_fileMask = fileMask; // Save strings |
141 | sauve_fileName = fileName; |
142 | } |
143 | else return(0); // fileMask ends with a joker |
144 | |
145 | } |
146 | else { // Compare two characters |
147 | compare_result = *fileMask++ - *fileName++; |
148 | |
149 | if (compare_result != 0) |
150 | if (beginning) |
151 | return (compare_result); /* 1ere chaine pas de joker au debut */ |
152 | else { // Look ahead for same string |
153 | fileMask = sauve_fileMask; |
154 | fileName = ++sauve_fileName; |
155 | while(*fileName && |
156 | *fileName != *fileMask) |
157 | fileName++; |
158 | sauve_fileName = fileName; |
159 | } |
160 | |
161 | } |
162 | |
163 | } |
164 | |
165 | while(*fileMask == NO_MASK) fileMask++; |
166 | return(*fileMask - *fileName); |
167 | } |
168 | #endif |
169 | |
170 | // Find next file entry in current directory |
171 | |
172 | void OSD_FileIterator::Next(){ |
173 | int again = 1; |
174 | struct stat stat_buf; |
175 | char full_name[255]; |
176 | |
177 | myFlag = 0; // Initialize to nothing found |
178 | |
179 | do { |
180 | myEntry = readdir((DIR *)myDescr); |
181 | |
182 | if (!myEntry){ // No file found |
183 | myEntry = NULL; // Keep pointer clean |
184 | myFlag = Standard_False; // No more files/directory |
185 | closedir((DIR *)myDescr) ; // so close directory |
186 | myDescr = NULL; |
187 | again = 0; |
188 | } |
189 | else { |
190 | if (!strcmp(((struct dirent *)myEntry)->d_name,".")) continue; |
191 | if (!strcmp(((struct dirent *)myEntry)->d_name,"..")) continue; |
192 | |
193 | // Is it a file ? |
194 | |
195 | sprintf(full_name,"%s/%s",myPlace.ToCString(), |
196 | ((struct dirent *)myEntry)->d_name); // LD debug |
197 | #ifdef DEBUG |
198 | cout << "Place : " << myPlace << endl; |
199 | cout << "FName : " << full_name << endl; |
200 | #endif |
201 | stat(full_name, &stat_buf); |
202 | if (S_ISREG(stat_buf.st_mode)) // LD : Ensure me it's a regular file |
203 | if (strcmp_joker(myMask.ToCString(), ((struct dirent *)myEntry)->d_name)){ |
204 | // Does it follow mask ? |
205 | myFlag = Standard_True; |
206 | again = 0; |
207 | } |
208 | } |
209 | |
210 | } while (again); |
211 | |
212 | } |
213 | |
214 | // Get Name of selected file |
215 | |
216 | OSD_File OSD_FileIterator::Values(){ |
217 | OSD_Path thisvalue; |
218 | TCollection_AsciiString Name; |
219 | TCollection_AsciiString Ext; |
220 | Standard_Integer position; |
221 | |
222 | if (myEntry) Name = ((struct dirent *)myEntry)->d_name ; |
223 | |
224 | position = Name.Search("."); |
225 | |
226 | if (position != -1){ |
227 | Ext = Name; |
228 | Ext.Remove(1,position-1); |
229 | Name.Remove( position,Ext.Length()); |
230 | } |
231 | |
232 | thisvalue.SetValues("", "", "", "", "", Name,Ext); |
233 | TheIterator.SetPath (thisvalue); |
234 | |
235 | return (TheIterator); |
236 | } |
237 | |
238 | |
239 | void OSD_FileIterator::Reset(){ |
240 | myError.Reset(); |
241 | } |
242 | |
243 | Standard_Boolean OSD_FileIterator::Failed()const{ |
244 | return( myError.Failed()); |
245 | } |
246 | |
247 | void OSD_FileIterator::Perror() { |
248 | myError.Perror(); |
249 | } |
250 | |
251 | |
252 | Standard_Integer OSD_FileIterator::Error()const{ |
253 | return( myError.Error()); |
254 | } |
255 | |
256 | #else |
257 | |
258 | //------------------------------------------------------------------------ |
259 | //------------------- Windows NT sources for OSD_FileIterator ----------- |
260 | //------------------------------------------------------------------------ |
261 | |
262 | #define STRICT |
263 | #include <windows.h> |
264 | |
265 | #include <OSD_FileIterator.ixx> |
266 | |
267 | #define _FD ( ( PWIN32_FIND_DATA )myData ) |
268 | |
269 | void _osd_wnt_set_error ( OSD_Error&, OSD_WhoAmI, ... ); |
270 | |
271 | OSD_FileIterator :: OSD_FileIterator ( |
272 | const OSD_Path& where, |
273 | const TCollection_AsciiString& Mask |
274 | ) { |
275 | |
276 | myFlag = Standard_False; |
277 | myHandle = ( Standard_Integer )INVALID_HANDLE_VALUE; |
278 | |
279 | where.SystemName ( myPlace ); |
280 | |
281 | if ( myPlace.Length () == 0 ) myPlace = TEXT( "." ); |
282 | |
283 | myMask = Mask; |
284 | myData = NULL; |
285 | |
286 | } // end constructor |
287 | |
288 | void OSD_FileIterator :: Destroy () { |
289 | |
290 | if ( myData != NULL ) HeapFree ( GetProcessHeap (), 0, myData ); |
291 | |
292 | if ( myHandle != ( Standard_Integer )INVALID_HANDLE_VALUE ) |
293 | |
294 | FindClose ( ( HANDLE )myHandle ); |
295 | |
296 | } // end OSD_DirectoryIterator :: Destroy |
297 | |
298 | Standard_Boolean OSD_FileIterator :: More () { |
299 | |
300 | if ( myHandle == ( Standard_Integer )INVALID_HANDLE_VALUE ) { |
301 | |
302 | TCollection_AsciiString wc = myPlace + TEXT( "/" ) + myMask; |
303 | |
304 | myData = HeapAlloc ( |
305 | GetProcessHeap (), HEAP_GENERATE_EXCEPTIONS, sizeof ( WIN32_FIND_DATA ) |
306 | ); |
307 | |
308 | myHandle = ( Standard_Integer )FindFirstFile ( |
309 | wc.ToCString (), ( PWIN32_FIND_DATA )myData |
310 | ); |
311 | |
312 | if ( myHandle == ( Standard_Integer )INVALID_HANDLE_VALUE ) |
313 | |
314 | _osd_wnt_set_error ( myError, OSD_WDirectoryIterator ); |
315 | |
316 | else { |
317 | |
318 | myFlag = Standard_True; |
319 | myFirstCall = Standard_True; |
320 | |
321 | Next (); |
322 | |
323 | } // end else |
324 | |
325 | } else if ( !myFlag ) { |
326 | |
327 | FindClose ( ( HANDLE )myHandle ); |
328 | myHandle = ( Standard_Integer )INVALID_HANDLE_VALUE; |
329 | |
330 | } // end if |
331 | |
332 | return myFlag; |
333 | |
334 | } // end OSD_FileIterator :: More |
335 | |
336 | void OSD_FileIterator :: Next () { |
337 | |
338 | if ( myFirstCall && ( _FD -> dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) || |
339 | !myFirstCall |
340 | ) { |
341 | |
342 | do { |
343 | |
344 | if ( !FindNextFile ( ( HANDLE )myHandle, _FD ) ) { |
345 | |
346 | myFlag = Standard_False; |
347 | |
348 | break; |
349 | |
350 | } // end if |
351 | |
352 | } while ( ( _FD -> dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) ); |
353 | |
354 | } // end if |
355 | |
356 | myFirstCall = Standard_False; |
357 | |
358 | } // end OSD_FileIterator :: Next |
359 | |
360 | OSD_File OSD_FileIterator :: Values () { |
361 | |
362 | TheIterator.SetPath ( OSD_Path ( _FD -> cFileName ) ); |
363 | |
364 | return TheIterator; |
365 | |
366 | } // end OSD_FileIterator :: Values |
367 | |
368 | Standard_Boolean OSD_FileIterator :: Failed () const { |
369 | |
370 | return myError.Failed (); |
371 | |
372 | } // end OSD_FileIterator :: Failed |
373 | |
374 | void OSD_FileIterator :: Reset () { |
375 | |
376 | myError.Reset (); |
377 | |
378 | } // end OSD_FileIterator :: Reset |
379 | |
380 | void OSD_FileIterator :: Perror () { |
381 | |
382 | myError.Perror (); |
383 | |
384 | } // end OSD_FileIterator :: Perror |
385 | |
386 | Standard_Integer OSD_FileIterator :: Error () const { |
387 | |
388 | return myError.Error (); |
389 | |
390 | } // end OSD_FileIterator :: Error |
391 | |
392 | // For compatibility with UNIX version |
393 | OSD_FileIterator::OSD_FileIterator() {} |
394 | |
395 | void OSD_FileIterator::Initialize( |
396 | const OSD_Path& where, |
397 | const TCollection_AsciiString& Mask){} |
398 | |
399 | #endif |