1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
#include <map>
#include <boost/variant.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <string>
typedef boost::variant<uint8_t, uint16_t, uint32_t, uint64_t, int8_t, int16_t, int32_t, int64_t, std::string> XMLAttribute;
struct Type
{
/// Name of the type
std::string name;
/// Variables of the type
std::map<std::string, std::string> vars;
void addType(const std::string& type, const std::string& name)
{
vars.insert(std::make_pair(type, name));
}
};
struct Namespace
{
/// Types contained in this namespace
std::map<std::string, Type> types;
/// Sub-namespaces
std::map<std::string, Namespace> namespaces;
Namespace& addNamespace(const std::string& namespace_)
{
namespaces.insert(std::make_pair(namespace_, (::Namespace())));
return namespaces[namespace_];
}
};
struct Format
{
/// Format Attributes
std::map<std::string, XMLAttribute> attributes;
/// Top level namespaces
std::map<std::string, Namespace> namespaces;
template<typename T>
void addAttribute(const std::string& key, T val)
{
attributes.insert(std::make_pair(key, val));
}
Namespace& addNamespace(const std::string& namespace_)
{
namespaces.insert(std::make_pair(namespace_, (::Namespace())));
return namespaces[namespace_];
}
};
struct Model
{
/// Formats found in this file
std::map<std::string, Format> formats;
void addFormat(std::string name, Format&& format)
{
formats.insert(std::make_pair(name, std::move(format)));
}
};
#define DEBUG
#if defined(DEBUG)
# define debug(message) std::cout << message << std::endl;
#else
#define debug(message) do {} while(false)
#endif
void iterate_namespace(Namespace& namespace_, boost::property_tree::ptree& tree)
{
for(auto child : tree)
{
if(child.first == "namespace")
{
debug("\tAdded Namespace: " << tree.get<std::string>("<xmlattr>.name"));
iterate_namespace(namespace_.addNamespace(tree.get<std::string>("<xmlattr>.name")), child.second);
}
else if(child.first == "type")
{
Type type;
type.name = child.second.get<std::string>("<xmlattr>.name");
debug("\t\tAdded Type: " << type.name);
for(auto var : child.second)
{
if(var.first != "<xmlattr>")
{
debug("\t\t\tAdded Variable: " << var.second.get<std::string>("<xmlattr>.name") << "(" << var.first << ")");
type.addType(var.first, var.second.get<std::string>("<xmlattr>.name"));
}
}
}
}
}
int main(int argc, char** argv)
{
std::cout << "test" << std::endl;
boost::property_tree::ptree tree;
try
{
Model model;
std::string file = "data.xml";
// Swap out for read_json or read_ini if need be.
boost::property_tree::xml_parser::read_xml(file, tree);
debug("Loading format file: " << file);
for(const auto& iter : tree)
{
if(iter.first != "format")
{
debug("Unknown Field: " << iter.first);
continue;
}
Format format;
/// Read Format Attributes
for(auto attrib : iter.second.get_child("<xmlattr>"))
{
format.addAttribute(attrib.first, attrib.second.data());
debug("\tAdded Attribute: " << attrib.first << ": " << attrib.second.data());
}
debug("");
/// Read Format
for(auto namespace_ : iter.second)
{
if(namespace_.first != "namespace") continue;
debug("\tAdded Namespace: " << namespace_.second.get<std::string>("<xmlattr>.name"));
iterate_namespace(format.addNamespace(namespace_.second.get<std::string>("<xmlattr>.name", "")), namespace_.second);
}
}
}
catch(const boost::property_tree::ptree_error& error)
{
debug(error.what());
}
}
|