diff options
Diffstat (limited to '')
-rw-r--r-- | src/fs/io/TextFile.cxx | 59 |
1 files changed, 13 insertions, 46 deletions
diff --git a/src/fs/io/TextFile.cxx b/src/fs/io/TextFile.cxx index b1a92b9cc..396d0f9cd 100644 --- a/src/fs/io/TextFile.cxx +++ b/src/fs/io/TextFile.cxx @@ -19,63 +19,30 @@ #include "config.h" #include "TextFile.hxx" -#include "util/Alloc.hxx" +#include "FileReader.hxx" +#include "BufferedReader.hxx" #include "fs/Path.hxx" -#include "fs/FileSystem.hxx" #include <assert.h> -#include <string.h> -#include <stdlib.h> -TextFile::TextFile(Path path_fs) - :file(FOpen(path_fs, FOpenMode::ReadText)), - buffer((char *)xalloc(step)), capacity(step), length(0) {} +TextFile::TextFile(Path path_fs, Error &error) + :file_reader(new FileReader(path_fs, error)), + buffered_reader(file_reader->IsDefined() + ? new BufferedReader(*file_reader) + : nullptr) +{ +} TextFile::~TextFile() { - free(buffer); - - if (file != nullptr) - fclose(file); + delete buffered_reader; + delete file_reader; } char * TextFile::ReadLine() { - assert(file != nullptr); - - while (true) { - if (length >= capacity) { - if (capacity >= max_length) - /* too large already - bail out */ - return nullptr; - - capacity <<= 1; - char *new_buffer = (char *)realloc(buffer, capacity); - if (new_buffer == nullptr) - /* out of memory - bail out */ - return nullptr; - } - - char *p = fgets(buffer + length, capacity - length, file); - if (p == nullptr) { - if (length == 0 || ferror(file)) - return nullptr; - break; - } - - length += strlen(buffer + length); - if (buffer[length - 1] == '\n') - break; - } - - /* remove the newline characters */ - if (buffer[length - 1] == '\n') - --length; - if (buffer[length - 1] == '\r') - --length; + assert(buffered_reader != nullptr); - buffer[length] = 0; - length = 0; - return buffer; + return buffered_reader->ReadLine(); } |