#ifndef _DIRECTORY_H #define _DIRECTORY_H // // author: Owen Astrachan // date: 9/21/93 // // modified 11/28/94 // modified 4/5/95 // modified 1/18/96 // modified 8/30/97, port to standard C++ string from CPstring.h // naming conventions supported // modified 5/10/99, ported to 32-bit windows, then back to g++ // classes for manipulating directories // provide a standard interface for directory // queries from C++ programs that can, in theory, be implemented // on several platforms // // currently supported: Unix, DOS, Mac in development // // the class DirEntry provides directory information // accessible via methods Name, Size, and IsDir // // the class DirStream does I/O on directories // it supports "standard" (for the Tapestry book) // iterator methods/member functions // // ********* DirEntry member functions: // // string Name() -- returns name of file // string Path() -- returns full path to file // int Size() -- returns size of file (in bytes) // bool IsDir() -- returns false if NOT directory, else true // // DirEntry() -- constructor, directory entry undefined attributes // ********* DirStream member functions: // // DirStream(string name) -- constructor (pass name of directory) // DirStream() -- default constructor (use current directory) // void open(string name) -- opens directory stream with given name // bool fail() -- returns true if directory operations has // failed, else returns false // void close() -- close stream // // // void Init() -- set DirStream so first entry is accessible // bool HasMore() -- returns true if current ok, else false // void Next() -- advance to next entry // DirEntry Current() -- return current directory entry // call only when HasMore = true // #include #include using namespace std; #include #include const string DIR_SEPARATOR = "/"; // platform specific class DirStream; // need forward reference for friendship #include "date.h" #include "clockt.h" class DirEntry { public: DirEntry(); // constructor ~DirEntry(); // destructor string Name() const; // return name (not full path) of file string Path() const; // return canonicalized path of file long Size() const; // return size (bytes) of file bool IsDir() const; // return false if file, true if directory Date GetDate()const; // date of file last modified ClockTime GetTime() const; // time last modified friend class DirStream; // class has access to internals private: // this is the private directory entry information // it is platform specific, probably should be a 'handle' // to a class PrivateDirEntry defined in directory.cc string myName; // NOT full path, just 'file' name string myPath; // full, canonicalized path long mySize; // in bytes ClockTime myTime; Date myDate; bool myIsDirectory; // true if directory, else false }; class DirStream { public: DirStream(const string & name); // name is path to directory DirStream(); // current directory ~DirStream(); // destructor void open(const string & name); // open, bind to file with name void close(); // close the stream bool fail(); // return true if failed, else false void Init(); // standard iterator functions void Next(); bool HasMore(); DirEntry Current(); // stuff below is 'esoteric' C++ // // the () method returning void * is what allows // the expression: while (dirstream) // to work [see Teale, The I/O Stream Handbook] operator void *() const { return myStatus ? (void *) this : (void *) 0; }; // allow the expression if (!dirstream) int operator !() const { return !myStatus; } private: DIR * myStream; // this field supports C/UNIX system calls bool myStatus; // if true, everything ok, else all done string myPath; // full path to this directory DirEntry myEntry; // cached, current entry bool myIsClosed; // already closed myStream void Initialize(const string & s); // private init commonality void SetEntry(); // common code Init/Next // disable assignment and copy DirStream operator = (const DirStream & dir); DirStream(const DirStream & dir); }; #endif /* _DIRECTORY_H not defined */