summaryrefslogtreecommitdiff
path: root/source/fud_c_file.cpp
diff options
context:
space:
mode:
authorDominick Allen <djallen@librehumanitas.org>2024-10-01 23:04:25 -0500
committerDominick Allen <djallen@librehumanitas.org>2024-10-01 23:04:25 -0500
commit3a18a6dcab45467e779e91c7b346aa3b148e8b9c (patch)
treeba0d7d521179c2d3fbd7d989eb2033cd2a86dbaf /source/fud_c_file.cpp
parent4ef88103f74a3a6e8e36ae9eff80f641e20bd1a1 (diff)
Fix move assignment operators or delete them to prevent leaks.
Diffstat (limited to 'source/fud_c_file.cpp')
-rw-r--r--source/fud_c_file.cpp199
1 files changed, 42 insertions, 157 deletions
diff --git a/source/fud_c_file.cpp b/source/fud_c_file.cpp
index b323847..e2e71bf 100644
--- a/source/fud_c_file.cpp
+++ b/source/fud_c_file.cpp
@@ -34,186 +34,71 @@ CBinaryFile::CBinaryFile(const String& filename, CFileMode mode, const String& e
{
}
-CBinaryFile::~CBinaryFile() {
- close();
-}
-
-FudStatus CBinaryFile::open()
+CBinaryFile::CBinaryFile(CBinaryFile&& rhs) :
+ m_filename{std::move(rhs.m_filename)},
+ m_extraFlags{std::move(rhs.m_extraFlags)},
+ m_mode{std::move(rhs.m_mode)},
+ m_modeFlags{std::move(rhs.m_modeFlags)},
+ m_file{rhs.m_file}
{
- if (!m_filename.valid()) {
- return FudStatus::ObjectInvalid;
- }
- m_file = fopen(m_filename.c_str(), m_mode.c_str());
- return m_file != nullptr ? FudStatus::Success : FudStatus::Failure;
}
-void CBinaryFile::close()
-{
- if (m_file != nullptr) {
- fclose(m_file);
- m_file = nullptr;
- }
+CBinaryFile::~CBinaryFile() {
+ close();
}
-const FILE* CBinaryFile::file() const
+CBinaryFile& CBinaryFile::operator=(CBinaryFile&& rhs)
{
- return m_file;
-}
+ close();
-FILE* CBinaryFile::file()
-{
- return m_file;
-}
+ m_filename = std::move(rhs.m_filename);
+ m_extraFlags = std::move(rhs.m_extraFlags);
+ m_mode = std::move(rhs.m_mode);
+ m_modeFlags = std::move(rhs.m_modeFlags);
+ m_file = rhs.m_file;
-bool CBinaryFile::isOpen() const
-{
- return m_file != nullptr;
+ return *this;
}
-Result<size_t, FudStatus> CBinaryFile::size() const
+CTextFile::CTextFile(const String& filename, CFileMode mode)
+ : m_filename{filename},
+ m_mode{CTextFileModeFromFlags(mode)},
+ m_modeFlags{mode}
{
- using RetType = Result<size_t, FudStatus>;
- if (!isOpen()) {
- return RetType::error(FudStatus::OperationInvalid);
- }
-
- auto seekStatus = fseek(m_file, 0, SEEK_END);
- if (seekStatus != 0) {
- return RetType::error(FudStatus::Failure);
- }
-
- auto fileSizeResult = ftell(m_file);
- if (fileSizeResult < 0) {
- return RetType::error(FudStatus::Failure);
- }
-
- auto fileSize = static_cast<size_t>(fileSizeResult);
-
- auto resetStatus = reset();
- if (resetStatus != FudStatus::Success) {
- return RetType::error(resetStatus);
- }
-
- return RetType::okay(fileSize);
}
-ReadResult CBinaryFile::read(void* destination, size_t destinationSize, size_t length)
+CTextFile::CTextFile(const String& filename, CFileMode mode, const String& extraFlags)
+ : m_filename{filename},
+ m_extraFlags{extraFlags},
+ m_mode{String(CTextFileModeFromFlags(mode)).catenate(extraFlags)},
+ m_modeFlags{mode}
{
- return read(destination, destinationSize, length, 0);
}
-ReadResult CBinaryFile::read(void* destination, size_t destinationSize, size_t length, size_t offset)
+CTextFile::CTextFile(CTextFile&& rhs) :
+ m_filename{std::move(rhs.m_filename)},
+ m_extraFlags{std::move(rhs.m_extraFlags)},
+ m_mode{std::move(rhs.m_mode)},
+ m_modeFlags{std::move(rhs.m_modeFlags)},
+ m_file{rhs.m_file}
{
- ReadResult result{};
- if (length == 0) {
- return result;
- }
-
- if (destination == nullptr) {
- result.status = FudStatus::NullPointer;
- return result;
- }
-
- if (offset > LONG_MAX || SIZE_MAX - offset < length || destinationSize < length) {
- result.status = FudStatus::InvalidInput;
- return result;
- }
-
- auto fileSizeResult = size();
- if (fileSizeResult.isError()) {
- result.status = fileSizeResult.getError();
- return result;
- }
-
- auto fileSize = fileSizeResult.getOkay();
- if (offset + length > fileSize) {
- result.status = FudStatus::InvalidInput;
- return result;
- }
-
- auto seekResult = fseek(m_file, static_cast<long>(offset), SEEK_SET);
- if (seekResult != 0) {
- result.status = FudStatus::Failure;
- return result;
- }
-
- auto* destBytes = static_cast<char*>(destination);
- result.bytesRead = fread(destBytes, 1, length, m_file);
- static_cast<void>(reset());
- if (result.bytesRead != length) {
- result.status = FudStatus::Partial;
- } else {
- result.status = FudStatus::Success;
- }
-
- return result;
}
-WriteResult CBinaryFile::write(const void* source, size_t sourceSize, size_t length)
-{
- auto offsetResult = size();
- if (offsetResult.isError()) {
- return WriteResult{0, offsetResult.getError()};
- }
-
- return write(source, sourceSize, length, offsetResult.getOkay());
+CTextFile::~CTextFile() {
+ close();
}
-WriteResult CBinaryFile::write(const void* source, size_t sourceSize, size_t length, size_t offset)
+CTextFile& CTextFile::operator=(CTextFile&& rhs)
{
- WriteResult result{};
- if (length == 0) {
- return result;
- }
-
- if (source == nullptr) {
- result.status = FudStatus::NullPointer;
- return result;
- }
-
- if (offset > LONG_MAX || SIZE_MAX - offset < length || sourceSize < length) {
- result.status = FudStatus::InvalidInput;
- return result;
- }
-
- auto fileSizeResult = size();
- if (fileSizeResult.isError()) {
- result.status = fileSizeResult.getError();
- return result;
- }
-
- // TODO: proper way of handling this
- auto fileSize = fileSizeResult.getOkay();
- int seekResult;
- if (offset > fileSize) {
- seekResult = fseek(m_file, 0, SEEK_END);
- } else {
- seekResult = fseek(m_file, static_cast<long>(offset), SEEK_SET);
- }
-
- if (seekResult != 0) {
- result.status = FudStatus::Failure;
- return result;
- }
-
- auto* sourceBytes = static_cast<const char*>(source);
- result.bytesWritten = fwrite(sourceBytes, 1, length, m_file);
- static_cast<void>(reset());
- if (result.bytesWritten != length) {
- result.status = FudStatus::Partial;
- } else {
- result.status = FudStatus::Success;
- }
-
- return result;
-}
+ close();
+
+ m_filename = std::move(rhs.m_filename);
+ m_extraFlags = std::move(rhs.m_extraFlags);
+ m_mode = std::move(rhs.m_mode);
+ m_modeFlags = std::move(rhs.m_modeFlags);
+ m_file = rhs.m_file;
-FudStatus CBinaryFile::reset() const {
- if (!isOpen()) {
- return FudStatus::OperationInvalid;
- }
- auto result = fseek(m_file, 0, SEEK_SET);
- return result == 0 ? FudStatus::Success : FudStatus::Failure;
+ return *this;
}
} // namespace fud