#ifndef DIRS_H #define DIRS_H /////////////////////////////////////////////////////////////// // dirs.h - File and Directory Operations // // ver 1.0 // // // // Language: Visual C++, ver 6.0 // // Platform: Micron Dual Pentium Pro 200, WinNT 4.0 // // Application: CSE691 demo // // Author: Jim Fawcett, CST 2-187, Syracuse Univ // // (315) 443-3948, fawcett@ecs.syr.edu // /////////////////////////////////////////////////////////////// /* Module Operations: ================== This module defines two classes: class directory and class file. - The directory class creates directories, and converts a relative path name to a fully qualified path name. Other operations are planned for this class. - The file class: - finds all files matching a pattern - extracts time, date, size, and attribute information for each file found - creates or opens files for reading, or writing, or both - accepts Read/Write or Creation attributes to qualify the create operation - returns handle from create or open operations; Directory Public Interface: =========================== directory dir; // create directory object string path = dir.fullPathName("."); // get fully qualified path name dir.createDir(".\temp"); // create a directory if(dir.error()) .... // check error in last operation cout << dir.errorType(); // check last error type dir.clear(); // clear last error // also cleared by next valid op. // File Public Interface: ====================== file fl; // create file object file fl(".\temp\foo.cpp"); // create named file object fl.find("..\*.cpp"); // find next matching file fl.closeFind(); // get ready for new search HANDLE hFile = fl.fileHandle(); // return handle from create cout << fl.path(); // insert path to file cout << fl.name(); // insert file name cout << fl.date(); // insert file last modify date cout << fl.time(); // insert file local time cout << fl.size(); // insert size in bytes cout << fl.getAttribs(); // insert list of attributes cout << fl.getFileInfo(); // insert file date,time,size, if(fl.createFile()) ... // attempt to open current file if(fl.createFile(".\temp\foo.cpp")) // attempt to create or open ... fl.setCRattrib(OPEN_EXISTING); // set create attributes // CREATE_NEW, CREATE_ALWAYS, // OPEN_EXISTING fl.setRWattrib(GENERIC_READ); // set read/write attributes // GENERIC_READ, GENERIC_WRITE // FILE_SHARE_DELETE, // FILE_SHARE_READ // FILE_SHARE_WRITE // size, file/dir attribute if(fl.error()) ... // check error in last operation cout << fl.errorType(); // insert error type message fl.clear(); // clear last error Note: ===== This class is not particularly easy to use - the public interface is large and complex. It should be possible to preserve the functionality provided here while making the interface simpler. That, however, is a task for some later time. */ // /////////////////////////////////////////////////////////////// // build process // /////////////////////////////////////////////////////////////// // required files: // // dir.h, dir.cpp // // // // compiler command: // // cl /GX /DTEST_DIR dir.cpp // /////////////////////////////////////////////////////////////// /* Maintenance History: ==================== ver 1.0 : 27 Nov 99 - first release */ #include #include class directory { bool _error; std::string _type; public: directory(); bool error(); void clear(); std::string errorType(); std::string fullPathName(const std::string &name); bool createDir(const std::string &dirName); }; inline directory::directory() : _error(false), _type("") { } inline bool directory::error() { return _error; } inline std::string directory::errorType() { return _type; } inline void directory::clear() { _error=false; _type = ""; } // class file { WIN32_FIND_DATA _findData; bool _finding; bool _found; bool _error; std::string _type; std::string _path; std::string _name; std::string _date; std::string _time; long int _size; DWORD _attrib; DWORD _RWattrib; DWORD _SHattrib; DWORD _CRattrib; LPSECURITY_ATTRIBUTES _pSecurity; HANDLE _hFile; HANDLE _search; void _getName(); void _getAttrib(); void _getSize(); void _getDate(); void _getTime(); void _validOp(); void _invalidOp(const std::string &type); public: file(const std::string &name=""); virtual ~file(); void setPath(const std::string &path); bool find(const std::string &pattern); void closeFind(); long int size(); std::string stripPath(const std::string &fullPath); std::string path(); std::string name(); std::string date(); std::string time(); std::string getAttribs(); std::string getFileInfo(); HANDLE fileHandle(); bool createFile(const std::string &name=""); bool setRWAttrib(DWORD attrib); bool setCRAttrib(DWORD attrib); bool isDir(); bool isFile(); bool error(); std::string& errorType(); void clear(); }; // inline file::~file() { ::FindClose(_search); ::CloseHandle(_hFile); } inline bool file::error() { return _error; } inline std::string& file::errorType() { return _type; } inline void file::clear() { _error = false; } inline void file::setPath(const std::string &path) { _path = path; } inline std::string file::path() { return _path; } inline void file::closeFind() { ::FindClose(_search); _finding = false; } inline HANDLE file::fileHandle() { return _hFile; } inline void file::_validOp() { _error = false; _type = ""; } inline void file::_invalidOp(const std::string &type) { _error = true; _type = type; } #endif