1 //
2 // Copyright (C) 2013 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "shill/file_reader.h"
18 
19 #include <base/files/file_util.h>
20 
21 using base::FilePath;
22 using std::string;
23 
24 namespace shill {
25 
FileReader()26 FileReader::FileReader() {
27 }
28 
~FileReader()29 FileReader::~FileReader() {
30 }
31 
Close()32 void FileReader::Close() {
33   file_.reset();
34 }
35 
Open(const FilePath & file_path)36 bool FileReader::Open(const FilePath& file_path) {
37   file_.reset(base::OpenFile(file_path, "rb"));
38   return file_.get() != nullptr;
39 }
40 
ReadLine(string * line)41 bool FileReader::ReadLine(string* line) {
42   CHECK(line) << "Invalid argument";
43 
44   FILE* fp = file_.get();
45   if (!fp)
46     return false;
47 
48   line->clear();
49   bool line_valid = false;
50   int ch;
51   while ((ch = fgetc(fp)) != EOF) {
52     if (ch == '\n')
53       return true;
54     line->push_back(static_cast<char>(ch));
55     line_valid = true;
56   }
57   return line_valid;
58 }
59 
60 }  // namespace shill
61