summaryrefslogtreecommitdiff
path: root/binparse/otreestream.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'binparse/otreestream.cpp')
-rw-r--r--binparse/otreestream.cpp47
1 files changed, 47 insertions, 0 deletions
diff --git a/binparse/otreestream.cpp b/binparse/otreestream.cpp
new file mode 100644
index 0000000..a93c0e9
--- /dev/null
+++ b/binparse/otreestream.cpp
@@ -0,0 +1,47 @@
+#include "otreestream.hpp"
+
+namespace binparse {
+
+int treebuf::sync() {
+ return this->sbuf->pubsync();
+}
+
+int treebuf::overflow(int c) {
+ if (c != std::char_traits<char>::eof()) {
+ if (this->need_prefix && !this->prefix.empty() && this->prefix.size() != this->sbuf->sputn(&this->prefix[0], this->prefix.size())) {
+ return std::char_traits<char>::eof();
+ }
+ this->need_prefix = c == '\n';
+ }
+ return this->sbuf->sputc(c);
+}
+
+treebuf::treebuf(std::streambuf* sbuf)
+: sbuf(sbuf)
+, need_prefix(true)
+{}
+
+void treebuf::indent() {
+ prefix += "\t";
+}
+
+void treebuf::unindent() {
+ prefix = std::string(prefix.begin(), prefix.end()-1);
+}
+
+otreestream::otreestream(std::ostream& out)
+: treebuf(out.rdbuf())
+, std::ios(static_cast<std::streambuf*>(this))
+, std::ostream(static_cast<std::streambuf*>(this)) {
+}
+
+indent::indent(std::ostream& os)
+: tb(dynamic_cast<treebuf&>(os)) {
+ tb.indent();
+}
+
+indent::~indent() {
+ tb.unindent();
+}
+
+}