blob: 450d7e08ddb7c5b8469f43d897b97c8cd2d8721c [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
7using std::string;
8
9namespace shill {
10
11FileReader::FileReader() {
12}
13
14FileReader::~FileReader() {
15}
16
17void FileReader::Close() {
18 file_.reset();
19}
20
21bool FileReader::Open(const FilePath &file_path) {
22 file_.reset(file_util::OpenFile(file_path, "rb"));
23 return file_.get() != NULL;
24}
25
26bool FileReader::ReadLine(string *line) {
27 CHECK(line) << "Invalid argument";
28
Arman Ugurayf4c61812013-01-10 18:58:39 -080029 FILE *fp = file_.get();
Arman Ugurayccbd1ac2013-01-10 13:03:19 -080030 if (fp == NULL)
31 return false;
32
33 line->clear();
34 bool line_valid = false;
35 int ch;
36 while ((ch = fgetc(fp)) != EOF) {
37 if (ch == '\n')
38 return true;
39 line->push_back(static_cast<char>(ch));
40 line_valid = true;
41 }
42 return line_valid;
43}
44
45} // namespace shill