1 | #include "mod2.h" |
---|
2 | |
---|
3 | #if defined(HAVE_MINOR) || defined(HAVE_WRAPPERS) |
---|
4 | |
---|
5 | #include <string> |
---|
6 | #include <fstream> |
---|
7 | #include "febase.h" |
---|
8 | #include "PrettyPrinter.h" |
---|
9 | |
---|
10 | PrettyPrinter::PrettyPrinter (const char* fileName1, const char* fileName2, |
---|
11 | const bool append1, const bool append2, |
---|
12 | const int console, const char* baseIndent) |
---|
13 | { |
---|
14 | m_baseIndent = new char[200]; |
---|
15 | m_fileName1 = new char[200]; |
---|
16 | m_fileName2 = new char[200]; |
---|
17 | m_indent = new char[10]; |
---|
18 | |
---|
19 | strcpy(m_baseIndent, baseIndent); |
---|
20 | strcpy(m_fileName1, fileName1); |
---|
21 | strcpy(m_fileName2, fileName2); |
---|
22 | strcpy(m_indent, ""); |
---|
23 | if (strcmp(m_fileName1, "") != 0) |
---|
24 | { |
---|
25 | if (append1) m_file1.open(m_fileName1, std::fstream::app); |
---|
26 | else m_file1.open(m_fileName1); |
---|
27 | } |
---|
28 | if (strcmp(m_fileName2, "") != 0) |
---|
29 | { |
---|
30 | if (append2) m_file2.open(m_fileName2, std::fstream::app); |
---|
31 | else m_file2.open(m_fileName2); |
---|
32 | } |
---|
33 | m_console = console; |
---|
34 | m_indents = 0; |
---|
35 | } |
---|
36 | |
---|
37 | PrettyPrinter::~PrettyPrinter () |
---|
38 | { |
---|
39 | if (strcmp(m_fileName1, "") != 0) m_file1.close(); |
---|
40 | if (strcmp(m_fileName2, "") != 0) m_file2.close(); |
---|
41 | delete m_baseIndent; |
---|
42 | delete m_fileName1; |
---|
43 | delete m_fileName2; |
---|
44 | delete m_indent; |
---|
45 | } |
---|
46 | |
---|
47 | void PrettyPrinter::flush () |
---|
48 | { |
---|
49 | if (strcmp(m_fileName1, "") != 0) m_file1.close(); |
---|
50 | if (strcmp(m_fileName2, "") != 0) m_file2.close(); |
---|
51 | m_file1.open(m_fileName1, std::fstream::app); |
---|
52 | m_file2.open(m_fileName2, std::fstream::app); |
---|
53 | } |
---|
54 | |
---|
55 | PrettyPrinter& PrettyPrinter::operator< (const char* s) |
---|
56 | { |
---|
57 | if (strcmp(m_fileName1, "") != 0) |
---|
58 | m_file1 << s; |
---|
59 | if (m_console == 0 || m_console == 1) |
---|
60 | PrintS(s); |
---|
61 | return *this; |
---|
62 | } |
---|
63 | |
---|
64 | PrettyPrinter& PrettyPrinter::operator<< (const char* s) |
---|
65 | { |
---|
66 | if (strcmp(m_fileName1, "") != 0) |
---|
67 | m_file1 << s; |
---|
68 | if (strcmp(m_fileName2, "") != 0) |
---|
69 | m_file2 << s; |
---|
70 | if (m_console == 0 || m_console == 1 || m_console == 2) |
---|
71 | PrintS(s); |
---|
72 | return *this; |
---|
73 | } |
---|
74 | |
---|
75 | PrettyPrinter& PrettyPrinter::operator< (const std::string s) |
---|
76 | { |
---|
77 | return (*this) < s.c_str(); |
---|
78 | } |
---|
79 | |
---|
80 | PrettyPrinter& PrettyPrinter::operator<< (const std::string s) |
---|
81 | { |
---|
82 | return (*this) << s.c_str(); |
---|
83 | } |
---|
84 | |
---|
85 | PrettyPrinter& PrettyPrinter::operator> (const std::string s) |
---|
86 | { |
---|
87 | return (*this) > s.c_str(); |
---|
88 | } |
---|
89 | |
---|
90 | PrettyPrinter& PrettyPrinter::operator>> (const std::string s) |
---|
91 | { |
---|
92 | return (*this) >> s.c_str(); |
---|
93 | } |
---|
94 | |
---|
95 | PrettyPrinter& PrettyPrinter::operator< (const int i) |
---|
96 | { |
---|
97 | char h[10]; |
---|
98 | sprintf(h, "%d", i); |
---|
99 | return (*this) < h; |
---|
100 | } |
---|
101 | |
---|
102 | PrettyPrinter& PrettyPrinter::operator<< (const int i) |
---|
103 | { |
---|
104 | char h[10]; |
---|
105 | sprintf(h, "%d", i); |
---|
106 | return (*this) << h; |
---|
107 | } |
---|
108 | |
---|
109 | PrettyPrinter& PrettyPrinter::operator> (const int i) |
---|
110 | { |
---|
111 | char h[10]; |
---|
112 | sprintf(h, "%d", i); |
---|
113 | return (*this) > h; |
---|
114 | } |
---|
115 | |
---|
116 | PrettyPrinter& PrettyPrinter::operator>> (const int i) |
---|
117 | { |
---|
118 | char h[10]; |
---|
119 | sprintf(h, "%d", i); |
---|
120 | return (*this) >> h; |
---|
121 | } |
---|
122 | |
---|
123 | PrettyPrinter& PrettyPrinter::operator< (const long l) |
---|
124 | { |
---|
125 | char h[10]; |
---|
126 | sprintf(h, "%ld", l); |
---|
127 | return (*this) < h; |
---|
128 | } |
---|
129 | |
---|
130 | PrettyPrinter& PrettyPrinter::operator<< (const long l) |
---|
131 | { |
---|
132 | char h[10]; |
---|
133 | sprintf(h, "%ld", l); |
---|
134 | return (*this) << h; |
---|
135 | } |
---|
136 | |
---|
137 | PrettyPrinter& PrettyPrinter::operator> (const long l) |
---|
138 | { |
---|
139 | char h[10]; |
---|
140 | sprintf(h, "%ld", l); |
---|
141 | return (*this) > h; |
---|
142 | } |
---|
143 | |
---|
144 | PrettyPrinter& PrettyPrinter::operator>> (const long l) |
---|
145 | { |
---|
146 | char h[10]; |
---|
147 | sprintf(h, "%ld", l); |
---|
148 | return (*this) >> h; |
---|
149 | } |
---|
150 | |
---|
151 | PrettyPrinter& PrettyPrinter::operator< (const unsigned long ul) |
---|
152 | { |
---|
153 | char h[10]; |
---|
154 | sprintf(h, "%lu", ul); |
---|
155 | return (*this) < h; |
---|
156 | } |
---|
157 | |
---|
158 | PrettyPrinter& PrettyPrinter::operator<< (const unsigned long ul) |
---|
159 | { |
---|
160 | char h[10]; |
---|
161 | sprintf(h, "%lu", ul); |
---|
162 | return (*this) << h; |
---|
163 | } |
---|
164 | |
---|
165 | PrettyPrinter& PrettyPrinter::operator> (const unsigned long ul) |
---|
166 | { |
---|
167 | char h[10]; |
---|
168 | sprintf(h, "%lu", ul); |
---|
169 | return (*this) > h; |
---|
170 | } |
---|
171 | |
---|
172 | PrettyPrinter& PrettyPrinter::operator>> (const unsigned long ul) |
---|
173 | { |
---|
174 | char h[10]; |
---|
175 | sprintf(h, "%lu", ul); |
---|
176 | return (*this) >> h; |
---|
177 | } |
---|
178 | |
---|
179 | PrettyPrinter& PrettyPrinter::operator+ () |
---|
180 | { |
---|
181 | if (strcmp(m_fileName1, "") != 0) |
---|
182 | m_file1 << "\n"; |
---|
183 | if (m_console == 0 || m_console == 1) |
---|
184 | PrintLn(); |
---|
185 | return *this; |
---|
186 | } |
---|
187 | |
---|
188 | PrettyPrinter& PrettyPrinter::operator++ () |
---|
189 | { |
---|
190 | if (strcmp(m_fileName1, "") != 0) |
---|
191 | m_file1 << "\n"; |
---|
192 | if (strcmp(m_fileName2, "") != 0) |
---|
193 | m_file2 << "\n"; |
---|
194 | if (m_console == 0 || m_console == 1 || m_console == 2) |
---|
195 | PrintLn(); |
---|
196 | return *this; |
---|
197 | } |
---|
198 | |
---|
199 | PrettyPrinter& PrettyPrinter::operator> (const char* s) |
---|
200 | { |
---|
201 | if (strcmp(m_fileName1, "") != 0) |
---|
202 | { |
---|
203 | m_file1 << m_indent; |
---|
204 | m_file1 << s; |
---|
205 | } |
---|
206 | if (m_console == 0 || m_console == 1) |
---|
207 | { |
---|
208 | PrintS(m_indent); |
---|
209 | PrintS(s); |
---|
210 | } |
---|
211 | return *this; |
---|
212 | } |
---|
213 | |
---|
214 | PrettyPrinter& PrettyPrinter::operator>> (const char* s) |
---|
215 | { |
---|
216 | if (strcmp(m_fileName1, "") != 0) |
---|
217 | { |
---|
218 | m_file1 << m_indent; |
---|
219 | m_file1 << s; |
---|
220 | } |
---|
221 | if (strcmp(m_fileName2, "") != 0) |
---|
222 | { |
---|
223 | m_file2 << m_indent; |
---|
224 | m_file2 << s; |
---|
225 | } |
---|
226 | if (m_console == 0 || m_console == 1 || m_console == 2) |
---|
227 | { |
---|
228 | PrintS(m_indent); |
---|
229 | PrintS(s); |
---|
230 | } |
---|
231 | return *this; |
---|
232 | } |
---|
233 | |
---|
234 | PrettyPrinter& PrettyPrinter::operator+ (const int i) |
---|
235 | { |
---|
236 | m_indents += i; |
---|
237 | if (m_indents < 0) m_indents = 0; |
---|
238 | delete m_indent; |
---|
239 | m_indent = new char[0]; |
---|
240 | strcpy(m_indent, ""); |
---|
241 | for (int j = 0; j < m_indents; j++) strcat(m_indent, m_baseIndent); |
---|
242 | return *this; |
---|
243 | } |
---|
244 | |
---|
245 | PrettyPrinter& PrettyPrinter::operator- (const int i) |
---|
246 | { |
---|
247 | m_indents -= i; |
---|
248 | if (m_indents < 0) m_indents = 0; |
---|
249 | delete m_indent; |
---|
250 | m_indent = new char[0]; |
---|
251 | strcpy(m_indent, ""); |
---|
252 | for (int j = 0; j < m_indents; j++) strcat(m_indent, m_baseIndent); |
---|
253 | return *this; |
---|
254 | } |
---|
255 | |
---|
256 | void PrettyPrinter::setBaseIndent (const char* baseIndent) |
---|
257 | { |
---|
258 | strcpy(m_baseIndent, baseIndent); |
---|
259 | } |
---|
260 | |
---|
261 | char* PrettyPrinter::getBaseIndent () const |
---|
262 | { |
---|
263 | return m_baseIndent; |
---|
264 | } |
---|
265 | |
---|
266 | void PrettyPrinter::setConsole (const int console) |
---|
267 | { |
---|
268 | m_console = console; |
---|
269 | } |
---|
270 | |
---|
271 | int PrettyPrinter::getConsole () const |
---|
272 | { |
---|
273 | return m_console; |
---|
274 | } |
---|
275 | |
---|
276 | #endif |
---|
277 | /* defined(HAVE_MINOR) || defined(HAVE_WRAPPERS) */ |
---|