diff --git a/include/fennec/langproc/filesystem/file.h b/include/fennec/langproc/filesystem/file.h index 657a72b..0626441 100644 --- a/include/fennec/langproc/filesystem/file.h +++ b/include/fennec/langproc/filesystem/file.h @@ -250,9 +250,6 @@ public: // Binary Read Operations ============================================================================================== - char getc(); - wchar_t getwc(); - size_t read(void* data, size_t size, size_t n); template @@ -265,9 +262,6 @@ public: return read(static_cast(data), sizeof(T), n); } - string getline(); - wstring getwline(); - // Binary Write Operations ============================================================================================= @@ -297,6 +291,15 @@ public: } +// Read Operations ===================================================================================================== + + char getc(); + wchar_t getwc(); + + string getline(); + wstring getwline(); + + // Printing Operations ================================================================================================= diff --git a/source/langproc/filesystem/file.cpp b/source/langproc/filesystem/file.cpp index 76eeb8c..ee5782e 100644 --- a/source/langproc/filesystem/file.cpp +++ b/source/langproc/filesystem/file.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -921,7 +922,7 @@ bool file::eof() const { char file::getc() { assert(_error == nullptr, "Attempted an Operation on a File in an Errored State"); - assert(not(_mode & fmode_wide), "Attempted Byte Operation on Wide File"); + assert(not(_mode & fmode_wide), "Attempted Wide Operation on Byte File"); // Check if there is a file if (_handle == nullptr) { @@ -933,7 +934,7 @@ char file::getc() { wchar_t file::getwc() { assert(_error == nullptr, "Attempted an Operation on a File in an Errored State"); - assert(_mode & fmode_wide, "Attempted Wide Operation on Byte File"); + assert(_mode & fmode_wide, "Attempted Byte Operation on Wide File"); // Check if there is a file if (_handle == nullptr) { @@ -959,92 +960,6 @@ size_t file::read(void* data, size_t size, size_t n) { return read; } -string file::getline() { - assert(_error == nullptr, "Attempted an Operation on a File in an Errored State"); - assert(not(_mode & fmode_wide), "Attempted Byte Operation on Wide File"); - - // Check if there is a file - if (_handle == nullptr) { - return 0; - } - - // Read the next line; - char* line = nullptr; - size_t size = 0; - const size_t read = ::getline(&line, &size, _handle); - if (read == npos && ferror(_handle)) { - _error = strerror(errno); - if (line) free(line); - return string(""); - } - - string res = string(line, read); - free(line); - return res; -} - -wstring file::getwline() { - assert(_error == nullptr, "Attempted an Operation on a File in an Errored State"); - assert(_mode & fmode_wide, "Attempted Wide Operation on Byte File"); - - // Check if there is a file - if (_handle == nullptr) { - return _wstring{ L"" }; - } - - // Read the next line; - wchar_t arr[257] = { L'\0' }; - wcstring buff = arr; - wstring res{L""}; - - // read until first newline or end of file - while (fgetws(arr, 257, _handle)) { - res += wcstring(arr, buff.length()); - if (buff.length() < 256 || buff[256] == L'\n') { - return res; - } - } - - _error = strerror(errno); - return wstring(L""); -} - -bool file::putc(char c) { - assert(_error == nullptr, "Attempted an Operation on a File in an Errored State"); - assert(not(_mode & fmode_wide), "Attempted Byte Operation on Wide File"); - - // Check if there is a file - if (_handle == nullptr) { - return false; - } - - int res; - if ((res = fputc(c, _handle)) != c && res != EOF) { - _error = strerror(errno); - return true; - } - - return false; -} - -bool file::putwc(wchar_t c) { - assert(_error == nullptr, "Attempted an Operation on a File in an Errored State"); - assert(_mode & fmode_wide, "Attempted Wide Operation on Byte File"); - - // Check if there is a file - if (_handle == nullptr) { - return false; - } - - int res; - if ((res = fputc(c, _handle)) != c && res != EOF) { - _error = strerror(errno); - return true; - } - - return false; -} - size_t file::write(const void* data, size_t size, size_t n) { assert(_error == nullptr, "Attempted an Operation on a File in an Errored State"); @@ -1063,4 +978,92 @@ size_t file::write(const void* data, size_t size, size_t n) { return r; } +string file::getline() { + assert(_error == nullptr, "Attempted an Operation on a File in an Errored State"); + assert(not(_mode & fmode_wide), "Attempted Wide Operation on Byte File"); + + // Check if there is a file + if (_handle == nullptr) { + return string{ "" }; + } + + // Read the next line; + char arr[LINE_MAX + 2] = { L'\0' }; + cstring buff = arr; + string res{""}; + + // read until first newline or end of file + while (fgets(arr, LINE_MAX + 2, _handle)) { + res += cstring(arr, buff.length()); + if (buff.length() < LINE_MAX || buff[LINE_MAX] == L'\n') { + return res; + } + } + + _error = strerror(errno); + return string(""); +} + +wstring file::getwline() { + assert(_error == nullptr, "Attempted an Operation on a File in an Errored State"); + assert(_mode & fmode_wide, "Attempted Byte Operation on Wide File"); + + // Check if there is a file + if (_handle == nullptr) { + return _wstring{ L"" }; + } + + // Read the next line; + wchar_t arr[LINE_MAX + 2] = { L'\0' }; + wcstring buff = arr; + wstring res{L""}; + + // read until first newline or end of file + while (fgetws(arr, LINE_MAX + 2, _handle)) { + res += wcstring(arr, buff.length()); + if (buff.length() < LINE_MAX || buff[LINE_MAX] == L'\n') { + return res; + } + } + + _error = strerror(errno); + return wstring(L""); +} + +bool file::putc(char c) { + assert(_error == nullptr, "Attempted an Operation on a File in an Errored State"); + assert(not(_mode & fmode_wide), "Attempted Wide Operation on Byte File"); + + // Check if there is a file + if (_handle == nullptr) { + return false; + } + + int res; + if ((res = fputc(c, _handle)) != c && res != EOF) { + _error = strerror(errno); + return true; + } + + return false; +} + +bool file::putwc(wchar_t c) { + assert(_error == nullptr, "Attempted an Operation on a File in an Errored State"); + assert(_mode & fmode_wide, "Attempted Byte Operation on Wide File"); + + // Check if there is a file + if (_handle == nullptr) { + return false; + } + + int res; + if ((res = fputc(c, _handle)) != c && res != EOF) { + _error = strerror(errno); + return true; + } + + return false; +} + }