#include using namespace std; #include #include "directory.h" //#include //#include //#include //#include const int PATH_SIZE = 1024; // local storage static char buf[PATH_SIZE]; // // author: Owen Astrachan // date: 10/15/93, ported to Dec stations on 11/8/93 // modified 1/18/96 to add Direntry constructor // modified 5/10/99, ported to NT, cleaned up redundant code // // implementation of directory classes for reading info // about directories. See directory.h // // // // this substitures for a WIN32 Handle, but encapsulated struct HHandle { HANDLE item; HHandle() { item = INVALID_HANDLE_VALUE;} }; // this substitutes for a WIN32_FIND_DATA, but encapsulated struct WIN32_DATA { WIN32_FIND_DATA item; WIN32_DATA() { } }; DirEntry::DirEntry() { //currently empty } DirEntry::DirEntry(WIN32_DATA* dat) : myName(""), myPath(""), mySize(0), myIsDirectory(false) { WIN32_FIND_DATA * data = &dat->item; myName = data->cFileName; mySize = data->nFileSizeHigh*(MAXDWORD+1) + data->nFileSizeLow; myIsDirectory = data->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; } DirEntry::~DirEntry() { //currently empty } apstring DirEntry::Path() const { return myPath; } apstring DirEntry::Name() const { return myName; } long int DirEntry::Size() const { return mySize; } bool DirEntry::IsDir() const { return myIsDirectory; } void DirStream::Initialize(const apstring & s) // postcondition: all private data initialized { myPath = s; if (s.find(":") != npos || s[0] == DIR_SEPARATOR[0]) { bool success = SetCurrentDirectory(s.c_str()); if (! success) { cout << "failed to find directory " << s << endl; myStatus = false; myStream->item = INVALID_HANDLE_VALUE; return; } } else { int len = GetCurrentDirectory(PATH_SIZE,buf); myPath = apstring(buf) + DIR_SEPARATOR + s; // remember path to this directory } if (myStream->item != INVALID_HANDLE_VALUE && !myIsClosed) { if (!FindClose(myStream->item)) { cout << "error closing directory " << myPath << endl; } } myIsClosed = false; apstring allFiles = myPath + DIR_SEPARATOR + "*"; // get all files myStream->item = FindFirstFile(allFiles.c_str(), &myData->item); SetEntry(); myStatus = myStream->item != INVALID_HANDLE_VALUE; // check if open succeeded } DirStream::DirStream(const apstring & s) : myStream(new HHandle), myData(new WIN32_DATA), myStatus(false), myIsClosed(true) // postcondition: initialized to directory whose path is s { Initialize(s); } DirStream::DirStream() : myStream(new HHandle), myData(new WIN32_DATA), myStatus(false), myIsClosed(true) // postcondition: initialized to current directory { int len = GetCurrentDirectory(PATH_SIZE,buf); apstring self(buf); Initialize(self); } bool DirStream::fail() { return ! myStatus || myStream->item == INVALID_HANDLE_VALUE; } void DirStream::open(const apstring & s) { Initialize(s); } DirStream::~DirStream() { close(); } void DirStream::close() { if (myStream->item != INVALID_HANDLE_VALUE) { FindClose(myStream->item); myIsClosed = true; } } void DirStream::SetEntry() { char * bufPtr; myEntry = DirEntry(myData); myEntry.myPath = myPath + DIR_SEPARATOR + myEntry.myName; } void DirStream::Init() { Initialize(myPath); } void DirStream::Next() { bool retval = FindNextFile(myStream->item,&myData->item); if (! retval) { myStatus = false; } else { SetEntry(); } } DirEntry DirStream::Current() { return myEntry; } bool DirStream::HasMore() { return ! fail(); }