Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 FORMATTER_H_ |
| 18 | |
| 19 | #define FORMATTER_H_ |
| 20 | |
| 21 | #include <android-base/macros.h> |
| 22 | #include <string> |
| 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | struct Formatter { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 27 | // Assumes ownership of file. Directed to stdout if file == NULL. |
| 28 | Formatter(FILE *file = NULL); |
| 29 | ~Formatter(); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 30 | |
| 31 | void indent(); |
| 32 | void unindent(); |
| 33 | |
| 34 | Formatter &operator<<(const std::string &out); |
| 35 | Formatter &operator<<(size_t n); |
| 36 | |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 37 | // Any substrings matching "space" will be stripped out of the output. |
| 38 | void setNamespace(const std::string &space); |
| 39 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 40 | private: |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 41 | FILE *mFile; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 42 | size_t mIndentDepth; |
| 43 | bool mAtStartOfLine; |
| 44 | |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 45 | std::string mSpace; |
| 46 | |
| 47 | void output(const std::string &text) const; |
| 48 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 49 | DISALLOW_COPY_AND_ASSIGN(Formatter); |
| 50 | }; |
| 51 | |
| 52 | } // namespace android |
| 53 | |
| 54 | #endif // FORMATTER_H_ |
| 55 | |