Skip to content

Commit cce95b9

Browse files
committed
Move entire file parsing to parseFile
1 parent e2e9243 commit cce95b9

File tree

2 files changed

+39
-43
lines changed

2 files changed

+39
-43
lines changed

pdns/arguments.cc

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -378,38 +378,46 @@ void ArgvMap::preParse(int &argc, char **argv, const string &arg)
378378
}
379379
}
380380

381-
bool ArgvMap::parseLine(ifstream& f, const string& arg, string& line, bool lax) {
381+
bool ArgvMap::parseFile(const char *fname, const string& arg, bool lax) {
382+
string line;
382383
string pline;
383384
string::size_type pos;
384385

385-
if (!getline(f, pline)) return false;
386-
trim_right(pline);
387-
if(pline[pline.size()-1]=='\\') {
388-
line+=pline.substr(0,pline.length()-1);
389-
return true;
390-
}
391-
else
392-
line+=pline;
393-
394-
// strip everything after a #
395-
if((pos=line.find("#"))!=string::npos) {
396-
// make sure it's either first char or has whitespace before
397-
// fixes issue #354
398-
if (pos == 0 || std::isspace(line[pos-1]))
399-
line=line.substr(0,pos);
400-
}
386+
ifstream f(fname);
387+
if(!f)
388+
return false;
401389

402-
// strip trailing spaces
403-
trim_right(line);
390+
while(getline(f,pline)) {
391+
trim_right(pline);
392+
393+
if(pline[pline.size()-1]=='\\') {
394+
line+=pline.substr(0,pline.length()-1);
395+
continue;
396+
}
397+
else
398+
line+=pline;
399+
400+
// strip everything after a #
401+
if((pos=line.find("#"))!=string::npos) {
402+
// make sure it's either first char or has whitespace before
403+
// fixes issue #354
404+
if (pos == 0 || std::isspace(line[pos-1]))
405+
line=line.substr(0,pos);
406+
}
407+
408+
// strip trailing spaces
409+
trim_right(line);
404410

405-
// strip leading spaces
406-
if((pos=line.find_first_not_of(" \t\r\n"))!=string::npos)
407-
line=line.substr(pos);
411+
// strip leading spaces
412+
if((pos=line.find_first_not_of(" \t\r\n"))!=string::npos)
413+
line=line.substr(pos);
408414

409-
// gpgsql-basic-query=sdfsdfs dfsdfsdf sdfsdfsfd
415+
// gpgsql-basic-query=sdfsdfs dfsdfsdf sdfsdfsfd
416+
417+
parseOne( string("--") + line, arg, lax );
418+
line="";
419+
}
410420

411-
parseOne( string("--") + line, arg, lax );
412-
line="";
413421
return true;
414422
}
415423

@@ -418,15 +426,7 @@ bool ArgvMap::preParseFile(const char *fname, const string &arg, const string& t
418426
{
419427
params[arg]=theDefault;
420428

421-
ifstream f(fname);
422-
if(!f)
423-
return false;
424-
425-
string line;
426-
427-
while(parseLine(f, arg, line, false));
428-
429-
return true;
429+
return parseFile(fname, arg, false);
430430
}
431431

432432
bool ArgvMap::file(const char *fname, bool lax)
@@ -436,17 +436,13 @@ bool ArgvMap::file(const char *fname, bool lax)
436436

437437
bool ArgvMap::file(const char *fname, bool lax, bool included)
438438
{
439-
ifstream f(fname);
440-
if(!f) {
441-
return false;
442-
}
443-
444439
if (!parmIsset("include-dir")) // inject include-dir
445440
set("include-dir","Directory to include configuration files from");
446441

447-
string line;
448-
449-
while(parseLine(f, "", line, lax));
442+
if(!parseFile(fname, "", lax)) {
443+
L << Logger::Warning << "Unable to open " << fname << std::endl;
444+
return false;
445+
}
450446

451447
// handle include here (avoid re-include)
452448
if (!included && !params["include-dir"].empty()) {

pdns/arguments.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public:
9292
{
9393
return file(fname,true);
9494
}
95-
bool parseLine(ifstream& f, const string& arg, string& line, bool lax); //<! parse one line
95+
bool parseFile(const char *fname, const string& arg, bool lax); //<! parse one line
9696
typedef map<string,string> param_t; //!< use this if you need to know the content of the map
9797
bool parmIsset(const string &var); //!< Checks if a parameter is set to *a* value
9898
bool mustDo(const string &var); //!< if a switch is given, if we must do something (--help)

0 commit comments

Comments
 (0)