/////////////////////////////////////////////////////////////////////// // wintools.cpp - Win32 API-based helper functions // // ver 1.1 // // Language: Visual C++, ver 7.1 // // 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 // /////////////////////////////////////////////////////////////////////// #include #include #include #include "wintools.h" #include "fileInfo.h" //----< convert from string to wstring >------------------------------- std::wstring Convert::StrToWstr(const std::string& src) { std::wstring dest; for(size_t i=0; i(src[i]); return dest; } //----< convert from wstring to string >------------------------------- std::string Convert::WstrToStr(const std::wstring& src) { std::string dest; for(size_t i=0; i(src[i]); return dest; } // //----< set path to return to on call to RestorePath() >--------------- bool Directory::SetRestorePath(const std::string& path) { if(Directory::DirectoryExists(path)) { RestorePath_ = path; return true; } return false; } //----< does this directory exist? >----------------------------------- bool Directory::DirectoryExists(const std::string& path) { fileInfo fi; if(fi.firstFile(path)) if(fi.isDirectory()) return true; return false; } //----< create directory >--------------------------------------------- bool Directory::CreateDirectory(const std::string& path) { return (::CreateDirectory(path.c_str(),0) != 0); } //----< remove directory >--------------------------------------------- bool Directory::RemoveDirectory(const std::string &path, bool confirm) { return (::RemoveDirectory(path.c_str()) != 0); } //----< get path of current directory >-------------------------------- std::string Directory::GetCurrentDirectory() { const size_t BufSize = 256; char buffer[BufSize]; ::GetCurrentDirectory(BufSize, buffer); return std::string(buffer); } //----< set path of current directory >-------------------------------- bool Directory::SetCurrentDirectory(const std::string& path) { return (::SetCurrentDirectory(path.c_str()) != 0); } // //----< get files >---------------------------------------------------- std::vector Directory::GetFiles() { std::vector files; fileInfo fi(GetCurrentDirectory()); if(fi.firstFile("*.*")) { if(!fi.isDirectory()) files.push_back(fi.name()); while(fi.nextFile()) if(!fi.isDirectory()) files.push_back(fi.name()); } return files; } //----< get directories >---------------------------------------------- std::vector Directory::GetDirectories() { std::vector files; fileInfo fi(GetCurrentDirectory()); if(fi.firstFile("*.*")) { if(fi.isDirectory()) files.push_back(fi.name()); while(fi.nextFile()) if(fi.isDirectory()) files.push_back(fi.name()); } return files; } // //----< convert string to lower case chars >--------------------------- std::string Path::toLower(const std::string& src) { std::string temp; for(size_t i=0; i--------------------------------------- std::string Path::getName(const std::string &fileSpec) { size_t pos = fileSpec.find_last_of("/"); if(pos >= fileSpec.length()) pos = fileSpec.find_last_of("\\"); if(pos >= fileSpec.length()) return toLower(fileSpec); return toLower(fileSpec.substr(pos+1,fileSpec.length()-pos)); } //----< get path from fileSpec >--------------------------------------- std::string Path::getPath(const std::string &fileSpec) { size_t pos = fileSpec.find_last_of("/"); if(pos >= fileSpec.length()) pos = fileSpec.find_last_of("\\"); if(pos >= fileSpec.length()) return "."; return toLower(fileSpec.substr(0,pos+1)); } // //----< throw exception string >--------------------------------------- void SystemError::ThrowString(const char *msg, const char *file, int line) { std::ostringstream collect; collect << msg << ": " << GetLastMsg() << " file: " << file << ", line: " << line; throw collect.str(); } //----< get system error message string >---------------------- std::string SystemError::GetLastMsg() { // ask system what type of error occurred DWORD errorCode = GetLastError(); if(errorCode == 0) return "no error"; // map errorCode into a system defined error string DWORD dwFlags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER; LPCVOID lpSource = NULL; DWORD dwMessageID = errorCode; DWORD dwLanguageId = MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US); LPSTR lpBuffer; DWORD nSize = 0; va_list *Arguments = NULL; FormatMessage( dwFlags,lpSource,dwMessageID,dwLanguageId, (LPTSTR)&lpBuffer,nSize,Arguments ); _msg = lpBuffer; LocalFree(lpBuffer); return _msg; } // #ifdef TEST_WINTOOLS //----< test stub >---------------------------------------------------- #include int main() { std::cout << "\n Testing WinTools "; std::cout << "\n ==================\n"; SystemError se; std::cout << "\n Last error code = " << ::GetLastError(); std::cout << "\n Last error message = " << se.GetLastMsg(); std::cout << std::endl; std::cout << "\n testing conversions "; std::cout << "\n ----------------------"; std::wstring wtest = Convert::StrToWstr("a test string"); std::wcout << L"\n wstring converted from string: " << wtest.c_str(); std::string test = Convert::WstrToStr(wtest); std::cout << "\n string converted from wstring: " << test.c_str(); std::cout << std::endl; // std::cout << "\n testing directory management "; std::cout << "\n ------------------------------"; Directory dir; if(dir.DirectoryExists("test")) { std::cout << "\n Directory test exists"; if(dir.RemoveDirectory("test")) std::cout << "\n successfully removed directory test"; } else { if(dir.CreateDirectory("test")) std::cout << "\n successfully created directory test"; else std::cout << "\n Directory creation failed"; } std::cout << "\n Current directory is: " << dir.GetCurrentDirectory(); if(dir.SetCurrentDirectory("./test")) { std::cout << "\n changed to: " << dir.GetCurrentDirectory(); dir.SetCurrentDirectory(".."); std::cout << "\n changed back to: " << dir.GetCurrentDirectory(); } std::cout << std::endl; std::vector files = Directory::GetFiles(); std::cout << "\n files in this directory are:"; for(size_t i=0; i dirs = Directory::GetDirectories(); std::cout << "\n directories in this directory are:"; for(size_t i=0; i