From f3ac764684c64fbdd2094853a80b23e570cd5d9c Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Tue, 15 Oct 2024 20:56:26 -0500 Subject: Conver to using static constructors for string, sqlite, files. --- source/fud_c_file.cpp | 100 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 74 insertions(+), 26 deletions(-) (limited to 'source/fud_c_file.cpp') diff --git a/source/fud_c_file.cpp b/source/fud_c_file.cpp index 3961042..a63923a 100644 --- a/source/fud_c_file.cpp +++ b/source/fud_c_file.cpp @@ -19,28 +19,51 @@ namespace fud { -CBinaryFile::CBinaryFile(const String& filename, CFileMode mode) - : m_filename{filename}, - m_mode{CBinaryFileModeFromFlags(mode)}, - m_modeFlags{mode} +CBinaryFileResult CBinaryFile::make(const String& filename, CFileMode mode) { + if (!filename.valid()) { + return CBinaryFileResult::error(FudStatus::InvalidInput); + } + auto modeResult{String::makeFromCString(CBinaryFileModeFromFlags(mode))}; + if (modeResult.isError()) { + return CBinaryFileResult::error(modeResult); + } + + CBinaryFile binaryFile{}; + binaryFile.m_filename = filename; + binaryFile.m_mode = modeResult.takeOkay(); + binaryFile.m_modeFlags = mode; + return CBinaryFileResult::okay(std::move(binaryFile)); } -CBinaryFile::CBinaryFile(const String& filename, CFileMode mode, const String& extraFlags) - : m_filename{filename}, - m_extraFlags{extraFlags}, - m_mode{String(CBinaryFileModeFromFlags(mode)).catenate(extraFlags)}, - m_modeFlags{mode} +CBinaryFileResult CBinaryFile::make(const String& filename, CFileMode mode, const char* extraFlags) { + if (!filename.valid()) { + return CBinaryFileResult::error(FudStatus::InvalidInput); + } + auto modeResult{String::makeFromCString(CBinaryFileModeFromFlags(mode), extraFlags)}; + if (modeResult.isError()) { + return CBinaryFileResult::error(modeResult); + } + + CBinaryFile binaryFile{}; + binaryFile.m_filename = filename; + binaryFile.m_mode = modeResult.takeOkay(); + binaryFile.m_modeFlags = mode; + return CBinaryFileResult::okay(std::move(binaryFile)); } CBinaryFile::CBinaryFile(CBinaryFile&& rhs) noexcept : - m_filename{std::move(rhs.m_filename)}, m_extraFlags{std::move(rhs.m_extraFlags)}, m_mode{std::move(rhs.m_mode)}, - m_modeFlags{rhs.m_modeFlags}, m_file{rhs.m_file} + m_filename{std::move(rhs.m_filename)}, + m_mode{std::move(rhs.m_mode)}, + m_modeFlags{rhs.m_modeFlags}, + m_file{rhs.m_file} { + rhs.m_file = nullptr; } -CBinaryFile::~CBinaryFile() { +CBinaryFile::~CBinaryFile() +{ close(); } @@ -49,36 +72,60 @@ CBinaryFile& CBinaryFile::operator=(CBinaryFile&& rhs) noexcept close(); m_filename = std::move(rhs.m_filename); - m_extraFlags = std::move(rhs.m_extraFlags); m_mode = std::move(rhs.m_mode); m_modeFlags = rhs.m_modeFlags; m_file = rhs.m_file; + rhs.m_file = nullptr; + return *this; } -CTextFile::CTextFile(const String& filename, CFileMode mode) - : m_filename{filename}, - m_mode{CTextFileModeFromFlags(mode)}, - m_modeFlags{mode} +CTextFileResult CTextFile::make(const String& filename, CFileMode mode) { + if (!filename.valid()) { + return CTextFileResult::error(FudStatus::InvalidInput); + } + auto modeResult{String::makeFromCString(CBinaryFileModeFromFlags(mode))}; + if (modeResult.isError()) { + return CTextFileResult::error(modeResult); + } + + CTextFile textFile{}; + textFile.m_filename = filename; + textFile.m_mode = modeResult.takeOkay(); + textFile.m_modeFlags = mode; + return CTextFileResult::okay(std::move(textFile)); } -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} +CTextFileResult CTextFile::make(const String& filename, CFileMode mode, const char* extraFlags) { + if (!filename.valid()) { + return CTextFileResult::error(FudStatus::InvalidInput); + } + auto modeResult{String::makeFromCString(CTextFileModeFromFlags(mode), extraFlags)}; + if (modeResult.isError()) { + return CTextFileResult::error(modeResult); + } + + CTextFile textFile{}; + textFile.m_filename = filename; + textFile.m_mode = modeResult.takeOkay(); + textFile.m_modeFlags = mode; + return CTextFileResult::okay(std::move(textFile)); } CTextFile::CTextFile(CTextFile&& rhs) noexcept : - m_filename{std::move(rhs.m_filename)}, m_extraFlags{std::move(rhs.m_extraFlags)}, m_mode{std::move(rhs.m_mode)}, - m_modeFlags{rhs.m_modeFlags}, m_file{rhs.m_file} + m_filename{std::move(rhs.m_filename)}, + m_mode{std::move(rhs.m_mode)}, + m_modeFlags{rhs.m_modeFlags}, + m_file{rhs.m_file} { + rhs.m_file = nullptr; } -CTextFile::~CTextFile() { +CTextFile::~CTextFile() +{ close(); } @@ -87,11 +134,12 @@ CTextFile& CTextFile::operator=(CTextFile&& rhs) noexcept close(); m_filename = std::move(rhs.m_filename); - m_extraFlags = std::move(rhs.m_extraFlags); m_mode = std::move(rhs.m_mode); m_modeFlags = rhs.m_modeFlags; m_file = rhs.m_file; + rhs.m_file = nullptr; + return *this; } -- cgit v1.2.3