Added a filesystem utility class for providing a fixed buffer.
This commit is contained in:
parent
260eab8e44
commit
f3532081aa
|
@ -141,5 +141,79 @@ bool DevLineCommand::IsWritable()
|
|||
return true;
|
||||
}
|
||||
|
||||
|
||||
DevMemoryBuffer::DevMemoryBuffer(uint8_t* buf, size_t bufsize, bool write,
|
||||
bool deletebuf)
|
||||
{
|
||||
this->buf = buf;
|
||||
this->bufsize = bufsize;
|
||||
this->write = write;
|
||||
this->deletebuf = deletebuf;
|
||||
}
|
||||
|
||||
DevMemoryBuffer::~DevMemoryBuffer()
|
||||
{
|
||||
if ( deletebuf )
|
||||
delete[] buf;
|
||||
}
|
||||
|
||||
size_t DevMemoryBuffer::BlockSize()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
uintmax_t DevMemoryBuffer::Size()
|
||||
{
|
||||
return bufsize;
|
||||
}
|
||||
|
||||
uintmax_t DevMemoryBuffer::Position()
|
||||
{
|
||||
return off;
|
||||
}
|
||||
|
||||
bool DevMemoryBuffer::Seek(uintmax_t position)
|
||||
{
|
||||
off = position;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DevMemoryBuffer::Resize(uintmax_t size)
|
||||
{
|
||||
if ( size != bufsize ) { Error::Set(EPERM); return false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
ssize_t DevMemoryBuffer::Read(byte* dest, size_t count)
|
||||
{
|
||||
if ( bufsize <= off ) { return 0; }
|
||||
size_t available = bufsize - off;
|
||||
if ( available < count ) { count = available; }
|
||||
Memory::Copy(dest, buf + off, count);
|
||||
off += count;
|
||||
return count;
|
||||
}
|
||||
|
||||
ssize_t DevMemoryBuffer::Write(const byte* src, size_t count)
|
||||
{
|
||||
if ( !write ) { Error::Set(EBADF); return -1; }
|
||||
if ( bufsize <= off ) { Error::Set(EPERM); return -1; }
|
||||
size_t available = bufsize - off;
|
||||
if ( available < count ) { count = available; }
|
||||
Memory::Copy(buf + off, src, count);
|
||||
off += count;
|
||||
return count;
|
||||
}
|
||||
|
||||
bool DevMemoryBuffer::IsReadable()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DevMemoryBuffer::IsWritable()
|
||||
{
|
||||
return write;
|
||||
}
|
||||
|
||||
} // namespace Sortix
|
||||
|
||||
|
|
|
@ -73,6 +73,33 @@ private:
|
|||
|
||||
};
|
||||
|
||||
class DevMemoryBuffer : public DevBuffer
|
||||
{
|
||||
public:
|
||||
DevMemoryBuffer(uint8_t* buf, size_t bufsize, bool write = true,
|
||||
bool deletebuf = true);
|
||||
~DevMemoryBuffer();
|
||||
|
||||
private:
|
||||
uint8_t* buf;
|
||||
size_t bufsize;
|
||||
size_t off;
|
||||
bool write;
|
||||
bool deletebuf;
|
||||
|
||||
public:
|
||||
virtual size_t BlockSize();
|
||||
virtual uintmax_t Size();
|
||||
virtual uintmax_t Position();
|
||||
virtual bool Seek(uintmax_t position);
|
||||
virtual bool Resize(uintmax_t size);
|
||||
virtual ssize_t Read(byte* dest, size_t count);
|
||||
virtual ssize_t Write(const byte* src, size_t count);
|
||||
virtual bool IsReadable();
|
||||
virtual bool IsWritable();
|
||||
|
||||
};
|
||||
|
||||
} // namespace Sortix
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue