diff --git a/src/UriListParser.cc b/src/UriListParser.cc index 634c541e..af297a11 100644 --- a/src/UriListParser.cc +++ b/src/UriListParser.cc @@ -34,7 +34,7 @@ /* copyright --> */ #include "UriListParser.h" -#include +#include #include #include "util.h" @@ -42,51 +42,61 @@ #include "OptionHandlerFactory.h" #include "OptionHandler.h" #include "A2STR.h" +#include "BufferedFile.h" namespace aria2 { -UriListParser::UriListParser(std::istream& in):in_(in) +UriListParser::UriListParser(const std::string& filename) + : fp_(filename, BufferedFile::READ) { optparser_.setOptionHandlers(OptionHandlerFactory::createOptionHandlers()); } UriListParser::~UriListParser() {} -void UriListParser::getOptions(Option& op) -{ - std::stringstream ss; - while(getline(in_, line_)) { - if(util::startsWith(line_, " ")) { - ss << line_ << "\n"; - } else if(util::startsWith(line_, A2STR::SHARP_C)) { - continue; - } else { - break; - } - } - optparser_.parse(op, ss); -} - void UriListParser::parseNext(std::vector& uris, Option& op) { + char buf[4096]; if(line_.empty()) { - getline(in_, line_); - } - if(!in_) { - return; - } - do { - if(!util::startsWith(line_, A2STR::SHARP_C) && !util::strip(line_).empty()){ - util::split(line_, std::back_inserter(uris), "\t", true); - getOptions(op); + if(!fp_.getsn(buf, sizeof(buf))) { + line_.clear(); return; } - } while(getline(in_, line_)); + line_.assign(&buf[0], &buf[strlen(buf)]); + } + while(1) { + if(!util::startsWith(line_, A2STR::SHARP_C) && !util::strip(line_).empty()){ + util::split(line_, std::back_inserter(uris), "\t", true); + // Read options + std::stringstream ss; + while(1) { + if(!fp_.getsn(buf, sizeof(buf))) { + line_.clear(); + break; + } + line_.assign(&buf[0], &buf[strlen(buf)]); + if(util::startsWith(line_, " ")) { + ss << line_ << "\n"; + } else if(util::startsWith(line_, A2STR::SHARP_C)) { + continue; + } else { + break; + } + } + optparser_.parse(op, ss); + return; + } + if(!fp_.getsn(buf, sizeof(buf))) { + line_.clear(); + return; + } + line_.assign(&buf[0], &buf[strlen(buf)]); + } } -bool UriListParser::hasNext() const +bool UriListParser::hasNext() { - return in_; + return !line_.empty() || (fp_ && !fp_.eof()); } } // namespace aria2 diff --git a/src/UriListParser.h b/src/UriListParser.h index 7ba9970e..9d3c2efd 100644 --- a/src/UriListParser.h +++ b/src/UriListParser.h @@ -43,26 +43,25 @@ #include "Option.h" #include "OptionParser.h" +#include "BufferedFile.h" namespace aria2 { class UriListParser { private: - std::istream& in_; + BufferedFile fp_; OptionParser optparser_; std::string line_; - - void getOptions(Option& op); public: - UriListParser(std::istream& in); + UriListParser(const std::string& filename); ~UriListParser(); void parseNext(std::vector& uris, Option& op); - bool hasNext() const; + bool hasNext(); }; } // namespace aria2 diff --git a/src/download_helper.cc b/src/download_helper.cc index 90db76f8..08a7e614 100644 --- a/src/download_helper.cc +++ b/src/download_helper.cc @@ -35,7 +35,6 @@ #include "download_helper.h" #include -#include #include #include @@ -506,9 +505,9 @@ namespace { void createRequestGroupForUriList (std::vector >& result, const SharedHandle