📄 directory.h
字号:
#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 5/10/99, ported to 32-bit windows//// 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, Windows//// 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// int Size() -- returns size of file (in bytes)// bool IsDir() -- returns false if NOT directory, else true// string Path() -- returns full path to file// 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 <string>using namespace std;const string DIR_SEPARATOR = "\\"; // platform specificclass DirStream; // need forward reference for friendshipclass WIN32_DATA; // defined in .cpp file (avoid parsing huge <windows.h>#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; ClockTime GetTime() const; friend 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 DirEntry(WIN32_DATA* dat); // from platform-specific constructor string myName; // NOT full path, just 'file' name string myPath; // full, canonicalized path Date myDate; // creation date ClockTime myTime; // creation time long mySize; // in bytes bool myIsDirectory; // true if directory, else false};class HHandle; // forward, really a HANDLE, but avoid parsing <windows.h>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: HHandle * myStream; // for Windows WIN32_DATA * myData; // for Windows 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 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -