From e80f2b44f155a6d93d1485657c4cd2008e06f5ad Mon Sep 17 00:00:00 2001 From: "sgunderson@bigfoot.com" <> Date: Wed, 21 Sep 2011 21:51:11 +0200 Subject: [PATCH] Move the C++0x file processing logic into a shared function. --- c++0x/passthru.cpp | 10 +--------- c++0x/simple.cpp | 11 +---------- c++0x/xml-template.cpp | 15 +++++++++++++++ c++0x/xml-template.h | 4 ++++ 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/c++0x/passthru.cpp b/c++0x/passthru.cpp index 2db2e3d..afc38ec 100644 --- a/c++0x/passthru.cpp +++ b/c++0x/passthru.cpp @@ -8,15 +8,7 @@ using namespace std; int main(int argc, char **argv) { - LIBXML_TEST_VERSION - unordered_map master_map; - - xmlDocPtr doc = xmlParseFile("../xml/passthru.xml"); - Substitute(master_map).process(xmlDocGetRootElement(doc), false); - xmlSaveFile(argv[1], doc); - - xmlCleanupParser(); - xmlMemoryDump(); + process_file("../xml/passthru.xml", argv[1], new Substitute(master_map)); return(0); } diff --git a/c++0x/simple.cpp b/c++0x/simple.cpp index cfc5041..36766f8 100644 --- a/c++0x/simple.cpp +++ b/c++0x/simple.cpp @@ -1,6 +1,4 @@ #include -#include -#include #include "xml-template.h" @@ -8,17 +6,10 @@ using namespace std; int main(int argc, char **argv) { - LIBXML_TEST_VERSION - unordered_map master_map; master_map.insert(make_pair("title", new Replace("A very basic example"))); master_map.insert(make_pair("#hello", new Replace("Hello world!"))); - xmlDocPtr doc = xmlParseFile("../xml/simple.xml"); - Substitute(master_map).process(xmlDocGetRootElement(doc), false); - xmlSaveFile(argv[1], doc); - - xmlCleanupParser(); - xmlMemoryDump(); + process_file("../xml/simple.xml", argv[1], new Substitute(master_map)); return(0); } diff --git a/c++0x/xml-template.cpp b/c++0x/xml-template.cpp index a158f31..3be1407 100644 --- a/c++0x/xml-template.cpp +++ b/c++0x/xml-template.cpp @@ -1,6 +1,7 @@ #include "xml-template.h" #include +#include using namespace std; @@ -44,3 +45,17 @@ void Substitute::process(xmlNode *node, bool clean) { } } } + +void process_file(const string &input_filename, + const string &output_filename, + Directive *root_directive) +{ + LIBXML_TEST_VERSION + + xmlDocPtr doc = xmlParseFile(input_filename.c_str()); + root_directive->process(xmlDocGetRootElement(doc), false); + xmlSaveFile(output_filename.c_str(), doc); + + xmlCleanupParser(); + xmlMemoryDump(); +} diff --git a/c++0x/xml-template.h b/c++0x/xml-template.h index cd417e2..95d485f 100644 --- a/c++0x/xml-template.h +++ b/c++0x/xml-template.h @@ -31,4 +31,8 @@ class Substitute : public Directive { const std::unordered_map &substitution_map; }; +void process_file(const std::string &input_filename, + const std::string &output_filename, + Directive *root_directive); + #endif // !defined(_XML_TEMPLATE_H) -- 2.39.2