Merged MallocIO.cpp with DataIO.cpp, some code refactoring,
and let a first, basic BString implementetation enter the game git-svn-id: file:///srv/svn/repos/haiku/trunk/current@988 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
+203
-31
@@ -20,7 +20,8 @@
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
// File Name: DataIO.cpp
|
||||
// Author(s): Jack Burton ([email protected])
|
||||
// Author(s): Stefano Ceccherini ([email protected])
|
||||
// The Storage Team
|
||||
// Description: Pure virtual BDataIO and BPositioIO classes provide
|
||||
// the protocol for Read()/Write()/Seek().
|
||||
//
|
||||
@@ -29,7 +30,10 @@
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
// Standard Includes -----------------------------------------------------------
|
||||
#include <cstdio>
|
||||
#include <algobase.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// System Includes -------------------------------------------------------------
|
||||
#include <DataIO.h>
|
||||
@@ -214,16 +218,15 @@ BMemoryIO::~BMemoryIO()
|
||||
ssize_t
|
||||
BMemoryIO::ReadAt(off_t pos, void *buffer, size_t size)
|
||||
{
|
||||
ssize_t result = size;
|
||||
|
||||
if (pos < 0 || pos >= fLen)
|
||||
result = 0;
|
||||
else if (pos + size >= fLen)
|
||||
result = fLen - pos;
|
||||
|
||||
memcpy(buffer, fBuf + pos, result);
|
||||
|
||||
return result;
|
||||
if (buffer == NULL)
|
||||
return B_BAD_VALUE;
|
||||
|
||||
ssize_t sizeRead = 0;
|
||||
if (pos >= 0 && pos < fLen) {
|
||||
sizeRead = min((off_t)size, fLen - pos);
|
||||
memcpy(buffer, fBuf + pos, sizeRead);
|
||||
}
|
||||
return sizeRead;
|
||||
}
|
||||
|
||||
|
||||
@@ -233,20 +236,19 @@ BMemoryIO::WriteAt(off_t pos, const void *buffer, size_t size)
|
||||
if (fReadOnly)
|
||||
return B_NOT_ALLOWED;
|
||||
|
||||
ssize_t result = size;
|
||||
if (buffer == NULL)
|
||||
return B_BAD_VALUE;
|
||||
|
||||
ssize_t sizeWritten = 0;
|
||||
if (pos >= 0 && pos < fPhys) {
|
||||
sizeWritten = min((off_t)size, fPhys - pos);
|
||||
memcpy(fBuf + pos, buffer, sizeWritten);
|
||||
}
|
||||
|
||||
if (pos < 0 || pos >= fPhys)
|
||||
result = 0;
|
||||
else if (pos + size >= fPhys)
|
||||
result = fPhys - pos;
|
||||
|
||||
if (pos + result > fLen)
|
||||
fLen = pos + result;
|
||||
|
||||
if (result > 0)
|
||||
memcpy(fBuf + pos, buffer, result);
|
||||
|
||||
return result;
|
||||
if (pos + sizeWritten > fLen)
|
||||
fLen = pos + sizeWritten;
|
||||
|
||||
return sizeWritten;
|
||||
}
|
||||
|
||||
|
||||
@@ -283,11 +285,8 @@ BMemoryIO::SetSize(off_t size)
|
||||
if (fReadOnly)
|
||||
return B_NOT_ALLOWED;
|
||||
|
||||
status_t err;
|
||||
|
||||
if (size < 0 || size > fPhys)
|
||||
err = B_ERROR;
|
||||
else {
|
||||
status_t err = B_ERROR;
|
||||
if (size >= 0 && size <= fPhys) {
|
||||
err = B_OK;
|
||||
fLen = size;
|
||||
}
|
||||
@@ -316,10 +315,183 @@ void
|
||||
BMemoryIO::_ReservedMemoryIO2(){}
|
||||
|
||||
|
||||
// *** BMallocIO ***
|
||||
// constructor
|
||||
BMallocIO::BMallocIO()
|
||||
: fBlockSize(256),
|
||||
fMallocSize(0),
|
||||
fLength(0),
|
||||
fData(NULL),
|
||||
fPosition(0)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
// destructor
|
||||
BMallocIO::~BMallocIO()
|
||||
{
|
||||
if (fData)
|
||||
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 >= 0 && pos < fLength) {
|
||||
sizeRead = min((off_t)size, fLength - pos);
|
||||
memcpy(buffer, fData + pos, sizeRead);
|
||||
}
|
||||
return sizeRead;
|
||||
}
|
||||
|
||||
|
||||
// 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(pos + size, (off_t)fLength);
|
||||
status_t error = _Resize(newSize);
|
||||
if (error == B_OK)
|
||||
memcpy(fData + pos, buffer, size);
|
||||
return (error != B_OK ? error : size);
|
||||
}
|
||||
|
||||
|
||||
// Seek
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
// Position
|
||||
off_t
|
||||
BMallocIO::Position() const
|
||||
{
|
||||
return fPosition;
|
||||
}
|
||||
|
||||
|
||||
// SetSize
|
||||
status_t
|
||||
BMallocIO::SetSize(off_t size)
|
||||
{
|
||||
status_t error = (size >= 0 ? B_OK : B_BAD_VALUE );
|
||||
if (error == B_OK)
|
||||
error = _Resize(size);
|
||||
return error;
|
||||
}
|
||||
|
||||
|
||||
// SetBlockSize
|
||||
void
|
||||
BMallocIO::SetBlockSize(size_t blockSize)
|
||||
{
|
||||
if (blockSize == 0)
|
||||
blockSize = 1;
|
||||
if (blockSize != fBlockSize) {
|
||||
fBlockSize = blockSize;
|
||||
_Resize(fLength);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Buffer
|
||||
const void *
|
||||
BMallocIO::Buffer() const
|
||||
{
|
||||
return fData;
|
||||
}
|
||||
|
||||
|
||||
// BufferLength
|
||||
size_t
|
||||
BMallocIO::BufferLength() const
|
||||
{
|
||||
return fLength;
|
||||
}
|
||||
|
||||
|
||||
// _Resize
|
||||
status_t
|
||||
BMallocIO::_Resize(off_t size)
|
||||
{
|
||||
status_t error = (size >= 0 ? B_OK : B_BAD_VALUE);
|
||||
if (error == B_OK) {
|
||||
if (size == 0) {
|
||||
// size == 0, free the memory
|
||||
free(fData);
|
||||
fData = NULL;
|
||||
} else {
|
||||
// size != 0, see, if necessary to resize
|
||||
size_t newSize = (size + fBlockSize - 1) / fBlockSize * fBlockSize;
|
||||
if (newSize != fMallocSize) {
|
||||
// we need to resize
|
||||
if (char *newData = (char*)realloc(fData, newSize)) {
|
||||
// set the new area to 0
|
||||
if (fMallocSize < newSize) {
|
||||
memset(newData + fMallocSize, 0,
|
||||
newSize - fMallocSize);
|
||||
}
|
||||
fData = newData;
|
||||
fMallocSize = newSize;
|
||||
} else // couldn't alloc the memory
|
||||
error = B_NO_MEMORY;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (error == B_OK)
|
||||
fLength = size;
|
||||
return error;
|
||||
}
|
||||
|
||||
|
||||
// FBC
|
||||
void BMallocIO::_ReservedMallocIO1() {}
|
||||
void BMallocIO::_ReservedMallocIO2() {}
|
||||
|
||||
// copy constructor
|
||||
BMallocIO::BMallocIO(const BMallocIO &)
|
||||
{
|
||||
// copying not allowed...
|
||||
}
|
||||
|
||||
|
||||
// assignment operator
|
||||
BMallocIO &
|
||||
BMallocIO::operator=(const BMallocIO &)
|
||||
{
|
||||
// copying not allowed...
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* $Log $
|
||||
*
|
||||
* $Id $
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
+383
-9
@@ -1,9 +1,383 @@
|
||||
/******************************************************************************
|
||||
/
|
||||
/ File: String.cpp
|
||||
/
|
||||
/ Description: Implementation of the BString class
|
||||
/
|
||||
/ Author: Steve Vallee
|
||||
/
|
||||
******************************************************************************/
|
||||
#include <String.h>
|
||||
#include <cstdlib>
|
||||
#include <cctype>
|
||||
|
||||
BString::BString()
|
||||
{
|
||||
_Init(NULL, 0);
|
||||
}
|
||||
|
||||
|
||||
BString::BString(const char* string)
|
||||
{
|
||||
_Init(string, strlen(string));
|
||||
}
|
||||
|
||||
|
||||
BString::BString(const BString &string)
|
||||
{
|
||||
_Init(string.String(), string.Length());
|
||||
}
|
||||
|
||||
|
||||
BString::BString(const char *string, int32 maxLength)
|
||||
{
|
||||
int32 stringLen = strlen(string);
|
||||
int32 len = (maxLength <= stringLen ? maxLength : stringLen);
|
||||
_Init(string, len);
|
||||
}
|
||||
|
||||
|
||||
BString::~BString()
|
||||
{
|
||||
if (_privateData) {
|
||||
_privateData -= sizeof(int32);
|
||||
free(_privateData);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*---- Access --------------------------------------------------------------*/
|
||||
int32
|
||||
BString::CountChars() const
|
||||
{
|
||||
return 0; //TODO: Implement
|
||||
}
|
||||
|
||||
|
||||
/*---- Assignment ----------------------------------------------------------*/
|
||||
BString&
|
||||
BString::operator=(const BString &string)
|
||||
{
|
||||
_DoAssign(string.String(), string.Length());
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
BString&
|
||||
BString::operator=(const char *string)
|
||||
{
|
||||
_DoAssign(string, strlen(string));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
BString&
|
||||
BString::operator=(char c)
|
||||
{
|
||||
char *tmp = (char*)calloc(2, sizeof(char));
|
||||
tmp[0] = c;
|
||||
_DoAssign(tmp, 1);
|
||||
free(tmp);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
BString&
|
||||
BString::SetTo(const char *string, int32 length)
|
||||
{
|
||||
_DoAssign(string, length);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
BString&
|
||||
BString::SetTo(const BString &from)
|
||||
{
|
||||
SetTo(from.String(), from.Length());
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
BString&
|
||||
BString::Adopt(BString &from)
|
||||
{
|
||||
//TODO: implement
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
BString&
|
||||
BString::SetTo(const BString &string, int32 length)
|
||||
{
|
||||
_DoAssign(string.String(), length);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
BString&
|
||||
BString::Adopt(BString &from, int32 length)
|
||||
{
|
||||
//TODO: implement
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
BString&
|
||||
BString::SetTo(char c, int32 count)
|
||||
{
|
||||
char *tmp = (char*)calloc(count + 1, sizeof(char));
|
||||
memset(tmp, c, count);
|
||||
_DoAssign(tmp, count);
|
||||
free(tmp);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/*---- Substring copying ---------------------------------------------------*/
|
||||
void
|
||||
BString::CopyInto(char *into, int32 fromOffset, int32 length) const
|
||||
{
|
||||
int32 len = (Length() - fromOffset < length ? Length() - fromOffset : length);
|
||||
strncpy(into, _privateData + fromOffset, len);
|
||||
}
|
||||
|
||||
|
||||
/*---- Appending -----------------------------------------------------------*/
|
||||
BString&
|
||||
BString::operator+=(const char *str)
|
||||
{
|
||||
_DoAppend(str, strlen(str));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
BString&
|
||||
BString::operator+=(char c)
|
||||
{
|
||||
char *tmp = (char*)calloc(2, sizeof(char));
|
||||
tmp[0] = c;
|
||||
_DoAppend(tmp, 1);
|
||||
free(tmp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
BString&
|
||||
BString::Append(const BString &string, int32 length)
|
||||
{
|
||||
_DoAppend(string.String(), length);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
BString&
|
||||
BString::Append(const char *str, int32 length)
|
||||
{
|
||||
_DoAppend(str, length);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
BString&
|
||||
BString::Append(char c, int32 count)
|
||||
{
|
||||
char *tmp = (char*)calloc(count + 1, sizeof(char));
|
||||
memset(tmp, c, count);
|
||||
_DoAppend(tmp, count);
|
||||
free(tmp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
/*---- Prepending ----------------------------------------------------------*/
|
||||
BString&
|
||||
BString::Prepend(const char *str)
|
||||
{
|
||||
_DoPrepend(str, strlen(str));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
BString&
|
||||
BString::Prepend(const BString &string)
|
||||
{
|
||||
_DoPrepend(string.String(), string.Length());
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
BString&
|
||||
BString::Prepend(const char *str, int32 len)
|
||||
{
|
||||
_DoPrepend(str, len);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
BString&
|
||||
BString::Prepend(const BString &string, int32 len)
|
||||
{
|
||||
_DoPrepend(string.String(), len);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
BString&
|
||||
BString::Prepend(char c, int32 count)
|
||||
{
|
||||
char *tmp = (char*)calloc(count + 1, sizeof(char));
|
||||
memset(tmp, c, count);
|
||||
_DoPrepend(tmp, count);
|
||||
free(tmp);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
/*---- Inserting ----------------------------------------------------------*/
|
||||
|
||||
/*---- Simple sprintf replacement calls ------------------------------------*/
|
||||
/*---- Slower than sprintf but type and overflow safe ----------------------*/
|
||||
BString&
|
||||
BString::operator<<(const char *str)
|
||||
{
|
||||
_DoAppend(str, strlen(str));
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
BString&
|
||||
BString::operator<<(const BString &string)
|
||||
{
|
||||
_DoAppend(string.String(), string.Length());
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
BString&
|
||||
BString::operator<<(char c)
|
||||
{
|
||||
char *tmp = (char*)calloc(2, sizeof(char));
|
||||
tmp[0] = c;
|
||||
_DoAppend(tmp, 1);
|
||||
free(tmp);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
BString&
|
||||
BString::operator<<(int i)
|
||||
{
|
||||
//TODO: implement
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
BString&
|
||||
BString::operator<<(unsigned int i)
|
||||
{
|
||||
//TODO: implement
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
BString&
|
||||
BString::operator<<(uint32 i)
|
||||
{
|
||||
//TODO: implement
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
BString&
|
||||
BString::operator<<(int32 i)
|
||||
{
|
||||
//TODO: implement
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
BString&
|
||||
BString::operator<<(uint64 i)
|
||||
{
|
||||
//TODO: implement
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
BString&
|
||||
BString::operator<<(int64 i)
|
||||
{
|
||||
//TODO: implement
|
||||
return *this;
|
||||
}
|
||||
|
||||
BString&
|
||||
BString::operator<<(float f)
|
||||
{
|
||||
//TODO: implement
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
/*---- Private or Reserved ------------------------------------------------*/
|
||||
void
|
||||
BString::_Init(const char* string, int32 len)
|
||||
{
|
||||
_privateData = (char*)calloc(len + sizeof(int32) + 1, sizeof(char));
|
||||
_SetLength(len);
|
||||
_privateData += sizeof(int32);
|
||||
strncpy(_privateData, string, len);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
BString::_DoAssign(const char *string, int32 len)
|
||||
{
|
||||
if (_privateData) {
|
||||
_privateData -= sizeof(int32);
|
||||
free(_privateData);
|
||||
}
|
||||
_privateData = (char*)calloc(len + sizeof(int32) + 1, sizeof(char));
|
||||
_SetLength(len);
|
||||
_privateData += sizeof(int32);
|
||||
strncpy(_privateData, string, len);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
BString::_DoAppend(const char *str, int32 len)
|
||||
{
|
||||
_privateData -= sizeof(int32);
|
||||
_privateData = _GrowBy(len);
|
||||
_SetLength(len);
|
||||
_privateData += sizeof(int32);
|
||||
strncat(_privateData, str, len);
|
||||
}
|
||||
|
||||
|
||||
char*
|
||||
BString::_GrowBy(int32 size)
|
||||
{
|
||||
int32 curLen = Length();
|
||||
_privateData = (char*)realloc(_privateData, curLen + size);
|
||||
return _privateData;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
BString::_DoPrepend(const char *str, int32 count)
|
||||
{
|
||||
char *tmp = strdup(_privateData);
|
||||
char *src = strdup(str);
|
||||
|
||||
_privateData -= sizeof(int32);
|
||||
free(_privateData);
|
||||
_privateData = (char*)calloc(strlen(str) + 1 + sizeof(int32), sizeof(char));
|
||||
|
||||
_SetLength(strlen(tmp) + count);
|
||||
_privateData += sizeof(int32);
|
||||
strncpy(_privateData, src, count);
|
||||
strcat(_privateData, tmp);
|
||||
free(tmp);
|
||||
free(src);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
BString::_SetLength(int32 length)
|
||||
{
|
||||
*(int32*)_privateData = length;
|
||||
}
|
||||
Reference in New Issue
Block a user