blob: 3e577bb4904312e9aab81317eda67e85299f398e [file] [log] [blame]
Ben Chanbeefd0d2011-07-25 09:31:34 -07001// 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 Chan97e20d42014-02-05 18:38:07 -080010#include <base/files/file_path.h>
Ben Chanaf25ddb2014-05-21 18:32:47 -070011#include <base/files/scoped_file.h>
Ben Chanbeefd0d2011-07-25 09:31:34 -070012
13namespace 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.
18class FileReader {
19 public:
Ben Chan55123552014-08-24 16:22:16 -070020 FileReader() = default;
Qijiang Fan6bc59e12020-11-11 02:51:06 +090021 FileReader(const FileReader&) = delete;
22 FileReader& operator=(const FileReader&) = delete;
23
Ben Chan55123552014-08-24 16:22:16 -070024 ~FileReader() = default;
Ben Chanbeefd0d2011-07-25 09:31:34 -070025
26 // Closes the file.
27 void Close();
28
29 // Opens the file of a given path. Returns true on success.
Ben Chan2e8aa6f2013-02-15 11:40:04 -080030 bool Open(const base::FilePath& file_path);
Ben Chanbeefd0d2011-07-25 09:31:34 -070031
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 Chanaf25ddb2014-05-21 18:32:47 -070039 base::ScopedFILE file_;
Ben Chanbeefd0d2011-07-25 09:31:34 -070040};
41
42} // namespace cros_disks
43
44#endif // CROS_DISKS_FILE_READER_H_