File: FXDir.cpp

package info (click to toggle)
gogglesmm 1.2.5-6
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 16,812 kB
  • sloc: cpp: 231,960; ansic: 893; xml: 222; makefile: 33
file content (662 lines) | stat: -rw-r--r-- 22,082 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/********************************************************************************
*                                                                               *
*                    D i r e c t o r y   E n u m e r a t o r                    *
*                                                                               *
*********************************************************************************
* Copyright (C) 2005,2022 by Jeroen van der Zijp.   All Rights Reserved.        *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify          *
* it under the terms of the GNU Lesser General Public License as published by   *
* the Free Software Foundation; either version 3 of the License, or             *
* (at your option) any later version.                                           *
*                                                                               *
* This library is distributed in the hope that it will be useful,               *
* but WITHOUT ANY WARRANTY; without even the implied warranty of                *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
* GNU Lesser General Public License for more details.                           *
*                                                                               *
* You should have received a copy of the GNU Lesser General Public License      *
* along with this program.  If not, see <http://www.gnu.org/licenses/>          *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "fxchar.h"
#include "fxmath.h"
#include "FXArray.h"
#include "FXHash.h"
#include "FXStream.h"
#include "FXString.h"
#include "FXIO.h"
#include "FXStat.h"
#include "FXFile.h"
#include "FXPath.h"
#include "FXDir.h"

/*
  Notes:
  - This class implements a way to list the files in a directory.
  - We just want to wrap directory iteration, nothing fancy.
  - Maybe add positioning for seek and tell type functions.
*/


using namespace FX;

/*******************************************************************************/

namespace FX {


#ifdef WIN32
struct SPACE {
  HANDLE          handle;
  FXuint          first;
  WIN32_FIND_DATA result;
  };
#else
struct SPACE {
  DIR*            handle;
  struct dirent*  dp;
  };
#endif


// Construct directory enumerator
FXDir::FXDir(){
  // If this fails on your machine, determine what sizeof(SPACE) is
  // on your machine and mail it to: jeroen@fox-toolkit.net!
  //FXTRACE((150,"sizeof(SPACE)=%ld\n",sizeof(SPACE)));
  FXASSERT(sizeof(SPACE)<=sizeof(space));
#ifdef WIN32
  alias_cast<SPACE>(space)->handle=INVALID_HANDLE_VALUE;
#else
  alias_cast<SPACE>(space)->handle=nullptr;
#endif
  }


// Construct directory enumerator
FXDir::FXDir(const FXString& path){
  // If this fails on your machine, determine what sizeof(SPACE) is
  // on your machine and mail it to: jeroen@fox-toolkit.net!
  //FXTRACE((150,"sizeof(SPACE)=%ld\n",sizeof(SPACE)));
  FXASSERT(sizeof(SPACE)<=sizeof(space));
#ifdef WIN32
  alias_cast<SPACE>(space)->handle=INVALID_HANDLE_VALUE;
#else
  alias_cast<SPACE>(space)->handle=nullptr;
#endif
  open(path);
  }


// Open directory to path, return true if ok.
FXbool FXDir::open(const FXString& path){
  if(!path.empty()){
#ifdef WIN32
#ifdef UNICODE
    FXnchar buffer[MAXPATHLEN+2];
    utf2ncs(buffer,path.text(),MAXPATHLEN);
    wcsncat(buffer,TEXT("\\*"),MAXPATHLEN+2);
#else
    FXchar buffer[MAXPATHLEN+2];
    fxstrlcpy(buffer,path.text(),MAXPATHLEN);
    fxstrlcat(buffer,"\\*",MAXPATHLEN+2);
#endif
    alias_cast<SPACE>(space)->handle=FindFirstFile(buffer,&alias_cast<SPACE>(space)->result);
    if(alias_cast<SPACE>(space)->handle!=INVALID_HANDLE_VALUE){
      alias_cast<SPACE>(space)->first=true;
      return true;
      }
#else
    alias_cast<SPACE>(space)->handle=opendir(path.text());
    if(alias_cast<SPACE>(space)->handle!=nullptr){
      return true;
      }
#endif
    }
  return false;
  }


// Returns true if the directory is open
FXbool FXDir::isOpen() const {
#ifdef WIN32
  return (alias_cast<const SPACE>(space)->handle!=INVALID_HANDLE_VALUE);
#else
  return alias_cast<const SPACE>(space)->handle!=nullptr;
#endif
  }


// Go to next directory entry and return its name
FXbool FXDir::next(FXString& name){
  if(isOpen()){
#if defined(WIN32)
    if(alias_cast<SPACE>(space)->first || FindNextFile(alias_cast<SPACE>(space)->handle,&alias_cast<SPACE>(space)->result)){
      alias_cast<SPACE>(space)->first=false;
      name.assign(alias_cast<SPACE>(space)->result.cFileName);
      return true;
      }
#else
    if((alias_cast<SPACE>(space)->dp=readdir(alias_cast<SPACE>(space)->handle))!=nullptr){
      name.assign(alias_cast<SPACE>(space)->dp->d_name);
      return true;
      }
#endif
    }
  name.clear();
  return false;
  }


// Close directory
void FXDir::close(){
  if(isOpen()){
#ifdef WIN32
    FindClose(alias_cast<SPACE>(space)->handle);
    alias_cast<SPACE>(space)->handle=INVALID_HANDLE_VALUE;
#else
    closedir(alias_cast<SPACE>(space)->handle);
    alias_cast<SPACE>(space)->handle=nullptr;
#endif
    }
  }


// Create new directory
FXbool FXDir::create(const FXString& path,FXuint perm){
  if(!path.empty()){
#ifdef WIN32
#ifdef UNICODE
    FXnchar buffer[MAXPATHLEN];
    utf2ncs(buffer,path.text(),MAXPATHLEN);
    return CreateDirectoryW(buffer,nullptr)!=0;
#else
    return CreateDirectoryA(path.text(),nullptr)!=0;
#endif
#else
    return ::mkdir(path.text(),perm)==0;
#endif
    }
  return false;
  }


// Remove directory
FXbool FXDir::remove(const FXString& path){
  if(!path.empty()){
#ifdef WIN32
#ifdef UNICODE
    FXnchar buffer[MAXPATHLEN];
    utf2ncs(buffer,path.text(),MAXPATHLEN);
    return RemoveDirectoryW(buffer)!=0;
#else
    return RemoveDirectoryA(path.text())!=0;
#endif
#else
    return ::rmdir(path.text())==0;
#endif
    }
  return false;
  }


// List all the files in directory
FXint FXDir::listFiles(FXString*& filelist,const FXString& path,const FXString& pattern,FXuint flags){
  FXDir dir(path);

  // Initialize to empty
  filelist=nullptr;

  // Get directory stream pointer
  if(dir.isOpen()){
    FXuint    mode=(flags&CaseFold)?(FXPath::PathName|FXPath::NoEscape|FXPath::CaseFold):(FXPath::PathName|FXPath::NoEscape);
    FXString *newlist;
    FXint     size=0;
    FXint     count=0;
    FXString  pathname;
    FXString  name;
    FXStat    data;

    // Loop over directory entries
    while(dir.next(name)){

      // Build full pathname
      pathname=path;
      if(!ISPATHSEP(pathname.tail())) pathname+=PATHSEPSTRING;
      pathname+=name;

      // Get info on file
      if(!FXStat::statFile(pathname,data)) continue;

#ifdef WIN32

      // Filter out files; a bit tricky...
      if(!data.isDirectory() && ((flags&NoFiles) || (data.isHidden() && !(flags&HiddenFiles)) || (!(flags&AllFiles) && !FXPath::match(name,pattern,mode)))) continue;

      // Filter out directories; even more tricky!
      if(data.isDirectory() && ((flags&NoDirs) || (data.isHidden() && !(flags&HiddenDirs)) || ((name[0]=='.' && (name[1]==0 || (name[1]=='.' && name[2]==0))) && (flags&NoParent)) || (!(flags&AllDirs) && !FXPath::match(name,pattern,mode)))) continue;

#else

      // Filter out files; a bit tricky...
      if(!data.isDirectory() && ((flags&NoFiles) || (name[0]=='.' && !(flags&HiddenFiles)) || (!(flags&AllFiles) && !FXPath::match(name,pattern,mode)))) continue;

      // Filter out directories; even more tricky!
      if(data.isDirectory() && ((flags&NoDirs) || (name[0]=='.' && !(flags&HiddenDirs)) || ((name[0]=='.' && (name[1]==0 || (name[1]=='.' && name[2]==0))) && (flags&NoParent)) || (!(flags&AllDirs) && !FXPath::match(name,pattern,mode)))) continue;

#endif

      // Grow list
      if(count+1>=size){
        size=size?(size<<1):256;
        newlist=new FXString [size];
        for(FXint i=0; i<count; i++){
          newlist[i].adopt(filelist[i]);
          }
        delete [] filelist;
        filelist=newlist;
        }

      // Add to list
      filelist[count++].adopt(name);
      }
    return count;
    }
  return 0;
  }


// List drives, i.e. roots of directory trees.
FXint FXDir::listDrives(FXString*& drivelist){
  FXint count=0;
#ifdef WIN32
  FXchar drives[256];
  clearElms(drives,ARRAYNUMBER(drives));
  GetLogicalDriveStringsA(ARRAYNUMBER(drives),drives);
  drivelist=new FXString [33];
  for(const FXchar* drive=drives; *drive && count<32; drive++){
    drivelist[count].assign(drive);
    while(*drive) drive++;
    count++;
    }
#else
  drivelist=new FXString [2];
  drivelist[count++].assign(PATHSEP);
#endif
  return count;
  }


#if 0

FXint FXDir::listShares(FXString*& sharelist){
  FXint count=0;
#ifdef WIN32
#else
  sharelist=new FXString [2];
  sharelist[count++].assign(PATHSEP);
#endif
  return count;
  }
#endif


// Create a directories recursively
FXbool FXDir::createDirectories(const FXString& path,FXuint perm){
  if(FXPath::isAbsolute(path)){
    if(FXStat::isDirectory(path)) return true;
    if(createDirectories(FXPath::upLevel(path),perm)){
      if(FXDir::create(path,perm)) return true;
      }
    }
  return false;
  }


// Cleanup
FXDir::~FXDir(){
  close();
  }


}




#if 0

// List all the files in directory
FXint FXDir::listFiles(FXString*& filelist,const FXString& path,const FXString& pattern,FXuint flags){
  FXuint matchmode=FILEMATCH_FILE_NAME|FILEMATCH_NOESCAPE;
  FXString pathname;
  FXString name;
  FXString *newlist;
  FXint count=0;
  FXint size=0;
  WIN32_FIND_DATA ffData;
  DWORD nCount,nSize,i,j;
  HANDLE hFindFile,hEnum;
  FXchar server[200];

  // Initialize to empty
  filelist=nullptr;

/*
  // Each drive is a root on windows
  if(path.empty()){
    FXchar letter[4];
    letter[0]='a';
    letter[1]=':';
    letter[2]=PATHSEP;
    letter[3]='\0';
    filelist=new FXString[28];
    for(DWORD mask=GetLogicalDrives(); mask; mask>>=1,letter[0]++){
      if(mask&1) list[count++]=letter;
      }
    filelist[count++]=PATHSEPSTRING PATHSEPSTRING;    // UNC for file shares
    return count;
    }
*/
/*
  // A UNC name was given of the form "\\" or "\\server"
  if(ISPATHSEP(path[0]) && ISPATHSEP(path[1]) && path.find(PATHSEP,2)<0){
    NETRESOURCE host;

    // Fill in
    host.dwScope=RESOURCE_GLOBALNET;
    host.dwType=RESOURCETYPE_DISK;
    host.dwDisplayType=RESOURCEDISPLAYTYPE_GENERIC;
    host.dwUsage=RESOURCEUSAGE_CONTAINER;
    host.lpLocalName=nullptr;
    host.lpRemoteName=(char*)path.text();
    host.lpComment=nullptr;
    host.lpProvider=nullptr;

    // Open network enumeration
    if(WNetOpenEnum((path[2]?RESOURCE_GLOBALNET:RESOURCE_CONTEXT),RESOURCETYPE_DISK,0,(path[2]?&host:nullptr),&hEnum)==NO_ERROR){
      NETRESOURCE resource[16384/sizeof(NETRESOURCE)];
      FXTRACE((1,"Enumerating=%s\n",path.text()));
      while(1){
        nCount=-1;    // Read as many as will fit
        nSize=sizeof(resource);
        if(WNetEnumResource(hEnum,&nCount,resource,&nSize)!=NO_ERROR) break;
        for(i=0; i<nCount; i++){

          // Dump what we found
          FXTRACE((1,"dwScope=%s\n",resource[i].dwScope==RESOURCE_CONNECTED?"RESOURCE_CONNECTED":resource[i].dwScope==RESOURCE_GLOBALNET?"RESOURCE_GLOBALNET":resource[i].dwScope==RESOURCE_REMEMBERED?"RESOURCE_REMEMBERED":"?"));
          FXTRACE((1,"dwType=%s\n",resource[i].dwType==RESOURCETYPE_ANY?"RESOURCETYPE_ANY":resource[i].dwType==RESOURCETYPE_DISK?"RESOURCETYPE_DISK":resource[i].dwType==RESOURCETYPE_PRINT?"RESOURCETYPE_PRINT":"?"));
          FXTRACE((1,"dwDisplayType=%s\n",resource[i].dwDisplayType==RESOURCEDISPLAYTYPE_DOMAIN?"RESOURCEDISPLAYTYPE_DOMAIN":resource[i].dwDisplayType==RESOURCEDISPLAYTYPE_SERVER?"RESOURCEDISPLAYTYPE_SERVER":resource[i].dwDisplayType==RESOURCEDISPLAYTYPE_SHARE?"RESOURCEDISPLAYTYPE_SHARE":resource[i].dwDisplayType==RESOURCEDISPLAYTYPE_GENERIC?"RESOURCEDISPLAYTYPE_GENERIC":resource[i].dwDisplayType==6?"RESOURCEDISPLAYTYPE_NETWORK":resource[i].dwDisplayType==7?"RESOURCEDISPLAYTYPE_ROOT":resource[i].dwDisplayType==8?"RESOURCEDISPLAYTYPE_SHAREADMIN":resource[i].dwDisplayType==9?"RESOURCEDISPLAYTYPE_DIRECTORY":resource[i].dwDisplayType==10?"RESOURCEDISPLAYTYPE_TREE":resource[i].dwDisplayType==11?"RESOURCEDISPLAYTYPE_NDSCONTAINER":"?"));
          FXTRACE((1,"dwUsage=%s\n",resource[i].dwUsage==RESOURCEUSAGE_CONNECTABLE?"RESOURCEUSAGE_CONNECTABLE":resource[i].dwUsage==RESOURCEUSAGE_CONTAINER?"RESOURCEUSAGE_CONTAINER":"?"));
          FXTRACE((1,"lpLocalName=%s\n",resource[i].lpLocalName));
          FXTRACE((1,"lpRemoteName=%s\n",resource[i].lpRemoteName));
          FXTRACE((1,"lpComment=%s\n",resource[i].lpComment));
          FXTRACE((1,"lpProvider=%s\n\n",resource[i].lpProvider));

          // Grow list
          if(count+1>=size){
            size=size?(size<<1):256;
            newlist=new FXString[size];
            for(j=0; j<count; j++) newlist[j]=list[j];
            delete [] filelist;
            filelist=newlist;
            }

          // Add remote name to list
          filelist[count]=resource[i].lpRemoteName;
          count++;
          }
        }
      WNetCloseEnum(hEnum);
      }
    return count;
    }
*/
  // Folding case
  if(flags&LIST_CASEFOLD) matchmode|=FILEMATCH_CASEFOLD;

  // Copy directory name
  pathname=path;
  if(!ISPATHSEP(pathname[pathname.length()-1])) pathname+=PATHSEPSTRING;
  pathname+="*";

  // Open directory
  hFindFile=FindFirstFile(pathname.text(),&ffData);
  if(hFindFile!=INVALID_HANDLE_VALUE){

    // Loop over directory entries
    do{

      // Get name
      name=ffData.cFileName;

      // Filter out files; a bit tricky...
      if(!(ffData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) && ((flags&LIST_NO_FILES) || ((ffData.dwFileAttributes&FILE_ATTRIBUTE_HIDDEN) && !(flags&LIST_HIDDEN_FILES)) || (!(flags&LIST_ALL_FILES) && !match(pattern,name,matchmode)))) continue;

      // Filter out directories; even more tricky!
      if((ffData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) && ((flags&LIST_NO_DIRS) || ((ffData.dwFileAttributes&FILE_ATTRIBUTE_HIDDEN) && !(flags&LIST_HIDDEN_DIRS)) || (name[0]=='.' && (name[1]==0 || (name[1]=='.' && name[2]==0 && (flags&LIST_NO_PARENT)))) || (!(flags&LIST_ALL_DIRS) && !match(pattern,name,matchmode)))) continue;

      // Grow list
      if(count+1>=size){
        size=size?(size<<1):256;
        newlist=new FXString[size];
        for(int f=0; f<count; f++){
          newlist[f].adopt(filelist[f]);
          }
        delete [] filelist;
        filelist=newlist;
        }

      // Add to list
      filelist[count++]=name;
      }
    while(FindNextFile(hFindFile,&ffData));
    FindClose(hFindFile);
    }
  return count;
  }














void fxenumWNetContainerResource(NETRESOURCE* netResource,FXObjectListOf<FXStringObject>& netResourceList,DWORD openEnumScope){
//  Comments are mine, unless indicated otherwise. - Danil Hrchner <dbjh@gmx.net>
//
//  Passing the value RESOURCE_GLOBALNET for openEnumScope will make this
//  function search recursively through the network shares for disk resources.
//  Passing the value RESOURCE_CONTEXT will make this function list the servers
//  in the network neighbourhood.
  DWORD retVal;
  HANDLE handle;

  //  WNetEnumResource() reports containers as being disk resources if
  //  WNetOpenEnum() is called with RESOURCETYPE_DISK. This does not happen if
  //  it's called with RESOURCETYPE_ANY.
  //  BTW RESOURCETYPE_DISK does not guarantee that only disk resources are
  //  reported (I also get a printer container in the list).
  if((retVal=WNetOpenEnum(openEnumScope,RESOURCETYPE_DISK,0,netResource,&handle))!=NO_ERROR){
    // we get here also if access was denied to enumerate the container
    FXTRACE((1,"ERROR: WNetOpenEnum() (%d)\n", retVal));
    return;
    }

  NETRESOURCE *netResources;
  DWORD netResourcesSize=16*1024;               // 16 kB is a good size, according to MSDN
  if ((netResources=(NETRESOURCE *)malloc(netResourcesSize))==nullptr){
    FXTRACE((1,"ERROR: Not enough memory for NETRESOURCE structures\n"));
    WNetCloseEnum(handle);
    return;
    }

  do{

    DWORD nEntries=(DWORD)-1;
    retVal=WNetEnumResource(handle,&nEntries,netResources,&netResourcesSize);
    // netResourcesSize is not modified if the buffer is large enough

    if(retVal==ERROR_MORE_DATA){
      // MSDN info is not correct; ERROR_MORE_DATA means the buffer was too
      //  small for a _single_ entry
      // netResourcesSize (now) contains required size
      if((netResources=(NETRESOURCE *)realloc(netResources,netResourcesSize))==nullptr){
        FXTRACE((1,"ERROR: Reallocation for NETRESOURCE structures failed\n"));
        WNetCloseEnum(handle);
        return;
        }
      nEntries=(DWORD)-1;
      retVal=WNetEnumResource(handle,&nEntries,netResources,&netResourcesSize);
      }

    if(retVal!=NO_ERROR && retVal!=ERROR_NO_MORE_ITEMS){
      char *str;
      switch (retVal){
        case ERROR_MORE_DATA: str="more data"; break; // shouldn't happen
        case ERROR_INVALID_HANDLE: str="invalid handle"; break;
        case ERROR_NO_NETWORK: str="no network"; break;
        case ERROR_EXTENDED_ERROR: str="extended error"; break;
        default: str="unknown";
        }
      FXTRACE((1,"ERROR: Network enum error: %s (%d)\n",str,retVal));
      free(netResources);
      WNetCloseEnum(handle);
      return;
      }

    for(DWORD n=0; n < nEntries; n++){
      FXbool isContainer=false;

      // if RESOURCE_CONTEXT was passed to WNetOpenEnum(), dwScope will be
      //  RESOURCE_GLOBALNET
      if(netResources[n].dwScope==RESOURCE_GLOBALNET && (netResources[n].dwUsage&RESOURCEUSAGE_CONTAINER)){
        isContainer=true;

        //  If RESOURCE_CONTEXT was passed to WNetOpenEnum(), the first entry is
        //  a "self reference". For example, starting from the network root one
        //  can find a container entry with lpComment "Entire Network"
        //  (lpRemoteName and lpLocalName are nullptr for me). This container
        //  contains an entry with the same properties. In order to avoid getting
        //  into an infinite loop, we must handle this case.
        //  However, trying to enumerate normal containers while RESOURCE_CONTEXT
        //  was used causes WNetOpenEnum() to return ERROR_INVALID_PARAMETER.
        if(netResources[n].lpRemoteName){
          netResourceList.append(new FXStringObject(((FXString)netResources[n].lpRemoteName)+PATHSEP));
          if(openEnumScope!=RESOURCE_CONTEXT){
            fxenumWNetContainerResource(&netResources[n],netResourceList,openEnumScope);
            }
          }
        }

      // Using the variable isContainer is necessary if WNetOpenEnum() is
      //  called with RESOURCETYPE_DISK. See above.
      if(netResources[n].dwType==RESOURCETYPE_DISK && !isContainer){
        netResourceList.append(new FXStringObject(((FXString)netResources[n].lpRemoteName)+PATHSEP));
        }
      }
    }
  while(retVal!=ERROR_NO_MORE_ITEMS);

  free(netResources);
  WNetCloseEnum(handle);                        // it makes no sense to check for NO_ERROR
  }






  // Folding case
  if(flags&LIST_CASEFOLD) matchmode|=FILEMATCH_CASEFOLD;

  if(FXFile::isShareServer(path)){
    pathname=path;
    // pathname must not have an ending back slash or else
    //  fxenumWNetContainerResource() (WNetOpenEnum()) will fail
    if(ISPATHSEP(pathname[pathname.length()-1])) pathname.trunc(pathname.length()-1);
    NETRESOURCE netResource;
    memset(&netResource,0,sizeof(NETRESOURCE));
    netResource.lpRemoteName=(char *)pathname.text();

    FXObjectListOf<FXStringObject> netResourceList;
    // a share server can only provide shares, which are similar to directories
    if(!(flags&LIST_NO_DIRS))
      fxenumWNetContainerResource(&netResource,netResourceList,RESOURCE_GLOBALNET);

    for(int n=0; n < netResourceList.no(); n++){
      // Get name
      name=*netResourceList[n];
      if(ISPATHSEP(name[name.length()-1])) name.trunc(name.length()-1);
      name=FXFile::name(name);

      // Filter out directories
      if(!(flags&LIST_ALL_DIRS) && !match(pattern,name,matchmode)) continue;

      // Grow list
      if(count+1>=size){
        size=size?(size<<1):256;
        newlist=new FXString[size];
        for(int f=0; f<count; f++) newlist[f]=filelist[f];
        delete [] filelist;
        filelist=newlist;
        }

      // Add to list
      filelist[count++]=name;
      }
    for(int n=0; n < netResourceList.no(); n++) delete netResourceList[n];
    netResourceList.clear();
    }
  else{
    // Copy directory name
    pathname=path;
    if(!ISPATHSEP(pathname[pathname.length()-1])) pathname+=PATHSEPSTRING;
    pathname+="*";

    // Open directory
    hFindFile=FindFirstFile(pathname.text(),&ffData);
    if(hFindFile!=INVALID_HANDLE_VALUE){

      // Loop over directory entries
      do{

        // Get name
        name=ffData.cFileName;

        // Filter out files; a bit tricky...
        if(!(ffData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) && ((flags&LIST_NO_FILES) || ((ffData.dwFileAttributes&FILE_ATTRIBUTE_HIDDEN) && !(flags&LIST_HIDDEN_FILES)) || (!(flags&LIST_ALL_FILES) && !match(pattern,name,matchmode)))) continue;

        // Filter out directories; even more tricky!
        if((ffData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) && ((flags&LIST_NO_DIRS) || ((ffData.dwFileAttributes&FILE_ATTRIBUTE_HIDDEN) && !(flags&LIST_HIDDEN_DIRS)) || (name[0]=='.' && (name[1]==0 || (name[1]=='.' && name[2]==0 && (flags&LIST_NO_PARENT)))) || (!(flags&LIST_ALL_DIRS) && !match(pattern,name,matchmode)))) continue;

        // Grow list
        if(count+1>=size){
          size=size?(size<<1):256;
          newlist=new FXString[size];
          for(int f=0; f<count; f++) newlist[f]=filelist[f];
          delete [] filelist;
          filelist=newlist;
          }

        // Add to list
        filelist[count++]=name;
        }
      while(FindNextFile(hFindFile,&ffData));
      FindClose(hFindFile);
      }
    }
  return count;
  }

#endif