///////////////////////////////////////////////////////////////// // sysUtils.H - utility classes to support Win32 API // // ver 1.2 // // Language: Visual C++, ver 6.0, SP 5 // // Platform: Dell Dimension 8100, Win 2000 Pro, SP 2 // // Application: CSE691/791 - SWM&A Demonstration // // Author: Jim Fawcett, CST 2-187, Syracuse Univ // // (315) 443-3948, jfawcett@twcny.rr.com // ///////////////////////////////////////////////////////////////// #include #include "sysUtils.h" using namespace std; //----< get system error message string >---------------------- string sysError::getLastMsg() { // ask system what type of error occurred DWORD errorCode = GetLastError(); // 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; } // //----< get path where this process was spawned >-------------- string procInfo::getSpawnPath() { const int bufferSize = 256; char nameBuffer[bufferSize]; GetModuleFileName(NULL,nameBuffer,bufferSize); string temp = nameBuffer; int pos = temp.find_last_of('\\'); temp.resize(pos+1); return temp; } //---< get name of file from which this process was spawned >-- string procInfo::getFileName() { const int bufferSize = 256; char nameBuffer[bufferSize]; GetModuleFileName(NULL,nameBuffer,bufferSize); string temp = nameBuffer; int pos = temp.find_last_of('\\'); temp.erase(temp.begin(), temp.begin()+pos+1); return temp; } // //----< time constructor >------------------------------------- time::time() { FILETIME UTCfileTime; GetSystemTimeAsFileTime(&UTCfileTime); FileTimeToLocalFileTime(&UTCfileTime,&_fileTime); FileTimeToSystemTime(&_fileTime,&_sysTime); } //----< retrieve date >---------------------------------------- std::string time::getDate() { std::ostringstream convert; convert.fill('0'); convert << setw(2) << _sysTime.wMonth << '/' << setw(2) << _sysTime.wDay << '/' << setw(4) << _sysTime.wYear; return convert.str(); } //----< retrieve time >---------------------------------------- std::string time::getTime() { std::ostringstream convert; convert.fill('0'); convert << setw(2) << _sysTime.wHour << ':' << setw(2) << _sysTime.wMinute << ':' << setw(2) << _sysTime.wSecond; return convert.str(); } //----< retrieve data and time string >------------------------ std::string time::getDateTime() { std::string ret = getDate() += ", "; return (ret += getTime()); } // //----< make unique identifier >------------------------------- uid::uid(const std::string &compName, const std::string &author) { _type = compName; _type += '@'; _type += author; time t; _date += '@'; _date += t.getDate(); _date += '@'; _date += t.getTime(); } //----< return ASCII string >---------------------------------- std::string uid::id() { return (_type + _date); } //----< return ASCII string >---------------------------------- std::string uid::type() { return _type; } // //----< file constructor >------------------------------------- file::file() { } //----< open file >-------------------------------------------- bool file::open(const char *fileName, file::fileType type) { DWORD access, share, dispose, attrib; switch(type) { case file::Read : access = GENERIC_READ; share = FILE_SHARE_READ; dispose = OPEN_EXISTING; attrib = FILE_FLAG_SEQUENTIAL_SCAN; break; case file::Write : access = GENERIC_WRITE; share = 0; dispose = CREATE_ALWAYS; attrib = FILE_FLAG_SEQUENTIAL_SCAN; break; default : return false; }; _fileHandle = CreateFile(fileName, access, share, NULL, dispose, attrib, NULL); return (_fileHandle != INVALID_HANDLE_VALUE); } //----< open file connected to stdout >------------------------ bool file::openStdOut() { _fileHandle = GetStdHandle(STD_OUTPUT_HANDLE); return (_fileHandle != INVALID_HANDLE_VALUE); } //----< close file >------------------------------------------- void file::close() { CloseHandle(_fileHandle); } // //----< read a block of bytes >-------------------------------- #pragma warning(disable : 4800) // cast to bool ok here unsigned long int file::read(char *buffer, unsigned long int bufferSize) { if(_fileHandle == INVALID_HANDLE_VALUE) return 0; unsigned long int numRead; bool readOK = (bool)ReadFile(_fileHandle, buffer, bufferSize, &numRead, NULL); if(readOK) return numRead; return 0; } //----< write a block of bytes >------------------------------- #pragma warning(disable : 4800) // cast to bool ok here unsigned long int file::write(char *buffer, unsigned long int bufferSize) { if(_fileHandle == INVALID_HANDLE_VALUE) return 0; unsigned long int numWritten; bool writeOK = WriteFile(_fileHandle, buffer, bufferSize, &numWritten, NULL); if(writeOK) return numWritten; return 0; } // //----< test stub >-------------------------------------------- #ifdef TEST_SYSUTILS #include #include void main() { cout << "\n Demonstrating sysUtils " << "\n ========================\n"; cout << "\n testing sysError message retrieval " << "\n ------------------------------------\n"; sysError myErr; cout << "\n last error message = " << myErr.getLastMsg(); ifstream in("doesNotExist"); if(!in.good()) cout << "\n last error message = " << myErr.getLastMsg() << "\n\n"; cout << "\n testing procInfo - my path and name evaluator " << "\n -----------------------------------------------\n"; procInfo pi; cout << "\n file path = " << pi.getSpawnPath() << endl; cout << "\n file name = " << pi.getFileName() << "\n\n\n"; cout << "\n testing file reads and writes " << "\n -------------------------------\n"; file outfile; outfile.openStdOut(); file infile; if(infile.open("..\\sysUtils.h",file::Read)) { int len = 1; while(len > 0) { const int bufSize = 64; char buffer[bufSize]; len = infile.read(buffer,bufSize); if(len > 0) outfile.write(buffer,len); } } cout << endl; // cout << "\n testing TtoStr and StrToT converts " << "\n ------------------------------------\n"; TtoStr dblStr(3.1415927); cout << "\n string representation of PI is " << dblStr.convert(); StrToT strDbl(dblStr.convert()); cout << "\n double retrieved from string is " << strDbl.convert(); // Using temporary objects we can convert from a primitive type to // a string and vice versa in a single expression: cout << "\n string representation of int(-17) is " << TtoStr(-17).convert(); cout << "\n integer retrieved from string is " << StrToT("-17").convert(); // This looks a little weird, but, if you think about it for awhile // the syntax actually makes sense. At least the author thinks so. cout << "\n\n"; cout << "\n testing time module " << "\n ---------------------\n"; time t; cout << "\n current date = " << t.getDate(); cout << "\n current time = " << t.getTime(); cout << "\n current datetime = " << t.getDateTime(); cout << "\n\n"; cout << "\n testing id maker " << "\n ------------------\n"; uid compid("sysUtil","Fawcett"); cout << "\n uid = " << compid.id(); cout << "\n type = " << compid.type(); cout << "\n\n"; } #endif