blob: 6df190ee4a727c8220bfc5e7d186878f5d7a2823 [file] [log] [blame]
Brian Silverman70325d62015-09-20 17:00:43 -04001// Copyright (c) 2009, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30// ---
31// Author: williasr@google.com (Scott Williams)
32
33#ifndef TEMPLATE_INDENTED_WRITER_H_
34#define TEMPLATE_INDENTED_WRITER_H_
35
36#include <config.h>
37#include <string>
38
39namespace ctemplate {
40
41using std::string;
42
43// An indented writer is a wrapper around a string buffer. It takes care of
44// tracking and applying leading whitespace to the buffer at the beginning of
45// new lines.
46class IndentedWriter {
47 public:
48 IndentedWriter(string* out, int starting_indentation)
49 : out_(out), current_indentation_(starting_indentation),
50 original_indentation_(starting_indentation), line_state_(AT_BEGINNING) { }
51
52 ~IndentedWriter() {
53 assert(original_indentation_ == current_indentation_);
54 }
55
56 // Append some output to the buffer. If the string ends with a newline, then
57 // the output buffer will be indented before the next Write() call. If the
58 // output contains embedded newlines, these won't have proper indentation, so
59 // call Write() at least once per physical line of output.
60 void Write(string s1,
61 string s2 = string(),
62 string s3 = string(),
63 string s4 = string(),
64 string s5 = string(),
65 string s6 = string(),
66 string s7 = string()) {
67 DoWrite(s1);
68 if (!s2.empty()) DoWrite(s2);
69 if (!s3.empty()) DoWrite(s3);
70 if (!s4.empty()) DoWrite(s4);
71 if (!s5.empty()) DoWrite(s5);
72 if (!s6.empty()) DoWrite(s6);
73 if (!s7.empty()) DoWrite(s7);
74 }
75
76 // Increment the indentation level. This only has a meaning after outputting a
77 // complete line (otherwise, are you saying you want to modify the indentation
78 // of the current line or the next line?)
79 void Indent() {
80 assert(line_state_ == AT_BEGINNING);
81 current_indentation_ += kIndent;
82 }
83
84 // Decrement the indentation level. This only has a meaning after outputting a
85 // complete line (otherwise, are you saying you want to modify the indentation
86 // of the current line or the next line?)
87 void Dedent() {
88 assert(line_state_ == AT_BEGINNING);
89 current_indentation_ -= kIndent;
90 assert(current_indentation_ >= original_indentation_);
91 }
92
93 // Get access to the underlying indentation level and string buffer. Most
94 // useful for interfacing with non-IndentedWriter printing code.
95 int GetIndent() const { return current_indentation_; }
96 string* GetBuffer() { return out_; }
97
98 private:
99 void DoWrite(const string& line) {
100 if (line_state_ == AT_BEGINNING) {
101 IndentLine();
102 }
103 out_->append(line);
104 if (EndsWithNewline(line)) {
105 line_state_ = AT_BEGINNING;
106 } else {
107 line_state_ = MID_LINE;
108 }
109 }
110
111 static bool EndsWithNewline(const string& line) {
112 return !line.empty() && (*(line.end() - 1) == '\n');
113 }
114
115 void IndentLine() {
116 assert(line_state_ == AT_BEGINNING);
117 out_->append(string(current_indentation_, ' ') +
118 (current_indentation_ ? " " : ""));
119 }
120
121 string* out_;
122 int current_indentation_;
123 int original_indentation_;
124 enum LineState {
125 AT_BEGINNING,
126 MID_LINE
127 } line_state_;
128
129 const static int kIndent = 2; // num spaces to indent each level
130};
131
132}
133
134#endif // TEMPLATE_INDENTED_WRITER_H_