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.
This commit is contained in:
@@ -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);
|
||||
assert(buffered_reader != 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;
|
||||
|
||||
buffer[length] = 0;
|
||||
length = 0;
|
||||
return buffer;
|
||||
return buffered_reader->ReadLine();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user