2007-12-20 12:14:21 -08:00
|
|
|
#ifndef INCLUDED_WRITE_BUFFER
|
|
|
|
|
#define INCLUDED_WRITE_BUFFER
|
|
|
|
|
|
2008-01-07 12:03:19 -08:00
|
|
|
#include "lib/file/file.h"
|
2007-12-22 10:15:52 -08:00
|
|
|
|
2008-01-07 12:03:19 -08:00
|
|
|
class WriteBuffer
|
2007-12-20 12:14:21 -08:00
|
|
|
{
|
|
|
|
|
public:
|
2007-12-22 10:15:52 -08:00
|
|
|
WriteBuffer();
|
2007-12-20 12:14:21 -08:00
|
|
|
|
2007-12-22 10:15:52 -08:00
|
|
|
void Append(const void* data, size_t size);
|
|
|
|
|
void Overwrite(const void* data, size_t size, size_t offset);
|
2007-12-20 12:14:21 -08:00
|
|
|
|
|
|
|
|
shared_ptr<u8> Data() const
|
|
|
|
|
{
|
|
|
|
|
return m_data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t Size() const
|
|
|
|
|
{
|
|
|
|
|
return m_size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
size_t m_capacity; // must come first (init order)
|
|
|
|
|
|
|
|
|
|
shared_ptr<u8> m_data;
|
|
|
|
|
size_t m_size;
|
|
|
|
|
};
|
|
|
|
|
|
2007-12-22 10:15:52 -08:00
|
|
|
|
2008-02-25 13:19:52 -08:00
|
|
|
class UnalignedWriter : public noncopyable
|
2007-12-22 10:15:52 -08:00
|
|
|
{
|
|
|
|
|
public:
|
2008-07-17 07:23:51 -07:00
|
|
|
UnalignedWriter(const PIFile& file, off_t ofs);
|
2007-12-22 10:15:52 -08:00
|
|
|
~UnalignedWriter();
|
|
|
|
|
|
|
|
|
|
/**
|
2008-06-16 11:36:36 -07:00
|
|
|
* add data to the align buffer, writing it out to disk if full.
|
|
|
|
|
**/
|
2007-12-22 10:15:52 -08:00
|
|
|
LibError Append(const u8* data, size_t size) const;
|
|
|
|
|
|
|
|
|
|
/**
|
2008-06-16 11:36:36 -07:00
|
|
|
* zero-initialize any remaining space in the align buffer and write
|
|
|
|
|
* it to the file. this is called by the destructor.
|
|
|
|
|
**/
|
2007-12-22 10:15:52 -08:00
|
|
|
void Flush() const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
LibError WriteBlock() const;
|
|
|
|
|
|
2008-01-07 12:03:19 -08:00
|
|
|
PIFile m_file;
|
2007-12-22 10:15:52 -08:00
|
|
|
shared_ptr<u8> m_alignedBuf;
|
|
|
|
|
mutable off_t m_alignedOfs;
|
|
|
|
|
mutable size_t m_bytesUsed;
|
|
|
|
|
};
|
|
|
|
|
|
2008-06-16 11:36:36 -07:00
|
|
|
typedef shared_ptr<UnalignedWriter> PUnalignedWriter;
|
|
|
|
|
|
2007-12-20 12:14:21 -08:00
|
|
|
#endif // #ifndef INCLUDED_WRITE_BUFFER
|