#ifndef WINTOOLS_H #define WINTOOLS_H /////////////////////////////////////////////////////////////////////// // wintools.h - Win32 API-based helper functions // // ver 2.0 // // Language: Visual C++, ver 8.0 // // Platform: Dell Dimension 9150, Windows XP Pro, SP2 // // Application: CSE687 Project #1, Spring 2006 // // Author: Jim Fawcett, Syracuse University CST 2-187, // // (315) 443-3948, jfawcett@twcny.rr.com // /////////////////////////////////////////////////////////////////////// /* Module WinTools Operations: --------------------------- This module defines three classes: - Directory supports directory management. Most functions in this class are static, and so may be called using either an instance name or the Directory class name. - Convert supports conversion between basic types. Now limited to conversions between std::string and std::wstring. All members are static. - SystemError provides system error reporting. The most important member is getLastMsg(). Public Interface: ----------------- Directory dir; dir.SetRestorePath(".."); dir.RestorePath(); bool ok = Directory::DirectoryExists(path); bool ok = Directory::CreateDirectory(path); bool ok = Directory::RemoveDirectory(path); bool ok = Directory::SetCurrentDirectory(path); std::string path = Directory::GetCurrentDirectory(); std::vector files = Directory::GetFiles("*.cpp"); std::vector dirs = Directory::GetDirectories(); std::string name = Path::getName(fileSpec); std::string path = Path::getPath(fileSpec); std::wstring wtest = Convert::strToWstr("a test string"); std::string test = Convert::wStrToStr(wtest); SystemError syserr; std::cout << "\n last error code = " << syserr.GetLastError(); std::cout << "\n last error = " << syserr.GetLastMsg(); */ // /////////////////////////////////////////////////////////////// // maintenance page // /////////////////////////////////////////////////////////////// // Build Process // // // // Files Required: // // wintools.h, wintools.cpp, fileInfo.h, fileInfo.cpp // // // // Building with Visual C++ , 8.0, from command line: // // cl -EHsc -DTEST_WINTOOLS wintools.cpp fileInfo.cpp // // // /////////////////////////////////////////////////////////////// /* Maintenance History =================== ver 2.0 : 28 Feb 06 - added Path members: fileSpec, getFullPath, toANSII, and toUnicode - added Directory member: CopyFiles - modified processing in getPath to take care of a special case ver 1.3 : 26 Feb 06 - fixed bug in Directory::DirectoryExists(const std::string& path); - added Path::toUpper utility function ver 1.2 : 21 Feb 06 - modified WinTools::GetFiles to accept a pattern to select specific files from the current directory ver 1.1 : 06 Feb 06 - added Path class ver 1.0 : 05 Feb 06 - first release */ // #include #include #include /////////////////////////////////////////////////////////////// // class SystemError - handle system errors class SystemError { public: SystemError(int n=0); ~SystemError() {} int GetLastError(); std::string GetLastMsg(); void ThrowString(const char *msg, const char *file, int line); private: std::string _msg; }; inline SystemError::SystemError(int n) { ::SetLastError(n); } inline int SystemError::GetLastError() { return ::GetLastError(); } /////////////////////////////////////////////////////////////// // class Convert - convert between basic types class Convert { public: static std::wstring StrToWstr(const std::string& src); static std::string WstrToStr(const std::wstring& src); }; /////////////////////////////////////////////////////////////// // class Directory - manage directory operations class Directory { public: Directory(); ~Directory() { } bool SetRestorePath(const std::string& RestorePath); bool RestoreFirstDirectory(); static bool DirectoryExists(const std::string& path); static bool CreateDirectory(const std::string& path); static bool RemoveDirectory(const std::string& path, bool confirm=true); static std::string GetCurrentDirectory(); static bool SetCurrentDirectory(const std::string& path); static std::vector GetFiles(const std::string& pattern="*.*"); static std::vector GetDirectories(); static bool CopyFiles( const std::string& from, const std::string& toPath, bool FailIfExits = true ); private: std::string RestorePath_; }; inline Directory::Directory() : RestorePath_(Directory::GetCurrentDirectory()) {} inline bool Directory::RestoreFirstDirectory() { return SetCurrentDirectory(RestorePath_); }; class Path { public: static std::string getPath(const std::string& fileSpec); static std::string getFullPath(const std::string& fileSpec); static std::string getName(const std::string& fileSpec); static std::string fileSpec(const std::string& path, const std::string& name); static std::string toLower(const std::string& src); static std::string toUpper(const std::string& src); static std::wstring toUnicode(const std::string& src); static std::string toANSII(const std::wstring& src); }; #endif