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 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 7 | using base::FilePath; |
Arman Uguray | ccbd1ac | 2013-01-10 13:03:19 -0800 | [diff] [blame] | 8 | using std::string; |
| 9 | |
| 10 | namespace shill { |
| 11 | |
| 12 | FileReader::FileReader() { |
| 13 | } |
| 14 | |
| 15 | FileReader::~FileReader() { |
| 16 | } |
| 17 | |
| 18 | void FileReader::Close() { |
| 19 | file_.reset(); |
| 20 | } |
| 21 | |
| 22 | bool FileReader::Open(const FilePath &file_path) { |
| 23 | file_.reset(file_util::OpenFile(file_path, "rb")); |
| 24 | return file_.get() != NULL; |
| 25 | } |
| 26 | |
| 27 | bool FileReader::ReadLine(string *line) { |
| 28 | CHECK(line) << "Invalid argument"; |
| 29 | |
Arman Uguray | f4c6181 | 2013-01-10 18:58:39 -0800 | [diff] [blame] | 30 | FILE *fp = file_.get(); |
Arman Uguray | ccbd1ac | 2013-01-10 13:03:19 -0800 | [diff] [blame] | 31 | if (fp == NULL) |
| 32 | return false; |
| 33 | |
| 34 | line->clear(); |
| 35 | bool line_valid = false; |
| 36 | int ch; |
| 37 | while ((ch = fgetc(fp)) != EOF) { |
| 38 | if (ch == '\n') |
| 39 | return true; |
| 40 | line->push_back(static_cast<char>(ch)); |
| 41 | line_valid = true; |
| 42 | } |
| 43 | return line_valid; |
| 44 | } |
| 45 | |
| 46 | } // namespace shill |