diff options
author | Max Kellermann <max@duempel.org> | 2014-08-07 18:54:06 +0200 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2014-08-07 19:38:25 +0200 |
commit | aa2e4d92e0005f4516eb591803120eff89f99109 (patch) | |
tree | beb83ec24f0cb5b527ec60bd56c2722681f1d3f5 /src/fs/io/TextFile.cxx | |
parent | 0ea66a1275da319e2443fa1536cec7ea7fc53b53 (diff) | |
download | mpd-aa2e4d92e0005f4516eb591803120eff89f99109.tar.gz mpd-aa2e4d92e0005f4516eb591803120eff89f99109.tar.xz mpd-aa2e4d92e0005f4516eb591803120eff89f99109.zip |
fs/io/BufferedReader: new class to replace class TextFile
The new class is pluggable, to prepare for gzipped database files.
For now, the TextFile class remains, and will be refactored away
later.
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(); } |