Rewritten Netrc using BufferedFile.

pull/1/head
Tatsuhiro Tsujikawa 2011-08-06 21:57:38 +09:00
parent c9f8cf75bf
commit f38c2d2012
1 changed files with 5 additions and 10 deletions

View File

@ -42,6 +42,7 @@
#include "fmt.h" #include "fmt.h"
#include "A2STR.h" #include "A2STR.h"
#include "util.h" #include "util.h"
#include "BufferedFile.h"
namespace aria2 { namespace aria2 {
@ -119,11 +120,11 @@ void Netrc::addAuthenticator(const SharedHandle<Authenticator>& authenticator)
} }
namespace { namespace {
void skipMacdef(FILE* fp) void skipMacdef(BufferedFile& fp)
{ {
char buf[4096]; char buf[4096];
while(1) { while(1) {
if(!fgets(buf, sizeof(buf), fp)) { if(!fp.gets(buf, sizeof(buf))) {
break; break;
} }
if(buf[0] == '\n' || buf[0] == '\r') { if(buf[0] == '\n' || buf[0] == '\r') {
@ -136,12 +137,10 @@ void skipMacdef(FILE* fp)
void Netrc::parse(const std::string& path) void Netrc::parse(const std::string& path)
{ {
authenticators_.clear(); authenticators_.clear();
FILE* fp = a2fopen(utf8ToWChar(path).c_str(), "rb"); BufferedFile fp(path, BufferedFile::READ);
if(!fp) { if(!fp) {
throw DL_ABORT_EX(fmt("Cannot open file: %s", utf8ToNative(path).c_str())); throw DL_ABORT_EX(fmt("Cannot open file: %s", utf8ToNative(path).c_str()));
} }
auto_delete_r<FILE*, int> deleter(fp, fclose);
enum STATE { enum STATE {
GET_TOKEN, GET_TOKEN,
SET_MACHINE, SET_MACHINE,
@ -154,13 +153,9 @@ void Netrc::parse(const std::string& path)
STATE state = GET_TOKEN; STATE state = GET_TOKEN;
char buf[4096]; char buf[4096];
while(1) { while(1) {
if(!fgets(buf, sizeof(buf), fp)) { if(!fp.getsn(buf, sizeof(buf))) {
break; break;
} }
size_t len = strlen(buf);
if(buf[len-1] == '\n') {
buf[len-1] = '\0';
}
std::string line(buf); std::string line(buf);
if(util::startsWith(line, "#")) { if(util::startsWith(line, "#")) {
continue; continue;