Arman Uguray | ccbd1ac | 2013-01-10 13:03:19 -0800 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/file_reader.h" |
| 6 | |
| 7 | using std::string; |
| 8 | |
| 9 | namespace shill { |
| 10 | |
| 11 | FileReader::FileReader() { |
| 12 | } |
| 13 | |
| 14 | FileReader::~FileReader() { |
| 15 | } |
| 16 | |
| 17 | void FileReader::Close() { |
| 18 | file_.reset(); |
| 19 | } |
| 20 | |
| 21 | bool FileReader::Open(const FilePath &file_path) { |
| 22 | file_.reset(file_util::OpenFile(file_path, "rb")); |
| 23 | return file_.get() != NULL; |
| 24 | } |
| 25 | |
| 26 | bool FileReader::ReadLine(string *line) { |
| 27 | CHECK(line) << "Invalid argument"; |
| 28 | |
Arman Uguray | f4c6181 | 2013-01-10 18:58:39 -0800 | [diff] [blame] | 29 | FILE *fp = file_.get(); |
Arman Uguray | ccbd1ac | 2013-01-10 13:03:19 -0800 | [diff] [blame] | 30 | if (fp == NULL) |
| 31 | return false; |
| 32 | |
| 33 | line->clear(); |
| 34 | bool line_valid = false; |
| 35 | int ch; |
| 36 | while ((ch = fgetc(fp)) != EOF) { |
| 37 | if (ch == '\n') |
| 38 | return true; |
| 39 | line->push_back(static_cast<char>(ch)); |
| 40 | line_valid = true; |
| 41 | } |
| 42 | return line_valid; |
| 43 | } |
| 44 | |
| 45 | } // namespace shill |