Handle EOF explicitly in incident_helper
getLine returns -1 when it reaches EOF, and currently incident_helper does not differentiate between EOF and an actual error when calling getLinefrom file. So incident_helper throws an error when it reaches EOF, and this is not good. This change separates these two cases. Bug: 149895022 Bug: 150145113 Test: incident_helper_test Change-Id: Ic5e67f8f9e55719c17d624718b730981c4249da1
This commit is contained in:
@@ -237,33 +237,38 @@ double toDouble(const std::string& s) {
|
||||
Reader::Reader(const int fd)
|
||||
{
|
||||
mFile = fdopen(fd, "r");
|
||||
mBuffer = new char[1024];
|
||||
mStatus = mFile == nullptr ? "Invalid fd " + std::to_string(fd) : "";
|
||||
}
|
||||
|
||||
Reader::~Reader()
|
||||
{
|
||||
if (mFile != nullptr) fclose(mFile);
|
||||
free(mBuffer);
|
||||
}
|
||||
|
||||
bool Reader::readLine(std::string* line) {
|
||||
if (mFile == nullptr) return false;
|
||||
|
||||
char* buf = nullptr;
|
||||
size_t len = 0;
|
||||
ssize_t read = getline(&buf, &len, mFile);
|
||||
ssize_t read = getline(&mBuffer, &len, mFile);
|
||||
if (read != -1) {
|
||||
std::string s(buf);
|
||||
std::string s(mBuffer);
|
||||
line->assign(trim(s, DEFAULT_NEWLINE));
|
||||
} else if (errno == EINVAL) {
|
||||
mStatus = "Bad Argument";
|
||||
return true;
|
||||
}
|
||||
free(buf);
|
||||
return read != -1;
|
||||
if (!feof(mFile)) {
|
||||
mStatus = "Error reading file. Ferror: " + std::to_string(ferror(mFile));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Reader::ok(std::string* error) {
|
||||
if (mStatus.empty()) {
|
||||
return true;
|
||||
}
|
||||
error->assign(mStatus);
|
||||
return mStatus.empty();
|
||||
return false;
|
||||
}
|
||||
|
||||
// ==============================================================================
|
||||
|
||||
@@ -117,6 +117,7 @@ public:
|
||||
|
||||
private:
|
||||
FILE* mFile;
|
||||
char* mBuffer;
|
||||
std::string mStatus;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user