From 4220c2aadcc84894e838636ed952a876eb24d1fa Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 7 Aug 2011 01:21:55 +0900 Subject: [PATCH] Read aria2.conf using BufferedFile Read aria2.conf using BufferedFile. Added BufferedFile::transfer(). --- src/BufferedFile.cc | 16 ++++++++++++++++ src/BufferedFile.h | 4 ++++ src/option_processing.cc | 12 +++++++++--- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/BufferedFile.cc b/src/BufferedFile.cc index 078acb27..080d251d 100644 --- a/src/BufferedFile.cc +++ b/src/BufferedFile.cc @@ -35,6 +35,7 @@ #include "BufferedFile.h" #include +#include #include "a2io.h" #include "util.h" @@ -102,4 +103,19 @@ bool BufferedFile::eof() return open_ && feof(fp_); } +size_t BufferedFile::transfer(std::ostream& out) +{ + size_t count = 0; + char buf[4096]; + while(1) { + size_t r = this->read(buf, sizeof(buf)); + out.write(buf, r); + count += r; + if(r < sizeof(buf)) { + break; + } + } + return count; +} + } // namespace aria2 diff --git a/src/BufferedFile.h b/src/BufferedFile.h index 8f9283ab..b28393b2 100644 --- a/src/BufferedFile.h +++ b/src/BufferedFile.h @@ -39,6 +39,7 @@ #include #include +#include namespace aria2 { @@ -65,6 +66,9 @@ public: int close(); // Return true if open_ && feof(fp_) != 0. Otherwise returns false. bool eof(); + // Convenient method. Read data to end of file and write them into + // given stream. Returns written size. + size_t transfer(std::ostream& out); // Mode for reading static const std::string READ; // Mode for writing diff --git a/src/option_processing.cc b/src/option_processing.cc index f4894f80..905deb45 100644 --- a/src/option_processing.cc +++ b/src/option_processing.cc @@ -36,7 +36,6 @@ #include #include -#include #include #include @@ -56,6 +55,7 @@ #include "error_code.h" #include "SimpleRandomizer.h" #include "bittorrent_helper.h" +#include "BufferedFile.h" #ifndef HAVE_DAEMON #include "daemon.h" #endif // !HAVE_DAEMON @@ -133,9 +133,15 @@ void option_processing(Option& op, std::vector& uris, ucfname; if(File(cfname).isFile()) { - std::ifstream cfstream(cfname.c_str(), std::ios::binary); + std::stringstream ss; + { + BufferedFile fp(cfname, BufferedFile::READ); + if(fp) { + fp.transfer(ss); + } + } try { - oparser.parse(op, cfstream); + oparser.parse(op, ss); } catch(OptionHandlerException& e) { std::cerr << "Parse error in " << cfname << "\n" << e.stackTrace() << std::endl;