Rewritten UriListParser using BufferedFile

pull/1/head
Tatsuhiro Tsujikawa 2011-08-07 01:05:02 +09:00
parent 945012a32f
commit a6c2ad53bf
4 changed files with 49 additions and 43 deletions

View File

@ -34,7 +34,7 @@
/* copyright --> */ /* copyright --> */
#include "UriListParser.h" #include "UriListParser.h"
#include <istream> #include <cstring>
#include <sstream> #include <sstream>
#include "util.h" #include "util.h"
@ -42,20 +42,39 @@
#include "OptionHandlerFactory.h" #include "OptionHandlerFactory.h"
#include "OptionHandler.h" #include "OptionHandler.h"
#include "A2STR.h" #include "A2STR.h"
#include "BufferedFile.h"
namespace aria2 { namespace aria2 {
UriListParser::UriListParser(std::istream& in):in_(in) UriListParser::UriListParser(const std::string& filename)
: fp_(filename, BufferedFile::READ)
{ {
optparser_.setOptionHandlers(OptionHandlerFactory::createOptionHandlers()); optparser_.setOptionHandlers(OptionHandlerFactory::createOptionHandlers());
} }
UriListParser::~UriListParser() {} UriListParser::~UriListParser() {}
void UriListParser::getOptions(Option& op) void UriListParser::parseNext(std::vector<std::string>& uris, Option& op)
{ {
char buf[4096];
if(line_.empty()) {
if(!fp_.getsn(buf, sizeof(buf))) {
line_.clear();
return;
}
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; std::stringstream ss;
while(getline(in_, line_)) { while(1) {
if(!fp_.getsn(buf, sizeof(buf))) {
line_.clear();
break;
}
line_.assign(&buf[0], &buf[strlen(buf)]);
if(util::startsWith(line_, " ")) { if(util::startsWith(line_, " ")) {
ss << line_ << "\n"; ss << line_ << "\n";
} else if(util::startsWith(line_, A2STR::SHARP_C)) { } else if(util::startsWith(line_, A2STR::SHARP_C)) {
@ -65,28 +84,19 @@ void UriListParser::getOptions(Option& op)
} }
} }
optparser_.parse(op, ss); optparser_.parse(op, ss);
}
void UriListParser::parseNext(std::vector<std::string>& uris, Option& op)
{
if(line_.empty()) {
getline(in_, line_);
}
if(!in_) {
return; return;
} }
do { if(!fp_.getsn(buf, sizeof(buf))) {
if(!util::startsWith(line_, A2STR::SHARP_C) && !util::strip(line_).empty()){ line_.clear();
util::split(line_, std::back_inserter(uris), "\t", true);
getOptions(op);
return; return;
} }
} while(getline(in_, line_)); line_.assign(&buf[0], &buf[strlen(buf)]);
}
} }
bool UriListParser::hasNext() const bool UriListParser::hasNext()
{ {
return in_; return !line_.empty() || (fp_ && !fp_.eof());
} }
} // namespace aria2 } // namespace aria2

View File

@ -43,26 +43,25 @@
#include "Option.h" #include "Option.h"
#include "OptionParser.h" #include "OptionParser.h"
#include "BufferedFile.h"
namespace aria2 { namespace aria2 {
class UriListParser { class UriListParser {
private: private:
std::istream& in_; BufferedFile fp_;
OptionParser optparser_; OptionParser optparser_;
std::string line_; std::string line_;
void getOptions(Option& op);
public: public:
UriListParser(std::istream& in); UriListParser(const std::string& filename);
~UriListParser(); ~UriListParser();
void parseNext(std::vector<std::string>& uris, Option& op); void parseNext(std::vector<std::string>& uris, Option& op);
bool hasNext() const; bool hasNext();
}; };
} // namespace aria2 } // namespace aria2

View File

@ -35,7 +35,6 @@
#include "download_helper.h" #include "download_helper.h"
#include <iostream> #include <iostream>
#include <fstream>
#include <algorithm> #include <algorithm>
#include <sstream> #include <sstream>
@ -506,9 +505,9 @@ namespace {
void createRequestGroupForUriList void createRequestGroupForUriList
(std::vector<SharedHandle<RequestGroup> >& result, (std::vector<SharedHandle<RequestGroup> >& result,
const SharedHandle<Option>& option, const SharedHandle<Option>& option,
std::istream& in) const std::string& filename)
{ {
UriListParser p(in); UriListParser p(filename);
while(p.hasNext()) { while(p.hasNext()) {
std::vector<std::string> uris; std::vector<std::string> uris;
SharedHandle<Option> tempOption(new Option()); SharedHandle<Option> tempOption(new Option());
@ -536,15 +535,14 @@ void createRequestGroupForUriList
const SharedHandle<Option>& option) const SharedHandle<Option>& option)
{ {
if(option->get(PREF_INPUT_FILE) == "-") { if(option->get(PREF_INPUT_FILE) == "-") {
createRequestGroupForUriList(result, option, std::cin); createRequestGroupForUriList(result, option, DEV_STDIN);
} else { } else {
if(!File(option->get(PREF_INPUT_FILE)).isFile()) { if(!File(option->get(PREF_INPUT_FILE)).isFile()) {
throw DL_ABORT_EX throw DL_ABORT_EX
(fmt(EX_FILE_OPEN, option->get(PREF_INPUT_FILE).c_str(), (fmt(EX_FILE_OPEN, option->get(PREF_INPUT_FILE).c_str(),
"No such file")); "No such file"));
} }
std::ifstream f(option->get(PREF_INPUT_FILE).c_str(), std::ios::binary); createRequestGroupForUriList(result, option, option->get(PREF_INPUT_FILE));
createRequestGroupForUriList(result, option, f);
} }
} }

View File

@ -3,7 +3,6 @@
#include <sstream> #include <sstream>
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
#include <fstream>
#include <iterator> #include <iterator>
#include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/HelperMacros.h>
@ -41,9 +40,9 @@ std::string UriListParserTest::list2String(const std::vector<std::string>& src)
void UriListParserTest::testHasNext() void UriListParserTest::testHasNext()
{ {
std::ifstream in(A2_TEST_DIR"/filelist1.txt", std::ios::binary); std::string filename = A2_TEST_DIR"/filelist1.txt";
UriListParser flp(in); UriListParser flp(filename);
std::vector<std::string> uris; std::vector<std::string> uris;
Option reqOp; Option reqOp;