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