瀏覽代碼

Rewritten Netrc using stdio instead of stream.

Tatsuhiro Tsujikawa 14 年之前
父節點
當前提交
07874696c5
共有 2 個文件被更改,包括 26 次插入15 次删除
  1. 26 12
      src/Netrc.cc
  2. 0 3
      src/Netrc.h

+ 26 - 12
src/Netrc.cc

@@ -34,7 +34,8 @@
 /* copyright --> */
 #include "Netrc.h"
 
-#include <fstream>
+#include <cstdio>
+#include <cstring>
 #include <algorithm>
 
 #include "DlAbortEx.h"
@@ -117,24 +118,29 @@ void Netrc::addAuthenticator(const SharedHandle<Authenticator>& authenticator)
   authenticators_.push_back(authenticator);
 }
 
-void Netrc::skipMacdef(std::ifstream& f) const
+namespace {
+void skipMacdef(FILE* fp)
 {
-  std::string line;
-  while(getline(f, line)) {
-    if(line == A2STR::CR_C || line.empty()) {
+  char buf[4096];
+  while(1) {
+    if(!fgets(buf, sizeof(buf), fp)) {
+      break;
+    }
+    if(buf[0] == '\n' || buf[0] == '\r') {
       break;
     }
   }
 }
+} // namespace
 
 void Netrc::parse(const std::string& path)
 {
   authenticators_.clear();
-  std::ifstream f(path.c_str(), std::ios::binary);
-  
-  if(!f) {
-    throw DL_ABORT_EX(fmt("File not found: %s", path.c_str()));
+  FILE* fp = a2fopen(utf8ToWChar(path).c_str(), "rb");
+  if(!fp) {
+    throw DL_ABORT_EX(fmt("Cannot open file: %s", utf8ToNative(path).c_str()));
   }
+  auto_delete_r<FILE*, int> deleter(fp, fclose);
 
   enum STATE {
     GET_TOKEN,
@@ -145,9 +151,17 @@ void Netrc::parse(const std::string& path)
     SET_MACDEF
   };
   SharedHandle<Authenticator> authenticator;
-  std::string line;
   STATE state = GET_TOKEN;
-  while(getline(f, line)) {
+  char buf[4096];
+  while(1) {
+    if(!fgets(buf, sizeof(buf), fp)) {
+      break;
+    }
+    size_t len = strlen(buf);
+    if(buf[len-1] == '\n') {
+      buf[len-1] = '\0';
+    }
+    std::string line(buf);
     if(util::startsWith(line, "#")) {
       continue;
     }
@@ -190,7 +204,7 @@ void Netrc::parse(const std::string& path)
         } else if(state == SET_ACCOUNT) {
           authenticator->setAccount(token);
         } else if(state == SET_MACDEF) {
-          skipMacdef(f);
+          skipMacdef(fp);
         }
         state = GET_TOKEN;
       } 

+ 0 - 3
src/Netrc.h

@@ -39,7 +39,6 @@
 
 #include <string>
 #include <vector>
-#include <iosfwd>
 
 #include "SharedHandle.h"
 
@@ -119,8 +118,6 @@ private:
   void storeAuthenticator(const SharedHandle<Authenticator>& authenticator);
 
   std::string getRequiredNextToken(std::ifstream& f) const;
-  
-  void skipMacdef(std::ifstream& f) const;
 public:
   Netrc();