1
2
3
4
5
6 #ifndef SOULNG_UTIL_CODE_FORMATTER_INCLUDED
7 #define SOULNG_UTIL_CODE_FORMATTER_INCLUDED
8 #include <soulng/util/UtilApi.hpp>
9 #include <ostream>
10 #include <string>
11 #include <stdint.h>
12
13 namespace soulng { namespace util {
14
15 class CodeFormatter
16 {
17 public:
18 CodeFormatter(std::ostream& stream_) : stream(stream_), indent(0), indentSize(4), atBeginningOfLine(true), line(1), start(false), preserveSpace(false), contentCount(0), logging(false) {}
19 int Indent() const { return indent; }
20 int IndentSize() const { return indentSize; }
21 void SetIndentSize(int indentSize_) { indentSize = indentSize_; }
22 int CurrentIndent() const { return indentSize * indent; }
23 void Write(const std::string& text);
24 void WriteLine(const std::string& text);
25 void NewLine();
26 void WriteLine() { NewLine(); }
27 void Flush();
28 void IncIndent()
29 {
30 ++indent;
31 }
32 void DecIndent()
33 {
34 --indent;
35 }
36 int Line() const { return line; }
37 void SetLine(int line_) { line = line_; }
38 bool Start() const { return start; }
39 void SetStart() { start = true; }
40 void ResetStart() { start = false; }
41 void SetStartText(const std::string& startText_) { startText = startText_; }
42 const std::string& StartText() const { return startText; }
43 bool PreserveSpace() const { return preserveSpace; }
44 void SetPreserveSpace(bool preserveSpace_) { preserveSpace = preserveSpace_; }
45 void SetLogging() { logging = true; }
46 void BeginContent() { ++contentCount; }
47 void EndContent() { --contentCount; }
48 typedef std::basic_ostream<char, std::char_traits<char>> CoutType;
49 typedef CoutType& (*StandardEndLine)(CoutType&);
50 CodeFormatter& operator<<(StandardEndLine manip);
51 private:
52 std::ostream& stream;
53 int indent;
54 int indentSize;
55 bool atBeginningOfLine;
56 int line;
57 bool start;
58 std::string startText;
59 bool preserveSpace;
60 int contentCount;
61 bool logging;
62 };
63
64 CodeFormatter& operator<<(CodeFormatter& f, const std::string& s);
65 CodeFormatter& operator<<(CodeFormatter& f, const char* s);
66 CodeFormatter& operator<<(CodeFormatter& f, char c);
67 CodeFormatter& operator<<(CodeFormatter& f, bool b);
68 CodeFormatter& operator<<(CodeFormatter& f, int x);
69 CodeFormatter& operator<<(CodeFormatter& f, double x);
70 CodeFormatter& operator<<(CodeFormatter& f, int64_t x);
71 CodeFormatter& operator<<(CodeFormatter& f, uint64_t x);
72
73 void WriteUtf8(std::ostream& s, const std::string& str);
74 bool IsHandleRedirected(int handle);
75
76 } }
77
78 #endif // SOULNG_UTIL_CODE_FORMATTER_INCLUDED