mirror of
https://github.com/libAthena/athena.git
synced 2025-12-09 21:47:52 +00:00
YAML file merge call to avoid clobbering existing document nodes
This commit is contained in:
@@ -12,16 +12,25 @@
|
||||
#include <utf8proc.h>
|
||||
#include "DNA.hpp"
|
||||
#include "FileReader.hpp"
|
||||
#include "FileWriter.hpp"
|
||||
|
||||
namespace athena::io
|
||||
{
|
||||
|
||||
enum class YAMLNodeStyle
|
||||
{
|
||||
Any,
|
||||
Flow,
|
||||
Block
|
||||
};
|
||||
|
||||
struct YAMLNode
|
||||
{
|
||||
yaml_node_type_t m_type;
|
||||
std::string m_scalarString;
|
||||
std::vector<std::unique_ptr<YAMLNode>> m_seqChildren;
|
||||
std::vector<std::pair<std::string, std::unique_ptr<YAMLNode>>> m_mapChildren;
|
||||
YAMLNodeStyle m_style = YAMLNodeStyle::Any;
|
||||
YAMLNode(yaml_node_type_t type) : m_type(type) {}
|
||||
inline const YAMLNode* findMapChild(std::string_view key) const
|
||||
{
|
||||
@@ -30,6 +39,16 @@ struct YAMLNode
|
||||
return item.second.get();
|
||||
return nullptr;
|
||||
}
|
||||
inline void assignMapChild(std::string_view key, std::unique_ptr<YAMLNode>&& node)
|
||||
{
|
||||
for (auto& item : m_mapChildren)
|
||||
if (!item.first.compare(key))
|
||||
{
|
||||
item.second = std::move(node);
|
||||
return;
|
||||
}
|
||||
m_mapChildren.emplace_back(key, std::move(node));
|
||||
}
|
||||
};
|
||||
|
||||
template <typename RETURNTYPE>
|
||||
@@ -322,14 +341,14 @@ public:
|
||||
|
||||
class YAMLDocWriter
|
||||
{
|
||||
YAMLNode m_rootNode;
|
||||
std::unique_ptr<YAMLNode> m_rootNode;
|
||||
std::vector<YAMLNode*> m_subStack;
|
||||
yaml_emitter_t m_emitter;
|
||||
static bool RecursiveFinish(yaml_emitter_t* doc, const YAMLNode& node);
|
||||
void _leaveSubRecord();
|
||||
void _leaveSubVector();
|
||||
public:
|
||||
YAMLDocWriter(const char* classType);
|
||||
YAMLDocWriter(const char* classType, athena::io::IStreamReader* reader = nullptr);
|
||||
~YAMLDocWriter();
|
||||
|
||||
yaml_emitter_t* getEmitter() { return &m_emitter; }
|
||||
@@ -440,6 +459,8 @@ public:
|
||||
void writeWString(const char* name, std::wstring_view val);
|
||||
void writeU16String(const char* name, std::u16string_view val);
|
||||
void writeU32String(const char* name, std::u32string_view val);
|
||||
|
||||
void setStyle(YAMLNodeStyle s);
|
||||
};
|
||||
|
||||
int YAMLAthenaReader(athena::io::IStreamReader* reader,
|
||||
@@ -571,6 +592,20 @@ struct DNAYaml : DNA<DNAE>
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename NameT>
|
||||
bool mergeToYAMLFile(const NameT& filename)
|
||||
{
|
||||
athena::io::FileReader r(filename);
|
||||
YAMLDocWriter docWriter(DNATypeV(), r.isOpen() ? &r : nullptr);
|
||||
r.close();
|
||||
|
||||
write(docWriter);
|
||||
athena::io::FileWriter w(filename);
|
||||
if (!w.isOpen())
|
||||
return false;
|
||||
return docWriter.finish(&w);
|
||||
}
|
||||
|
||||
template<class DNASubtype>
|
||||
static bool ValidateFromYAMLStream(athena::io::IStreamReader& fin)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user