Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015, 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 "line_reader.h" |
| 18 | |
| 19 | #include <fstream> |
| 20 | #include <sstream> |
| 21 | |
| 22 | using std::istringstream; |
| 23 | using std::ifstream; |
| 24 | using std::string; |
| 25 | using std::unique_ptr; |
| 26 | |
| 27 | namespace android { |
| 28 | namespace aidl { |
| 29 | |
| 30 | class FileLineReader : public LineReader { |
| 31 | public: |
| 32 | FileLineReader() = default; |
Yi Kong | de13891 | 2019-03-30 01:38:17 -0700 | [diff] [blame] | 33 | ~FileLineReader() override { |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 34 | input_stream_.close(); |
| 35 | } |
| 36 | |
Jiyong Park | d800fef | 2020-07-22 18:09:43 +0900 | [diff] [blame] | 37 | // non-copyable, non-movable |
| 38 | FileLineReader(const FileLineReader&) = delete; |
| 39 | FileLineReader(FileLineReader&&) = delete; |
| 40 | FileLineReader& operator=(const FileLineReader&) = delete; |
| 41 | FileLineReader& operator=(FileLineReader&&) = delete; |
| 42 | |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 43 | bool Init(const std::string& file_path) { |
| 44 | input_stream_.open(file_path, ifstream::in | ifstream::binary); |
| 45 | return input_stream_.is_open() && input_stream_.good(); |
| 46 | } |
| 47 | |
| 48 | bool ReadLine(string* line) override { |
| 49 | if (!input_stream_.good()) { |
| 50 | return false; |
| 51 | } |
| 52 | line->clear(); |
| 53 | std::getline(input_stream_, *line); |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | ifstream input_stream_; |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 59 | }; // class FileLineReader |
| 60 | |
| 61 | class MemoryLineReader : public LineReader { |
| 62 | public: |
Chih-Hung Hsieh | e4fecc7 | 2016-04-25 12:04:59 -0700 | [diff] [blame] | 63 | explicit MemoryLineReader(const string& contents) : input_stream_(contents) {} |
Yi Kong | de13891 | 2019-03-30 01:38:17 -0700 | [diff] [blame] | 64 | ~MemoryLineReader() override = default; |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 65 | |
Jiyong Park | d800fef | 2020-07-22 18:09:43 +0900 | [diff] [blame] | 66 | // non-copyable, non-movable |
| 67 | MemoryLineReader(const MemoryLineReader&) = delete; |
| 68 | MemoryLineReader(MemoryLineReader&&) = delete; |
| 69 | MemoryLineReader& operator=(const MemoryLineReader&) = delete; |
| 70 | MemoryLineReader& operator=(MemoryLineReader&&) = delete; |
| 71 | |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 72 | bool ReadLine(string* line) override { |
| 73 | if (!input_stream_.good()) { |
| 74 | return false; |
| 75 | } |
| 76 | line->clear(); |
| 77 | std::getline(input_stream_, *line); |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | istringstream input_stream_; |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 83 | }; // class MemoryLineReader |
| 84 | |
| 85 | unique_ptr<LineReader> LineReader::ReadFromFile(const string& file_path) { |
| 86 | unique_ptr<FileLineReader> file_reader(new FileLineReader()); |
| 87 | unique_ptr<LineReader> ret; |
| 88 | if (file_reader->Init(file_path)) { |
| 89 | ret.reset(file_reader.release()); |
| 90 | } |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | unique_ptr<LineReader> LineReader::ReadFromMemory(const string& contents) { |
| 95 | return unique_ptr<LineReader>(new MemoryLineReader(contents)); |
| 96 | } |
| 97 | |
Christopher Wiley | ef14093 | 2015-11-03 09:29:19 -0800 | [diff] [blame] | 98 | } // namespace aidl |
Steven Moreland | f4c64df | 2019-07-29 19:54:04 -0700 | [diff] [blame] | 99 | } // namespace android |