From: Christian P. <cp...@us...> - 2005-01-30 17:07:20
|
Update of /cvsroot/pclasses/pclasses2/src/System In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30544/src/System Modified Files: File.posix.cpp Pipe.posix.cpp Added Files: File.common.cpp File.win32.cpp IOHandle.h IOHandle.posix.cpp Pipe.common.cpp Log Message: IOHandle: Added internal I/O handle class. File, Pipe: ported to IOHandle. --- NEW FILE: File.common.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 "pclasses/System/File.h" #include "pclasses/System/FileInfo.h" #include "IOHandle.h" namespace P { namespace System { File::File() throw() : _handle(0) { } File::File(const File& f) throw(IO::IOError) : IODevice(f), _handle(0) { if(f.valid()) { IOHandle* h = new IOHandle(f.handle(), false); _handle = (unsigned long)h; } } File::File(const Unicode::String& name, AccessMode amode, OpenMode omode, ShareMode smode) throw(IO::IOError) : _handle(0) { open(name,amode,omode,smode); } File::~File() throw() { if(valid()) { try { close(); } catch(...) { } } } void File::open(const Unicode::String& name, AccessMode amode, OpenMode omode, ShareMode smode) throw(IO::IOError) { if(valid()) close(); IOHandle* h = new IOHandle(name, amode, omode, smode); _handle = (unsigned long)h; IODevice::setAccess(amode); IODevice::setValid(true); IODevice::setEof(false); } void File::_close() throw(IO::IOError) { IOHandle* h = (IOHandle*)_handle; h->close(); delete h; _handle = 0; IODevice::setAccess(None); IODevice::setValid(false); } size_t File::_read(char* buffer, size_t count) throw(IO::IOError) { IOHandle* h = (IOHandle*)_handle; size_t ret = h->read(buffer, count); if(ret == 0 && !eof()) setEof(true); return ret; } size_t File::_peek(char* buffer, size_t count) throw(IO::IOError) { IOHandle* h = (IOHandle*)_handle; return h->peek(buffer, count); } size_t File::_write(const char* buffer, size_t count) throw(IO::IOError) { IOHandle* h = (IOHandle*)_handle; return h->write(buffer, count); } offset_t File::_seek(offset_t offset, SeekMode mode) throw(IO::IOError) { IOHandle* h = (IOHandle*)_handle; return h->seek(offset, mode); } bool File::_isSeekable() const throw() { IOHandle* h = (IOHandle*)_handle; return h->isSeekable(); } void File::_sync() const throw(IO::IOError) { IOHandle* h = (IOHandle*)_handle; h->sync(); } offset_t File::_size() const throw(IO::IOError) { IOHandle* h = (IOHandle*)_handle; return h->size(); } void File::resize(size_t sz) throw(IO::IOError) { sync(); IOHandle* h = (IOHandle*)_handle; h->resize(sz); } File& File::operator=(const File& f) throw(IO::IOError) { // strong exception guarantee... if(f.valid()) { IOHandle* h = new IOHandle(f.handle(), false); if(valid()) { try { close(); } catch(...) { delete h; throw; } } IODevice::operator=(f); _handle = (unsigned long)h; } else { if(valid()) close(); setValid(false); setEof(true); setAccess(None); } return *this; } FileInfo File::stat(const Unicode::String& path) throw(IO::IOError) { return FileInfo(path); } unsigned long File::handle() const throw() { return ((IOHandle*)_handle)->handle(); } } // !namespace System } // !namespace P Index: File.posix.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/File.posix.cpp,v retrieving revision 1.12 retrieving revision 1.13 diff -u -d -r1.12 -r1.13 --- File.posix.cpp 6 Jan 2005 17:23:07 -0000 1.12 +++ File.posix.cpp 30 Jan 2005 17:07:03 -0000 1.13 @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2004 by Christian Prochnow * + * Copyright (C) 2004,2005 by Christian Prochnow * * cp...@se... * * * * This program is free software; you can redistribute it and/or modify * @@ -19,288 +19,12 @@ ***************************************************************************/ #include "pclasses/System/File.h" -#include "pclasses/System/FileInfo.h" - -#include <sys/types.h> -#include <sys/stat.h> -#include <limits.h> -#include <fcntl.h> -#include <unistd.h> #include <errno.h> namespace P { namespace System { -int OpenMode2Flags(File::OpenMode omode) -{ - int flags = 0; - switch(omode) - { - case File::OpenCreate: - flags |= O_CREAT; - break; - - case File::CreateFail: - flags |= O_CREAT|O_EXCL; - break; - - case File::OpenFail: - break; - } - - return flags; -} - -int AccessMode2Flags(File::AccessMode amode) -{ - int flags = 0; - - switch(amode) - { - case File::None: - break; - - case File::Read: - flags |= O_RDONLY; - break; - - case File::Write: - flags |= O_WRONLY; - break; - - case File::ReadWrite: - flags |= O_RDWR; - break; - } - - return flags; -} - -File::File() throw() -: _handle((unsigned long)-1) -{ -} - -File::File(const File& f) throw(IO::IOError) -: IODevice(f), _handle((unsigned long)-1) -{ - if(f.valid()) - { - int handle = ::dup((int)f._handle); - if(handle == -1) - throw IO::IOError(errno, "Could not duplicate file handle", P_SOURCEINFO); - - _handle = (unsigned long)handle; - } -} - -File::File(const Unicode::String& name, AccessMode amode, OpenMode omode, - ShareMode smode) throw(IO::IOError) -: _handle((unsigned long)-1) -{ - open(name,amode,omode,smode); -} - -File::~File() throw() -{ - if(valid()) - { - try { close(); } catch(...) { } - } -} - -void File::open(const Unicode::String& name, AccessMode amode, OpenMode omode, - ShareMode smode) throw(IO::IOError) -{ - if(valid()) - close(); - - int flags = OpenMode2Flags(omode) | AccessMode2Flags(amode); - - int handle = ::open(name.utf8().c_str(), flags, 0644); - if(handle == -1) - throw IO::IOError(errno, "Could not open file", P_SOURCEINFO); - - _handle = (unsigned long)handle; - IODevice::setAccess(amode); - IODevice::setValid(true); - IODevice::setEof(false); -} - -void File::_close() throw(IO::IOError) -{ - if(::close((int)_handle) == -1) - throw IO::IOError(errno, "Could not close file", P_SOURCEINFO); - - _handle = (unsigned long)-1; - IODevice::setAccess(None); - IODevice::setValid(false); -} - -size_t File::_read(char* buffer, size_t count) throw(IO::IOError) -{ - if(count > SSIZE_MAX) - count = SSIZE_MAX; - - File_read: - ssize_t ret = ::read((int)_handle, (void*)buffer, count); - if(ret == -1) - { - if(errno == EINTR) - goto File_read; - - throw IO::IOError(errno, "Could not read from file", P_SOURCEINFO); - } - else if(ret == 0 && !eof()) - setEof(true); - - return ret; -} - -size_t File::_peek(char* buffer, size_t count) throw(IO::IOError) -{ - if(count > SSIZE_MAX) - count = SSIZE_MAX; - - File_peek: - ssize_t ret = ::read((int)_handle, (void*)buffer, count); - if(ret == -1) - { - if(errno == EINTR) - goto File_peek; - - throw IO::IOError(errno, "Could not read from file", P_SOURCEINFO); - } - - seek(-count, SeekCurrent); - return ret; -} - -size_t File::_write(const char* buffer, size_t count) throw(IO::IOError) -{ - if(count > SSIZE_MAX) - count = SSIZE_MAX; - - File_write: - ssize_t ret = ::write((int)_handle, (const void*)buffer, count); - if(ret == -1) - { - if(errno == EINTR) - goto File_write; - - throw IO::IOError(errno, "Could not read from file", P_SOURCEINFO); - } - - return ret; -} - -offset_t File::_seek(offset_t offset, SeekMode mode) throw(IO::IOError) -{ - int whence; - switch(mode) - { - case SeekSet: - whence = SEEK_SET; - break; - - case SeekCurrent: - whence = SEEK_CUR; - break; - - case SeekEnd: - whence = SEEK_END; - break; - } - - off_t ret = lseek((int)_handle, offset, whence); - if(ret == (off_t)-1) - throw IO::IOError(errno, "Could not seek on file", P_SOURCEINFO); - - return ret; -} - -bool File::_isSeekable() const throw() -{ - struct stat buff; - int ret = fstat((int)_handle, &buff); - if(ret == 0) - { - if(S_ISREG(buff.st_mode) || S_ISBLK(buff.st_mode)) - return true; - } - - return false; -} - -void File::_sync() const throw(IO::IOError) -{ - int ret = fsync((int)_handle); - if(ret == -1) - throw IO::IOError(errno, "Could not sync file to disk", P_SOURCEINFO); -} - -offset_t File::_size() const throw(IO::IOError) -{ - struct stat buff; - int ret = fstat((int)_handle, &buff); - if(ret == -1) - throw IO::IOError(errno, "Could not stat file", P_SOURCEINFO); - - return buff.st_size; -} - -void File::resize(size_t sz) throw(IO::IOError) -{ - sync(); - - int ret = ftruncate((int)_handle, sz); - if(ret == -1) - throw IO::IOError(errno, "Could not truncate file", P_SOURCEINFO); -} - -File& File::operator=(const File& f) throw(IO::IOError) -{ - if(f.valid()) - { - int handle = ::dup((int)f._handle); - if(handle == -1) - throw IO::IOError(errno, "Could not duplicate file handle", P_SOURCEINFO); - - if(valid()) - { - try - { - close(); - } - catch(...) - { - ::close(handle); - throw; - } - } - - IODevice::operator=(f); - _handle = (unsigned long)handle; - } - else - { - if(valid()) - close(); - - setValid(false); - setEof(true); - setAccess(None); - } - - return *this; -} - -FileInfo File::stat(const Unicode::String& path) throw(IO::IOError) -{ - return FileInfo(path); -} - void File::unlink(const Unicode::String& path) throw(IO::IOError) { if(::unlink(path.utf8().c_str()) == -1) --- NEW FILE: IOHandle.h --- /*************************************************************************** * 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. * ***************************************************************************/ #ifndef P_System_IOHandle_h #define P_System_IOHandle_h #include "pclasses/IO/IODevice.h" #include "pclasses/IO/IOError.h" #include "pclasses/Unicode/String.h" namespace P { namespace System { //! Internal System I/O handle class IOHandle { public: IOHandle(const IOHandle& h) throw(IO::IOError); IOHandle(unsigned long handle, bool takeOwnership) throw(IO::IOError); IOHandle(const Unicode::String& name, IO::IODevice::AccessMode amode, IO::IODevice::OpenMode omode = IO::IODevice::OpenCreate, IO::IODevice::ShareMode smode = IO::IODevice::AllowNone) throw(IO::IOError); ~IOHandle() throw(); void close() throw(IO::IOError); size_t read(char* buffer, size_t count) throw(IO::IOError); size_t write(const char* buffer, size_t count) throw(IO::IOError); bool isSeekable() const throw(); offset_t seek(offset_t offset, IO::IODevice::SeekMode mode) throw(IO::IOError); size_t peek(char* buffer, size_t count) throw(IO::IOError); void sync() const throw(IO::IOError); offset_t size() const throw(IO::IOError); void resize(size_t sz) throw(IO::IOError); //! Returns the native O/S handle unsigned long handle() const throw(); private: unsigned long _handle; }; } // !namespace System } // !namespace P #endif --- NEW FILE: File.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 "pclasses/System/File.h" namespace P { namespace System { void File::unlink(const Unicode::String& path) throw(IO::IOError) { if(::DeleteFile(path.utf8().c_str()) == -1) throw IO::IOError(errno, "Could not unlink file", P_SOURCEINFO); } } // !namespace System } // !namespace P --- NEW FILE: Pipe.common.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 "pclasses/System/Pipe.h" #include "IOHandle.h" namespace P { namespace System { Pipe::Pipe(unsigned long handle, bool readEnd) throw() : IODevice(), _handle(0) { // takes ownership of the handle ... IOHandle* h = new IOHandle(handle, true); _handle = (unsigned long)h; IODevice::setAccess(readEnd ? Read : Write); IODevice::setValid(true); IODevice::setEof(false); } Pipe::Pipe(const Pipe& p) throw(IO::IOError) : IODevice(p), _handle(0) { if(p.valid()) { // duplicates the handle ... IOHandle* h = new IOHandle(p.handle(), false); _handle = (unsigned long)h; } } Pipe::~Pipe() throw() { if(valid()) { try { close(); } catch(...) { } } } void Pipe::_close() throw(IO::IOError) { IOHandle* h = (IOHandle*)_handle; h->close(); delete h; _handle = 0; IODevice::setAccess(None); IODevice::setValid(false); } size_t Pipe::_read(char* buffer, size_t count) throw(IO::IOError) { IOHandle* h = (IOHandle*)_handle; return h->read(buffer, count); } size_t Pipe::_write(const char* buffer, size_t count) throw(IO::IOError) { IOHandle* h = (IOHandle*)_handle; return h->write(buffer, count); } void Pipe::_sync() const throw(IO::IOError) { /* pipes cant be synced !! int ret = fsync((int)_handle); if(ret == -1) throw IO::IOError(errno, "Could not commit to pipe", P_SOURCEINFO); */ } Pipe& Pipe::operator=(const Pipe& p) throw(IO::IOError) { if(p.valid()) { IOHandle* h = new IOHandle(p.handle(), false); if(valid()) { try { close(); } catch(...) { delete h; throw; } } IODevice::operator=(p); _handle = (unsigned long)h; } else { if(valid()) close(); setValid(false); setEof(true); setAccess(None); } return *this; } unsigned long Pipe::handle() const throw() { return ((IOHandle*)_handle)->handle(); } } // !namespace System } // !namespace P --- NEW FILE: IOHandle.posix.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 <sys/types.h> #include <sys/stat.h> #include <limits.h> #include <fcntl.h> #include <unistd.h> #include <errno.h> namespace P { namespace System { int OpenMode2Flags(IO::IODevice::OpenMode omode) { int flags = 0; switch(omode) { case IO::IODevice::OpenCreate: flags |= O_CREAT; break; case IO::IODevice::CreateFail: flags |= O_CREAT|O_EXCL; break; case IO::IODevice::OpenFail: break; } return flags; } int AccessMode2Flags(IO::IODevice::AccessMode amode) { int flags = 0; switch(amode) { case IO::IODevice::None: break; case IO::IODevice::Read: flags |= O_RDONLY; break; case IO::IODevice::Write: flags |= O_WRONLY; break; case IO::IODevice::ReadWrite: flags |= O_RDWR; break; } return flags; } IOHandle::IOHandle(const IOHandle& h) throw(IO::IOError) { int handle = ::dup((int)h._handle); if(handle == -1) throw IO::IOError(errno, "Could not duplicate handle", P_SOURCEINFO); _handle = (unsigned long)handle; } IOHandle::IOHandle(unsigned long h, bool takeOwnership) throw(IO::IOError) { if(takeOwnership) { _handle = h; } else { int handle = ::dup((int)h); if(handle == -1) throw IO::IOError(errno, "Could not duplicate handle", P_SOURCEINFO); _handle = (unsigned long)handle; } } IOHandle::IOHandle(const Unicode::String& name, IO::IODevice::AccessMode amode, IO::IODevice::OpenMode omode, IO::IODevice::ShareMode smode) throw(IO::IOError) : _handle((unsigned long)-1) { int flags = OpenMode2Flags(omode) | AccessMode2Flags(amode); int handle; if(omode == IO::IODevice::OpenCreate || omode == IO::IODevice::CreateFail) handle = ::open(name.utf8().c_str(), flags, 0644); else handle = ::open(name.utf8().c_str(), flags); if(handle == -1) throw IO::IOError(errno, "Could not open handle", P_SOURCEINFO); _handle = (unsigned long)handle; } IOHandle::~IOHandle() throw() { if((int)_handle != -1) { try { close(); } catch(...) { } } } void IOHandle::close() throw(IO::IOError) { if(::close((int)_handle) == -1) throw IO::IOError(errno, "Could not close handle", P_SOURCEINFO); _handle = (unsigned long)-1; } size_t IOHandle::read(char* buffer, size_t count) throw(IO::IOError) { if(count > SSIZE_MAX) count = SSIZE_MAX; Handle_read: ssize_t ret = ::read((int)_handle, (void*)buffer, count); if(ret == -1) { if(errno == EINTR) goto Handle_read; throw IO::IOError(errno, "Could not read from handle", P_SOURCEINFO); } return ret; } size_t IOHandle::write(const char* buffer, size_t count) throw(IO::IOError) { if(count > SSIZE_MAX) count = SSIZE_MAX; Handle_write: ssize_t ret = ::write((int)_handle, (const void*)buffer, count); if(ret == -1) { if(errno == EINTR) goto Handle_write; throw IO::IOError(errno, "Could not read from handle", P_SOURCEINFO); } return ret; } bool IOHandle::isSeekable() const throw() { struct stat buff; int ret = fstat((int)_handle, &buff); if(ret == 0) { if(S_ISREG(buff.st_mode) || S_ISBLK(buff.st_mode)) return true; } return false; } offset_t IOHandle::seek(offset_t offset, IO::IODevice::SeekMode mode) throw(IO::IOError) { int whence; switch(mode) { case IO::IODevice::SeekSet: whence = SEEK_SET; break; case IO::IODevice::SeekCurrent: whence = SEEK_CUR; break; case IO::IODevice::SeekEnd: whence = SEEK_END; break; } off_t ret = lseek((int)_handle, offset, whence); if(ret == (off_t)-1) throw IO::IOError(errno, "Could not seek on handle", P_SOURCEINFO); return ret; } size_t IOHandle::peek(char* buffer, size_t count) throw(IO::IOError) { if(count > SSIZE_MAX) count = SSIZE_MAX; Handle_peek: ssize_t ret = ::read((int)_handle, (void*)buffer, count); if(ret == -1) { if(errno == EINTR) goto Handle_peek; throw IO::IOError(errno, "Could not read from handle", P_SOURCEINFO); } seek(-count, IO::IODevice::SeekCurrent); return ret; } void IOHandle::sync() const throw(IO::IOError) { int ret = fsync((int)_handle); if(ret == -1) throw IO::IOError(errno, "Could not sync handle", P_SOURCEINFO); } offset_t IOHandle::size() const throw(IO::IOError) { struct stat buff; int ret = fstat((int)_handle, &buff); if(ret == -1) throw IO::IOError(errno, "Could not stat file", P_SOURCEINFO); return buff.st_size; } void IOHandle::resize(size_t sz) throw(IO::IOError) { int ret = ftruncate((int)_handle, sz); if(ret == -1) throw IO::IOError(errno, "Could not truncate file", P_SOURCEINFO); } unsigned long IOHandle::handle() const throw() { return _handle; } } // !namespace System } // !namespace P Index: Pipe.posix.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/Pipe.posix.cpp,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- Pipe.posix.cpp 3 Jan 2005 13:50:45 -0000 1.8 +++ Pipe.posix.cpp 30 Jan 2005 17:07:03 -0000 1.9 @@ -19,10 +19,8 @@ ***************************************************************************/ #include "pclasses/System/Pipe.h" +#include "IOHandle.h" -#include <sys/types.h> -#include <sys/stat.h> -#include <limits.h> #include <unistd.h> #include <errno.h> @@ -30,40 +28,11 @@ namespace System { -Pipe::Pipe(unsigned long h, bool readEnd) throw() -: IODevice(), _handle(h) -{ - IODevice::setAccess(readEnd ? Read : Write); - IODevice::setValid(true); - IODevice::setEof(false); -} - -Pipe::Pipe(const Pipe& p) throw(IO::IOError) -: IODevice(p), _handle((unsigned long)-1) -{ - if(p.valid()) - { - int handle = ::dup((int)p._handle); - if(handle == -1) - throw IO::IOError(errno, "Could not duplicate pipe handle", P_SOURCEINFO); - - _handle = (unsigned long)handle; - } -} - -Pipe::~Pipe() throw() -{ - if(valid()) - { - try { close(); } catch(...) { } - } -} - Pipe::Pair Pipe::create() throw(IO::IOError) { int fds[2]; - if(pipe(fds) == -1) + if(::pipe(fds) == -1) throw IO::IOError(errno, "Could not create pipe", P_SOURCEINFO); Pipe readPipe((unsigned long)fds[0], true); @@ -72,100 +41,6 @@ return std::make_pair(readPipe, writePipe); } -void Pipe::_close() throw(IO::IOError) -{ - if(::close((int)_handle) == -1) - throw IO::IOError(errno, "Could not close pipe", P_SOURCEINFO); - - _handle = (unsigned long)-1; - IODevice::setAccess(None); - IODevice::setValid(false); -} - -size_t Pipe::_read(char* buffer, size_t count) throw(IO::IOError) -{ - if(count > SSIZE_MAX) - count = SSIZE_MAX; - - Pipe_read: - ssize_t ret = ::read((int)_handle, (void*)buffer, count); - if(ret == -1) - { - if(errno == EINTR) - goto Pipe_read; - - throw IO::IOError(errno, "Could not read from pipe", P_SOURCEINFO); - } - else if(ret == 0 && !eof()) - setEof(true); - - return ret; -} - -size_t Pipe::_write(const char* buffer, size_t count) throw(IO::IOError) -{ - if(count > SSIZE_MAX) - count = SSIZE_MAX; - - Pipe_write: - ssize_t ret = ::write((int)_handle, - (const void*)buffer, count); - - if(ret == -1) - { - if(errno == EINTR) - goto Pipe_write; - - throw IO::IOError(errno, "Could not read from pipe", P_SOURCEINFO); - } - - return ret; -} - -void Pipe::_sync() const throw(IO::IOError) -{ - /* pipes cant be synced !! - int ret = fsync((int)_handle); - if(ret == -1) - throw IO::IOError(errno, "Could not commit to pipe", P_SOURCEINFO); */ -} - -Pipe& Pipe::operator=(const Pipe& p) throw(IO::IOError) -{ - if(p.valid()) - { - int handle = ::dup((int)p._handle); - if(handle == -1) - throw IO::IOError(errno, "Could not duplicate pipe handle", P_SOURCEINFO); - - if(valid()) - { - try - { - close(); - } - catch(...) - { - ::close(handle); - throw; - } - } - - IODevice::operator=(p); - _handle = (unsigned long)handle; - } - else - { - if(valid()) - close(); - - setValid(false); - setEof(true); - setAccess(None); - } - - return *this; -} } // !namespace System |