add gcount to fs utils
parent
1b4e36416a
commit
d3ed46686e
|
@ -11,7 +11,8 @@
|
|||
#include <ios>
|
||||
#include "memory.h"
|
||||
|
||||
namespace blt::fs {
|
||||
namespace blt::fs
|
||||
{
|
||||
|
||||
/**
|
||||
* A simple interface which provides a way of reading the next block of data from a resource.
|
||||
|
@ -20,12 +21,14 @@ namespace blt::fs {
|
|||
* Reading of a large number of bytes ( > block size) is guaranteed to not significantly increase the read time and will likely result in a
|
||||
* direct passthrough to the underlying system. Small reads will be buffered, hence the name "block" reader.
|
||||
*/
|
||||
class block_reader {
|
||||
class block_reader
|
||||
{
|
||||
protected:
|
||||
// 32768 block size seems the fastest on my system
|
||||
unsigned long m_bufferSize;
|
||||
public:
|
||||
explicit block_reader(size_t bufferSize): m_bufferSize(bufferSize) {}
|
||||
explicit block_reader(size_t bufferSize): m_bufferSize(bufferSize)
|
||||
{}
|
||||
|
||||
/**
|
||||
* Reads bytes from the internal filesystem implementation
|
||||
|
@ -34,7 +37,11 @@ namespace blt::fs {
|
|||
* @return status code. non-zero return codes indicates a failure has occurred.
|
||||
*/
|
||||
virtual int read(char* buffer, size_t bytes) = 0;
|
||||
virtual char get(){
|
||||
|
||||
virtual int gcount() = 0;
|
||||
|
||||
virtual char get()
|
||||
{
|
||||
char c[1];
|
||||
read(c, 1);
|
||||
return c[0];
|
||||
|
@ -44,11 +51,13 @@ namespace blt::fs {
|
|||
/**
|
||||
* A buffered block writer without a definite backend implementation. Exactly the same as a block_reader but for writing to the filesystem.
|
||||
*/
|
||||
class block_writer {
|
||||
class block_writer
|
||||
{
|
||||
protected:
|
||||
unsigned long m_bufferSize;
|
||||
public:
|
||||
explicit block_writer(unsigned long bufferSize): m_bufferSize(bufferSize) {}
|
||||
explicit block_writer(unsigned long bufferSize): m_bufferSize(bufferSize)
|
||||
{}
|
||||
|
||||
/**
|
||||
* Writes the bytes to the filesystem backend implementation
|
||||
|
@ -57,7 +66,9 @@ namespace blt::fs {
|
|||
* @return non-zero code if failure
|
||||
*/
|
||||
virtual int write(char* buffer, size_t bytes) = 0;
|
||||
virtual int put(char c){
|
||||
|
||||
virtual int put(char c)
|
||||
{
|
||||
char a[1];
|
||||
a[0] = c;
|
||||
return write(a, 1);
|
||||
|
@ -72,7 +83,8 @@ namespace blt::fs {
|
|||
/**
|
||||
* fstream implementation of the block reader.
|
||||
*/
|
||||
class fstream_block_reader : public block_reader {
|
||||
class fstream_block_reader : public block_reader
|
||||
{
|
||||
private:
|
||||
std::fstream& m_stream;
|
||||
char* m_buffer = nullptr;
|
||||
|
@ -90,20 +102,29 @@ namespace blt::fs {
|
|||
|
||||
int read(char* buffer, size_t bytes) override;
|
||||
|
||||
~fstream_block_reader() {
|
||||
int gcount() final {
|
||||
return m_stream.gcount();
|
||||
}
|
||||
|
||||
~fstream_block_reader()
|
||||
{
|
||||
delete[] m_buffer;
|
||||
}
|
||||
};
|
||||
|
||||
class fstream_block_writer : public block_writer {
|
||||
class fstream_block_writer : public block_writer
|
||||
{
|
||||
private:
|
||||
std::fstream& m_stream;
|
||||
char* m_buffer;
|
||||
size_t writeIndex = 0;
|
||||
|
||||
void flush_internal();
|
||||
|
||||
public:
|
||||
explicit fstream_block_writer(std::fstream& stream, size_t bufferSize = 131072):
|
||||
block_writer(bufferSize), m_stream(stream), m_buffer(new char[bufferSize]) {}
|
||||
block_writer(bufferSize), m_stream(stream), m_buffer(new char[bufferSize])
|
||||
{}
|
||||
|
||||
explicit fstream_block_writer(fstream_block_writer& copy) = delete;
|
||||
|
||||
|
@ -114,11 +135,14 @@ namespace blt::fs {
|
|||
fstream_block_writer& operator=(const fstream_block_writer&& move) = delete;
|
||||
|
||||
int write(char* buffer, size_t bytes) override;
|
||||
inline void flush() override {
|
||||
|
||||
inline void flush() override
|
||||
{
|
||||
flush_internal();
|
||||
}
|
||||
|
||||
~fstream_block_writer() {
|
||||
~fstream_block_writer()
|
||||
{
|
||||
flush_internal();
|
||||
delete[] m_buffer;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue