fs/io/BufferedReader: count line numbers
Replaces the dirty code in config/ConfigFile.cxx.
This commit is contained in:
parent
d8bef3270d
commit
6cdb2a4896
|
@ -78,9 +78,9 @@ config_read_name_value(struct config_param *param, char *input, unsigned line,
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct config_param *
|
static struct config_param *
|
||||||
config_read_block(BufferedReader &reader, int *count, Error &error)
|
config_read_block(BufferedReader &reader, Error &error)
|
||||||
{
|
{
|
||||||
struct config_param *ret = new config_param(*count);
|
struct config_param *ret = new config_param(reader.GetLineNumber());
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
char *line = reader.ReadLine();
|
char *line = reader.ReadLine();
|
||||||
|
@ -93,7 +93,6 @@ config_read_block(BufferedReader &reader, int *count, Error &error)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
(*count)++;
|
|
||||||
line = StripLeft(line);
|
line = StripLeft(line);
|
||||||
if (*line == 0 || *line == CONF_COMMENT)
|
if (*line == 0 || *line == CONF_COMMENT)
|
||||||
continue;
|
continue;
|
||||||
|
@ -106,8 +105,8 @@ config_read_block(BufferedReader &reader, int *count, Error &error)
|
||||||
if (*line != 0 && *line != CONF_COMMENT) {
|
if (*line != 0 && *line != CONF_COMMENT) {
|
||||||
delete ret;
|
delete ret;
|
||||||
error.Format(config_file_domain,
|
error.Format(config_file_domain,
|
||||||
"line %i: Unknown tokens after '}'",
|
"line %y: Unknown tokens after '}'",
|
||||||
*count);
|
reader.GetLineNumber());
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,10 +115,11 @@ config_read_block(BufferedReader &reader, int *count, Error &error)
|
||||||
|
|
||||||
/* parse name and value */
|
/* parse name and value */
|
||||||
|
|
||||||
if (!config_read_name_value(ret, line, *count, error)) {
|
if (!config_read_name_value(ret, line, reader.GetLineNumber(),
|
||||||
|
error)) {
|
||||||
assert(*line != 0);
|
assert(*line != 0);
|
||||||
delete ret;
|
delete ret;
|
||||||
error.FormatPrefix("line %i: ", *count);
|
error.FormatPrefix("line %u: ", reader.GetLineNumber());
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -141,7 +141,6 @@ Append(config_param *&head, config_param *p)
|
||||||
static bool
|
static bool
|
||||||
ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
|
ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
|
||||||
{
|
{
|
||||||
int count = 0;
|
|
||||||
struct config_param *param;
|
struct config_param *param;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
@ -151,8 +150,6 @@ ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
|
||||||
|
|
||||||
const char *name, *value;
|
const char *name, *value;
|
||||||
|
|
||||||
count++;
|
|
||||||
|
|
||||||
line = StripLeft(line);
|
line = StripLeft(line);
|
||||||
if (*line == 0 || *line == CONF_COMMENT)
|
if (*line == 0 || *line == CONF_COMMENT)
|
||||||
continue;
|
continue;
|
||||||
|
@ -164,7 +161,7 @@ ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
|
||||||
name = tokenizer.NextWord(error);
|
name = tokenizer.NextWord(error);
|
||||||
if (name == nullptr) {
|
if (name == nullptr) {
|
||||||
assert(!tokenizer.IsEnd());
|
assert(!tokenizer.IsEnd());
|
||||||
error.FormatPrefix("line %i: ", count);
|
error.FormatPrefix("line %u: ", reader.GetLineNumber());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,7 +172,8 @@ ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
|
||||||
if (o == ConfigOption::MAX) {
|
if (o == ConfigOption::MAX) {
|
||||||
error.Format(config_file_domain,
|
error.Format(config_file_domain,
|
||||||
"unrecognized parameter in config file at "
|
"unrecognized parameter in config file at "
|
||||||
"line %i: %s\n", count, name);
|
"line %u: %s\n",
|
||||||
|
reader.GetLineNumber(), name);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -187,8 +185,9 @@ ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
|
||||||
param = head;
|
param = head;
|
||||||
error.Format(config_file_domain,
|
error.Format(config_file_domain,
|
||||||
"config parameter \"%s\" is first defined "
|
"config parameter \"%s\" is first defined "
|
||||||
"on line %i and redefined on line %i\n",
|
"on line %d and redefined on line %u\n",
|
||||||
name, param->line, count);
|
name, param->line,
|
||||||
|
reader.GetLineNumber());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,19 +198,20 @@ ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
|
||||||
|
|
||||||
if (tokenizer.CurrentChar() != '{') {
|
if (tokenizer.CurrentChar() != '{') {
|
||||||
error.Format(config_file_domain,
|
error.Format(config_file_domain,
|
||||||
"line %i: '{' expected", count);
|
"line %u: '{' expected",
|
||||||
|
reader.GetLineNumber());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
line = StripLeft(tokenizer.Rest() + 1);
|
line = StripLeft(tokenizer.Rest() + 1);
|
||||||
if (*line != 0 && *line != CONF_COMMENT) {
|
if (*line != 0 && *line != CONF_COMMENT) {
|
||||||
error.Format(config_file_domain,
|
error.Format(config_file_domain,
|
||||||
"line %i: Unknown tokens after '{'",
|
"line %u: Unknown tokens after '{'",
|
||||||
count);
|
reader.GetLineNumber());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
param = config_read_block(reader, &count, error);
|
param = config_read_block(reader, error);
|
||||||
if (param == nullptr) {
|
if (param == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -222,10 +222,11 @@ ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
|
||||||
if (value == nullptr) {
|
if (value == nullptr) {
|
||||||
if (tokenizer.IsEnd())
|
if (tokenizer.IsEnd())
|
||||||
error.Format(config_file_domain,
|
error.Format(config_file_domain,
|
||||||
"line %i: Value missing",
|
"line %u: Value missing",
|
||||||
count);
|
reader.GetLineNumber());
|
||||||
else
|
else
|
||||||
error.FormatPrefix("line %i: ", count);
|
error.FormatPrefix("line %u: ",
|
||||||
|
reader.GetLineNumber());
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -233,12 +234,13 @@ ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
|
||||||
if (!tokenizer.IsEnd() &&
|
if (!tokenizer.IsEnd() &&
|
||||||
tokenizer.CurrentChar() != CONF_COMMENT) {
|
tokenizer.CurrentChar() != CONF_COMMENT) {
|
||||||
error.Format(config_file_domain,
|
error.Format(config_file_domain,
|
||||||
"line %i: Unknown tokens after value",
|
"line %u: Unknown tokens after value",
|
||||||
count);
|
reader.GetLineNumber());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
param = new config_param(value, count);
|
param = new config_param(value,
|
||||||
|
reader.GetLineNumber());
|
||||||
}
|
}
|
||||||
|
|
||||||
Append(head, param);
|
Append(head, param);
|
||||||
|
|
|
@ -59,8 +59,10 @@ BufferedReader::ReadLine()
|
||||||
{
|
{
|
||||||
do {
|
do {
|
||||||
char *line = ReadBufferedLine(buffer);
|
char *line = ReadBufferedLine(buffer);
|
||||||
if (line != nullptr)
|
if (line != nullptr) {
|
||||||
|
++line_number;
|
||||||
return line;
|
return line;
|
||||||
|
}
|
||||||
} while (Fill(true));
|
} while (Fill(true));
|
||||||
|
|
||||||
if (last_error.IsDefined() || !eof || buffer.IsEmpty())
|
if (last_error.IsDefined() || !eof || buffer.IsEmpty())
|
||||||
|
@ -78,5 +80,6 @@ BufferedReader::ReadLine()
|
||||||
|
|
||||||
char *line = buffer.Read().data;
|
char *line = buffer.Read().data;
|
||||||
buffer.Clear();
|
buffer.Clear();
|
||||||
|
++line_number;
|
||||||
return line;
|
return line;
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,9 +41,12 @@ class BufferedReader {
|
||||||
|
|
||||||
bool eof;
|
bool eof;
|
||||||
|
|
||||||
|
unsigned line_number;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BufferedReader(Reader &_reader)
|
BufferedReader(Reader &_reader)
|
||||||
:reader(_reader), buffer(4096), eof(false) {}
|
:reader(_reader), buffer(4096), eof(false),
|
||||||
|
line_number(0) {}
|
||||||
|
|
||||||
gcc_pure
|
gcc_pure
|
||||||
bool Check() const {
|
bool Check() const {
|
||||||
|
@ -70,6 +73,10 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
char *ReadLine();
|
char *ReadLine();
|
||||||
|
|
||||||
|
unsigned GetLineNumber() const {
|
||||||
|
return line_number;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue