Skip to content

Commit 0667aed

Browse files
committed
io/config/ConfigParser: use class BufferedReader instead of cstdio
1 parent 086fe76 commit 0667aed

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

src/io/config/ConfigParser.cxx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
#include "ConfigParser.hxx"
66
#include "FileLineParser.hxx"
7+
#include "io/BufferedReader.hxx"
8+
#include "io/FdReader.hxx"
9+
#include "io/Open.hxx"
10+
#include "io/UniqueFileDescriptor.hxx"
711
#include "lib/fmt/SystemError.hxx"
8-
#include "util/ScopeExit.hxx"
912

1013
#include <algorithm>
1114
#include <exception>
@@ -14,7 +17,6 @@
1417
#include <assert.h>
1518
#include <errno.h>
1619
#include <fnmatch.h>
17-
#include <stdio.h>
1820
#include <string.h>
1921

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

305307
static void
306-
ParseConfigFile(const std::filesystem::path &path, FILE *file,
308+
ParseConfigFile(const std::filesystem::path &path, BufferedReader &reader,
307309
ConfigParser &parser)
308310
{
309-
char buffer[4096], *line;
310311
unsigned i = 1;
311-
while ((line = fgets(buffer, sizeof(buffer), file)) != nullptr) {
312+
while (char *line = reader.ReadLine()) {
312313
FileLineParser line_parser(path, line);
313314

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

331-
FILE *file = fopen(sub.path.c_str(), "r");
332-
if (file == nullptr) {
332+
UniqueFileDescriptor fd;
333+
if (!fd.OpenReadOnly(sub.path.c_str())) {
333334
int e = errno;
334335
switch (e) {
335336
case ENOENT:
@@ -342,21 +343,20 @@ IncludeConfigParser::IncludeOptionalPath(std::filesystem::path &&p)
342343
}
343344
}
344345

345-
AtScopeExit(file) { fclose(file); };
346+
FdReader fd_reader{fd};
347+
BufferedReader buffered_reader{fd_reader};
346348

347-
ParseConfigFile(sub.path, file, sub);
349+
ParseConfigFile(sub.path, buffered_reader, sub);
348350
sub.Finish();
349351
}
350352

351353
void
352354
ParseConfigFile(const std::filesystem::path &path, ConfigParser &parser)
353355
{
354-
FILE *file = fopen(path.c_str(), "r");
355-
if (file == nullptr)
356-
throw FmtErrno("Failed to open {}", path.native());
356+
const auto fd = OpenReadOnly(path.c_str());
357+
FdReader fd_reader{fd};
358+
BufferedReader buffered_reader{fd_reader};
357359

358-
AtScopeExit(file) { fclose(file); };
359-
360-
ParseConfigFile(path, file, parser);
360+
ParseConfigFile(path, buffered_reader, parser);
361361
parser.Finish();
362362
}

src/io/config/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ io_config = static_library(
1414
'IniParser.cxx',
1515
include_directories: inc,
1616
dependencies: [
17+
io_dep,
1718
std_filesystem_dep,
1819
fmt_dep,
1920
],

0 commit comments

Comments
 (0)