Parse continuation lines in --set-all

This commit is contained in:
Frédéric Mangano 2023-02-10 15:20:15 +09:00
parent d8a1a78274
commit 3b20617de4
2 changed files with 31 additions and 2 deletions

View File

@ -242,6 +242,7 @@ std::list<std::string> ot::read_comments(FILE* input, bool raw)
char* source_line = nullptr;
size_t buflen = 0;
ssize_t nread;
std::string* previous_comment = nullptr;
while ((nread = getline(&source_line, &buflen, input)) != -1) {
if (nread > 0 && source_line[nread - 1] == '\n')
--nread; // Chomp.
@ -260,14 +261,26 @@ std::list<std::string> ot::read_comments(FILE* input, bool raw)
if (line.empty()) {
// Ignore empty lines.
} else if (line.front() == '#') {
previous_comment = nullptr;
} else if (line[0] == '#') {
// Ignore comments.
previous_comment = nullptr;
} else if (line[0] == '\t') {
// Continuation line: append the current line to the previous tag.
if (previous_comment == nullptr) {
ot::status rc = {ot::st::error, "Unexpected continuation line: " + std::string(source_line, nread)};
free(source_line);
throw rc;
} else {
line[0] = '\n';
previous_comment->append(line);
}
} else if (line.find('=') == std::string::npos) {
ot::status rc = {ot::st::error, "Malformed tag: " + std::string(source_line, nread)};
free(source_line);
throw rc;
} else {
comments.push_back(std::move(line));
previous_comment = &comments.emplace_back(std::move(line));
}
}
free(source_line);

View File

@ -45,6 +45,15 @@ void check_read_comments()
if (comments.front() != "RAW=\xFF\xFF")
throw failure("parsed user comments did not match expectations");
}
{
std::string txt = "MULTILINE=First\n\tSecond\n"s;
ot::file input = fmemopen((char*) txt.data(), txt.size(), "r");
rc = read_comments(input.get(), comments, true);
if (rc != ot::st::ok)
throw failure("could not read comments");
if (comments.front() != "MULTILINE=First\nSecond")
throw failure("parsed user comments did not match expectations");
}
{
std::string txt = "MALFORMED\n"s;
ot::file input = fmemopen((char*) txt.data(), txt.size(), "r");
@ -52,6 +61,13 @@ void check_read_comments()
if (rc != ot::st::error)
throw failure("did not get the expected error reading malformed comments");
}
{
std::string txt = "\tBad"s;
ot::file input = fmemopen((char*) txt.data(), txt.size(), "r");
rc = read_comments(input.get(), comments, true);
if (rc != ot::st::error)
throw failure("did not get the expected error reading bad continuation line");
}
}
/**