Skip to content

Instantly share code, notes, and snippets.

@Reputeless
Last active December 18, 2020 15:07
Show Gist options
  • Save Reputeless/4db63094cb3e95502d0f5a8e4ed8b75c to your computer and use it in GitHub Desktop.
Save Reputeless/4db63094cb3e95502d0f5a8e4ed8b75c to your computer and use it in GitHub Desktop.
# include <Siv3D.hpp> // OpenSiv3D v0.4.3
# include "tinyxml2.h" // https://github.com/leethomason/tinyxml2 (tinyxml2.cpp もプロジェクトに加える)
namespace s3dx // extension
{
class XMLWriter : public tinyxml2::XMLPrinter
{
private:
BinaryWriter m_writer;
public:
XMLWriter() = default;
explicit XMLWriter(const FilePath& path)
: m_writer(path) {}
~XMLWriter()
{
close();
}
bool open(const FilePath& path)
{
close();
return m_writer.open(path);
}
void close()
{
if (!m_writer)
{
return;
}
if (CStrSize() > 1)
{
m_writer.write(CStr(), (CStrSize() - 1));
}
ClearBuffer();
m_writer.close();
}
bool isOpen() const
{
return m_writer.isOpen();
}
explicit operator bool() const
{
return isOpen();
}
};
}
void Main()
{
{
s3dx::XMLWriter xml{ U"test.xml" };
if (!xml)
{
return;
}
xml.OpenElement("root");
{
xml.OpenElement("foo");
{
xml.PushAttribute("attrib-text", "text");
xml.PushAttribute("attrib-int32", int32(111));
xml.PushAttribute("attrib-int64", int64(222));
xml.PushAttribute("attrib-uint64", uint64(333));
xml.PushAttribute("attrib-bool", true);
xml.PushAttribute("attrib-double", 3.1415);
}
xml.CloseElement();
xml.OpenElement("text");
{
xml.PushText("OpenSiv3D");
}
xml.CloseElement();
}
xml.CloseElement();
// 明示的な XMLWriter::close() または
// デストラクタでファイルに保存
}
while (System::Update())
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment