#ifndef _DIRADAPTER_H #define _DIRADAPTER_H #include "directory.h" class dir_iterator { public: // define typedefs expected by STL input iterators typedef input_iterator_tag iterator_category; typedef DirEntry value_type; typedef ptrdiff_t difference_type; typedef const DirEntry * pointer; typedef const DirEntry& reference; dir_iterator() : myDir(0), myPastEnd(true) // post: nothin to read, all done (one past end iterator) { } dir_iterator(DirStream& ds) : myDir(&ds), myPastEnd(false) // post: bound to ds, iterate over all entries { ds.Init(); getEntry(); } reference operator*() const // pre: ! myPastEnd // post: return current entry in stream { return myEntry; } pointer operator->() const // pre: ! myPastEnd // post: return pointer to current entry { return &myEntry; } dir_iterator operator++() // post: entry updated (this is pre-increment) { getEntry(); return *this; } dir_iterator operator++(int) // post: entry updated (this is post-increment) { dir_iterator tmp = *this; getEntry(); return tmp; } bool operator==(const dir_iterator& rhs) const // post: return true iff *this and *rhs both past end OR // if they share the same stream and have the same "end-ness" { return (myDir == rhs.myDir && myPastEnd == rhs.myPastEnd) || (myPastEnd == true && rhs.myPastEnd == true); } bool operator!=(const dir_iterator& rhs) const { return !( *this == rhs); } private: void getEntry() { if (myDir->HasMore()) { myEntry = myDir->Current(); myDir->Next(); } else { myPastEnd = true; } } DirStream * myDir; // wrapper for this stream DirEntry myEntry; // current entry bool myPastEnd; // true iff one past end }; #endif