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 #ifndef SHILL_FILE_READER_H_ 18 #define SHILL_FILE_READER_H_ 19 20 #include <string> 21 22 #include <base/macros.h> 23 #include <base/files/file_path.h> 24 #include <base/files/scoped_file.h> 25 26 namespace shill { 27 28 // A helper class for reading a file line-by-line, which is expected to 29 // be a substitute for std::getline() as the Google C++ style guide disallows 30 // the use of stream. 31 class FileReader { 32 public: 33 FileReader(); 34 ~FileReader(); 35 36 // Closes the file. 37 void Close(); 38 39 // Opens the file of a given path. Returns true on success. 40 bool Open(const base::FilePath& file_path); 41 42 // Reads a line, terminated by either LF or EOF, from the file into 43 // a given string, with LF excluded. Returns false if no more line 44 // can be read from the file. 45 bool ReadLine(std::string* line); 46 47 private: 48 // The file to read. 49 base::ScopedFILE file_; 50 51 DISALLOW_COPY_AND_ASSIGN(FileReader); 52 }; 53 54 } // namespace shill 55 56 #endif // SHILL_FILE_READER_H_ 57