fs/io/BufferedReader: count line numbers

Replaces the dirty code in config/ConfigFile.cxx.
This commit is contained in:
Max Kellermann 2015-01-21 23:43:32 +01:00
parent d8bef3270d
commit 6cdb2a4896
3 changed files with 38 additions and 26 deletions

View File

@ -78,9 +78,9 @@ config_read_name_value(struct config_param *param, char *input, unsigned line,
}
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) {
char *line = reader.ReadLine();
@ -93,7 +93,6 @@ config_read_block(BufferedReader &reader, int *count, Error &error)
return nullptr;
}
(*count)++;
line = StripLeft(line);
if (*line == 0 || *line == CONF_COMMENT)
continue;
@ -106,8 +105,8 @@ config_read_block(BufferedReader &reader, int *count, Error &error)
if (*line != 0 && *line != CONF_COMMENT) {
delete ret;
error.Format(config_file_domain,
"line %i: Unknown tokens after '}'",
*count);
"line %y: Unknown tokens after '}'",
reader.GetLineNumber());
return nullptr;
}
@ -116,10 +115,11 @@ config_read_block(BufferedReader &reader, int *count, Error &error)
/* 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);
delete ret;
error.FormatPrefix("line %i: ", *count);
error.FormatPrefix("line %u: ", reader.GetLineNumber());
return nullptr;
}
}
@ -141,7 +141,6 @@ Append(config_param *&head, config_param *p)
static bool
ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
{
int count = 0;
struct config_param *param;
while (true) {
@ -151,8 +150,6 @@ ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
const char *name, *value;
count++;
line = StripLeft(line);
if (*line == 0 || *line == CONF_COMMENT)
continue;
@ -164,7 +161,7 @@ ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
name = tokenizer.NextWord(error);
if (name == nullptr) {
assert(!tokenizer.IsEnd());
error.FormatPrefix("line %i: ", count);
error.FormatPrefix("line %u: ", reader.GetLineNumber());
return false;
}
@ -175,7 +172,8 @@ ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
if (o == ConfigOption::MAX) {
error.Format(config_file_domain,
"unrecognized parameter in config file at "
"line %i: %s\n", count, name);
"line %u: %s\n",
reader.GetLineNumber(), name);
return false;
}
@ -187,8 +185,9 @@ ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
param = head;
error.Format(config_file_domain,
"config parameter \"%s\" is first defined "
"on line %i and redefined on line %i\n",
name, param->line, count);
"on line %d and redefined on line %u\n",
name, param->line,
reader.GetLineNumber());
return false;
}
@ -199,19 +198,20 @@ ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
if (tokenizer.CurrentChar() != '{') {
error.Format(config_file_domain,
"line %i: '{' expected", count);
"line %u: '{' expected",
reader.GetLineNumber());
return false;
}
line = StripLeft(tokenizer.Rest() + 1);
if (*line != 0 && *line != CONF_COMMENT) {
error.Format(config_file_domain,
"line %i: Unknown tokens after '{'",
count);
"line %u: Unknown tokens after '{'",
reader.GetLineNumber());
return false;
}
param = config_read_block(reader, &count, error);
param = config_read_block(reader, error);
if (param == nullptr) {
return false;
}
@ -222,10 +222,11 @@ ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
if (value == nullptr) {
if (tokenizer.IsEnd())
error.Format(config_file_domain,
"line %i: Value missing",
count);
"line %u: Value missing",
reader.GetLineNumber());
else
error.FormatPrefix("line %i: ", count);
error.FormatPrefix("line %u: ",
reader.GetLineNumber());
return false;
}
@ -233,12 +234,13 @@ ReadConfigFile(ConfigData &config_data, BufferedReader &reader, Error &error)
if (!tokenizer.IsEnd() &&
tokenizer.CurrentChar() != CONF_COMMENT) {
error.Format(config_file_domain,
"line %i: Unknown tokens after value",
count);
"line %u: Unknown tokens after value",
reader.GetLineNumber());
return false;
}
param = new config_param(value, count);
param = new config_param(value,
reader.GetLineNumber());
}
Append(head, param);

View File

@ -59,8 +59,10 @@ BufferedReader::ReadLine()
{
do {
char *line = ReadBufferedLine(buffer);
if (line != nullptr)
if (line != nullptr) {
++line_number;
return line;
}
} while (Fill(true));
if (last_error.IsDefined() || !eof || buffer.IsEmpty())
@ -78,5 +80,6 @@ BufferedReader::ReadLine()
char *line = buffer.Read().data;
buffer.Clear();
++line_number;
return line;
}

View File

@ -41,9 +41,12 @@ class BufferedReader {
bool eof;
unsigned line_number;
public:
BufferedReader(Reader &_reader)
:reader(_reader), buffer(4096), eof(false) {}
:reader(_reader), buffer(4096), eof(false),
line_number(0) {}
gcc_pure
bool Check() const {
@ -70,6 +73,10 @@ public:
}
char *ReadLine();
unsigned GetLineNumber() const {
return line_number;
}
};
#endif