blah/src/streams/filestream.cpp

96 lines
1.5 KiB
C++
Raw Normal View History

2020-08-26 15:38:01 +08:00
#include <blah/streams/filestream.h>
#include <blah/common.h>
#include "../internal/platform_backend.h"
2021-05-07 12:48:06 +08:00
#include <cstring>
2020-08-26 15:38:01 +08:00
using namespace Blah;
FileStream::FileStream()
{
2021-04-05 16:07:16 +08:00
m_mode = FileMode::OpenRead;
2020-08-26 15:38:01 +08:00
}
2021-03-21 17:08:28 +08:00
FileStream::FileStream(const FilePath& path, FileMode mode)
2020-08-26 15:38:01 +08:00
: m_mode(mode)
, m_file(File::open(path, mode))
2020-08-26 15:38:01 +08:00
{
2020-08-26 15:38:01 +08:00
}
FileStream::FileStream(FileStream&& src) noexcept
{
m_file = src.m_file;
2020-08-26 15:38:01 +08:00
m_mode = src.m_mode;
}
FileStream& FileStream::operator=(FileStream&& src) noexcept
{
m_file = src.m_file;
2020-08-26 15:38:01 +08:00
m_mode = src.m_mode;
return *this;
}
size_t FileStream::length() const
2020-08-26 15:38:01 +08:00
{
if (m_file)
return m_file->length();
return 0;
2020-08-26 15:38:01 +08:00
}
size_t FileStream::position() const
2020-08-26 15:38:01 +08:00
{
if (m_file)
return m_file->position();
return 0;
2020-08-26 15:38:01 +08:00
}
size_t FileStream::seek(size_t seek_to)
2020-08-26 15:38:01 +08:00
{
if (m_file)
return m_file->seek(seek_to);
return 0;
2020-08-26 15:38:01 +08:00
}
2021-03-21 17:08:28 +08:00
bool FileStream::is_open() const
{
return m_file.get();
2021-03-21 17:08:28 +08:00
}
bool FileStream::is_readable() const
{
return m_file.get() && (m_mode != FileMode::CreateWrite);
2021-03-21 17:08:28 +08:00
}
bool FileStream::is_writable() const
{
return m_file.get() && (m_mode != FileMode::OpenRead);
2021-03-21 17:08:28 +08:00
}
size_t FileStream::read_into(void* ptr, size_t length)
2020-08-26 15:38:01 +08:00
{
if (length <= 0)
2020-08-26 15:38:01 +08:00
return 0;
if (m_file)
return m_file->read((unsigned char*)ptr, length);
BLAH_ASSERT(false, "Unable to read from Stream");
return 0;
2020-08-26 15:38:01 +08:00
}
size_t FileStream::write_from(const void* ptr, size_t length)
2020-08-26 15:38:01 +08:00
{
if (length <= 0)
return 0;
if (m_file)
return m_file->write((const unsigned char*)ptr, length);
2020-08-26 15:38:01 +08:00
BLAH_ASSERT(false, "Unable to write to Stream");
return 0;
2020-08-26 15:38:01 +08:00
}
void FileStream::close()
{
m_file.reset();
2020-08-26 15:38:01 +08:00
}