1
2
3
4
5
6 using System;
7 using System.IO;
8
9 namespace System.Text
10 {
11 public class CodeFormatter
12 {
13 public nothrow CodeFormatter(TextWriter& writer_) :
14 writer(writer_), indent(0), indentSize(4), atBeginningOfLine(true), line(1), start(false), startText(), preserveSpace(false), pos(0)
15 {
16 }
17 public void Write(const string& text)
18 {
19 if (atBeginningOfLine)
20 {
21 if (indent != 0)
22 {
23 string s(' ', indentSize * indent);
24 writer.Write(s);
25 pos = pos + indentSize * indent;
26 atBeginningOfLine = false;
27 }
28 }
29 writer.Write(text);
30 pos = pos + text.Length();
31 }
32 public void Write(const char* text)
33 {
34 string s(text);
35 Write(s);
36 }
37 public void WriteLine(const char* text)
38 {
39 Write(text);
40 WriteLine();
41 }
42 public void WriteLine(const string& text)
43 {
44 Write(text);
45 WriteLine();
46 }
47 public void WriteLine()
48 {
49 writer.WriteLine();
50 atBeginningOfLine = true;
51 ++line;
52 pos = 0;
53 }
54 public inline nothrow int Indent() const
55 {
56 return indent;
57 }
58 public inline nothrow void IncIndent()
59 {
60 ++indent;
61 }
62 public inline nothrow void DecIndent()
63 {
64 --indent;
65 }
66 public inline nothrow int IndentSize() const
67 {
68 return indentSize;
69 }
70 public inline nothrow void SetIndentSize(int indentSize_)
71 {
72 indentSize = indentSize_;
73 }
74 public inline nothrow int CurrentIndent() const
75 {
76 return indent * indentSize;
77 }
78 public inline nothrow bool AtBeginningOfLine() const
79 {
80 return atBeginningOfLine;
81 }
82 public inline nothrow int Line() const
83 {
84 return line;
85 }
86 public inline nothrow void SetLine(int line_)
87 {
88 line = line_;
89 }
90 public inline nothrow bool Start() const
91 {
92 return start;
93 }
94 public inline nothrow void SetStart(bool start_)
95 {
96 start = start_;
97 }
98 public inline nothrow const string& StartText() const
99 {
100 return startText;
101 }
102 public nothrow void SetStartText(const string& startText_)
103 {
104 startText = startText_;
105 }
106 public inline nothrow TextWriter& Writer()
107 {
108 return writer;
109 }
110 public inline nothrow bool PreserveSpace() const
111 {
112 return preserveSpace;
113 }
114 public inline nothrow void SetPreserveSpace(bool preserveSpace_)
115 {
116 preserveSpace = preserveSpace_;
117 }
118 public inline nothrow long Pos() const
119 {
120 return pos;
121 }
122 private TextWriter& writer;
123 private int indent;
124 private int indentSize;
125 private bool atBeginningOfLine;
126 private int line;
127 private bool start;
128 private string startText;
129 private bool preserveSpace;
130 private long pos;
131 }
132
133 public CodeFormatter& operator<<(CodeFormatter& formatter, const char* s)
134 {
135 formatter.Write(s);
136 return formatter;
137 }
138
139 public CodeFormatter& operator<<(CodeFormatter& formatter, const string& s)
140 {
141 formatter.Write(s);
142 return formatter;
143 }
144
145 public CodeFormatter& operator<<(CodeFormatter& formatter, long x)
146 {
147 string s = ToString(x);
148 formatter.Write(s);
149 return formatter;
150 }
151
152 public CodeFormatter& operator<<(CodeFormatter& formatter, ulong x)
153 {
154 string s = ToString(x);
155 formatter.Write(s);
156 return formatter;
157 }
158
159 public CodeFormatter& operator<<(CodeFormatter& formatter, char c)
160 {
161 string s = ToString(c);
162 formatter.Write(s);
163 return formatter;
164 }
165
166 public CodeFormatter& operator<<(CodeFormatter& formatter, wchar c)
167 {
168 string s = ToString(c);
169 formatter.Write(s);
170 return formatter;
171 }
172
173 public CodeFormatter& operator<<(CodeFormatter& formatter, uchar c)
174 {
175 string s = ToString(c);
176 formatter.Write(s);
177 return formatter;
178 }
179
180 public CodeFormatter& operator<<(CodeFormatter& formatter, const Endl&)
181 {
182 formatter.WriteLine();
183 return formatter;
184 }
185 }