FileFind.h

// FileFind.h
//
// Author David Barrett-Lennard
// (C)opyright Cedanet Pty Ltd 2005

#pragma once
#ifndef Ceda_cxUtils_FileFind_H
#define Ceda_cxUtils_FileFind_H

#include "cxUtils.h"
#include "MsWindows.h"
#include "HPTime.h"
#include "xstring.h"

namespace ceda
{
#ifdef _WIN32

///////////////////////////////////////////////////////////////////////////////////////////////////
// IFileOrDirInfo

// Provides information about a file or directory
struct IFileOrDirInfo
{
    virtual bool IsArchive() const = 0;
    virtual bool IsCompressed() const = 0;
    virtual bool IsDirectory() const = 0;
    virtual bool IsEncrypted() const = 0;
    virtual bool IsHidden() const = 0;
    virtual bool IsOffLine() const = 0;
    virtual bool IsReadOnly() const = 0;
    virtual bool IsSparseFile() const = 0;
    virtual bool IsSystem() const = 0;

    // The time the file was first created in UTC format    
    virtual HPTime GetCreationTime() const = 0;

    // The time the file was last accessed in UTC format
    virtual HPTime GetLastAccessTime() const = 0;
    
    // The time the file was last written to in UTC format
    virtual HPTime GetLastWriteTime() const = 0;

    virtual int64 GetFileSize() const = 0;
    virtual xstring GetFileName() const = 0;
};

///////////////////////////////////////////////////////////////////////////////////////////////////
// FileOrDirInfo

class cxUtils_API FileOrDirInfo : public IFileOrDirInfo
{
public:
    FileOrDirInfo();
    
    // Implementation of IFileOrDirInfo 
    virtual bool IsArchive() const;
    virtual bool IsCompressed() const;
    virtual bool IsDirectory() const;
    virtual bool IsEncrypted() const;
    virtual bool IsHidden() const;
    virtual bool IsOffLine() const;
    virtual bool IsReadOnly() const;
    virtual bool IsSparseFile() const;
    virtual bool IsSystem() const;
    virtual HPTime GetCreationTime() const;
    virtual HPTime GetLastAccessTime() const;
    virtual HPTime GetLastWriteTime() const;
    virtual int64 GetFileSize() const;
    virtual xstring GetFileName() const;

private:
    // The WIN32_FIND_DATAW structure describes a file found by the FindFirstFile, 
    // FindFirstFileEx, or FindNextFile function. 
    WIN32_FIND_DATAW m_wfd;

    friend class FileFind;
};

///////////////////////////////////////////////////////////////////////////////////
// FileFind

/* This class searches for files by name only; it cannot be used for attribute-based 
   searches. 

Example usage

    FileFind w("c:\\*.*");
    while(IFileOrDirInfo* info = w.GetNext())
    {
        xstring filename = info->GetFileName();            
    }
*/ 

class cxUtils_API FileFind
{
public:
    FileFind();
    FileFind(const xstring& filename);
    ~FileFind();
    
    // filename is a string that specifies a valid directory or path and filename, which 
    // can contain wildcard characters (* and ?). This string must not exceed MAX_PATH 
    // characters
    void SetFileName(const xstring& filename);

    IFileOrDirInfo* GetNext();
private:
    FileOrDirInfo m_info; 
    bool m_haveFirst;
    HANDLE m_handle;

    void CloseIfNotAlreadyClosed();
};

#endif // _WIN32
} // namespace ceda

#endif // include guard