Files
revng-revng/include/revng/PTML/IndentedOstream.h
Giacomo Vercesi efc2d36a14 Introduce ptml namespace
The `ptml` namespace contains utilities for easier PTML manipulation.
These include:

* A few constants (e.g. standard attributes) available in
  `revng/PTML/Constants.h`
* The `Tag` class to easily construct html/xml tags with attributes
  without having to use format strings.
* The `PTMLIndentedOstream`, a llvm::raw_ostream wrapper that
  automatically adds tagged indentation to the output
2022-07-28 08:47:17 +02:00

50 lines
1.2 KiB
C++

#pragma once
//
// This file is distributed under the MIT License. See LICENSE.md for details.
//
#include "llvm/Support/raw_ostream.h"
namespace ptml {
class PTMLIndentedOstream : public llvm::raw_ostream {
private:
int IndentSize;
int IndentDepth;
// If the buffer ends with a newline, we want to delay emitting indentation on
// the next character, so that we can account for (de)indentations that could
// happen in the meantime. This boolean tracks if we last read a newline
// character.
bool TrailingNewline;
raw_ostream &OS;
public:
explicit PTMLIndentedOstream(llvm::raw_ostream &OS, int IndentSize = 2) :
IndentSize(IndentSize), IndentDepth(0), TrailingNewline(false), OS(OS) {
SetUnbuffered();
}
struct Scope {
private:
PTMLIndentedOstream &OS;
public:
Scope(PTMLIndentedOstream &OS) : OS(OS) { OS.indent(); }
~Scope() { OS.unindent(); }
};
Scope scope() { return Scope(*this); }
void indent() { IndentDepth = std::min(INT_MAX, IndentDepth + 1); }
void unindent() { IndentDepth = std::max(0, IndentDepth - 1); }
private:
void write_impl(const char *Ptr, size_t Size) override;
uint64_t current_pos() const override;
void writeIndent();
};
} // namespace ptml