Files
haiku-beta6/src/kits/support/DataIO.cpp
T

435 lines
6.9 KiB
C++
Raw Normal View History

2006-06-30 12:40:15 +00:00
/*
* Copyright 2005-2006, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* Stefano Ceccherini ([email protected])
*/
/*!
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 <DataIO.h>
2006-06-30 12:40:15 +00:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
2002-09-23 11:59:30 +00:00
BDataIO::BDataIO()
{
}
BDataIO::~BDataIO()
{
}
2002-09-23 11:59:30 +00:00
// Private or Reserved
2006-06-30 12:40:15 +00:00
BDataIO::BDataIO(const BDataIO &)
{
2006-06-30 12:40:15 +00:00
// Copying not allowed
}
BDataIO &
BDataIO::operator=(const BDataIO &)
{
2006-06-30 12:40:15 +00:00
// Copying not allowed
return *this;
}
2002-09-23 11:59:30 +00:00
// FBC
void BDataIO::_ReservedDataIO1(){}
void BDataIO::_ReservedDataIO2(){}
void BDataIO::_ReservedDataIO3(){}
void BDataIO::_ReservedDataIO4(){}
void BDataIO::_ReservedDataIO5(){}
void BDataIO::_ReservedDataIO6(){}
void BDataIO::_ReservedDataIO7(){}
void BDataIO::_ReservedDataIO8(){}
void BDataIO::_ReservedDataIO9(){}
void BDataIO::_ReservedDataIO10(){}
void BDataIO::_ReservedDataIO11(){}
void BDataIO::_ReservedDataIO12(){}
2002-09-23 11:59:30 +00:00
2006-06-30 12:40:15 +00:00
// #pragma mark -
2002-09-23 11:59:30 +00:00
BPositionIO::BPositionIO()
{
}
BPositionIO::~BPositionIO()
{
}
ssize_t
BPositionIO::Read(void *buffer, size_t size)
{
off_t curPos = Position();
ssize_t result = ReadAt(curPos, buffer, size);
if (result > 0)
Seek(result, SEEK_CUR);
2006-06-30 12:40:15 +00:00
return result;
}
ssize_t
BPositionIO::Write(const void *buffer, size_t size)
{
off_t curPos = Position();
ssize_t result = WriteAt(curPos, buffer, size);
if (result > 0)
Seek(result, SEEK_CUR);
2006-06-30 12:40:15 +00:00
return result;
}
status_t
BPositionIO::SetSize(off_t size)
{
return B_ERROR;
}
2006-06-30 12:40:15 +00:00
status_t
BPositionIO::GetSize(off_t* size) const
{
if (!size)
return B_BAD_VALUE;
off_t currentPos = Position();
if (currentPos < 0)
return (status_t)currentPos;
*size = const_cast<BPositionIO*>(this)->Seek(0, SEEK_END);
if (*size < 0)
return (status_t)*size;
off_t pos = const_cast<BPositionIO*>(this)->Seek(currentPos, SEEK_SET);
if (pos != currentPos)
return pos < 0 ? (status_t)pos : B_ERROR;
return B_OK;
}
2002-09-23 11:59:30 +00:00
// FBC
extern "C" void _ReservedPositionIO1__11BPositionIO() {}
2002-09-23 11:59:30 +00:00
void BPositionIO::_ReservedPositionIO2(){}
void BPositionIO::_ReservedPositionIO3(){}
void BPositionIO::_ReservedPositionIO4(){}
void BPositionIO::_ReservedPositionIO5(){}
void BPositionIO::_ReservedPositionIO6(){}
void BPositionIO::_ReservedPositionIO7(){}
void BPositionIO::_ReservedPositionIO8(){}
void BPositionIO::_ReservedPositionIO9(){}
void BPositionIO::_ReservedPositionIO10(){}
void BPositionIO::_ReservedPositionIO11(){}
void BPositionIO::_ReservedPositionIO12(){}
2002-09-23 11:59:30 +00:00
2006-06-30 12:40:15 +00:00
// #pragma mark -
2002-09-23 11:59:30 +00:00
2006-06-30 12:40:15 +00:00
BMemoryIO::BMemoryIO(void *buffer, size_t length)
:
fReadOnly(false),
fBuffer(static_cast<char*>(buffer)),
fLength(length),
fBufferSize(length),
fPosition(0)
{
}
2006-06-30 12:40:15 +00:00
BMemoryIO::BMemoryIO(const void *buffer, size_t length)
:
fReadOnly(true),
fBuffer(const_cast<char*>(static_cast<const char*>(buffer))),
fLength(length),
fBufferSize(length),
fPosition(0)
{
}
BMemoryIO::~BMemoryIO()
{
}
ssize_t
BMemoryIO::ReadAt(off_t pos, void *buffer, size_t size)
{
2003-05-23 23:55:32 +00:00
if (buffer == NULL || pos < 0)
return B_BAD_VALUE;
2006-06-30 12:40:15 +00:00
ssize_t sizeRead = 0;
2006-06-30 12:40:15 +00:00
if (pos < fLength) {
sizeRead = min_c(static_cast<off_t>(size), fLength - pos);
memcpy(buffer, fBuffer + pos, sizeRead);
}
return sizeRead;
}
ssize_t
BMemoryIO::WriteAt(off_t pos, const void *buffer, size_t size)
{
if (fReadOnly)
return B_NOT_ALLOWED;
2006-06-30 12:40:15 +00:00
2003-05-23 23:55:32 +00:00
if (buffer == NULL || pos < 0)
return B_BAD_VALUE;
2006-06-30 12:40:15 +00:00
ssize_t sizeWritten = 0;
2006-06-30 12:40:15 +00:00
if (pos < fBufferSize) {
sizeWritten = min_c(static_cast<off_t>(size), fBufferSize - pos);
memcpy(fBuffer + pos, buffer, sizeWritten);
}
2006-06-30 12:40:15 +00:00
if (pos + sizeWritten > fLength)
fLength = pos + sizeWritten;
return sizeWritten;
}
off_t
BMemoryIO::Seek(off_t position, uint32 seek_mode)
{
switch (seek_mode) {
case SEEK_SET:
2006-06-30 12:40:15 +00:00
fPosition = position;
break;
case SEEK_CUR:
2006-06-30 12:40:15 +00:00
fPosition += position;
break;
case SEEK_END:
2006-06-30 12:40:15 +00:00
fPosition = fLength + position;
break;
default:
break;
}
2006-06-30 12:40:15 +00:00
return fPosition;
}
off_t
BMemoryIO::Position() const
{
2006-06-30 12:40:15 +00:00
return fPosition;
}
status_t
BMemoryIO::SetSize(off_t size)
{
if (fReadOnly)
return B_NOT_ALLOWED;
2006-06-30 12:40:15 +00:00
if (size > fBufferSize)
2005-10-30 09:34:32 +00:00
return B_ERROR;
2006-06-30 12:40:15 +00:00
fLength = size;
2005-10-30 09:34:32 +00:00
return B_OK;
}
2002-09-23 11:59:30 +00:00
// Private or Reserved
2006-06-30 12:40:15 +00:00
BMemoryIO::BMemoryIO(const BMemoryIO &)
{
//Copying not allowed
}
BMemoryIO &
BMemoryIO::operator=(const BMemoryIO &)
{
//Copying not allowed
return *this;
}
2002-09-23 11:59:30 +00:00
// FBC
void BMemoryIO::_ReservedMemoryIO1(){}
void BMemoryIO::_ReservedMemoryIO2(){}
2006-06-30 12:40:15 +00:00
// #pragma mark -
2002-09-23 11:59:30 +00:00
BMallocIO::BMallocIO()
2006-06-30 12:40:15 +00:00
:
fBlockSize(256),
fMallocSize(0),
fLength(0),
fData(NULL),
fPosition(0)
{
}
BMallocIO::~BMallocIO()
{
free(fData);
}
ssize_t
BMallocIO::ReadAt(off_t pos, void *buffer, size_t size)
{
if (buffer == NULL)
return B_BAD_VALUE;
2006-06-30 12:40:15 +00:00
ssize_t sizeRead = 0;
2002-09-27 16:28:11 +00:00
if (pos < fLength) {
2003-06-03 18:34:34 +00:00
sizeRead = min_c(static_cast<off_t>(size), fLength - pos);
memcpy(buffer, fData + pos, sizeRead);
}
return sizeRead;
}
ssize_t
BMallocIO::WriteAt(off_t pos, const void *buffer, size_t size)
{
if (buffer == NULL)
return B_BAD_VALUE;
2006-06-30 12:40:15 +00:00
2003-06-03 18:34:34 +00:00
size_t newSize = max_c(pos + size, static_cast<off_t>(fLength));
status_t error = B_OK;
2006-06-30 12:40:15 +00:00
if (newSize > fMallocSize)
error = SetSize(newSize);
2006-06-30 12:40:15 +00:00
2002-10-07 13:49:16 +00:00
if (error == B_OK) {
memcpy(fData + pos, buffer, size);
2002-10-07 13:49:16 +00:00
if (pos + size > fLength)
fLength = pos + size;
}
return error != B_OK ? error : size;
}
off_t
BMallocIO::Seek(off_t position, uint32 seekMode)
{
switch (seekMode) {
case SEEK_SET:
fPosition = position;
break;
case SEEK_END:
fPosition = fLength + position;
break;
case SEEK_CUR:
fPosition += position;
break;
default:
break;
}
return fPosition;
}
off_t
BMallocIO::Position() const
{
return fPosition;
}
status_t
BMallocIO::SetSize(off_t size)
{
2002-09-23 21:02:00 +00:00
status_t error = B_OK;
2002-09-27 16:28:11 +00:00
if (size == 0) {
2002-09-23 21:02:00 +00:00
// size == 0, free the memory
free(fData);
fData = NULL;
fMallocSize = 0;
2002-09-23 21:02:00 +00:00
} else {
// size != 0, see, if necessary to resize
size_t newSize = (size + fBlockSize - 1) / fBlockSize * fBlockSize;
if (size != fMallocSize) {
2002-09-23 21:02:00 +00:00
// we need to resize
if (char *newData = static_cast<char*>(realloc(fData, newSize))) {
2002-09-23 21:02:00 +00:00
// set the new area to 0
if (newSize > fMallocSize)
memset(newData + fMallocSize, 0, newSize - fMallocSize);
2002-09-23 21:02:00 +00:00
fData = newData;
fMallocSize = newSize;
2002-09-23 21:02:00 +00:00
} else // couldn't alloc the memory
error = B_NO_MEMORY;
}
}
2006-06-30 12:40:15 +00:00
if (error == B_OK)
fLength = size;
2006-06-30 12:40:15 +00:00
return error;
}
void
BMallocIO::SetBlockSize(size_t blockSize)
{
if (blockSize == 0)
blockSize = 1;
2002-09-23 21:02:00 +00:00
if (blockSize != fBlockSize)
fBlockSize = blockSize;
}
const void *
BMallocIO::Buffer() const
{
return fData;
}
size_t
BMallocIO::BufferLength() const
{
return fLength;
}
2002-09-23 11:59:30 +00:00
// Private or Reserved
2006-06-30 12:40:15 +00:00
BMallocIO::BMallocIO(const BMallocIO &)
{
// copying not allowed...
}
BMallocIO &
BMallocIO::operator=(const BMallocIO &)
{
// copying not allowed...
return *this;
}
2002-09-23 21:02:00 +00:00
// FBC
void BMallocIO::_ReservedMallocIO1() {}
void BMallocIO::_ReservedMallocIO2() {}