Last active
August 11, 2022 19:12
-
-
Save t-mat/7150417 to your computer and use it in GitHub Desktop.
Revisions
-
t-mat revised this gist
Feb 9, 2014 . 1 changed file with 1 addition and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -85,9 +85,7 @@ std::vector<char> loadFile(const String& filename) { int main(int argc, const char* argv[]) { const auto args = std::vector<std::string>(argv+1, argv+argc); for(const auto& arg : args) { const auto xmlFile = loadFile(arg); if(! xmlFile.empty()) { -
t-mat revised this gist
Feb 9, 2014 . 1 changed file with 11 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,7 +12,7 @@ #include <cstdint> #if defined(RAPIDXML_NO_EXCEPTIONS) void rapidxml::parse_error_handler(const char* what, void* where) { printf("Parse error(@%p): %s\n", where, what); std::abort(); } @@ -71,22 +71,25 @@ void processXmlFile(const char* data) { } template<class String> std::vector<char> loadFile(const String& filename) { std::vector<char> file; if(std::ifstream is { filename, is.binary | is.ate }) { file.resize(static_cast<size_t>(is.tellg())); is.seekg(0); is.read(file.data(), file.size()); file.push_back(0); } return file; } int main(int argc, const char* argv[]) { using Char = std::remove_const<std::remove_reference<decltype(**argv)>::type>::type; using String = std::basic_string<Char>; const auto args = std::vector<String>(argv+1, argv+argc); for(const auto& arg : args) { const auto xmlFile = loadFile(arg); if(! xmlFile.empty()) { const auto tmp = xmlFile; processXmlFile(xmlFile.data()); -
t-mat revised this gist
Jan 5, 2014 . 1 changed file with 68 additions and 32 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ // RapidXml : http://rapidxml.sourceforge.net/ #include <cstddef> #include <cassert> #define RAPIDXML_NO_STDLIB @@ -17,48 +18,83 @@ void rapidxml::parse_error_handler(const char *what, void *where) { } #endif void walk(const rapidxml::xml_node<>* node, int indent = 0) { const auto ind = std::string(indent * 4, ' '); printf("%s", ind.c_str()); const rapidxml::node_type t = node->type(); switch(t) { case rapidxml::node_element: { printf("<%.*s", node->name_size(), node->name()); for(const rapidxml::xml_attribute<>* a = node->first_attribute() ; a ; a = a->next_attribute() ) { printf(" %.*s", a->name_size(), a->name()); printf("='%.*s'", a->value_size(), a->value()); } printf(">\n"); for(const rapidxml::xml_node<>* n = node->first_node() ; n ; n = n->next_sibling() ) { walk(n, indent+1); } printf("%s</%.*s>\n", ind.c_str(), node->name_size(), node->name()); } break; case rapidxml::node_data: printf("DATA:[%.*s]\n", node->value_size(), node->value()); break; default: printf("NODE-TYPE:%d\n", t); break; } } void processXmlFile(const char* data) { enum { PARSE_FLAGS = rapidxml::parse_non_destructive }; // NOTE : There is a `const_cast<>`, but `rapidxml::parse_non_destructive` // guarantees `data` is not overwritten. rapidxml::xml_document<> xmlDoc; xmlDoc.parse<PARSE_FLAGS>(const_cast<char*>(data)); walk(xmlDoc.first_node()); } std::vector<char> loadFile(const char* filename) { std::vector<char> file; if(std::ifstream is { filename, is.binary | is.ate }) { // NOTE : `+1` for terminal NUL(0x00) character file.resize(static_cast<size_t>(is.tellg()) + 1); is.seekg(0); is.read(file.data(), file.size() - 1); file.back() = '\0'; } return file; } int main(int argc, const char* argv[]) { for(int iarg = 1; iarg < argc; ++iarg) { std::vector<char> xmlFile = loadFile(argv[iarg]); if(! xmlFile.empty()) { const auto tmp = xmlFile; processXmlFile(xmlFile.data()); // check for const correctness if(memcmp(xmlFile.data(), tmp.data(), tmp.size()) != 0) { printf("ERROR: xmlFile is overwritten.\n"); } } } } -
t-mat created this gist
Oct 25, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,64 @@ #include <cstddef> #include <cassert> #define RAPIDXML_NO_STDLIB #define RAPIDXML_NO_EXCEPTIONS #include "rapidxml-1.13/rapidxml.hpp" #include <stdio.h> #include <string> #include <vector> #include <fstream> #include <cstdint> #if defined(RAPIDXML_NO_EXCEPTIONS) void rapidxml::parse_error_handler(const char *what, void *where) { printf("Parse error(@%p): %s\n", where, what); std::abort(); } #endif void walk(const rapidxml::xml_node<>* node, int indent = 0) { if(0 == indent) { printf("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"); } const auto* name = node->name(); printf("%*s<%s", indent*2 , "", name); for(const auto* attr = node->first_attribute(); attr; attr = attr->next_attribute()) { const auto* name = attr->name(); const auto* val = attr->value(); printf(" %s=\"%s\"", name, val); } const auto* n = node->first_node(); if(n) { printf(">\n"); for(; n; n = n->next_sibling()) { walk(n, indent+1); } printf("%*s</%s>\n", indent*2, "", name); } else { printf(" />\n"); } } int main(int argc, const char* argv[]) { for(int iarg = 1; iarg < argc; ++iarg) { std::ifstream is { argv[iarg] }; if(is) { std::vector<char> xml; is.seekg(0, is.end); xml.resize(static_cast<size_t>(is.tellg()) + 1); is.seekg(0); is.read(xml.data(), xml.size() - 1); xml.back() = '\0'; rapidxml::xml_document<> xmlDoc; xmlDoc.parse<0>(xml.data()); const auto* root = xmlDoc.first_node(); walk(root); } } }