Skip to content

Commit

Permalink
io/config/ConfigParser: use class BufferedReader instead of cstdio
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Nov 15, 2023
1 parent 086fe76 commit 0667aed
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/io/config/ConfigParser.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

#include "ConfigParser.hxx"
#include "FileLineParser.hxx"
#include "io/BufferedReader.hxx"
#include "io/FdReader.hxx"
#include "io/Open.hxx"
#include "io/UniqueFileDescriptor.hxx"
#include "lib/fmt/SystemError.hxx"
#include "util/ScopeExit.hxx"

#include <algorithm>
#include <exception>
Expand All @@ -14,7 +17,6 @@
#include <assert.h>
#include <errno.h>
#include <fnmatch.h>
#include <stdio.h>
#include <string.h>

using std::string_view_literals::operator""sv;
Expand Down Expand Up @@ -303,12 +305,11 @@ IncludeConfigParser::IncludePath(std::filesystem::path &&p)
}

static void
ParseConfigFile(const std::filesystem::path &path, FILE *file,
ParseConfigFile(const std::filesystem::path &path, BufferedReader &reader,
ConfigParser &parser)
{
char buffer[4096], *line;
unsigned i = 1;
while ((line = fgets(buffer, sizeof(buffer), file)) != nullptr) {
while (char *line = reader.ReadLine()) {
FileLineParser line_parser(path, line);

try {
Expand All @@ -328,8 +329,8 @@ IncludeConfigParser::IncludeOptionalPath(std::filesystem::path &&p)
{
IncludeConfigParser sub(std::move(p), child, false);

FILE *file = fopen(sub.path.c_str(), "r");
if (file == nullptr) {
UniqueFileDescriptor fd;
if (!fd.OpenReadOnly(sub.path.c_str())) {
int e = errno;
switch (e) {
case ENOENT:
Expand All @@ -342,21 +343,20 @@ IncludeConfigParser::IncludeOptionalPath(std::filesystem::path &&p)
}
}

AtScopeExit(file) { fclose(file); };
FdReader fd_reader{fd};
BufferedReader buffered_reader{fd_reader};

ParseConfigFile(sub.path, file, sub);
ParseConfigFile(sub.path, buffered_reader, sub);
sub.Finish();
}

void
ParseConfigFile(const std::filesystem::path &path, ConfigParser &parser)
{
FILE *file = fopen(path.c_str(), "r");
if (file == nullptr)
throw FmtErrno("Failed to open {}", path.native());
const auto fd = OpenReadOnly(path.c_str());
FdReader fd_reader{fd};
BufferedReader buffered_reader{fd_reader};

AtScopeExit(file) { fclose(file); };

ParseConfigFile(path, file, parser);
ParseConfigFile(path, buffered_reader, parser);
parser.Finish();
}
1 change: 1 addition & 0 deletions src/io/config/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ io_config = static_library(
'IniParser.cxx',
include_directories: inc,
dependencies: [
io_dep,
std_filesystem_dep,
fmt_dep,
],
Expand Down

0 comments on commit 0667aed

Please sign in to comment.