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