-
Notifications
You must be signed in to change notification settings - Fork 37
/
indenter.cpp
51 lines (45 loc) · 1.36 KB
/
indenter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
#include "indenter.hpp"
namespace indent {
const int indentbuf::idx = std::ios_base::xalloc();
std::ostream &wrap(std::ostream &os) {
os << wrap(70);
return os;
}
std::ostream &comment(std::ostream &os) {
if(os.pword(indentbuf::idx) != nullptr) {
indentbuf *buf = static_cast<indentbuf *>(os.pword(indentbuf::idx));
buf->comment();
} else {
indentbuf *newbuf = new indentbuf(os, 0);
newbuf->comment();
os.pword(indentbuf::idx) = newbuf;
}
return os;
}
std::ostream& incr(std::ostream& os) {
if(os.pword(indentbuf::idx) != nullptr) {
indentbuf *buf = static_cast<indentbuf *>(os.pword(indentbuf::idx));
buf->adjust_indent(2);
} else {
indentbuf *newbuf = new indentbuf(os, 2);
os.pword(indentbuf::idx) = newbuf;
}
return os;
}
std::ostream& decr(std::ostream& os) {
if(os.pword(indentbuf::idx) != nullptr) {
indentbuf *buf = static_cast<indentbuf *>(os.pword(indentbuf::idx));
buf->adjust_indent(-2);
}
return os;
}
// This removes all indenting, wrapping and commenting from the output stream.
std::ostream& clear(std::ostream& os) {
if(os.pword(indentbuf::idx) != nullptr) {
indentbuf *buf = static_cast<indentbuf *>(os.pword(indentbuf::idx));
delete buf;
os.pword(indentbuf::idx) = nullptr;
}
return os;
}
}