config/File: use FileReader/BufferedReader instead of stdio
This commit is contained in:
parent
a33db8fe6f
commit
d8bef3270d
|
@ -26,15 +26,12 @@
|
||||||
#include "util/StringUtil.hxx"
|
#include "util/StringUtil.hxx"
|
||||||
#include "util/Error.hxx"
|
#include "util/Error.hxx"
|
||||||
#include "util/Domain.hxx"
|
#include "util/Domain.hxx"
|
||||||
#include "fs/Limits.hxx"
|
|
||||||
#include "fs/Path.hxx"
|
#include "fs/Path.hxx"
|
||||||
#include "fs/FileSystem.hxx"
|
#include "fs/io/FileReader.hxx"
|
||||||
|
#include "fs/io/BufferedReader.hxx"
|
||||||
#include "Log.hxx"
|
#include "Log.hxx"
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
static constexpr size_t MAX_STRING_SIZE = MPD_PATH_MAX + 80;
|
|
||||||
|
|
||||||
static constexpr char CONF_COMMENT = '#';
|
static constexpr char CONF_COMMENT = '#';
|
||||||
|
|
||||||
|
@ -81,18 +78,18 @@ config_read_name_value(struct config_param *param, char *input, unsigned line,
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct config_param *
|
static struct config_param *
|
||||||
config_read_block(FILE *fp, int *count, char *string, Error &error)
|
config_read_block(BufferedReader &reader, int *count, Error &error)
|
||||||
{
|
{
|
||||||
struct config_param *ret = new config_param(*count);
|
struct config_param *ret = new config_param(*count);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
char *line;
|
char *line = reader.ReadLine();
|
||||||
|
|
||||||
line = fgets(string, MAX_STRING_SIZE, fp);
|
|
||||||
if (line == nullptr) {
|
if (line == nullptr) {
|
||||||
delete ret;
|
delete ret;
|
||||||
error.Set(config_file_domain,
|
|
||||||
"Expected '}' before end-of-file");
|
if (reader.Check(error))
|
||||||
|
error.Set(config_file_domain,
|
||||||
|
"Expected '}' before end-of-file");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,21 +139,21 @@ Append(config_param *&head, config_param *p)
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
ReadConfigFile(ConfigData &config_data, FILE *fp, Error &error)
|
ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
|
||||||
{
|
{
|
||||||
assert(fp != nullptr);
|
|
||||||
|
|
||||||
char string[MAX_STRING_SIZE + 1];
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
struct config_param *param;
|
struct config_param *param;
|
||||||
|
|
||||||
while (fgets(string, MAX_STRING_SIZE, fp)) {
|
while (true) {
|
||||||
char *line;
|
char *line = reader.ReadLine();
|
||||||
|
if (line == nullptr)
|
||||||
|
return true;
|
||||||
|
|
||||||
const char *name, *value;
|
const char *name, *value;
|
||||||
|
|
||||||
count++;
|
count++;
|
||||||
|
|
||||||
line = StripLeft(string);
|
line = StripLeft(line);
|
||||||
if (*line == 0 || *line == CONF_COMMENT)
|
if (*line == 0 || *line == CONF_COMMENT)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
@ -214,7 +211,7 @@ ReadConfigFile(ConfigData &config_data, FILE *fp, Error &error)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
param = config_read_block(fp, &count, string, error);
|
param = config_read_block(reader, &count, error);
|
||||||
if (param == nullptr) {
|
if (param == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -246,8 +243,6 @@ ReadConfigFile(ConfigData &config_data, FILE *fp, Error &error)
|
||||||
|
|
||||||
Append(head, param);
|
Append(head, param);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
|
@ -258,13 +253,11 @@ ReadConfigFile(ConfigData &config_data, Path path, Error &error)
|
||||||
|
|
||||||
FormatDebug(config_file_domain, "loading file %s", path_utf8.c_str());
|
FormatDebug(config_file_domain, "loading file %s", path_utf8.c_str());
|
||||||
|
|
||||||
FILE *fp = FOpen(path, FOpenMode::ReadText);
|
FileReader file(path, error);
|
||||||
if (fp == nullptr) {
|
if (!file.IsDefined())
|
||||||
error.FormatErrno("Failed to open %s", path_utf8.c_str());
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
bool result = ReadConfigFile(config_data, fp, error);
|
BufferedReader reader(file);
|
||||||
fclose(fp);
|
return ReadConfigFile(config_data, reader, error) &&
|
||||||
return result;
|
reader.Check(error);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue