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