blob: 6b841eeb3aba090cdf6caa5ad6758db906bb163d [file] [log] [blame]
Arman Ugurayccbd1ac2013-01-10 13:03:19 -08001// 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 Chan11c213f2014-09-05 08:21:06 -07007#include <base/files/file_util.h>
Ben Chan6fbf64f2014-05-21 18:07:01 -07008
Albert Chaulk0e1cdea2013-02-27 15:32:55 -08009using base::FilePath;
Arman Ugurayccbd1ac2013-01-10 13:03:19 -080010using std::string;
11
12namespace shill {
13
14FileReader::FileReader() {
15}
16
17FileReader::~FileReader() {
18}
19
20void FileReader::Close() {
21 file_.reset();
22}
23
24bool FileReader::Open(const FilePath &file_path) {
Ben Chana0ddf462014-02-06 11:32:42 -080025 file_.reset(base::OpenFile(file_path, "rb"));
Ben Chancc225ef2014-09-30 13:26:51 -070026 return file_.get() != nullptr;
Arman Ugurayccbd1ac2013-01-10 13:03:19 -080027}
28
29bool FileReader::ReadLine(string *line) {
30 CHECK(line) << "Invalid argument";
31
Arman Ugurayf4c61812013-01-10 18:58:39 -080032 FILE *fp = file_.get();
Ben Chancc225ef2014-09-30 13:26:51 -070033 if (!fp)
Arman Ugurayccbd1ac2013-01-10 13:03:19 -080034 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