lix/src/libutil/xml-writer.hh

66 lines
1,015 B
C++
Raw Normal View History

#pragma once
#include <iostream>
#include <string>
#include <list>
#include <map>
namespace nix {
2022-02-25 12:39:38 +00:00
typedef std::map<std::string, std::string> XMLAttrs;
class XMLWriter
{
private:
2014-02-26 17:55:18 +00:00
std::ostream & output;
bool indent;
bool closed;
2022-02-25 12:39:38 +00:00
std::list<std::string> pendingElems;
public:
XMLWriter(bool indent, std::ostream & output);
~XMLWriter();
void close();
2022-02-25 12:39:38 +00:00
void openElement(std::string_view name,
const XMLAttrs & attrs = XMLAttrs());
void closeElement();
2022-02-25 12:39:38 +00:00
void writeEmptyElement(std::string_view name,
const XMLAttrs & attrs = XMLAttrs());
private:
void writeAttrs(const XMLAttrs & attrs);
2018-05-02 11:56:34 +00:00
void indent_(size_t depth);
};
class XMLOpenElement
{
private:
XMLWriter & writer;
public:
2022-02-25 12:39:38 +00:00
XMLOpenElement(XMLWriter & writer, std::string_view name,
const XMLAttrs & attrs = XMLAttrs())
: writer(writer)
{
writer.openElement(name, attrs);
}
~XMLOpenElement()
{
writer.closeElement();
}
};
2014-02-26 17:55:18 +00:00
}