4
4
5
5
#include " ConfigParser.hxx"
6
6
#include " FileLineParser.hxx"
7
+ #include " io/BufferedReader.hxx"
8
+ #include " io/FdReader.hxx"
9
+ #include " io/Open.hxx"
10
+ #include " io/UniqueFileDescriptor.hxx"
7
11
#include " lib/fmt/SystemError.hxx"
8
- #include " util/ScopeExit.hxx"
9
12
10
13
#include < algorithm>
11
14
#include < exception>
14
17
#include < assert.h>
15
18
#include < errno.h>
16
19
#include < fnmatch.h>
17
- #include < stdio.h>
18
20
#include < string.h>
19
21
20
22
using std::string_view_literals::operator " " sv;
@@ -303,12 +305,11 @@ IncludeConfigParser::IncludePath(std::filesystem::path &&p)
303
305
}
304
306
305
307
static void
306
- ParseConfigFile (const std::filesystem::path &path, FILE *file ,
308
+ ParseConfigFile (const std::filesystem::path &path, BufferedReader &reader ,
307
309
ConfigParser &parser)
308
310
{
309
- char buffer[4096 ], *line;
310
311
unsigned i = 1 ;
311
- while (( line = fgets (buffer, sizeof (buffer), file)) != nullptr ) {
312
+ while (char * line = reader. ReadLine () ) {
312
313
FileLineParser line_parser (path, line);
313
314
314
315
try {
@@ -328,8 +329,8 @@ IncludeConfigParser::IncludeOptionalPath(std::filesystem::path &&p)
328
329
{
329
330
IncludeConfigParser sub (std::move (p), child, false );
330
331
331
- FILE *file = fopen (sub. path . c_str (), " r " ) ;
332
- if (file == nullptr ) {
332
+ UniqueFileDescriptor fd ;
333
+ if (!fd. OpenReadOnly (sub. path . c_str ()) ) {
333
334
int e = errno;
334
335
switch (e) {
335
336
case ENOENT:
@@ -342,21 +343,20 @@ IncludeConfigParser::IncludeOptionalPath(std::filesystem::path &&p)
342
343
}
343
344
}
344
345
345
- AtScopeExit (file) { fclose (file); };
346
+ FdReader fd_reader{fd};
347
+ BufferedReader buffered_reader{fd_reader};
346
348
347
- ParseConfigFile (sub.path , file , sub);
349
+ ParseConfigFile (sub.path , buffered_reader , sub);
348
350
sub.Finish ();
349
351
}
350
352
351
353
void
352
354
ParseConfigFile (const std::filesystem::path &path, ConfigParser &parser)
353
355
{
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} ;
357
359
358
- AtScopeExit (file) { fclose (file); };
359
-
360
- ParseConfigFile (path, file, parser);
360
+ ParseConfigFile (path, buffered_reader, parser);
361
361
parser.Finish ();
362
362
}
0 commit comments