• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
~FileLineReader()33   ~FileLineReader() override {
34     input_stream_.close();
35   }
36 
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 
Init(const std::string & file_path)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 
ReadLine(string * line)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_;
59 };  // class FileLineReader
60 
61 class MemoryLineReader : public LineReader {
62  public:
MemoryLineReader(const string & contents)63   explicit MemoryLineReader(const string& contents) : input_stream_(contents) {}
64   ~MemoryLineReader() override = default;
65 
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 
ReadLine(string * line)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_;
83 };  // class MemoryLineReader
84 
ReadFromFile(const string & file_path)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 
ReadFromMemory(const string & contents)94 unique_ptr<LineReader> LineReader::ReadFromMemory(const string& contents) {
95   return unique_ptr<LineReader>(new MemoryLineReader(contents));
96 }
97 
98 }  // namespace aidl
99 }  // namespace android
100