/////////////////////////////////////////////////////////////////////// // wintools.cpp - 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 // /////////////////////////////////////////////////////////////////////// #include #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) { std::string temp = GetCurrentDirectory(); if(SetCurrentDirectory(path)) { SetCurrentDirectory(temp); 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(const std::string& pattern) { std::vector files; fileInfo fi(GetCurrentDirectory()); if(fi.firstFile(pattern)) { 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; } // //----< copy files matching from filespec to toPath >---------------- bool Directory::CopyFiles( const std::string& from, const std::string& toPath, bool FailIfExists ) { std::string SrcDir = Path::getFullPath(from); std::string CurrDir = Directory::GetCurrentDirectory(); bool ok = true; if(Directory::SetCurrentDirectory(SrcDir)) { std::vector files = Directory::GetFiles(Path::getName(from)); if(files.size() == 0) ok = false; for(size_t i=0; i--------------------------- std::string Path::toLower(const std::string& src) { std::string temp; for(size_t i=0; i--------------------------- std::string Path::toUpper(const std::string& src) { std::string temp; for(size_t i=0; i-------------------------------- std::wstring toUnicode(const std::string& src) { std::wstring temp; for(size_t i=0; i(src[i]); return temp; } //----< convert from Unicode to ANSII >-------------------------------- std::string toANSII(const std::wstring& src) { std::string temp; for(size_t i=0; i(src[i]); return temp; } //----< get path from fileSpec >--------------------------------------- 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 "."; if(fileSpec.find(".",pos+1)) return toLower(fileSpec.substr(0,pos+1)); return fileSpec; } //----< get absoluth path from fileSpec >------------------------------ std::string Path::getFullPath(const std::string &fileSpec) { const size_t BufSize = 256; char buffer[BufSize]; char filebuffer[BufSize]; // don't use but GetFullPathName will char* name = filebuffer; ::GetFullPathName(fileSpec.c_str(),BufSize, buffer, &name); return std::string(getPath(buffer)); } //----< create file spec from path and name >-------------------------- std::string Path::fileSpec(const std::string &path, const std::string &name) { std::string fs; size_t len = path.size(); if(path[len-1] == '/' || path[len-1] == '\\') fs = path + name; else { if(path.find("/") < path.size()) fs = path + "/" + name; else if(path.find("\\") < path.size()) fs = path + "\\" + name; } return fs; } // //----< 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