diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2011-08-05 23:11:57 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2011-08-05 23:33:30 +0900 |
| commit | 07874696c5fef449fbcfe386dcacec9db205188b (patch) | |
| tree | ba562727ecf6e7638fde2a939d474c6541734768 /src | |
| parent | 13158de0d33be64b6c3f1e268aca519914bad41b (diff) | |
Rewritten Netrc using stdio instead of stream.
Diffstat (limited to 'src')
| -rw-r--r-- | src/Netrc.cc | 38 | ||||
| -rw-r--r-- | src/Netrc.h | 3 |
2 files changed, 26 insertions, 15 deletions
diff --git a/src/Netrc.cc b/src/Netrc.cc index 507a7f81..edb83d5c 100644 --- a/src/Netrc.cc +++ b/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; } diff --git a/src/Netrc.h b/src/Netrc.h index 896b8f43..57e1efc0 100644 --- a/src/Netrc.h +++ b/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(); |
