Explorar el Código

Read aria2.conf using BufferedFile

Read aria2.conf using BufferedFile. Added BufferedFile::transfer().
Tatsuhiro Tsujikawa hace 14 años
padre
commit
4220c2aadc
Se han modificado 3 ficheros con 29 adiciones y 3 borrados
  1. 16 0
      src/BufferedFile.cc
  2. 4 0
      src/BufferedFile.h
  3. 9 3
      src/option_processing.cc

+ 16 - 0
src/BufferedFile.cc

@@ -35,6 +35,7 @@
 #include "BufferedFile.h"
 
 #include <cstring>
+#include <ostream>
 
 #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

+ 4 - 0
src/BufferedFile.h

@@ -39,6 +39,7 @@
 
 #include <cstdio>
 #include <string>
+#include <iosfwd>
 
 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

+ 9 - 3
src/option_processing.cc

@@ -36,7 +36,6 @@
 
 #include <cstdlib>
 #include <cstring>
-#include <fstream>
 #include <sstream>
 #include <iostream>
 
@@ -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<std::string>& 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;