From 570331adb5be4f3d7382f5b05faafdf7195ea21d Mon Sep 17 00:00:00 2001 From: mfeemster Date: Mon, 6 Jul 2015 20:04:38 -0700 Subject: [PATCH] --Bug fixes -Allow for empty fields in template files. --Code changes -When parsing, a boolean is not available to specify whether to use the default values if they are not present. This will be false for template files. --- Source/Ember/XmlToEmber.h | 19 +++++++++++++------ Source/EmberCommon/EmberCommon.h | 5 +++-- Source/EmberGenome/EmberGenome.cpp | 2 +- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Source/Ember/XmlToEmber.h b/Source/Ember/XmlToEmber.h index 07ea62f..253dcd2 100644 --- a/Source/Ember/XmlToEmber.h +++ b/Source/Ember/XmlToEmber.h @@ -253,8 +253,9 @@ public: /// The buffer to parse /// Full path and filename, optionally empty /// The newly constructed embers based on what was parsed + /// True to use defaults if they are not present in the file, else false to use invalid values as placeholders to indicate the values were not present. Default: true. /// True if there were no errors, else false. - bool Parse(byte* buf, const char* filename, vector>& embers) + bool Parse(byte* buf, const char* filename, vector>& embers, bool useDefaults = true) { char* bn; const char* xmlPtr; @@ -286,7 +287,7 @@ public: //Scan for nodes, starting with this node. //t.Tic(); bn = basename(const_cast(filename)); - ScanForEmberNodes(rootnode, bn, embers); + ScanForEmberNodes(rootnode, bn, embers, useDefaults); xmlFreeDoc(doc); emberSize = embers.size(); //t.Toc("ScanForEmberNodes"); @@ -337,8 +338,9 @@ public: /// /// Full path and filename /// The newly constructed embers based on what was parsed + /// True to use defaults if they are not present in the file, else false to use invalid values as placeholders to indicate the values were not present. Default: true. /// True if there were no errors, else false. - bool Parse(const char* filename, vector>& embers) + bool Parse(const char* filename, vector>& embers, bool useDefaults = true) { const char* loc = __FUNCTION__; string buf; @@ -353,7 +355,7 @@ public: if (ReadFile(filename, buf)) { std::replace(buf.begin(), buf.end(), '&', '+'); - return Parse(reinterpret_cast(const_cast(buf.data())), filename, embers); + return Parse(reinterpret_cast(const_cast(buf.data())), filename, embers, useDefaults); } else return false; @@ -488,7 +490,8 @@ private: /// The current node to parse /// The full path and filename /// The newly constructed embers based on what was parsed - void ScanForEmberNodes(xmlNode* curNode, char* parentFile, vector>& embers) + /// True to use defaults if they are not present in the file, else false to use invalid values as placeholders to indicate the values were not present. + void ScanForEmberNodes(xmlNode* curNode, char* parentFile, vector>& embers, bool useDefaults) { bool parseEmberSuccess; xmlNodePtr thisNode = nullptr; @@ -504,6 +507,10 @@ private: { Ember currentEmber;//Place this inside here so its constructor is called each time. + //Useful for parsing templates when not every member should be set. + if (!useDefaults) + currentEmber.Clear(false); + parseEmberSuccess = ParseEmberElement(thisNode, currentEmber); if (!parseEmberSuccess) @@ -532,7 +539,7 @@ private: else { //Check all of the children of this element. - ScanForEmberNodes(thisNode->children, parentFile, embers); + ScanForEmberNodes(thisNode->children, parentFile, embers, useDefaults); } } } diff --git a/Source/EmberCommon/EmberCommon.h b/Source/EmberCommon/EmberCommon.h index 69926eb..a96f12a 100644 --- a/Source/EmberCommon/EmberCommon.h +++ b/Source/EmberCommon/EmberCommon.h @@ -81,11 +81,12 @@ private: /// The parser to use /// The full path and name of the file /// Storage for the embers read from the file +/// True to use defaults if they are not present in the file, else false to use invalid values as placeholders to indicate the values were not present. Default: true. /// True if success, else false. template -static bool ParseEmberFile(XmlToEmber& parser, string filename, vector>& embers) +static bool ParseEmberFile(XmlToEmber& parser, string filename, vector>& embers, bool useDefaults = true) { - if (!parser.Parse(filename.c_str(), embers)) + if (!parser.Parse(filename.c_str(), embers, useDefaults)) { cout << "Error parsing flame file " << filename << ", returning without executing." << endl; return false; diff --git a/Source/EmberGenome/EmberGenome.cpp b/Source/EmberGenome/EmberGenome.cpp index 4292f82..b4b7f18 100644 --- a/Source/EmberGenome/EmberGenome.cpp +++ b/Source/EmberGenome/EmberGenome.cpp @@ -239,7 +239,7 @@ bool EmberGenome(EmberOptions& opt) if (opt.TemplateFile() != "") { - if (!ParseEmberFile(parser, opt.TemplateFile(), templateEmbers)) + if (!ParseEmberFile(parser, opt.TemplateFile(), templateEmbers, false))//Do not use defaults here to ensure only present fields get used when applying the template. return false; if (templateEmbers.size() > 1)