Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 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 CROS_DISKS_FILE_READER_H_ |
| 6 | #define CROS_DISKS_FILE_READER_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
Ben Chan | 97e20d4 | 2014-02-05 18:38:07 -0800 | [diff] [blame] | 10 | #include <base/files/file_path.h> |
Ben Chan | af25ddb | 2014-05-21 18:32:47 -0700 | [diff] [blame] | 11 | #include <base/files/scoped_file.h> |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 12 | |
| 13 | namespace cros_disks { |
| 14 | |
| 15 | // A helper class for reading a file line-by-line, which is expected to |
| 16 | // be a substitute for std::getline() as the Google C++ style guide disallows |
| 17 | // the use of stream. |
| 18 | class FileReader { |
| 19 | public: |
Ben Chan | 5512355 | 2014-08-24 16:22:16 -0700 | [diff] [blame] | 20 | FileReader() = default; |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 21 | FileReader(const FileReader&) = delete; |
| 22 | FileReader& operator=(const FileReader&) = delete; |
| 23 | |
Ben Chan | 5512355 | 2014-08-24 16:22:16 -0700 | [diff] [blame] | 24 | ~FileReader() = default; |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 25 | |
| 26 | // Closes the file. |
| 27 | void Close(); |
| 28 | |
| 29 | // Opens the file of a given path. Returns true on success. |
Ben Chan | 2e8aa6f | 2013-02-15 11:40:04 -0800 | [diff] [blame] | 30 | bool Open(const base::FilePath& file_path); |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 31 | |
| 32 | // Reads a line, terminated by either LF or EOF, from the file into |
| 33 | // a given string, with LF excluded. Returns false if no more line |
| 34 | // can be read from the file. |
| 35 | bool ReadLine(std::string* line); |
| 36 | |
| 37 | private: |
| 38 | // The file to read. |
Ben Chan | af25ddb | 2014-05-21 18:32:47 -0700 | [diff] [blame] | 39 | base::ScopedFILE file_; |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | } // namespace cros_disks |
| 43 | |
| 44 | #endif // CROS_DISKS_FILE_READER_H_ |