Small style changes, just to improve my commit stats :)

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@13214 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stefano Ceccherini
2005-06-20 08:28:12 +00:00
parent f353b33a85
commit d0164ea34a
+316 -339
View File
@@ -1,339 +1,316 @@
//------------------------------------------------------------------------------ /*
// Copyright (c) 2001-2002, OpenBeOS * Copyright (c) 2001-2005, Haiku
// * Distributed under the terms of the MIT license
// Permission is hereby granted, free of charge, to any person obtaining a * Authors:
// copy of this software and associated documentation files (the "Software"), Stefano Ceccherini ([email protected])
// 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 // A buffered adapter for BPositionIO objects.
// Software is furnished to do so, subject to the following conditions:
// #include <stdio.h>
// The above copyright notice and this permission notice shall be included in #include <stdlib.h>
// all copies or substantial portions of the Software. #include <string.h>
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #include <BufferIO.h>
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE /*! \class BBufferIO
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER \brief A buffered adapter for BPositionIO objects.
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING \author <a href='mailto:[email protected]>Stefano Ceccherini</a>
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER */
// DEALINGS IN THE SOFTWARE.
//
// File Name: BufferIO.cpp /*! \brief Initializes a BBufferIO object.
// Author(s): Stefano Ceccherini ([email protected])
// Initializes the object, creates a buffer of the given size
// Description: A buffered adapter for BPositionIO objects. and associates the object with the given BPositionIO stream.
//------------------------------------------------------------------------------
\param stream A pointer to a BPositionIO object.
// Standard Includes ----------------------------------------------------------- \param buf_size The size of the buffer that the object will allocate and use.
#include <stdio.h> \param owns_stream Specifies if the object will delete the stream on destruction.
#include <stdlib.h> */
#include <string.h> BBufferIO::BBufferIO(BPositionIO *stream, size_t buf_size, bool owns_stream)
:m_buffer_start(0),
// System Includes ------------------------------------------------------------- m_stream(stream),
#include <BufferIO.h> m_buffer(NULL),
m_buffer_used(0),
/*! \class BBufferIO m_buffer_dirty(false),
\brief A buffered adapter for BPositionIO objects. m_owns_stream(owns_stream)
\author <a href='mailto:[email protected]>Stefano Ceccherini</a>
*/ {
if (buf_size < 512)
buf_size = 512;
/*! \brief Initializes a BBufferIO object.
m_buffer_phys = buf_size;
Initializes the object, creates a buffer of the given size
and associates the object with the given BPositionIO stream. // What can we do if this malloc fails ?
// I think R5 uses new, but doesn't catch the thrown exception
\param stream A pointer to a BPositionIO object. // (if you specify a very big buffer, the application just
\param buf_size The size of the buffer that the object will allocate and use. // terminates with abort).
\param owns_stream Specifies if the object will delete the stream on destruction. m_buffer = (char *)malloc(m_buffer_phys * sizeof(char));
*/ }
BBufferIO::BBufferIO(BPositionIO *stream, size_t buf_size, bool owns_stream)
:m_buffer_start(0),
m_stream(stream), /*! \brief Frees the resources allocated by the object
m_buffer(NULL),
m_buffer_used(0), Flushes pending changes to the stream and frees the allocated memory.
m_buffer_dirty(false), If the owns_stream property is true, the destructor also
m_owns_stream(owns_stream) deletes the stream associated with the BBufferIO object.
*/
{ BBufferIO::~BBufferIO()
if (buf_size < 512) {
buf_size = 512; if (m_buffer_dirty)
Flush(); // Writes pending changes to the stream
m_buffer_phys = buf_size;
free(m_buffer);
// What can we do if this malloc fails ?
// I think R5 uses new, but doesn't catch the thrown exception if (m_owns_stream)
// (if you specify a very big buffer, the application just delete m_stream;
// terminates with abort). }
m_buffer = (char*)malloc(m_buffer_phys * sizeof(char));
}
/*! \brief Reads the specified amount of bytes at the given position.
\param pos The offset into the stream where to read.
/*! \brief Frees the resources allocated by the object \param buffer A pointer to a buffer where to copy the read data.
\param size The amount of bytes to read.
Flushes pending changes to the stream and frees the allocated memory. \return The amount of bytes actually read, or an error code.
If the owns_stream property is true, the destructor also */
deletes the stream associated with the BBufferIO object. ssize_t
*/ BBufferIO::ReadAt(off_t pos, void *buffer, size_t size)
BBufferIO::~BBufferIO() {
{ // We refuse to crash, even if
if (m_buffer_dirty) // you were lazy and didn't give a valid
Flush(); // Writes pending changes to the stream // stream on construction.
if (m_stream == NULL)
free(m_buffer); return B_NO_INIT;
if (m_owns_stream) if (buffer == NULL)
delete m_stream; return B_BAD_VALUE;
}
// If the amount of data we want doesn't fit in the buffer, just
// read it directly from the disk (and don't touch the buffer).
/*! \brief Reads the specified amount of bytes at the given position. if (size > m_buffer_phys) {
\param pos The offset into the stream where to read. if (m_buffer_dirty)
\param buffer A pointer to a buffer where to copy the read data. Flush();
\param size The amount of bytes to read. return m_stream->ReadAt(pos, buffer, size);
\return The amount of bytes actually read, or an error code. }
*/
ssize_t // If the data we are looking for is not in the buffer...
BBufferIO::ReadAt(off_t pos, void *buffer, size_t size) if (size > m_buffer_used
{ || pos < m_buffer_start
// We refuse to crash, even if || pos > m_buffer_start + m_buffer_used
// you were lazy and didn't give a valid || pos + size > m_buffer_start + m_buffer_used) {
// stream on construction.
if (m_stream == NULL) if (m_buffer_dirty)
return B_NO_INIT; Flush(); // If there are pending writes, do them.
if (buffer == NULL) // ...cache as much as we can from the stream
return B_BAD_VALUE; m_buffer_used = m_stream->ReadAt(pos, m_buffer, m_buffer_phys);
// If the amount of data we want doesn't fit in the buffer, just if (m_buffer_used > 0)
// read it directly from the disk (and don't touch the buffer). m_buffer_start = pos; // The data is buffered starting from this offset
if (size > m_buffer_phys) }
{
if (m_buffer_dirty) size = min_c(size, m_buffer_used);
Flush();
return m_stream->ReadAt(pos, buffer, size); // copy data from the cache to the given buffer
} memcpy(buffer, m_buffer + pos - m_buffer_start, size);
// If the data we are looking for is not in the buffer... return size;
if (size > m_buffer_used }
|| pos < m_buffer_start
|| pos > m_buffer_start + m_buffer_used
|| pos + size > m_buffer_start + m_buffer_used) /*! \brief Writes the specified amount of bytes at the given position.
{ \param pos The offset into the stream where to write.
if (m_buffer_dirty) \param buffer A pointer to a buffer which contains the data to write.
Flush(); // If there are pending writes, do them. \param size The amount of bytes to write.
\return The amount of bytes actually written, or an error code.
// ...cache as much as we can from the stream */
m_buffer_used = m_stream->ReadAt(pos, m_buffer, m_buffer_phys); ssize_t
BBufferIO::WriteAt(off_t pos, const void *buffer, size_t size)
if (m_buffer_used > 0) {
m_buffer_start = pos; // The data is buffered starting from this offset if (m_stream == NULL)
} return B_NO_INIT;
size = min_c(size, m_buffer_used); if (buffer == NULL)
return B_BAD_VALUE;
// copy data from the cache to the given buffer
memcpy(buffer, m_buffer + pos - m_buffer_start, size); // If data doesn't fit into the buffer, write it directly to the stream
if (size > m_buffer_phys)
return size; return m_stream->WriteAt(pos, buffer, size);
}
// If we have cached data in the buffer, whose offset into the stream
// is > 0, and the buffer isn't dirty, drop the data.
/*! \brief Writes the specified amount of bytes at the given position. if (!m_buffer_dirty && m_buffer_start > pos) {
\param pos The offset into the stream where to write. m_buffer_start = 0;
\param buffer A pointer to a buffer which contains the data to write. m_buffer_used = 0;
\param size The amount of bytes to write. }
\return The amount of bytes actually written, or an error code.
*/ // If we want to write beyond the cached data...
ssize_t if (pos > m_buffer_start + m_buffer_used
BBufferIO::WriteAt(off_t pos, const void *buffer, size_t size) || pos < m_buffer_start) {
{ ssize_t read;
if (m_stream == NULL) off_t where = pos;
return B_NO_INIT;
if (pos + size <= m_buffer_phys) // Can we just cache from the beginning ?
if (buffer == NULL) where = 0;
return B_BAD_VALUE;
// ...cache more.
// If data doesn't fit into the buffer, write it directly to the stream read = m_stream->ReadAt(where, m_buffer, m_buffer_phys);
if (size > m_buffer_phys) if (read > 0) {
return m_stream->WriteAt(pos, buffer, size); m_buffer_used = read;
m_buffer_start = where;
// If we have cached data in the buffer, whose offset into the stream }
// is > 0, and the buffer isn't dirty, drop the data. }
if (!m_buffer_dirty && m_buffer_start > pos)
{ memcpy(m_buffer + pos - m_buffer_start, buffer, size);
m_buffer_start = 0;
m_buffer_used = 0; m_buffer_dirty = true;
} m_buffer_used = max_c((size + pos), m_buffer_used);
// If we want to write beyond the cached data... return size;
if (pos > m_buffer_start + m_buffer_used }
|| pos < m_buffer_start)
{
ssize_t read; /*! \brief Sets the position in the stream.
off_t where = pos;
Sets the position in the stream where the Read() and Write() functions
if (pos + size <= m_buffer_phys) // Can we just cache from the beginning ? (inherited from BPositionIO) begin reading and writing.
where = 0; How the position argument is understood depends on the seek_mode flag.
// ...cache more. \param position The position where you want to seek.
read = m_stream->ReadAt(where, m_buffer, m_buffer_phys); \param seek_mode Can have three values:
if (read > 0)
{ - \c SEEK_SET. The position passed is an offset from the beginning of the stream;
m_buffer_used = read; in other words, the current position is set to position.
m_buffer_start = where; For this mode, position should be a positive value.
}
} - \c SEEK_CUR. The position argument is an offset from the current position;
memcpy(m_buffer + pos - m_buffer_start, buffer, size); the value of the argument is added to the current position.
m_buffer_dirty = true;
- \c SEEK_END. The position argument is an offset from the end of the stream.
m_buffer_used = max_c((size + pos), m_buffer_used); In this mode the position argument should be negative (or zero).
return size; \return The current position as an offset in bytes
} from the beginning of the stream.
*/
off_t
/*! \brief Sets the position in the stream. BBufferIO::Seek(off_t position, uint32 seek_mode)
{
Sets the position in the stream where the Read() and Write() functions if (m_stream == NULL)
(inherited from BPositionIO) begin reading and writing. return B_NO_INIT;
How the position argument is understood depends on the seek_mode flag.
return m_stream->Seek(position, seek_mode);
\param position The position where you want to seek. }
\param seek_mode Can have three values:
- \c SEEK_SET. The position passed is an offset from the beginning of the stream; /*! \brief Return the current position in the stream.
in other words, the current position is set to position. \return The current position as an offset in bytes
For this mode, position should be a positive value. from the beginning of the stream.
*/
- \c SEEK_CUR. The position argument is an offset from the current position; off_t
the value of the argument is added to the current position. BBufferIO::Position() const
{
- \c SEEK_END. The position argument is an offset from the end of the stream. if (m_stream == NULL)
In this mode the position argument should be negative (or zero). return B_NO_INIT;
\return The current position as an offset in bytes return m_stream->Position();
from the beginning of the stream. }
*/
off_t
BBufferIO::Seek(off_t position, uint32 seek_mode) /*! \brief Calls the SetSize() function of the assigned BPositionIO object.
{ \param size The new size of the BPositionIO object.
if (m_stream == NULL) \return An error code.
return B_NO_INIT; */
status_t
return m_stream->Seek(position, seek_mode); BBufferIO::SetSize(off_t size)
} {
if (m_stream == NULL)
return B_NO_INIT;
/*! \brief Return the current position in the stream.
\return The current position as an offset in bytes return m_stream->SetSize(size);
from the beginning of the stream. }
*/
off_t
BBufferIO::Position() const /*! \brief Writes pending modifications to the stream.
{ \return An error code.
if (m_stream == NULL) */
return B_NO_INIT; status_t
BBufferIO::Flush()
return m_stream->Position(); {
} if (!m_buffer_dirty)
return B_OK;
/*! \brief Calls the SetSize() function of the assigned BPositionIO object. // Write the cached data to the stream
\param size The new size of the BPositionIO object. status_t err = m_stream->WriteAt(m_buffer_start, m_buffer, m_buffer_used);
\return An error code.
*/ if (err > 0)
status_t m_buffer_dirty = false;
BBufferIO::SetSize(off_t size)
{ return (err < 0) ? err : B_OK;
if (m_stream == NULL) }
return B_NO_INIT;
return m_stream->SetSize(size); /*! \brief Returns a pointer to the stream specified on construction
} \return A pointer to the BPositionIO stream specified on construction.
*/
BPositionIO *
/*! \brief Writes pending modifications to the stream. BBufferIO::Stream() const
\return An error code. {
*/ return m_stream;
status_t }
BBufferIO::Flush()
{
if (!m_buffer_dirty) /*! \brief Returns the size of the internal buffer
return B_OK; \return The size of the buffer allocated by the object
*/
// Write the cached data to the stream size_t
status_t err = m_stream->WriteAt(m_buffer_start, m_buffer, m_buffer_used); BBufferIO::BufferSize() const
{
if (err > 0) return m_buffer_phys;
m_buffer_dirty = false; }
return (err < 0) ? err : B_OK;
} /*! \brief Tells if the BBufferIO object "owns" the specified stream.
\return A boolean value, which is true if the object "owns"
the stream (and so will delete it upon destruction), false if not.
/*! \brief Returns a pointer to the stream specified on construction */
\return A pointer to the BPositionIO stream specified on construction. bool
*/ BBufferIO::OwnsStream() const
BPositionIO * {
BBufferIO::Stream() const return m_owns_stream;
{ }
return m_stream;
}
/*! \brief Set the "owns_stream" property of the object.
\param owns_stream If it's true, the object will delete the stream
/*! \brief Returns the size of the internal buffer upon destruction, if it's false it will not.
\return The size of the buffer allocated by the object */
*/ void
size_t BBufferIO::SetOwnsStream(bool owns_stream)
BBufferIO::BufferSize() const {
{ m_owns_stream = owns_stream;
return m_buffer_phys; }
}
/*! \brief Prints the object to stdout.
/*! \brief Tells if the BBufferIO object "owns" the specified stream. */
\return A boolean value, which is true if the object "owns" void
the stream (and so will delete it upon destruction), false if not. BBufferIO::PrintToStream() const
*/ {
bool printf("stream %p\n", m_stream);
BBufferIO::OwnsStream() const printf("buffer %p\n", m_buffer);
{ printf("start %lld\n", m_buffer_start);
return m_owns_stream; printf("used %ld\n", m_buffer_used);
} printf("phys %ld\n", m_buffer_phys);
printf("dirty %s\n", (m_buffer_dirty) ? "true" : "false");
printf("owns %s\n", (m_owns_stream) ? "true" : "false");
/*! \brief Set the "owns_stream" property of the object. }
\param owns_stream If it's true, the object will delete the stream
upon destruction, if it's false it will not.
*/ // These functions are here to maintain future binary
void // compatibility.
BBufferIO::SetOwnsStream(bool owns_stream) status_t BBufferIO::_Reserved_BufferIO_0(void *) { return B_ERROR; }
{ status_t BBufferIO::_Reserved_BufferIO_1(void *) { return B_ERROR; }
m_owns_stream = owns_stream; status_t BBufferIO::_Reserved_BufferIO_2(void *) { return B_ERROR; }
} status_t BBufferIO::_Reserved_BufferIO_3(void *) { return B_ERROR; }
status_t BBufferIO::_Reserved_BufferIO_4(void *) { return B_ERROR; }
/*! \brief Prints the object to stdout.
*/
void
BBufferIO::PrintToStream() const
{
printf("stream %p\n", m_stream);
printf("buffer %p\n", m_buffer);
printf("start %lld\n", m_buffer_start);
printf("used %ld\n", m_buffer_used);
printf("phys %ld\n", m_buffer_phys);
printf("dirty %s\n", (m_buffer_dirty) ? "true" : "false");
printf("owns %s\n", (m_owns_stream) ? "true" : "false");
}
// These functions are here to maintain future binary
// compatibility.
status_t BBufferIO::_Reserved_BufferIO_0(void *) { return B_ERROR; }
status_t BBufferIO::_Reserved_BufferIO_1(void *) { return B_ERROR; }
status_t BBufferIO::_Reserved_BufferIO_2(void *) { return B_ERROR; }
status_t BBufferIO::_Reserved_BufferIO_3(void *) { return B_ERROR; }
status_t BBufferIO::_Reserved_BufferIO_4(void *) { return B_ERROR; }