diff --git a/headers/os/support/DataIO.h b/headers/os/support/DataIO.h index 8b3317aab4..d69237feb1 100644 --- a/headers/os/support/DataIO.h +++ b/headers/os/support/DataIO.h @@ -1,165 +1,139 @@ -//------------------------------------------------------------------------------ -// Copyright (c) 2001-2002, OpenBeOS -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. -// -// File Name: DataIO.cpp -// Author(s): Ingo Weinhold (bonefish@users.sf.net) -// Stefano Ceccherini (burton666@libero.it) -// The Storage Team -// Description: Pure virtual BDataIO and BPositionIO classes provide -// the protocol for Read()/Write()/Seek(). -// -// BMallocIO and BMemoryIO classes implement the protocol, -// as does BFile in the Storage Kit. -//------------------------------------------------------------------------------ +/* + * Copyright 2005-2006, Haiku. + * Distributed under the terms of the MIT License. + * + * Authors: + * Ingo Weinhold, bonefish@users.sf.net + */ #ifndef _DATA_IO_H #define _DATA_IO_H + #include -// DataIO + class BDataIO { -public: - BDataIO(); - virtual ~BDataIO(); + public: + BDataIO(); + virtual ~BDataIO(); - virtual ssize_t Read(void *buffer, size_t size) = 0; - virtual ssize_t Write(const void *buffer, size_t size) = 0; + virtual ssize_t Read(void *buffer, size_t size) = 0; + virtual ssize_t Write(const void *buffer, size_t size) = 0; -private: - BDataIO(const BDataIO &); - BDataIO &operator=(const BDataIO &); + private: + BDataIO(const BDataIO &); + BDataIO &operator=(const BDataIO &); - virtual void _ReservedDataIO1(); - virtual void _ReservedDataIO2(); - virtual void _ReservedDataIO3(); - virtual void _ReservedDataIO4(); - virtual void _ReservedDataIO5(); - virtual void _ReservedDataIO6(); - virtual void _ReservedDataIO7(); - virtual void _ReservedDataIO8(); - virtual void _ReservedDataIO9(); - virtual void _ReservedDataIO10(); - virtual void _ReservedDataIO11(); - virtual void _ReservedDataIO12(); + virtual void _ReservedDataIO1(); + virtual void _ReservedDataIO2(); + virtual void _ReservedDataIO3(); + virtual void _ReservedDataIO4(); + virtual void _ReservedDataIO5(); + virtual void _ReservedDataIO6(); + virtual void _ReservedDataIO7(); + virtual void _ReservedDataIO8(); + virtual void _ReservedDataIO9(); + virtual void _ReservedDataIO10(); + virtual void _ReservedDataIO11(); + virtual void _ReservedDataIO12(); - uint32 _reserved[2]; + uint32 _reserved[2]; }; -// BPositionIO class BPositionIO : public BDataIO { -public: - BPositionIO(); - virtual ~BPositionIO(); + public: + BPositionIO(); + virtual ~BPositionIO(); - virtual ssize_t Read(void *buffer, size_t size); - virtual ssize_t Write(const void *buffer, size_t size); + virtual ssize_t Read(void *buffer, size_t size); + virtual ssize_t Write(const void *buffer, size_t size); - virtual ssize_t ReadAt(off_t position, void *buffer, size_t size) = 0; - virtual ssize_t WriteAt(off_t position, const void *buffer, + virtual ssize_t ReadAt(off_t position, void *buffer, size_t size) = 0; + virtual ssize_t WriteAt(off_t position, const void *buffer, size_t size) = 0; - virtual off_t Seek(off_t position, uint32 seekMode) = 0; - virtual off_t Position() const = 0; + virtual off_t Seek(off_t position, uint32 seekMode) = 0; + virtual off_t Position() const = 0; - virtual status_t SetSize(off_t size); - virtual status_t GetSize(off_t* size) const; + virtual status_t SetSize(off_t size); + virtual status_t GetSize(off_t* size) const; -private: - virtual void _ReservedPositionIO2(); - virtual void _ReservedPositionIO3(); - virtual void _ReservedPositionIO4(); - virtual void _ReservedPositionIO5(); - virtual void _ReservedPositionIO6(); - virtual void _ReservedPositionIO7(); - virtual void _ReservedPositionIO8(); - virtual void _ReservedPositionIO9(); - virtual void _ReservedPositionIO10(); - virtual void _ReservedPositionIO11(); - virtual void _ReservedPositionIO12(); + private: + virtual void _ReservedPositionIO2(); + virtual void _ReservedPositionIO3(); + virtual void _ReservedPositionIO4(); + virtual void _ReservedPositionIO5(); + virtual void _ReservedPositionIO6(); + virtual void _ReservedPositionIO7(); + virtual void _ReservedPositionIO8(); + virtual void _ReservedPositionIO9(); + virtual void _ReservedPositionIO10(); + virtual void _ReservedPositionIO11(); + virtual void _ReservedPositionIO12(); - uint32 _reserved[2]; + uint32 _reserved[2]; }; -// BMemoryIO class BMemoryIO : public BPositionIO { -public: - BMemoryIO(void *data, size_t len); - BMemoryIO(const void *data, size_t len); - virtual ~BMemoryIO(); + public: + BMemoryIO(void *data, size_t length); + BMemoryIO(const void *data, size_t length); + virtual ~BMemoryIO(); - virtual ssize_t ReadAt(off_t position, void *buffer, size_t size); - virtual ssize_t WriteAt(off_t position, const void *buffer, size_t size); + virtual ssize_t ReadAt(off_t position, void *buffer, size_t size); + virtual ssize_t WriteAt(off_t position, const void *buffer, size_t size); - virtual off_t Seek(off_t position, uint32 seekMode); - virtual off_t Position() const; + virtual off_t Seek(off_t position, uint32 seekMode); + virtual off_t Position() const; - virtual status_t SetSize(off_t size); + virtual status_t SetSize(off_t size); -private: - BMemoryIO(const BMemoryIO &); - BMemoryIO &operator=(const BMemoryIO &); + private: + BMemoryIO(const BMemoryIO &); + BMemoryIO &operator=(const BMemoryIO &); - virtual void _ReservedMemoryIO1(); - virtual void _ReservedMemoryIO2(); + virtual void _ReservedMemoryIO1(); + virtual void _ReservedMemoryIO2(); - bool fReadOnly; - char *fBuf; - size_t fLen; - size_t fPhys; - size_t fPos; - uint32 _reserved[1]; + bool fReadOnly; + char *fBuffer; + size_t fLength; + size_t fBufferSize; + size_t fPosition; + uint32 _reserved[1]; }; -// BMallocIO class BMallocIO : public BPositionIO { -public: - BMallocIO(); - virtual ~BMallocIO(); + public: + BMallocIO(); + virtual ~BMallocIO(); - virtual ssize_t ReadAt(off_t position, void *buffer, size_t size); - virtual ssize_t WriteAt(off_t position, const void *buffer, size_t size); + virtual ssize_t ReadAt(off_t position, void *buffer, size_t size); + virtual ssize_t WriteAt(off_t position, const void *buffer, size_t size); - virtual off_t Seek(off_t position, uint32 seekMode); - virtual off_t Position() const; + virtual off_t Seek(off_t position, uint32 seekMode); + virtual off_t Position() const; - virtual status_t SetSize(off_t size); + virtual status_t SetSize(off_t size); - void SetBlockSize(size_t blockSize); + void SetBlockSize(size_t blockSize); - const void *Buffer() const; - size_t BufferLength() const; + const void *Buffer() const; + size_t BufferLength() const; -private: - BMallocIO(const BMallocIO &); - BMallocIO &operator=(const BMallocIO &); + private: + BMallocIO(const BMallocIO &); + BMallocIO &operator=(const BMallocIO &); - virtual void _ReservedMallocIO1(); - virtual void _ReservedMallocIO2(); + virtual void _ReservedMallocIO1(); + virtual void _ReservedMallocIO2(); - size_t fBlockSize; - size_t fMallocSize; - size_t fLength; - char *fData; - off_t fPosition; - uint32 _reserved[1]; + size_t fBlockSize; + size_t fMallocSize; + size_t fLength; + char *fData; + off_t fPosition; + uint32 _reserved[1]; }; -#endif +#endif // _DATA_IO_H diff --git a/src/kits/support/DataIO.cpp b/src/kits/support/DataIO.cpp index 1a91e66125..bf83eeaf8f 100644 --- a/src/kits/support/DataIO.cpp +++ b/src/kits/support/DataIO.cpp @@ -1,65 +1,48 @@ -//------------------------------------------------------------------------------ -// Copyright (c) 2001-2005, Haiku, Inc. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. -// -// File Name: DataIO.cpp -// Author(s): Stefano Ceccherini (burton666@libero.it) -// The Storage Team -// Description: Pure virtual BDataIO and BPositioIO classes provide -// the protocol for Read()/Write()/Seek(). -// -// BMallocIO and BMemoryIO classes implement the protocol, -// as does BFile in the Storage Kit. -//------------------------------------------------------------------------------ +/* + * Copyright 2005-2006, Haiku. + * Distributed under the terms of the MIT License. + * + * Authors: + * Stefano Ceccherini (burton666@libero.it) + */ + +/*! + Pure virtual BDataIO and BPositioIO classes provide + the protocol for Read()/Write()/Seek(). + + BMallocIO and BMemoryIO classes implement the protocol, + as does BFile in the Storage Kit. +*/ + +#include + #include #include #include -#include - -// *** BDataIO *** - -// Construction BDataIO::BDataIO() { } -// Destruction BDataIO::~BDataIO() { } // Private or Reserved + BDataIO::BDataIO(const BDataIO &) { - //Copying not allowed + // Copying not allowed } BDataIO & BDataIO::operator=(const BDataIO &) { - //Copying not allowed + // Copying not allowed return *this; } @@ -69,8 +52,6 @@ void BDataIO::_ReservedDataIO1(){} void BDataIO::_ReservedDataIO2(){} void BDataIO::_ReservedDataIO3(){} void BDataIO::_ReservedDataIO4(){} - -#if !_PR3_COMPATIBLE_ void BDataIO::_ReservedDataIO5(){} void BDataIO::_ReservedDataIO6(){} void BDataIO::_ReservedDataIO7(){} @@ -79,24 +60,21 @@ void BDataIO::_ReservedDataIO9(){} void BDataIO::_ReservedDataIO10(){} void BDataIO::_ReservedDataIO11(){} void BDataIO::_ReservedDataIO12(){} -#endif -// *** BPositionIO *** +// #pragma mark - + -// Construction BPositionIO::BPositionIO() { } -// Destruction BPositionIO::~BPositionIO() { } -// Read ssize_t BPositionIO::Read(void *buffer, size_t size) { @@ -104,12 +82,11 @@ BPositionIO::Read(void *buffer, size_t size) ssize_t result = ReadAt(curPos, buffer, size); if (result > 0) Seek(result, SEEK_CUR); - + return result; } -// Write ssize_t BPositionIO::Write(const void *buffer, size_t size) { @@ -117,19 +94,18 @@ BPositionIO::Write(const void *buffer, size_t size) ssize_t result = WriteAt(curPos, buffer, size); if (result > 0) Seek(result, SEEK_CUR); - + return result; } -// SetSize status_t BPositionIO::SetSize(off_t size) { return B_ERROR; } -// GetSize + status_t BPositionIO::GetSize(off_t* size) const { @@ -158,8 +134,6 @@ extern "C" void _ReservedPositionIO1__11BPositionIO() {} void BPositionIO::_ReservedPositionIO2(){} void BPositionIO::_ReservedPositionIO3(){} void BPositionIO::_ReservedPositionIO4(){} - -#if !_PR3_COMPATIBLE_ void BPositionIO::_ReservedPositionIO5(){} void BPositionIO::_ReservedPositionIO6(){} void BPositionIO::_ReservedPositionIO7(){} @@ -168,124 +142,118 @@ void BPositionIO::_ReservedPositionIO9(){} void BPositionIO::_ReservedPositionIO10(){} void BPositionIO::_ReservedPositionIO11(){} void BPositionIO::_ReservedPositionIO12(){} -#endif -// *** BMemoryIO *** +// #pragma mark - -// Construction -BMemoryIO::BMemoryIO(void *p, size_t len) - :fReadOnly(false), - fBuf(static_cast(p)), - fLen(len), - fPhys(len), - fPos(0) - + +BMemoryIO::BMemoryIO(void *buffer, size_t length) + : + fReadOnly(false), + fBuffer(static_cast(buffer)), + fLength(length), + fBufferSize(length), + fPosition(0) { } -BMemoryIO::BMemoryIO(const void *p, size_t len) - :fReadOnly(true), - fBuf(const_cast(static_cast(p))), - fLen(len), - fPhys(len), - fPos(0) +BMemoryIO::BMemoryIO(const void *buffer, size_t length) + : + fReadOnly(true), + fBuffer(const_cast(static_cast(buffer))), + fLength(length), + fBufferSize(length), + fPosition(0) { } -// Destruction BMemoryIO::~BMemoryIO() { } -// ReadAt ssize_t BMemoryIO::ReadAt(off_t pos, void *buffer, size_t size) { if (buffer == NULL || pos < 0) return B_BAD_VALUE; - + ssize_t sizeRead = 0; - if (pos < fLen) { - sizeRead = min_c(static_cast(size), fLen - pos); - memcpy(buffer, fBuf + pos, sizeRead); + if (pos < fLength) { + sizeRead = min_c(static_cast(size), fLength - pos); + memcpy(buffer, fBuffer + pos, sizeRead); } return sizeRead; } -// WriteAt ssize_t BMemoryIO::WriteAt(off_t pos, const void *buffer, size_t size) { if (fReadOnly) return B_NOT_ALLOWED; - + if (buffer == NULL || pos < 0) return B_BAD_VALUE; - + ssize_t sizeWritten = 0; - if (pos < fPhys) { - sizeWritten = min_c(static_cast(size), fPhys - pos); - memcpy(fBuf + pos, buffer, sizeWritten); + if (pos < fBufferSize) { + sizeWritten = min_c(static_cast(size), fBufferSize - pos); + memcpy(fBuffer + pos, buffer, sizeWritten); } - - if (pos + sizeWritten > fLen) - fLen = pos + sizeWritten; - + + if (pos + sizeWritten > fLength) + fLength = pos + sizeWritten; + return sizeWritten; } -// Seek off_t BMemoryIO::Seek(off_t position, uint32 seek_mode) { switch (seek_mode) { case SEEK_SET: - fPos = position; + fPosition = position; break; case SEEK_CUR: - fPos += position; + fPosition += position; break; case SEEK_END: - fPos = fLen + position; + fPosition = fLength + position; break; default: break; } - return fPos; + return fPosition; } -// Position off_t BMemoryIO::Position() const { - return fPos; + return fPosition; } -// SetSize status_t BMemoryIO::SetSize(off_t size) { if (fReadOnly) return B_NOT_ALLOWED; - if (size > fPhys) + if (size > fBufferSize) return B_ERROR; - - fLen = size; - + + fLength = size; return B_OK; } // Private or Reserved + BMemoryIO::BMemoryIO(const BMemoryIO &) { //Copying not allowed @@ -305,33 +273,32 @@ void BMemoryIO::_ReservedMemoryIO1(){} void BMemoryIO::_ReservedMemoryIO2(){} -// *** BMallocIO *** +// #pragma mark - + -// Construction BMallocIO::BMallocIO() - : fBlockSize(256), - fMallocSize(0), - fLength(0), - fData(NULL), - fPosition(0) + : + fBlockSize(256), + fMallocSize(0), + fLength(0), + fData(NULL), + fPosition(0) { } -// Destruction BMallocIO::~BMallocIO() { free(fData); } -// ReadAt ssize_t BMallocIO::ReadAt(off_t pos, void *buffer, size_t size) { if (buffer == NULL) return B_BAD_VALUE; - + ssize_t sizeRead = 0; if (pos < fLength) { sizeRead = min_c(static_cast(size), fLength - pos); @@ -341,20 +308,18 @@ BMallocIO::ReadAt(off_t pos, void *buffer, size_t size) } -// WriteAt ssize_t BMallocIO::WriteAt(off_t pos, const void *buffer, size_t size) { if (buffer == NULL) return B_BAD_VALUE; - + size_t newSize = max_c(pos + size, static_cast(fLength)); - status_t error = B_OK; - + if (newSize > fMallocSize) error = SetSize(newSize); - + if (error == B_OK) { memcpy(fData + pos, buffer, size); if (pos + size > fLength) @@ -364,7 +329,6 @@ BMallocIO::WriteAt(off_t pos, const void *buffer, size_t size) } -// Seek off_t BMallocIO::Seek(off_t position, uint32 seekMode) { @@ -385,7 +349,6 @@ BMallocIO::Seek(off_t position, uint32 seekMode) } -// Position off_t BMallocIO::Position() const { @@ -393,7 +356,6 @@ BMallocIO::Position() const } -// SetSize status_t BMallocIO::SetSize(off_t size) { @@ -418,15 +380,14 @@ BMallocIO::SetSize(off_t size) error = B_NO_MEMORY; } } - + if (error == B_OK) fLength = size; - + return error; } -// SetBlockSize void BMallocIO::SetBlockSize(size_t blockSize) { @@ -437,7 +398,6 @@ BMallocIO::SetBlockSize(size_t blockSize) } -// Buffer const void * BMallocIO::Buffer() const { @@ -445,7 +405,6 @@ BMallocIO::Buffer() const } -// BufferLength size_t BMallocIO::BufferLength() const { @@ -454,6 +413,7 @@ BMallocIO::BufferLength() const // Private or Reserved + BMallocIO::BMallocIO(const BMallocIO &) { // copying not allowed... @@ -472,10 +432,3 @@ BMallocIO::operator=(const BMallocIO &) void BMallocIO::_ReservedMallocIO1() {} void BMallocIO::_ReservedMallocIO2() {} - -/* - * $Log $ - * - * $Id $ - * - */