mirror of https://github.com/aria2/aria2
				
				
				
			Rewritten UriListParser using BufferedFile
							parent
							
								
									945012a32f
								
							
						
					
					
						commit
						a6c2ad53bf
					
				| 
						 | 
				
			
			@ -34,7 +34,7 @@
 | 
			
		|||
/* copyright --> */
 | 
			
		||||
#include "UriListParser.h"
 | 
			
		||||
 | 
			
		||||
#include <istream>
 | 
			
		||||
#include <cstring>
 | 
			
		||||
#include <sstream>
 | 
			
		||||
 | 
			
		||||
#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<std::string>& 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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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<std::string>& uris, Option& op);
 | 
			
		||||
 | 
			
		||||
  bool hasNext() const;
 | 
			
		||||
  bool hasNext();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
} // namespace aria2
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -35,7 +35,6 @@
 | 
			
		|||
#include "download_helper.h"
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <fstream>
 | 
			
		||||
#include <algorithm>
 | 
			
		||||
#include <sstream>
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -506,9 +505,9 @@ namespace {
 | 
			
		|||
void createRequestGroupForUriList
 | 
			
		||||
(std::vector<SharedHandle<RequestGroup> >& result,
 | 
			
		||||
 const SharedHandle<Option>& option,
 | 
			
		||||
 std::istream& in)
 | 
			
		||||
 const std::string& filename)
 | 
			
		||||
{
 | 
			
		||||
  UriListParser p(in);
 | 
			
		||||
  UriListParser p(filename);
 | 
			
		||||
  while(p.hasNext()) {
 | 
			
		||||
    std::vector<std::string> uris;
 | 
			
		||||
    SharedHandle<Option> tempOption(new Option());
 | 
			
		||||
| 
						 | 
				
			
			@ -536,15 +535,14 @@ void createRequestGroupForUriList
 | 
			
		|||
 const SharedHandle<Option>& option)
 | 
			
		||||
{
 | 
			
		||||
  if(option->get(PREF_INPUT_FILE) == "-") {
 | 
			
		||||
    createRequestGroupForUriList(result, option, std::cin);
 | 
			
		||||
    createRequestGroupForUriList(result, option, DEV_STDIN);
 | 
			
		||||
  } else {
 | 
			
		||||
    if(!File(option->get(PREF_INPUT_FILE)).isFile()) {
 | 
			
		||||
      throw DL_ABORT_EX
 | 
			
		||||
        (fmt(EX_FILE_OPEN, option->get(PREF_INPUT_FILE).c_str(),
 | 
			
		||||
             "No such file"));
 | 
			
		||||
    }
 | 
			
		||||
    std::ifstream f(option->get(PREF_INPUT_FILE).c_str(), std::ios::binary);
 | 
			
		||||
    createRequestGroupForUriList(result, option, f);
 | 
			
		||||
    createRequestGroupForUriList(result, option, option->get(PREF_INPUT_FILE));
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,7 +3,6 @@
 | 
			
		|||
#include <sstream>
 | 
			
		||||
#include <algorithm>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <fstream>
 | 
			
		||||
#include <iterator>
 | 
			
		||||
 | 
			
		||||
#include <cppunit/extensions/HelperMacros.h>
 | 
			
		||||
| 
						 | 
				
			
			@ -41,9 +40,9 @@ std::string UriListParserTest::list2String(const std::vector<std::string>& src)
 | 
			
		|||
 | 
			
		||||
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;
 | 
			
		||||
  Option reqOp;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue