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 | #ifndef SHILL_FILE_READER_H_ |
| 6 | #define SHILL_FILE_READER_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
Ben Chan | cc67c52 | 2014-09-03 07:19:18 -0700 | [diff] [blame] | 10 | #include <base/macros.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 11 | #include <base/files/file_path.h> |
Ben Chan | 6fbf64f | 2014-05-21 18:07:01 -0700 | [diff] [blame] | 12 | #include <base/files/scoped_file.h> |
Arman Uguray | ccbd1ac | 2013-01-10 13:03:19 -0800 | [diff] [blame] | 13 | |
| 14 | namespace shill { |
| 15 | |
| 16 | // A helper class for reading a file line-by-line, which is expected to |
| 17 | // be a substitute for std::getline() as the Google C++ style guide disallows |
| 18 | // the use of stream. |
| 19 | class FileReader { |
| 20 | public: |
| 21 | FileReader(); |
| 22 | ~FileReader(); |
| 23 | |
| 24 | // Closes the file. |
| 25 | void Close(); |
| 26 | |
| 27 | // Opens the file of a given path. Returns true on success. |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 28 | bool Open(const base::FilePath& file_path); |
Arman Uguray | ccbd1ac | 2013-01-10 13:03:19 -0800 | [diff] [blame] | 29 | |
| 30 | // Reads a line, terminated by either LF or EOF, from the file into |
| 31 | // a given string, with LF excluded. Returns false if no more line |
| 32 | // can be read from the file. |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 33 | bool ReadLine(std::string* line); |
Arman Uguray | ccbd1ac | 2013-01-10 13:03:19 -0800 | [diff] [blame] | 34 | |
| 35 | private: |
| 36 | // The file to read. |
Ben Chan | 6fbf64f | 2014-05-21 18:07:01 -0700 | [diff] [blame] | 37 | base::ScopedFILE file_; |
Arman Uguray | ccbd1ac | 2013-01-10 13:03:19 -0800 | [diff] [blame] | 38 | |
| 39 | DISALLOW_COPY_AND_ASSIGN(FileReader); |
| 40 | }; |
| 41 | |
| 42 | } // namespace shill |
| 43 | |
| 44 | #endif // SHILL_FILE_READER_H_ |