TFileNamIterator Fix

Date: 12 February 2000
Author: Hasfjord at btinternet dot com

TFileNameIterator is a nice gem in the class library. It encapsulates Win32 FindFirstFile and FindNextFile calls. Unfortunately, it has a bug: The internal iterator flag isn't set correctly when no files are found, hence always indicating one or more files.

Here is a bug-fix in the form of a proxy header. Include this header instead of <classlib/filename.h> and use TFileNameIterator as normal --- no need to rebuild classlib!

FILENAME.H

// classlib/filename.h proxy - fixes bug in TFileNameIterator
// Vidar Hasfjord, 2000

// This file fixes a bug in TFileNameIterator: 
// The Done flag is not properly set when no file is found.
                                                             
#ifndef _FILENAME_H_FIX
#define _FILENAME_H_FIX

// Scream if classlib/filename.h has already been included.

#if defined(CLASSLIB_FILENAME_H)
#error classlib/filename.h already included --- can't patch!
#endif

// Include the same headers as classlib/filename.h does.
// Include these here to avoid macro trickery in these files (see below).

#if !defined(CLASSLIB_DEFS_H)
# include <classlib/defs.h>
#endif
#if !defined(SERVICES_CSTRING_H)
# include <services/cstring.h>
#endif
#include <dir.h>  // struct ffblk
#if !defined(CLASSLIB_FILE_H)
# include <classlib/file.h>
#endif

// Include classlib/filename.h.
// Force access to private parts by macro trickery (damned Classlib designers :-).

#define Handle Handle;friend class TFileNameIterator_fix
#include <classlib/filename.h>
#undef Handle

// Derive a new class that fixes the bug.

#if defined(BI_NAMESPACE)
namespace ClassLib {
#endif

class TFileNameIterator_fix : public TFileNameIterator
  {
  public:
    TFileNameIterator_fix (const string& wildName)
      : TFileNameIterator (wildName)
      {
      // Ensure that Done is set properly.
      Done = Handle == INVALID_HANDLE_VALUE;
      }
  };

#if defined(BI_NAMESPACE)
}   // namespace ClassLib
#endif

// Redefine ordinary name to fixed one.
#define TFileNameIterator TFileNameIterator_fix

#endif