0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[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
d48df25d 29OSD_DirectoryIterator::OSD_DirectoryIterator()
dde68833 30: myFlag(false),
d48df25d 31 myDescr(0),
32 myEntry(0),
33 myInit(0)
34{
7fd59977 35}
36
37OSD_DirectoryIterator::OSD_DirectoryIterator(const OSD_Path& where,
d48df25d 38 const TCollection_AsciiString& Mask)
dde68833 39: myFlag(false),
d48df25d 40 myDescr(0),
41 myEntry(0),
42 myInit(0)
43{
7fd59977 44 Initialize(where, Mask) ;
45}
46
47// For Windows NT compatibility -----------
48void OSD_DirectoryIterator :: Destroy () {}
49//-----------------------------------------
50
51void OSD_DirectoryIterator::Initialize(const OSD_Path& where,
52 const TCollection_AsciiString& Mask){
53
54 myFlag = Standard_False;
55 where.SystemName(myPlace);
56 if (myPlace.Length()==0) myPlace = ".";
57 myMask = Mask;
58 if (myDescr) {
59 closedir((DIR *)myDescr) ;
60 myDescr = NULL ;
61 }
62 myInit = 1 ;
63}
64
65// Is there another directory entry ?
66
67Standard_Boolean OSD_DirectoryIterator::More(){
68 if (myInit) {
69 myInit = 0 ;
70 myDescr = (Standard_Address) opendir(myPlace.ToCString());
71 if (myDescr) { // LD : Si repertoire inaccessible retourner False
72 myFlag = Standard_True;
73 myInit = 0 ;
74 Next(); // Now find first entry
75 }
76 }
dde68833 77 return myFlag;
7fd59977 78}
79
80// Private : See if directory name matches with a mask (like "*.c")
81// LD : reecrit (original dans OSD_FileIterator.cxx)
82
83
84static int strcmp_joker(const char *Mask,const char *Name)
85{
86 const char *p, *s ;
87
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++ ;
92 if (!*p) return 1 ;
93 for (;*s; s++)
94 if (strcmp_joker(p,s)) return 1 ;
95 return 0 ;
96}
97
98// Find next directory entry in current directory
99
100void OSD_DirectoryIterator::Next(){
101int again = 1;
102struct stat stat_buf;
dde68833 103 myFlag = false; // Initialize to nothing found
7fd59977 104
105 do{
106 myEntry = readdir((DIR *)myDescr);
65ada1f1 107
7fd59977 108 if (!myEntry){ // No file found
109 myEntry = NULL; // Keep pointer clean
110 myFlag = Standard_False; // No more files/directory
111 closedir((DIR *)myDescr) ; // so close directory
112 myDescr = NULL;
113 again = 0;
114 }
115 else {
116// if (!strcmp(entry->d_name,".")) continue; LD : on prend ces
117// if (!strcmp(entry->d_name,"..")) continue; 2 directories.
118
119 // Is it a directory ?
65ada1f1 120 const TCollection_AsciiString aFullName = myPlace + "/" + ((struct dirent* )myEntry)->d_name;
121 stat(aFullName.ToCString(), &stat_buf);
7fd59977 122 if (S_ISDIR(stat_buf.st_mode)) // Ensure me it's not a file
123 if (strcmp_joker(myMask.ToCString(), ((struct dirent *)myEntry)->d_name)){
124 // Does it follow mask ?
125 myFlag = Standard_True;
126 again = 0;
127 }
128 }
129
130 } while (again);
131
132}
133
134
135// Get Name of selected directory
136
137OSD_Directory OSD_DirectoryIterator::Values(){
138OSD_Path thisvalue;
139TCollection_AsciiString Name;
140TCollection_AsciiString Ext;
141Standard_Integer position;
142
143 if (myEntry) Name = ((struct dirent *)myEntry)->d_name ;
144
145 position = Name.Search(".");
146
147 if (position != -1){
148 Ext = Name.Split(position - 1) ; // Debug LD
149// Ext.Remove(1,position);
150// Name.Remove( position,Ext.Length()+1);
151 }
152
153 thisvalue.SetValues("", "", "", "", "", Name,Ext);
154 TheIterator.SetPath (thisvalue);
155
156 return (TheIterator);
157}
158
159
160void OSD_DirectoryIterator::Reset(){
161 myError.Reset();
162}
163
164Standard_Boolean OSD_DirectoryIterator::Failed()const{
165 return( myError.Failed());
166}
167
168void OSD_DirectoryIterator::Perror() {
169 myError.Perror();
170}
171
172
173Standard_Integer OSD_DirectoryIterator::Error()const{
174 return( myError.Error());
175}
176
177#else
178
179//------------------------------------------------------------------------
180//------------------- Windows NT sources for OSD_DirectoryIterator ------
181//------------------------------------------------------------------------
182
183
7fd59977 184#include <windows.h>
185
42cf5bc1 186
187#include <OSD_Directory.hxx>
188#include <OSD_DirectoryIterator.hxx>
189#include <OSD_OSDError.hxx>
190#include <OSD_Path.hxx>
191#include <TCollection_AsciiString.hxx>
d9ff84e8 192#include <TCollection_ExtendedString.hxx>
7fd59977 193
d9ff84e8 194#define _FD ( ( PWIN32_FIND_DATAW )myData )
7fd59977 195
1bd04b5a 196void _osd_wnt_set_error ( OSD_Error&, Standard_Integer, ... );
7fd59977 197
198OSD_DirectoryIterator :: OSD_DirectoryIterator (
199 const OSD_Path& where,
200 const TCollection_AsciiString& Mask
201 ) {
202
203 myFlag = Standard_False;
7dc9e047 204 myHandle = INVALID_HANDLE_VALUE;
7fd59977 205
206 where.SystemName ( myPlace );
207
d9ff84e8 208 if ( myPlace.Length () == 0 ) myPlace = ".";
7fd59977 209
210 myMask = Mask;
211 myData = NULL;
212
213} // end constructor
214
215void OSD_DirectoryIterator :: Destroy () {
216
217 if ( myData != NULL ) HeapFree ( GetProcessHeap (), 0, myData );
218
7dc9e047 219 if ( myHandle != INVALID_HANDLE_VALUE )
7fd59977 220
221 FindClose ( ( HANDLE )myHandle );
222
223} // end OSD_DirectoryIterator :: Destroy
224
225Standard_Boolean OSD_DirectoryIterator :: More () {
226
7dc9e047 227 if ( myHandle == INVALID_HANDLE_VALUE ) {
7fd59977 228
d9ff84e8 229 TCollection_AsciiString wc = myPlace + "/" + myMask;
7fd59977 230
231 myData = HeapAlloc (
d9ff84e8 232 GetProcessHeap (), HEAP_GENERATE_EXCEPTIONS, sizeof ( WIN32_FIND_DATAW )
7fd59977 233 );
234
d9ff84e8 235 // make wchar_t string from UTF-8
236 TCollection_ExtendedString wcW(wc);
fb0b0531 237 myHandle = FindFirstFileExW (wcW.ToWideString(), FindExInfoStandard, (PWIN32_FIND_DATAW)myData, FindExSearchNameMatch, NULL, 0);
7fd59977 238
7dc9e047 239 if ( myHandle == INVALID_HANDLE_VALUE )
240
241 _osd_wnt_set_error ( myError, OSD_WDirectoryIterator );
7fd59977 242
243 else {
244
245 myFlag = Standard_True;
246 myFirstCall = Standard_True;
247
248 Next ();
249
250 } // end else
251
252 } else if ( !myFlag ) {
253
254 FindClose ( ( HANDLE )myHandle );
7dc9e047 255 myHandle = INVALID_HANDLE_VALUE;
7fd59977 256
257 } // end if
258
259 return myFlag;
260
261} // end OSD_DirectoryIterator :: More
262
263void OSD_DirectoryIterator :: Next () {
264
7c65581d 265 if ( ! myFirstCall || ! ( _FD -> dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) ) {
7fd59977 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
1bd04b5a 321OSD_DirectoryIterator::OSD_DirectoryIterator()
322: myFlag(false),
323 myHandle(0),
324 myData(0),
325 myFirstCall(Standard_False)
326{}
7fd59977 327
328void OSD_DirectoryIterator::Initialize(
35e08fe8 329 const OSD_Path&,
330 const TCollection_AsciiString&){}
7fd59977 331
332#endif