1
2
3
4
5
6 #include <soulng/util/CodeFormatter.hpp>
7 #include <soulng/util/Unicode.hpp>
8 #include <soulng/util/Ansi.hpp>
9 #include <string>
10 #include <iostream>
11
12 #ifdef _WIN32
13 #include <io.h>
14 #include <fcntl.h>
15 #else
16 #include <unistd.h>
17 #endif
18
19 namespace soulng { namespace util {
20
21 #ifdef _WIN32
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 #else // !_WIN32
96
97 bool IsHandleRedirected(int handle)
98 {
99 return !isatty(handle);
100 }
101
102 void WriteUtf8(std::ostream& s, const std::string& str)
103 {
104 s << str;
105 }
106
107 #endif
108
109 void CodeFormatter::Write(const std::string& text)
110 {
111 if (atBeginningOfLine)
112 {
113 if (indent > 0)
114 {
115 stream << std::string(indentSize * indent, ' ');
116 atBeginningOfLine = false;
117 }
118 }
119 if (logging && contentCount > 0)
120 {
121 WriteUtf8(stream, "length=" + std::to_string(text.length()));
122 }
123 else
124 {
125 WriteUtf8(stream, text);
126 }
127 }
128
129 void CodeFormatter::WriteLine(const std::string& text)
130 {
131 Write(text);
132 NewLine();
133 }
134
135 void CodeFormatter::NewLine()
136 {
137 stream << "\n";
138 atBeginningOfLine = true;
139 ++line;
140 }
141
142 void CodeFormatter::Flush()
143 {
144 stream.flush();
145 }
146
147 CodeFormatter& CodeFormatter::operator<<(StandardEndLine manip)
148 {
149 WriteLine();
150 Flush();
151 return *this;
152 }
153
154 CodeFormatter& operator<<(CodeFormatter& f, const std::string& s)
155 {
156 f.Write(s);
157 return f;
158 }
159
160 CodeFormatter& operator<<(CodeFormatter& f, const char* s)
161 {
162 f.Write(s);
163 return f;
164 }
165
166 CodeFormatter& operator<<(CodeFormatter& f, char c)
167 {
168 f.Write(std::string(1, c));
169 return f;
170 }
171
172 CodeFormatter& operator<<(CodeFormatter& f, bool b)
173 {
174 if (b)
175 {
176 f.Write("true");
177 }
178 else
179 {
180 f.Write("false");
181 }
182 return f;
183 }
184
185 CodeFormatter& operator<<(CodeFormatter& f, int x)
186 {
187 f.Write(std::to_string(x));
188 return f;
189 }
190
191 CodeFormatter& operator<<(CodeFormatter& f, double x)
192 {
193 f.Write(std::to_string(x));
194 return f;
195 }
196
197 CodeFormatter& operator<<(CodeFormatter& f, int64_t x)
198 {
199 f.Write(std::to_string(x));
200 return f;
201 }
202
203 CodeFormatter& operator<<(CodeFormatter& f, uint64_t x)
204 {
205 f.Write(std::to_string(x));
206 return f;
207 }
208
209 } }