#ifndef __DIRECT_H

#define __DIRECT_H

enum SearchAttributes
{
  MustBeNormal        = 0x0000,
  MayBeReadOnly       = 0x0001,
  MayBeHidden         = 0x0002,
  MayBeSystem         = 0x0004,
  MayBeDirectory      = 0x0010,
  MayBeArchived       = 0x0020,
  MustBeReadOnly      = 0x0101,
  MustBeHidden        = 0x0202,
  MustBeSystem        = 0x0404,
  MustBeDirectory     = 0x1010,
  MustBeArchived      = 0x2020
};

enum SearchEA
{
  NoNeedEASize = 0,
  NeedEASize   = 1
};

class Directory
{
private:
  char *mask,
       *path;

  unsigned long handle;
  SearchAttributes searchAttrib;
  void *buf;
  int bufSize;
  SearchEA ea;

  int totalFiles;
  long totalSize;
  long totalEASize;

  int done;

  char name[256];
  unsigned short cdate;
  unsigned short ctime;
  unsigned short mdate;
  unsigned short mtime;
  unsigned short adate;
  unsigned short atime;
  unsigned long size;
  unsigned long EAsize;
  unsigned long attributes;
  unsigned long aunix;
  unsigned long munix;
  unsigned long cunix;

protected:
  void startSearch();
  void nextEntry();
  void closeHandle();
  void processEntry( int );

public:
  Directory( char *,
             SearchAttributes = SearchAttributes( MayBeArchived|MayBeReadOnly ),
             SearchEA = NoNeedEASize );

  ~Directory()
    { closeHandle(); delete mask; delete path; delete buf; }

  char *getName() { return name; }
  char *getPath() const { return path; }
  char *getMask() const { return mask; }
  char *getFSPoint() const;

  unsigned short getCreateDate()   const { return cdate; }
  unsigned short getCreateTime()   const { return ctime; }
  unsigned short getModifyDate()   const { return mdate; }
  unsigned short getModifyTime()   const { return mtime; }
  unsigned short getAccessDate()   const { return adate; }
  unsigned short getAccessTime()   const { return atime; }

  unsigned long getUNIXCreate()   const { return cunix; }
  unsigned long getUNIXModify()   const { return munix; }
  unsigned long getUNIXAccess()   const { return aunix; }

  unsigned long  getSize()   const { return size; }
  unsigned long  getEASize() const { return EAsize; }
  unsigned long  getAttrib() const { return attributes; }

  int getTotalFiles() const { return totalFiles; }
  long getTotalSize() const { return totalSize; }
  long getTotalEASize() const { return totalEASize; }

  int isDoneSearch() const { return done; }
  void restartSearch() { closeHandle(); startSearch(); }

  operator int() const { return !isDoneSearch(); }
  int operator ++()  { nextEntry(); return *this; }
  int operator ++( int )  { nextEntry(); return *this; }
};

#endif
