Files
revng-revng/lib/PTML/IndentedOstream.cpp
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

49 lines
1.1 KiB
C++

//
// This file is distributed under the MIT License. See LICENSE.md for details.
//
#include "llvm/Support/ErrorHandling.h"
#include "revng/PTML/Constants.h"
#include "revng/PTML/IndentedOstream.h"
#include "revng/PTML/Tag.h"
namespace ptml {
uint64_t PTMLIndentedOstream::current_pos() const {
return OS.tell();
}
void PTMLIndentedOstream::write_impl(const char *Ptr, size_t Size) {
llvm::StringRef Str(Ptr, Size);
bool BufferEndsNewline = Str.endswith("\n");
std::pair<llvm::StringRef, llvm::StringRef> Pair;
while (Pair = Str.split('\n'), Pair.first != "") {
if (TrailingNewline)
writeIndent();
OS << Pair.first;
if (Pair.second != "") {
OS << '\n';
writeIndent();
}
Str = Pair.second;
}
if (BufferEndsNewline)
OS << '\n';
TrailingNewline = BufferEndsNewline;
}
void PTMLIndentedOstream::writeIndent() {
if (IndentDepth > 0) {
OS << Tag(tags::Span, std::string(IndentSize * IndentDepth, ' '))
.addAttribute(attributes::Token, ptml::tokens::Indentation)
.serialize();
}
TrailingNewline = false;
}
} // namespace ptml