diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2012-02-29 02:50:12 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2012-02-29 02:50:12 +0900 |
| commit | 037f1512dbcdf19237d6064660d5c75b4121f0bf (patch) | |
| tree | ec092c918a1396e382d776932d2265b019888b9e /src | |
| parent | c63a524a889415bb321231ac723de8547318900d (diff) | |
Added BufferedFile::getLine() to make reading one line more flexible.
Fixes bug#3495336
Diffstat (limited to 'src')
| -rw-r--r-- | src/BufferedFile.cc | 22 | ||||
| -rw-r--r-- | src/BufferedFile.h | 6 | ||||
| -rw-r--r-- | src/Netrc.cc | 29 | ||||
| -rw-r--r-- | src/NsCookieParser.cc | 22 | ||||
| -rw-r--r-- | src/ServerStatMan.cc | 13 | ||||
| -rw-r--r-- | src/UriListParser.cc | 44 |
6 files changed, 86 insertions, 50 deletions
diff --git a/src/BufferedFile.cc b/src/BufferedFile.cc index 1bca85f4..fe763501 100644 --- a/src/BufferedFile.cc +++ b/src/BufferedFile.cc @@ -99,6 +99,28 @@ char* BufferedFile::getsn(char* s, int size) return ptr; } +std::string BufferedFile::getLine() +{ + std::string res; + if(eof()) { + return res; + } + char buf[4096]; + while(gets(buf, sizeof(buf))) { + size_t len = strlen(buf); + bool lineBreak = false; + if(buf[len-1] == '\n') { + --len; + lineBreak = true; + } + res.append(buf, len); + if(lineBreak) { + break; + } + } + return res; +} + int BufferedFile::close() { if(open_) { diff --git a/src/BufferedFile.h b/src/BufferedFile.h index db006c8a..421cc8e8 100644 --- a/src/BufferedFile.h +++ b/src/BufferedFile.h @@ -52,8 +52,8 @@ public: BufferedFile(const std::string& filename, const std::string& mode); BufferedFile(FILE* fp); virtual ~BufferedFile(); - // Returns true if file is opened and both ferror and feof returns - // 0. Otherwise returns false. + // Returns true if file is opened and ferror returns 0. Otherwise + // returns false. operator unspecified_bool_type() const; // wrapper for fread. Using 1 for 2nd argument of fread. size_t read(void* ptr, size_t count); @@ -64,6 +64,8 @@ public: char* gets(char* s, int size); // wrapper for fgets, but trailing '\n' is replaced with '\0'. char* getsn(char* s, int size); + // Reads one line and returns it. The last '\n' is removed. + std::string getLine(); // wrapper for fclose int close(); // Return true if open_ && feof(fp_) != 0. Otherwise returns false. diff --git a/src/Netrc.cc b/src/Netrc.cc index 50713224..1eb10aa6 100644 --- a/src/Netrc.cc +++ b/src/Netrc.cc @@ -115,12 +115,16 @@ void Netrc::addAuthenticator(const SharedHandle<Authenticator>& authenticator) namespace { void skipMacdef(BufferedFile& fp) { - char buf[4096]; + std::string s; while(1) { - if(!fp.gets(buf, sizeof(buf))) { + s = fp.getLine(); + if(s.empty() || fp.eof()) { break; } - if(buf[0] == '\n' || buf[0] == '\r') { + if(!fp) { + throw DL_ABORT_EX("Netrc:I/O error."); + } + if(s[0] == '\n' || s[0] == '\r') { break; } } @@ -144,18 +148,23 @@ void Netrc::parse(const std::string& path) }; SharedHandle<Authenticator> authenticator; STATE state = GET_TOKEN; - char buf[4096]; while(1) { - if(!fp.getsn(buf, sizeof(buf))) { - break; + std::string line = fp.getLine(); + if(line.empty()) { + if(fp.eof()) { + break; + } else if(!fp) { + throw DL_ABORT_EX("Netrc:I/O error."); + } else { + continue; + } } - size_t len = strlen(buf); - if(len == 0 || buf[0] == '#') { + if(line[0] == '#') { continue; } std::vector<Scip> tokens; - util::splitIterM(&buf[0], &buf[len], std::back_inserter(tokens), " \t", - true); + util::splitIterM(line.begin(), line.end(), std::back_inserter(tokens), + " \t", true); for(std::vector<Scip>::const_iterator iter = tokens.begin(), eoi = tokens.end(); iter != eoi; ++iter) { if(state == GET_TOKEN) { diff --git a/src/NsCookieParser.cc b/src/NsCookieParser.cc index ae26c789..2d9c0ab9 100644 --- a/src/NsCookieParser.cc +++ b/src/NsCookieParser.cc @@ -55,10 +55,11 @@ NsCookieParser::~NsCookieParser() {} namespace { bool parseNsCookie -(Cookie& cookie, const char* buf, size_t buflen, time_t creationTime) +(Cookie& cookie, const std::string& cookieStr, time_t creationTime) { std::vector<Scip> vs; - util::splitIter(&buf[0], &buf[buflen], std::back_inserter(vs), '\t', true); + util::splitIter(cookieStr.begin(), cookieStr.end(), std::back_inserter(vs), + '\t', true); if(vs.size() < 6) { return false; } @@ -105,17 +106,22 @@ std::vector<Cookie> NsCookieParser::parse throw DL_ABORT_EX(fmt("Failed to open file %s", filename.c_str())); } std::vector<Cookie> cookies; - char buf[8192]; while(1) { - if(!fp.getsn(buf, sizeof(buf))) { - break; + std::string line = fp.getLine(); + if(line.empty()) { + if(fp.eof()) { + break; + } else if(!fp) { + throw DL_ABORT_EX("CookieParser:I/O error."); + } else { + continue; + } } - size_t len = strlen(buf); - if(len == 0 || buf[0] == '#') { + if(line[0] == '#') { continue; } Cookie c; - if(parseNsCookie(c, buf, len, creationTime)) { + if(parseNsCookie(c, line, creationTime)) { cookies.push_back(c); } } diff --git a/src/ServerStatMan.cc b/src/ServerStatMan.cc index de00165a..0b10837d 100644 --- a/src/ServerStatMan.cc +++ b/src/ServerStatMan.cc @@ -137,26 +137,27 @@ bool ServerStatMan::load(const std::string& filename) filename.c_str())); return false; } - char buf[4096]; while(1) { - if(!fp.getsn(buf, sizeof(buf))) { + std::string line = fp.getLine(); + if(line.empty()) { if(fp.eof()) { break; - } else { + } else if(!fp) { A2_LOG_ERROR(fmt(MSG_READING_SERVER_STAT_FILE_FAILED, filename.c_str())); return false; + } else { + continue; } } std::pair<std::string::const_iterator, std::string::const_iterator> p = - util::stripIter(&buf[0], &buf[strlen(buf)]); + util::stripIter(line.begin(), line.end()); if(p.first == p.second) { continue; } - std::string line(p.first, p.second); std::vector<Scip> items; - util::splitIter(line.begin(), line.end(), std::back_inserter(items), ','); + util::splitIter(p.first, p.second, std::back_inserter(items), ','); std::map<std::string, std::string> m; for(std::vector<Scip>::const_iterator i = items.begin(), eoi = items.end(); i != eoi; ++i) { diff --git a/src/UriListParser.cc b/src/UriListParser.cc index 853e40b4..b040d582 100644 --- a/src/UriListParser.cc +++ b/src/UriListParser.cc @@ -55,14 +55,6 @@ UriListParser::~UriListParser() {} void UriListParser::parseNext(std::vector<std::string>& uris, Option& op) { - char buf[4096]; - if(line_.empty()) { - if(!fp_.getsn(buf, sizeof(buf))) { - line_.clear(); - return; - } - line_.assign(&buf[0], &buf[strlen(buf)]); - } const SharedHandle<OptionParser>& optparser = OptionParser::getInstance(); while(1) { if(!line_.empty() && line_[0] != '#') { @@ -71,31 +63,35 @@ void UriListParser::parseNext(std::vector<std::string>& uris, Option& op) // Read options std::stringstream ss; while(1) { - if(!fp_.getsn(buf, sizeof(buf))) { - line_.clear(); - break; - } - line_.assign(&buf[0], &buf[strlen(buf)]); + line_ = fp_.getLine(); if(line_.empty()) { - continue; - } else { - if(line_[0] == ' ' || line_[0] == '\t') { - ss << line_ << "\n"; - } else if(line_[0] == '#') { - continue; - } else { + if(fp_.eof()) { break; + } else if(!fp_) { + throw DL_ABORT_EX("UriListParser:I/O error."); + } else { + continue; } } + if(line_[0] == ' ' || line_[0] == '\t') { + ss << line_ << "\n"; + } else if(line_[0] == '#') { + continue; + } else { + break; + } } optparser->parse(op, ss); return; } - if(!fp_.getsn(buf, sizeof(buf))) { - line_.clear(); - return; + line_ = fp_.getLine(); + if(line_.empty()) { + if(fp_.eof()) { + return; + } else if(!fp_) { + throw DL_ABORT_EX("UriListParser:I/O error."); + } } - line_.assign(&buf[0], &buf[strlen(buf)]); } } |
