+#ifdef __WINDOWS__
+ #include "wx/msw/private.h"
+#endif
+
+// ----------------------------------------------------------------------------
+// define the types and functions used for file searching
+// ----------------------------------------------------------------------------
+
+namespace
+{
+
+typedef WIN32_FIND_DATA FIND_STRUCT;
+typedef HANDLE FIND_DATA;
+typedef DWORD FIND_ATTR;
+
+inline FIND_DATA InitFindData()
+{
+ return INVALID_HANDLE_VALUE;
+}
+
+inline bool IsFindDataOk(FIND_DATA fd)
+{
+ return fd != INVALID_HANDLE_VALUE;
+}
+
+inline void FreeFindData(FIND_DATA fd)
+{
+ if ( !::FindClose(fd) )
+ {
+ wxLogLastError(wxT("FindClose"));
+ }
+}
+
+const wxChar *GetNameFromFindData(const FIND_STRUCT *finddata)
+{
+ return finddata->cFileName;
+}
+
+// Helper function checking that the contents of the given FIND_STRUCT really
+// match our filter. We need to do it ourselves as native Windows functions
+// apply the filter to both the long and the short names of the file, so
+// something like "*.bar" matches "foo.bar.baz" too and not only "foo.bar", so
+// we have to double check that we have a real match.
+inline bool
+CheckFoundMatch(const FIND_STRUCT* finddata, const wxString& filter)
+{
+ return filter.empty() ||
+ wxString(GetNameFromFindData(finddata)).Matches(filter);
+}
+
+inline bool
+FindNext(FIND_DATA fd, const wxString& filter, FIND_STRUCT *finddata)
+{
+ for ( ;; )
+ {
+ if ( !::FindNextFile(fd, finddata) )
+ return false;
+
+ // If we did find something, check that it really matches.
+ if ( CheckFoundMatch(finddata, filter) )
+ return true;
+ }
+}
+
+inline FIND_DATA
+FindFirst(const wxString& spec,
+ const wxString& filter,
+ FIND_STRUCT *finddata)
+{
+ FIND_DATA fd = ::FindFirstFile(spec.t_str(), finddata);
+
+ // As in FindNext() above, we need to check that the file name we found
+ // really matches our filter and look for the next match if it doesn't.
+ if ( IsFindDataOk(fd) && !CheckFoundMatch(finddata, filter) )
+ {
+ if ( !FindNext(fd, filter, finddata) )
+ {
+ // As we return the invalid handle from here to indicate that we
+ // didn't find anything, close the one we initially received
+ // ourselves.
+ FreeFindData(fd);
+
+ return INVALID_HANDLE_VALUE;
+ }
+ }
+
+ return fd;
+}
+
+inline FIND_ATTR GetAttrFromFindData(FIND_STRUCT *finddata)
+{
+ return finddata->dwFileAttributes;
+}
+
+inline bool IsDir(FIND_ATTR attr)
+{
+ return (attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
+}
+
+inline bool IsHidden(FIND_ATTR attr)
+{
+ return (attr & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) != 0;
+}
+
+} // anonymous namespace