| Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [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 |  | 
| Steven Moreland | 9fccf58 | 2018-08-27 20:36:27 -0700 | [diff] [blame] | 17 | #pragma once | 
| Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 18 |  | 
|  | 19 | #include <memory> | 
| Jeongik Cha | 9a7f21f | 2019-02-13 14:42:47 +0900 | [diff] [blame] | 20 | #include <ostream> | 
| Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 21 | #include <string> | 
|  | 22 |  | 
|  | 23 | #include <stdio.h> | 
|  | 24 |  | 
| Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 25 | namespace android { | 
|  | 26 | namespace aidl { | 
|  | 27 |  | 
| Jiyong Park | b78e15b | 2018-07-04 20:31:03 +0900 | [diff] [blame] | 28 | class CodeWriter; | 
|  | 29 | using CodeWriterPtr = std::unique_ptr<CodeWriter>; | 
|  | 30 |  | 
| Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 31 | class CodeWriter { | 
|  | 32 | public: | 
| Jiyong Park | b78e15b | 2018-07-04 20:31:03 +0900 | [diff] [blame] | 33 | // Get a CodeWriter that writes to a file. When filename is "-", | 
|  | 34 | // it is written to stdout. | 
|  | 35 | static CodeWriterPtr ForFile(const std::string& filename); | 
|  | 36 | // Get a CodeWriter that writes to a string buffer. | 
|  | 37 | // The buffer gets updated only after Close() is called or the CodeWriter | 
|  | 38 | // is deleted -- much like a real file. | 
|  | 39 | static CodeWriterPtr ForString(std::string* buf); | 
| Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 40 | // Write a formatted string to this writer in the usual printf sense. | 
|  | 41 | // Returns false on error. | 
| Steven Moreland | d3721d6 | 2019-12-09 13:04:16 -0800 | [diff] [blame] | 42 | virtual bool Write(const char* format, ...) __attribute__((format(printf, 2, 3))); | 
| Steven Moreland | 201a316 | 2019-03-06 12:04:46 -0800 | [diff] [blame] | 43 | void Indent(); | 
|  | 44 | void Dedent(); | 
| Jiyong Park | b78e15b | 2018-07-04 20:31:03 +0900 | [diff] [blame] | 45 | virtual bool Close(); | 
|  | 46 | virtual ~CodeWriter() = default; | 
|  | 47 | CodeWriter() = default; | 
|  | 48 |  | 
| Jiyong Park | 22b4ecd | 2018-07-23 23:34:42 +0900 | [diff] [blame] | 49 | CodeWriter& operator<<(const char* s); | 
|  | 50 | CodeWriter& operator<<(const std::string& str); | 
|  | 51 |  | 
| Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 52 | private: | 
| Jiyong Park | b78e15b | 2018-07-04 20:31:03 +0900 | [diff] [blame] | 53 | CodeWriter(std::unique_ptr<std::ostream> ostream); | 
| Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 54 | std::string ApplyIndent(const std::string& str); | 
| Jiyong Park | b78e15b | 2018-07-04 20:31:03 +0900 | [diff] [blame] | 55 | const std::unique_ptr<std::ostream> ostream_; | 
| Jiyong Park | a755dc7 | 2018-06-29 13:52:24 +0900 | [diff] [blame] | 56 | int indent_level_ {0}; | 
|  | 57 | bool start_of_line_ {true}; | 
| Jiyong Park | b78e15b | 2018-07-04 20:31:03 +0900 | [diff] [blame] | 58 | }; | 
| Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 59 |  | 
|  | 60 | }  // namespace aidl | 
|  | 61 | }  // namespace android |