Add 'third_party/nix/' from commit 'be66c7a6b24e3c3c6157fd37b86c7203d14acf10'
git-subtree-dir: third_party/nix
git-subtree-mainline: cf8cd640c1
git-subtree-split: be66c7a6b24e3c3c6157fd37b86c7203d14acf10
This commit is contained in:
commit
7994fd1d54
737 changed files with 105390 additions and 0 deletions
94
third_party/nix/src/libutil/xml-writer.cc
vendored
Normal file
94
third_party/nix/src/libutil/xml-writer.cc
vendored
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
#include <assert.h>
|
||||
|
||||
#include "xml-writer.hh"
|
||||
|
||||
|
||||
namespace nix {
|
||||
|
||||
|
||||
XMLWriter::XMLWriter(bool indent, std::ostream & output)
|
||||
: output(output), indent(indent)
|
||||
{
|
||||
output << "<?xml version='1.0' encoding='utf-8'?>" << std::endl;
|
||||
closed = false;
|
||||
}
|
||||
|
||||
|
||||
XMLWriter::~XMLWriter()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
|
||||
void XMLWriter::close()
|
||||
{
|
||||
if (closed) return;
|
||||
while (!pendingElems.empty()) closeElement();
|
||||
closed = true;
|
||||
}
|
||||
|
||||
|
||||
void XMLWriter::indent_(size_t depth)
|
||||
{
|
||||
if (!indent) return;
|
||||
output << string(depth * 2, ' ');
|
||||
}
|
||||
|
||||
|
||||
void XMLWriter::openElement(const string & name,
|
||||
const XMLAttrs & attrs)
|
||||
{
|
||||
assert(!closed);
|
||||
indent_(pendingElems.size());
|
||||
output << "<" << name;
|
||||
writeAttrs(attrs);
|
||||
output << ">";
|
||||
if (indent) output << std::endl;
|
||||
pendingElems.push_back(name);
|
||||
}
|
||||
|
||||
|
||||
void XMLWriter::closeElement()
|
||||
{
|
||||
assert(!pendingElems.empty());
|
||||
indent_(pendingElems.size() - 1);
|
||||
output << "</" << pendingElems.back() << ">";
|
||||
if (indent) output << std::endl;
|
||||
pendingElems.pop_back();
|
||||
if (pendingElems.empty()) closed = true;
|
||||
}
|
||||
|
||||
|
||||
void XMLWriter::writeEmptyElement(const string & name,
|
||||
const XMLAttrs & attrs)
|
||||
{
|
||||
assert(!closed);
|
||||
indent_(pendingElems.size());
|
||||
output << "<" << name;
|
||||
writeAttrs(attrs);
|
||||
output << " />";
|
||||
if (indent) output << std::endl;
|
||||
}
|
||||
|
||||
|
||||
void XMLWriter::writeAttrs(const XMLAttrs & attrs)
|
||||
{
|
||||
for (auto & i : attrs) {
|
||||
output << " " << i.first << "=\"";
|
||||
for (size_t j = 0; j < i.second.size(); ++j) {
|
||||
char c = i.second[j];
|
||||
if (c == '"') output << """;
|
||||
else if (c == '<') output << "<";
|
||||
else if (c == '>') output << ">";
|
||||
else if (c == '&') output << "&";
|
||||
/* Escape newlines to prevent attribute normalisation (see
|
||||
XML spec, section 3.3.3. */
|
||||
else if (c == '\n') output << "
";
|
||||
else output << c;
|
||||
}
|
||||
output << "\"";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue