From: Christian P. <cp...@us...> - 2005-06-07 22:42:27
|
Update of /cvsroot/pclasses/pclasses2/src/System In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14600 Modified Files: Directory.win32.cpp IOHandle.h Added Files: IOHandle.win32.cpp Log Message: - Added win32 implementation of IOHandle and Directory Index: Directory.win32.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/Directory.win32.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- Directory.win32.cpp 7 Jun 2005 12:15:46 -0000 1.2 +++ Directory.win32.cpp 7 Jun 2005 22:42:09 -0000 1.3 @@ -54,7 +54,7 @@ if(lastError == ERROR_NO_MORE_FILES) return; - throw SystemError(lastError, "Could not read directory", P_SOURCEINFO); + throw IO::IOError(lastError, "Could not read directory", P_SOURCEINFO); } _entries.push_back(findData.cFileName); @@ -67,7 +67,7 @@ FindClose(h); if(lastError != ERROR_NO_MORE_FILES) - throw SystemError(lastError, "Could not read directory", P_SOURCEINFO); + throw IO::IOError(lastError, "Could not read directory", P_SOURCEINFO); } Directory::Iterator Directory::begin() const @@ -85,7 +85,7 @@ DWORD reqdSize = GetCurrentDirectory(0, 0); char* cwd = new char[reqdSize + 1]; - DWORD cwdRet = GetCurrentDirectory(cwdSize, cwd); + DWORD cwdRet = GetCurrentDirectory(reqdSize + 1, cwd); if(cwdRet == 0) throw IO::IOError(GetLastError(), "Could not get current working directroy", P_SOURCEINFO); Index: IOHandle.h =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/IOHandle.h,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- IOHandle.h 7 Jun 2005 12:01:19 -0000 1.2 +++ IOHandle.h 7 Jun 2005 22:42:09 -0000 1.3 @@ -25,6 +25,10 @@ #include "pclasses/IO/IOError.h" #include "pclasses/Unicode/String.h" +#ifdef WIN32 +#include <windows.h> +#endif + namespace P { namespace System { --- NEW FILE: IOHandle.win32.cpp --- /*************************************************************************** * Copyright (C) 2005 by Christian Prochnow * * cp...@se... * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU Library General Public License as * * published by the Free Software Foundation; either version 2 of the * * License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU Library General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "IOHandle.h" #include <windows.h> namespace P { namespace System { #define INVALID_HANDLE INVALID_HANDLE_VALUE DWORD OpenMode2Flags(IO::IODevice::OpenMode omode) { DWORD flags = 0; switch(omode) { case IO::IODevice::OpenCreate: flags = OPEN_ALWAYS; break; case IO::IODevice::CreateFail: flags = CREATE_NEW; break; case IO::IODevice::OpenFail: flags = OPEN_EXISTING; break; } return flags; } DWORD ShareMode2Flags(IO::IODevice::ShareMode smode) { DWORD flags = 0; switch(smode) { case IO::IODevice::AllowNone: break; case IO::IODevice::AllowRead: flags = FILE_SHARE_READ; break; case IO::IODevice::AllowWrite: flags = FILE_SHARE_WRITE; break; case IO::IODevice::AllowReadWrite: flags = FILE_SHARE_READ|FILE_SHARE_WRITE; break; } return flags; } DWORD AccessMode2Flags(IO::IODevice::AccessMode amode) { DWORD flags = 0; switch(amode) { case IO::IODevice::None: break; case IO::IODevice::Read: flags = GENERIC_READ; break; case IO::IODevice::Write: flags = GENERIC_WRITE; break; case IO::IODevice::ReadWrite: flags = GENERIC_READ|GENERIC_WRITE; break; } return flags; } IOHandle::IOHandle(const IOHandle& h) throw(IO::IOError) { HANDLE handle; DWORD ret = DuplicateHandle(GetCurrentProcess(), h._handle, GetCurrentProcess(), &handle, DUPLICATE_SAME_ACCESS, TRUE, 0); if(ret == 0) throw IO::IOError(GetLastError(), "Could not duplicate handle", P_SOURCEINFO); _handle = handle; } IOHandle::IOHandle(iohandle_t h, bool takeOwnership) throw(IO::IOError) { if(takeOwnership) { _handle = h; } else { HANDLE handle; DWORD ret = DuplicateHandle(GetCurrentProcess(), h, GetCurrentProcess(), &handle, DUPLICATE_SAME_ACCESS, TRUE, 0); if(ret == 0) throw IO::IOError(GetLastError(), "Could not duplicate handle", P_SOURCEINFO); _handle = handle; } } IOHandle::IOHandle(const Unicode::String& name, IO::IODevice::AccessMode amode, IO::IODevice::OpenMode omode, IO::IODevice::ShareMode smode) throw(IO::IOError) : _handle(INVALID_HANDLE) { DWORD accessMode = AccessMode2Flags(amode); DWORD shareMode = ShareMode2Flags(smode); DWORD createMode = OpenMode2Flags(omode); HANDLE handle = ::CreateFile(name.utf8().c_str(), accessMode, shareMode, NULL, createMode, FILE_ATTRIBUTE_NORMAL, NULL); if(handle == INVALID_HANDLE) throw IO::IOError(GetLastError(), "Could not open handle", P_SOURCEINFO); _handle = handle; } IOHandle::~IOHandle() throw() { if(_handle != INVALID_HANDLE) { try { close(); } catch(...) { } } } void IOHandle::close() throw(IO::IOError) { if(!::CloseHandle(_handle)) throw IO::IOError(GetLastError(), "Could not close handle", P_SOURCEINFO); _handle = INVALID_HANDLE; } size_t IOHandle::read(char* buffer, size_t count) throw(IO::IOError) { DWORD bytesRead = 0; DWORD bytesToRead = count > (DWORD)-1 ? (DWORD)-1 : count; if(!::ReadFile(_handle, (void*)buffer, bytesToRead, &bytesRead, NULL)) throw IO::IOError(GetLastError(), "Could not read from handle", P_SOURCEINFO); return bytesRead; } size_t IOHandle::write(const char* buffer, size_t count) throw(IO::IOError) { DWORD bytesWritten = 0; DWORD bytesToWrite = count > (DWORD)-1 ? (DWORD)-1 : count; if(!::WriteFile(_handle, (const void*)buffer, bytesToWrite, &bytesWritten, NULL)) throw IO::IOError(GetLastError(), "Could not read from handle", P_SOURCEINFO); return bytesWritten; } bool IOHandle::isSeekable() const throw() { DWORD fileType = ::GetFileType(_handle); if(fileType == FILE_TYPE_DISK) return true; return false; } offset_t IOHandle::seek(offset_t offset, IO::IODevice::SeekMode mode) throw(IO::IOError) { DWORD moveMethod; switch(mode) { case IO::IODevice::SeekSet: moveMethod = FILE_BEGIN; break; case IO::IODevice::SeekCurrent: moveMethod = FILE_CURRENT; break; case IO::IODevice::SeekEnd: moveMethod = FILE_END; break; } LARGE_INTEGER moveDist, newPos; moveDist.QuadPart = offset; if(!SetFilePointerEx(_handle, moveDist, &newPos, moveMethod)) throw IO::IOError(GetLastError(), "Could not seek on handle", P_SOURCEINFO); return (offset_t)newPos.QuadPart; } size_t IOHandle::peek(char* buffer, size_t count) throw(IO::IOError) { DWORD bytesRead = 0; DWORD bytesToRead = count > (DWORD)-1 ? (DWORD)-1 : count; if(!::ReadFile(_handle, (void*)buffer, bytesToRead, &bytesRead, NULL)) throw IO::IOError(GetLastError(), "Could not read from handle", P_SOURCEINFO); offset_t seekOff = bytesRead; seek(-seekOff, IO::IODevice::SeekCurrent); return bytesRead; } void IOHandle::sync() const throw(IO::IOError) { if(!::FlushFileBuffers(_handle)) throw IO::IOError(GetLastError(), "Could not sync handle", P_SOURCEINFO); } offset_t IOHandle::size() const throw(IO::IOError) { LARGE_INTEGER fSize; if(!::GetFileSizeEx(_handle, &fSize)) throw IO::IOError(GetLastError(), "Could not stat file", P_SOURCEINFO); return (offset_t)fSize.QuadPart; } void IOHandle::resize(size_t sz) throw(IO::IOError) { if(!::SetFileValidData(_handle, sz)) throw IO::IOError(GetLastError(), "Could not truncate file", P_SOURCEINFO); } iohandle_t IOHandle::handle() const throw() { return _handle; } } // !namespace System } // !namespace P |