From cce95b9732f13e5324c1e2b61382cff8ae14918a Mon Sep 17 00:00:00 2001 From: Aki Tuomi Date: Tue, 7 Apr 2015 21:29:32 +0300 Subject: [PATCH] Move entire file parsing to parseFile --- pdns/arguments.cc | 80 ++++++++++++++++++++++------------------------- pdns/arguments.hh | 2 +- 2 files changed, 39 insertions(+), 43 deletions(-) diff --git a/pdns/arguments.cc b/pdns/arguments.cc index ba4bbb173..9f4e1cacc 100644 --- a/pdns/arguments.cc +++ b/pdns/arguments.cc @@ -378,38 +378,46 @@ void ArgvMap::preParse(int &argc, char **argv, const string &arg) } } -bool ArgvMap::parseLine(ifstream& f, const string& arg, string& line, bool lax) { +bool ArgvMap::parseFile(const char *fname, const string& arg, bool lax) { + string line; string pline; string::size_type pos; - if (!getline(f, pline)) return false; - trim_right(pline); - if(pline[pline.size()-1]=='\\') { - line+=pline.substr(0,pline.length()-1); - return true; - } - else - line+=pline; - - // strip everything after a # - if((pos=line.find("#"))!=string::npos) { - // make sure it's either first char or has whitespace before - // fixes issue #354 - if (pos == 0 || std::isspace(line[pos-1])) - line=line.substr(0,pos); - } + ifstream f(fname); + if(!f) + return false; - // strip trailing spaces - trim_right(line); + while(getline(f,pline)) { + trim_right(pline); + + if(pline[pline.size()-1]=='\\') { + line+=pline.substr(0,pline.length()-1); + continue; + } + else + line+=pline; + + // strip everything after a # + if((pos=line.find("#"))!=string::npos) { + // make sure it's either first char or has whitespace before + // fixes issue #354 + if (pos == 0 || std::isspace(line[pos-1])) + line=line.substr(0,pos); + } + + // strip trailing spaces + trim_right(line); - // strip leading spaces - if((pos=line.find_first_not_of(" \t\r\n"))!=string::npos) - line=line.substr(pos); + // strip leading spaces + if((pos=line.find_first_not_of(" \t\r\n"))!=string::npos) + line=line.substr(pos); - // gpgsql-basic-query=sdfsdfs dfsdfsdf sdfsdfsfd + // gpgsql-basic-query=sdfsdfs dfsdfsdf sdfsdfsfd + + parseOne( string("--") + line, arg, lax ); + line=""; + } - parseOne( string("--") + line, arg, lax ); - line=""; return true; } @@ -418,15 +426,7 @@ bool ArgvMap::preParseFile(const char *fname, const string &arg, const string& t { params[arg]=theDefault; - ifstream f(fname); - if(!f) - return false; - - string line; - - while(parseLine(f, arg, line, false)); - - return true; + return parseFile(fname, arg, false); } bool ArgvMap::file(const char *fname, bool lax) @@ -436,17 +436,13 @@ bool ArgvMap::file(const char *fname, bool lax) bool ArgvMap::file(const char *fname, bool lax, bool included) { - ifstream f(fname); - if(!f) { - return false; - } - if (!parmIsset("include-dir")) // inject include-dir set("include-dir","Directory to include configuration files from"); - string line; - - while(parseLine(f, "", line, lax)); + if(!parseFile(fname, "", lax)) { + L << Logger::Warning << "Unable to open " << fname << std::endl; + return false; + } // handle include here (avoid re-include) if (!included && !params["include-dir"].empty()) { diff --git a/pdns/arguments.hh b/pdns/arguments.hh index 87c9199fc..93caa5639 100644 --- a/pdns/arguments.hh +++ b/pdns/arguments.hh @@ -92,7 +92,7 @@ public: { return file(fname,true); } - bool parseLine(ifstream& f, const string& arg, string& line, bool lax); // param_t; //!< use this if you need to know the content of the map bool parmIsset(const string &var); //!< Checks if a parameter is set to *a* value bool mustDo(const string &var); //!< if a switch is given, if we must do something (--help) -- 2.40.0