git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@17981 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2006-06-30 12:40:15 +00:00
parent b9d454b6ff
commit 64f10d7d89
2 changed files with 176 additions and 249 deletions
+24 -50
View File
@@ -1,49 +1,26 @@
//------------------------------------------------------------------------------ /*
// Copyright (c) 2001-2002, OpenBeOS * Copyright 2005-2006, Haiku.
// * Distributed under the terms of the MIT License.
// Permission is hereby granted, free of charge, to any person obtaining a *
// copy of this software and associated documentation files (the "Software"), * Authors:
// to deal in the Software without restriction, including without limitation * Ingo Weinhold, [email protected]
// 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 ([email protected])
// Stefano Ceccherini ([email protected])
// 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.
//------------------------------------------------------------------------------
#ifndef _DATA_IO_H #ifndef _DATA_IO_H
#define _DATA_IO_H #define _DATA_IO_H
#include <SupportDefs.h> #include <SupportDefs.h>
// DataIO
class BDataIO { class BDataIO {
public: public:
BDataIO(); BDataIO();
virtual ~BDataIO(); virtual ~BDataIO();
virtual ssize_t Read(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; virtual ssize_t Write(const void *buffer, size_t size) = 0;
private: private:
BDataIO(const BDataIO &); BDataIO(const BDataIO &);
BDataIO &operator=(const BDataIO &); BDataIO &operator=(const BDataIO &);
@@ -63,9 +40,8 @@ private:
uint32 _reserved[2]; uint32 _reserved[2];
}; };
// BPositionIO
class BPositionIO : public BDataIO { class BPositionIO : public BDataIO {
public: public:
BPositionIO(); BPositionIO();
virtual ~BPositionIO(); virtual ~BPositionIO();
@@ -82,7 +58,7 @@ public:
virtual status_t SetSize(off_t size); virtual status_t SetSize(off_t size);
virtual status_t GetSize(off_t* size) const; virtual status_t GetSize(off_t* size) const;
private: private:
virtual void _ReservedPositionIO2(); virtual void _ReservedPositionIO2();
virtual void _ReservedPositionIO3(); virtual void _ReservedPositionIO3();
virtual void _ReservedPositionIO4(); virtual void _ReservedPositionIO4();
@@ -98,11 +74,10 @@ private:
uint32 _reserved[2]; uint32 _reserved[2];
}; };
// BMemoryIO
class BMemoryIO : public BPositionIO { class BMemoryIO : public BPositionIO {
public: public:
BMemoryIO(void *data, size_t len); BMemoryIO(void *data, size_t length);
BMemoryIO(const void *data, size_t len); BMemoryIO(const void *data, size_t length);
virtual ~BMemoryIO(); virtual ~BMemoryIO();
virtual ssize_t ReadAt(off_t position, void *buffer, size_t size); virtual ssize_t ReadAt(off_t position, void *buffer, size_t size);
@@ -113,7 +88,7 @@ public:
virtual status_t SetSize(off_t size); virtual status_t SetSize(off_t size);
private: private:
BMemoryIO(const BMemoryIO &); BMemoryIO(const BMemoryIO &);
BMemoryIO &operator=(const BMemoryIO &); BMemoryIO &operator=(const BMemoryIO &);
@@ -121,16 +96,15 @@ private:
virtual void _ReservedMemoryIO2(); virtual void _ReservedMemoryIO2();
bool fReadOnly; bool fReadOnly;
char *fBuf; char *fBuffer;
size_t fLen; size_t fLength;
size_t fPhys; size_t fBufferSize;
size_t fPos; size_t fPosition;
uint32 _reserved[1]; uint32 _reserved[1];
}; };
// BMallocIO
class BMallocIO : public BPositionIO { class BMallocIO : public BPositionIO {
public: public:
BMallocIO(); BMallocIO();
virtual ~BMallocIO(); virtual ~BMallocIO();
@@ -147,7 +121,7 @@ public:
const void *Buffer() const; const void *Buffer() const;
size_t BufferLength() const; size_t BufferLength() const;
private: private:
BMallocIO(const BMallocIO &); BMallocIO(const BMallocIO &);
BMallocIO &operator=(const BMallocIO &); BMallocIO &operator=(const BMallocIO &);
@@ -162,4 +136,4 @@ private:
uint32 _reserved[1]; uint32 _reserved[1];
}; };
#endif #endif // _DATA_IO_H
+60 -107
View File
@@ -1,65 +1,48 @@
//------------------------------------------------------------------------------ /*
// Copyright (c) 2001-2005, Haiku, Inc. * Copyright 2005-2006, Haiku.
// * Distributed under the terms of the MIT License.
// Permission is hereby granted, free of charge, to any person obtaining a *
// copy of this software and associated documentation files (the "Software"), * Authors:
// to deal in the Software without restriction, including without limitation * Stefano Ceccherini ([email protected])
// 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: /*!
// Pure virtual BDataIO and BPositioIO classes provide
// The above copyright notice and this permission notice shall be included in the protocol for Read()/Write()/Seek().
// all copies or substantial portions of the Software.
// BMallocIO and BMemoryIO classes implement the protocol,
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR as does BFile in the Storage Kit.
// 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 #include <DataIO.h>
// 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 ([email protected])
// 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.
//------------------------------------------------------------------------------
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <DataIO.h>
// *** BDataIO ***
// Construction
BDataIO::BDataIO() BDataIO::BDataIO()
{ {
} }
// Destruction
BDataIO::~BDataIO() BDataIO::~BDataIO()
{ {
} }
// Private or Reserved // Private or Reserved
BDataIO::BDataIO(const BDataIO &) BDataIO::BDataIO(const BDataIO &)
{ {
//Copying not allowed // Copying not allowed
} }
BDataIO & BDataIO &
BDataIO::operator=(const BDataIO &) BDataIO::operator=(const BDataIO &)
{ {
//Copying not allowed // Copying not allowed
return *this; return *this;
} }
@@ -69,8 +52,6 @@ void BDataIO::_ReservedDataIO1(){}
void BDataIO::_ReservedDataIO2(){} void BDataIO::_ReservedDataIO2(){}
void BDataIO::_ReservedDataIO3(){} void BDataIO::_ReservedDataIO3(){}
void BDataIO::_ReservedDataIO4(){} void BDataIO::_ReservedDataIO4(){}
#if !_PR3_COMPATIBLE_
void BDataIO::_ReservedDataIO5(){} void BDataIO::_ReservedDataIO5(){}
void BDataIO::_ReservedDataIO6(){} void BDataIO::_ReservedDataIO6(){}
void BDataIO::_ReservedDataIO7(){} void BDataIO::_ReservedDataIO7(){}
@@ -79,24 +60,21 @@ void BDataIO::_ReservedDataIO9(){}
void BDataIO::_ReservedDataIO10(){} void BDataIO::_ReservedDataIO10(){}
void BDataIO::_ReservedDataIO11(){} void BDataIO::_ReservedDataIO11(){}
void BDataIO::_ReservedDataIO12(){} void BDataIO::_ReservedDataIO12(){}
#endif
// *** BPositionIO *** // #pragma mark -
// Construction
BPositionIO::BPositionIO() BPositionIO::BPositionIO()
{ {
} }
// Destruction
BPositionIO::~BPositionIO() BPositionIO::~BPositionIO()
{ {
} }
// Read
ssize_t ssize_t
BPositionIO::Read(void *buffer, size_t size) BPositionIO::Read(void *buffer, size_t size)
{ {
@@ -109,7 +87,6 @@ BPositionIO::Read(void *buffer, size_t size)
} }
// Write
ssize_t ssize_t
BPositionIO::Write(const void *buffer, size_t size) BPositionIO::Write(const void *buffer, size_t size)
{ {
@@ -122,14 +99,13 @@ BPositionIO::Write(const void *buffer, size_t size)
} }
// SetSize
status_t status_t
BPositionIO::SetSize(off_t size) BPositionIO::SetSize(off_t size)
{ {
return B_ERROR; return B_ERROR;
} }
// GetSize
status_t status_t
BPositionIO::GetSize(off_t* size) const BPositionIO::GetSize(off_t* size) const
{ {
@@ -158,8 +134,6 @@ extern "C" void _ReservedPositionIO1__11BPositionIO() {}
void BPositionIO::_ReservedPositionIO2(){} void BPositionIO::_ReservedPositionIO2(){}
void BPositionIO::_ReservedPositionIO3(){} void BPositionIO::_ReservedPositionIO3(){}
void BPositionIO::_ReservedPositionIO4(){} void BPositionIO::_ReservedPositionIO4(){}
#if !_PR3_COMPATIBLE_
void BPositionIO::_ReservedPositionIO5(){} void BPositionIO::_ReservedPositionIO5(){}
void BPositionIO::_ReservedPositionIO6(){} void BPositionIO::_ReservedPositionIO6(){}
void BPositionIO::_ReservedPositionIO7(){} void BPositionIO::_ReservedPositionIO7(){}
@@ -168,40 +142,38 @@ void BPositionIO::_ReservedPositionIO9(){}
void BPositionIO::_ReservedPositionIO10(){} void BPositionIO::_ReservedPositionIO10(){}
void BPositionIO::_ReservedPositionIO11(){} void BPositionIO::_ReservedPositionIO11(){}
void BPositionIO::_ReservedPositionIO12(){} void BPositionIO::_ReservedPositionIO12(){}
#endif
// *** BMemoryIO *** // #pragma mark -
// Construction
BMemoryIO::BMemoryIO(void *p, size_t len)
:fReadOnly(false),
fBuf(static_cast<char*>(p)),
fLen(len),
fPhys(len),
fPos(0)
BMemoryIO::BMemoryIO(void *buffer, size_t length)
:
fReadOnly(false),
fBuffer(static_cast<char*>(buffer)),
fLength(length),
fBufferSize(length),
fPosition(0)
{ {
} }
BMemoryIO::BMemoryIO(const void *p, size_t len) BMemoryIO::BMemoryIO(const void *buffer, size_t length)
:fReadOnly(true), :
fBuf(const_cast<char*>(static_cast<const char*>(p))), fReadOnly(true),
fLen(len), fBuffer(const_cast<char*>(static_cast<const char*>(buffer))),
fPhys(len), fLength(length),
fPos(0) fBufferSize(length),
fPosition(0)
{ {
} }
// Destruction
BMemoryIO::~BMemoryIO() BMemoryIO::~BMemoryIO()
{ {
} }
// ReadAt
ssize_t ssize_t
BMemoryIO::ReadAt(off_t pos, void *buffer, size_t size) BMemoryIO::ReadAt(off_t pos, void *buffer, size_t size)
{ {
@@ -209,15 +181,14 @@ BMemoryIO::ReadAt(off_t pos, void *buffer, size_t size)
return B_BAD_VALUE; return B_BAD_VALUE;
ssize_t sizeRead = 0; ssize_t sizeRead = 0;
if (pos < fLen) { if (pos < fLength) {
sizeRead = min_c(static_cast<off_t>(size), fLen - pos); sizeRead = min_c(static_cast<off_t>(size), fLength - pos);
memcpy(buffer, fBuf + pos, sizeRead); memcpy(buffer, fBuffer + pos, sizeRead);
} }
return sizeRead; return sizeRead;
} }
// WriteAt
ssize_t ssize_t
BMemoryIO::WriteAt(off_t pos, const void *buffer, size_t size) BMemoryIO::WriteAt(off_t pos, const void *buffer, size_t size)
{ {
@@ -228,64 +199,61 @@ BMemoryIO::WriteAt(off_t pos, const void *buffer, size_t size)
return B_BAD_VALUE; return B_BAD_VALUE;
ssize_t sizeWritten = 0; ssize_t sizeWritten = 0;
if (pos < fPhys) { if (pos < fBufferSize) {
sizeWritten = min_c(static_cast<off_t>(size), fPhys - pos); sizeWritten = min_c(static_cast<off_t>(size), fBufferSize - pos);
memcpy(fBuf + pos, buffer, sizeWritten); memcpy(fBuffer + pos, buffer, sizeWritten);
} }
if (pos + sizeWritten > fLen) if (pos + sizeWritten > fLength)
fLen = pos + sizeWritten; fLength = pos + sizeWritten;
return sizeWritten; return sizeWritten;
} }
// Seek
off_t off_t
BMemoryIO::Seek(off_t position, uint32 seek_mode) BMemoryIO::Seek(off_t position, uint32 seek_mode)
{ {
switch (seek_mode) { switch (seek_mode) {
case SEEK_SET: case SEEK_SET:
fPos = position; fPosition = position;
break; break;
case SEEK_CUR: case SEEK_CUR:
fPos += position; fPosition += position;
break; break;
case SEEK_END: case SEEK_END:
fPos = fLen + position; fPosition = fLength + position;
break; break;
default: default:
break; break;
} }
return fPos; return fPosition;
} }
// Position
off_t off_t
BMemoryIO::Position() const BMemoryIO::Position() const
{ {
return fPos; return fPosition;
} }
// SetSize
status_t status_t
BMemoryIO::SetSize(off_t size) BMemoryIO::SetSize(off_t size)
{ {
if (fReadOnly) if (fReadOnly)
return B_NOT_ALLOWED; return B_NOT_ALLOWED;
if (size > fPhys) if (size > fBufferSize)
return B_ERROR; return B_ERROR;
fLen = size; fLength = size;
return B_OK; return B_OK;
} }
// Private or Reserved // Private or Reserved
BMemoryIO::BMemoryIO(const BMemoryIO &) BMemoryIO::BMemoryIO(const BMemoryIO &)
{ {
//Copying not allowed //Copying not allowed
@@ -305,11 +273,12 @@ void BMemoryIO::_ReservedMemoryIO1(){}
void BMemoryIO::_ReservedMemoryIO2(){} void BMemoryIO::_ReservedMemoryIO2(){}
// *** BMallocIO *** // #pragma mark -
// Construction
BMallocIO::BMallocIO() BMallocIO::BMallocIO()
: fBlockSize(256), :
fBlockSize(256),
fMallocSize(0), fMallocSize(0),
fLength(0), fLength(0),
fData(NULL), fData(NULL),
@@ -318,14 +287,12 @@ BMallocIO::BMallocIO()
} }
// Destruction
BMallocIO::~BMallocIO() BMallocIO::~BMallocIO()
{ {
free(fData); free(fData);
} }
// ReadAt
ssize_t ssize_t
BMallocIO::ReadAt(off_t pos, void *buffer, size_t size) BMallocIO::ReadAt(off_t pos, void *buffer, size_t size)
{ {
@@ -341,7 +308,6 @@ BMallocIO::ReadAt(off_t pos, void *buffer, size_t size)
} }
// WriteAt
ssize_t ssize_t
BMallocIO::WriteAt(off_t pos, const void *buffer, size_t size) BMallocIO::WriteAt(off_t pos, const void *buffer, size_t size)
{ {
@@ -349,7 +315,6 @@ BMallocIO::WriteAt(off_t pos, const void *buffer, size_t size)
return B_BAD_VALUE; return B_BAD_VALUE;
size_t newSize = max_c(pos + size, static_cast<off_t>(fLength)); size_t newSize = max_c(pos + size, static_cast<off_t>(fLength));
status_t error = B_OK; status_t error = B_OK;
if (newSize > fMallocSize) if (newSize > fMallocSize)
@@ -364,7 +329,6 @@ BMallocIO::WriteAt(off_t pos, const void *buffer, size_t size)
} }
// Seek
off_t off_t
BMallocIO::Seek(off_t position, uint32 seekMode) BMallocIO::Seek(off_t position, uint32 seekMode)
{ {
@@ -385,7 +349,6 @@ BMallocIO::Seek(off_t position, uint32 seekMode)
} }
// Position
off_t off_t
BMallocIO::Position() const BMallocIO::Position() const
{ {
@@ -393,7 +356,6 @@ BMallocIO::Position() const
} }
// SetSize
status_t status_t
BMallocIO::SetSize(off_t size) BMallocIO::SetSize(off_t size)
{ {
@@ -426,7 +388,6 @@ BMallocIO::SetSize(off_t size)
} }
// SetBlockSize
void void
BMallocIO::SetBlockSize(size_t blockSize) BMallocIO::SetBlockSize(size_t blockSize)
{ {
@@ -437,7 +398,6 @@ BMallocIO::SetBlockSize(size_t blockSize)
} }
// Buffer
const void * const void *
BMallocIO::Buffer() const BMallocIO::Buffer() const
{ {
@@ -445,7 +405,6 @@ BMallocIO::Buffer() const
} }
// BufferLength
size_t size_t
BMallocIO::BufferLength() const BMallocIO::BufferLength() const
{ {
@@ -454,6 +413,7 @@ BMallocIO::BufferLength() const
// Private or Reserved // Private or Reserved
BMallocIO::BMallocIO(const BMallocIO &) BMallocIO::BMallocIO(const BMallocIO &)
{ {
// copying not allowed... // copying not allowed...
@@ -472,10 +432,3 @@ BMallocIO::operator=(const BMallocIO &)
void BMallocIO::_ReservedMallocIO1() {} void BMallocIO::_ReservedMallocIO1() {}
void BMallocIO::_ReservedMallocIO2() {} void BMallocIO::_ReservedMallocIO2() {}
/*
* $Log $
*
* $Id $
*
*/