shill: Add FileReader class for reading lines from a file.
This CL adds a file reader class for reading lines from a file. This
will be useful to parse .bfd output generated from seviceproviders.xml.
Implementation is copied from src/platform/cros-disks/file-reader.h/.cc.
BUG=chromium-os:37670
TEST=Implementation is a copy of cros_disks::FileReader, which is known
to work.
Change-Id: Ief0941972443ea91d8405c42f0126005c9483bae
Reviewed-on: https://gerrit.chromium.org/gerrit/41056
Reviewed-by: Thieu Le <thieule@chromium.org>
Reviewed-by: Ben Chan <benchan@chromium.org>
Commit-Queue: Arman Uguray <armansito@chromium.org>
Tested-by: Arman Uguray <armansito@chromium.org>
diff --git a/file_reader.cc b/file_reader.cc
new file mode 100644
index 0000000..81e52a4
--- /dev/null
+++ b/file_reader.cc
@@ -0,0 +1,45 @@
+// Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "shill/file_reader.h"
+
+using std::string;
+
+namespace shill {
+
+FileReader::FileReader() {
+}
+
+FileReader::~FileReader() {
+}
+
+void FileReader::Close() {
+ file_.reset();
+}
+
+bool FileReader::Open(const FilePath &file_path) {
+ file_.reset(file_util::OpenFile(file_path, "rb"));
+ return file_.get() != NULL;
+}
+
+bool FileReader::ReadLine(string *line) {
+ CHECK(line) << "Invalid argument";
+
+ FILE* fp = file_.get();
+ if (fp == NULL)
+ return false;
+
+ line->clear();
+ bool line_valid = false;
+ int ch;
+ while ((ch = fgetc(fp)) != EOF) {
+ if (ch == '\n')
+ return true;
+ line->push_back(static_cast<char>(ch));
+ line_valid = true;
+ }
+ return line_valid;
+}
+
+} // namespace shill