Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 ART_SRC_INDENTER_H_ |
| 18 | #define ART_SRC_INDENTER_H_ |
| 19 | |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 20 | #include "base/macros.h" |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 21 | #include <streambuf> |
| 22 | |
| 23 | const char kIndentChar =' '; |
| 24 | const size_t kIndentBy1Count = 2; |
| 25 | |
| 26 | class Indenter : public std::streambuf { |
| 27 | public: |
| 28 | Indenter(std::streambuf* out, char text, size_t count) |
| 29 | : indent_next_(true), out_sbuf_(out), text_(text), count_(count) {} |
| 30 | |
| 31 | private: |
| 32 | int_type overflow(int_type c) { |
| 33 | if (c != std::char_traits<char>::eof()) { |
| 34 | if (indent_next_) { |
| 35 | for (size_t i = 0; i < count_; ++i) { |
| 36 | out_sbuf_->sputc(text_); |
| 37 | } |
| 38 | } |
| 39 | out_sbuf_->sputc(c); |
| 40 | indent_next_ = (c == '\n'); |
| 41 | } |
| 42 | return std::char_traits<char>::not_eof(c); |
| 43 | } |
| 44 | |
| 45 | int sync() { |
| 46 | return out_sbuf_->pubsync(); |
| 47 | } |
| 48 | |
| 49 | bool indent_next_; |
| 50 | |
| 51 | // Buffer to write output to. |
| 52 | std::streambuf* const out_sbuf_; |
| 53 | |
| 54 | // Text output as indent. |
| 55 | const char text_; |
| 56 | |
| 57 | // Number of times text is output. |
| 58 | const size_t count_; |
| 59 | |
| 60 | DISALLOW_COPY_AND_ASSIGN(Indenter); |
| 61 | }; |
| 62 | |
| 63 | #endif // ART_SRC_INDENTER_H_ |