Add private BDataPositionIOWrapper

Implements the BPositionIO interface on top of a BDataIO, requiring the
{Read,Write}At() accesses to be sequential.
This commit is contained in:
Ingo Weinhold 2014-07-13 01:07:34 +02:00
parent 44c4771163
commit 05b565f4f2
5 changed files with 143 additions and 0 deletions

View File

@ -0,0 +1 @@
#include <../private/support/DataPositionIOWrapper.h>

View File

@ -0,0 +1,37 @@
/*
* Copyright 2014, Ingo Weinhold, ingo_weinhold@gmx.de.
* Distributed under the terms of the MIT License.
*/
#ifndef _DATA_POSITION_IO_WRAPPER_H_
#define _DATA_POSITION_IO_WRAPPER_H_
#include <DataIO.h>
class BDataPositionIOWrapper : public BPositionIO {
public:
BDataPositionIOWrapper(BDataIO* io);
~BDataPositionIOWrapper();
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);
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 status_t SetSize(off_t size);
virtual status_t GetSize(off_t* size) const;
private:
BDataIO* fIO;
off_t fPosition;
};
#endif // _DATA_POSITION_IO_WRAPPER_H_

View File

@ -12,6 +12,7 @@ BuildPlatformMergeObjectPIC <libbe_build>support_kit.o :
ByteOrder.cpp
CompressionAlgorithm.cpp
DataIO.cpp
DataPositionIOWrapper.cpp
Flattenable.cpp
List.cpp
Locker.cpp

View File

@ -0,0 +1,103 @@
/*
* Copyright 2014, Ingo Weinhold, ingo_weinhold@gmx.de.
* Distributed under the terms of the MIT License.
*/
#include <DataPositionIOWrapper.h>
#include <stdio.h>
BDataPositionIOWrapper::BDataPositionIOWrapper(BDataIO* io)
:
BPositionIO(),
fIO(io),
fPosition(0)
{
}
BDataPositionIOWrapper::~BDataPositionIOWrapper()
{
}
ssize_t
BDataPositionIOWrapper::Read(void* buffer, size_t size)
{
ssize_t bytesRead = fIO->Read(buffer, size);
if (bytesRead > 0)
fPosition += bytesRead;
return bytesRead;
}
ssize_t
BDataPositionIOWrapper::Write(const void* buffer, size_t size)
{
ssize_t bytesWritten = fIO->Write(buffer, size);
if (bytesWritten > 0)
fPosition += bytesWritten;
return bytesWritten;
}
ssize_t
BDataPositionIOWrapper::ReadAt(off_t position, void* buffer, size_t size)
{
if (position != fPosition)
return B_NOT_SUPPORTED;
return Read(buffer, size);
}
ssize_t
BDataPositionIOWrapper::WriteAt(off_t position, const void* buffer,
size_t size)
{
if (position != fPosition)
return B_NOT_SUPPORTED;
return Write(buffer, size);
}
off_t
BDataPositionIOWrapper::Seek(off_t position, uint32 seekMode)
{
switch (seekMode) {
case SEEK_CUR:
return position == 0 ? B_OK : B_NOT_SUPPORTED;
case SEEK_SET:
return position == fPosition ? B_OK : B_NOT_SUPPORTED;
case SEEK_END:
return B_NOT_SUPPORTED;
default:
return B_BAD_VALUE;
}
}
off_t
BDataPositionIOWrapper::Position() const
{
return fPosition;
}
status_t
BDataPositionIOWrapper::SetSize(off_t size)
{
return size == fPosition ? B_OK : B_NOT_SUPPORTED;
}
status_t
BDataPositionIOWrapper::GetSize(off_t* size) const
{
return B_NOT_SUPPORTED;
}

View File

@ -26,6 +26,7 @@ for architectureObject in [ MultiArchSubDirSetup ] {
ByteOrder.cpp
CompressionAlgorithm.cpp
DataIO.cpp
DataPositionIOWrapper.cpp
DateTime.cpp
Flattenable.cpp
List.cpp