From 91b4c34c1fcc19ecf16f53ce5c097376b9175233 Mon Sep 17 00:00:00 2001 From: An-Cheng Huang Date: Wed, 10 Nov 2010 20:17:56 -0800 Subject: consolidate similar logic and simplify code --- src/cnode/cnode-algorithm.cpp | 102 +++++++++++++----------------------------- src/cnode/cnode.cpp | 8 +--- src/cnode/cnode.hpp | 2 - 3 files changed, 33 insertions(+), 79 deletions(-) diff --git a/src/cnode/cnode-algorithm.cpp b/src/cnode/cnode-algorithm.cpp index 58e8a65..d1101d7 100644 --- a/src/cnode/cnode-algorithm.cpp +++ b/src/cnode/cnode-algorithm.cpp @@ -79,19 +79,19 @@ _diff_print_indent(CfgNode *cfg1, CfgNode *cfg2, int level, const char *pfx_diff) { const char *pfx_deact = PFX_DEACT_NONE.c_str(); - if (cfg1 && cfg2) { - if (cfg1->isDeactivated()) { - if (cfg2->isDeactivated()) { - pfx_deact = PFX_DEACT_D.c_str(); - } else { - pfx_deact = PFX_DEACT_AP.c_str(); - } + bool de1 = (cfg1 ? cfg1->isDeactivated() : false); + bool de2 = (cfg2 ? cfg2->isDeactivated() : false); + if (de1) { + if (de2) { + pfx_deact = PFX_DEACT_D.c_str(); } else { - if (cfg2->isDeactivated()) { - pfx_deact = PFX_DEACT_DP.c_str(); - } - // 4th case handled by default + pfx_deact = PFX_DEACT_AP.c_str(); } + } else { + if (de2) { + pfx_deact = PFX_DEACT_DP.c_str(); + } + // 4th case handled by default } printf("%s %s", pfx_deact, pfx_diff); @@ -236,54 +236,6 @@ _diff_check_and_show_leaf(CfgNode *cfg1, CfgNode *cfg2, int level, return true; } -static bool -_diff_check_and_show_tag(CfgNode *cfg1, CfgNode *cfg2, int level, - bool show_def, bool hide_secret) -{ - if ((cfg1 && !cfg1->isTag()) || (cfg2 && !cfg2->isTag())) { - // not a tag node - return false; - } - - vector vals1, vals2; - if (cfg1) { - vals1 = cfg1->getTagValues(); - } - if (cfg2) { - vals2 = cfg2->getTagValues(); - } - - Cstore::MapT vmap; - Cstore::MapT vnmap1, vnmap2; - for (size_t i = 0; i < vals1.size(); i++) { - vmap[vals1[i]->getValue()] = true; - vnmap1[vals1[i]->getValue()] = vals1[i]; - } - for (size_t i = 0; i < vals2.size(); i++) { - vmap[vals2[i]->getValue()] = true; - vnmap2[vals2[i]->getValue()] = vals2[i]; - } - - vector values; - Cstore::MapT::iterator it = vmap.begin(); - for (; it != vmap.end(); ++it) { - values.push_back((*it).first); - } - Cstore::sortNodes(values); - - for (size_t i = 0; i < values.size(); i++) { - bool in1 = (vnmap1.find(values[i]) != vnmap1.end()); - bool in2 = (vnmap2.find(values[i]) != vnmap2.end()); - CfgNode *c1 = (in1 ? vnmap1[values[i]] : NULL); - CfgNode *c2 = (in2 ? vnmap2[values[i]] : NULL); - /* note: if the root is a tag node (level == -1), then need to make - * level 0 when calling tag values' show(). - */ - _show_diff(c1, c2, ((level >= 0) ? level : 0), show_def, hide_secret); - } - return true; -} - static void _diff_show_other(CfgNode *cfg1, CfgNode *cfg2, int level, bool show_def, bool hide_secret) @@ -302,8 +254,14 @@ _diff_show_other(CfgNode *cfg1, CfgNode *cfg2, int level, bool show_def, } } + /* only print "this" node if it + * (1) is a tag value or an intermediate node, + * (2) is not "root", and + * (3) has a "name". + */ const string& name = cfg->getName(); - bool print_this = (level >= 0 && name.size() > 0); + bool print_this = (((cfg1 && !cfg1->isTag()) || (cfg2 && !cfg2->isTag())) + && level >= 0 && name.size() > 0); if (print_this) { _diff_print_comment(cfg1, cfg2, level); _diff_print_indent(cfg1, cfg2, level, pfx_diff); @@ -317,6 +275,7 @@ _diff_show_other(CfgNode *cfg1, CfgNode *cfg2, int level, bool show_def, printf("%s\n", (cfg->isLeafTypeless() ? "" : " {")); } + // handle child nodes vector cnodes1, cnodes2; if (cfg1) { cnodes1 = cfg1->getChildNodes(); @@ -328,12 +287,16 @@ _diff_show_other(CfgNode *cfg1, CfgNode *cfg2, int level, bool show_def, Cstore::MapT map; Cstore::MapT nmap1, nmap2; for (size_t i = 0; i < cnodes1.size(); i++) { - map[cnodes1[i]->getName()] = true; - nmap1[cnodes1[i]->getName()] = cnodes1[i]; + string key + = (cfg->isTag() ? cnodes1[i]->getValue() : cnodes1[i]->getName()); + map[key] = true; + nmap1[key] = cnodes1[i]; } for (size_t i = 0; i < cnodes2.size(); i++) { - map[cnodes2[i]->getName()] = true; - nmap2[cnodes2[i]->getName()] = cnodes2[i]; + string key + = (cfg->isTag() ? cnodes2[i]->getValue() : cnodes2[i]->getName()); + map[key] = true; + nmap2[key] = cnodes2[i]; } vector cnodes; @@ -348,9 +311,12 @@ _diff_show_other(CfgNode *cfg1, CfgNode *cfg2, int level, bool show_def, bool in2 = (nmap2.find(cnodes[i]) != nmap2.end()); CfgNode *c1 = (in1 ? nmap1[cnodes[i]] : NULL); CfgNode *c2 = (in2 ? nmap2[cnodes[i]] : NULL); - _show_diff(c1, c2, level + 1, show_def, hide_secret); + + int next_level = (cfg->isTag() ? ((level >= 0) ? level : 0) : (level + 1)); + _show_diff(c1, c2, next_level, show_def, hide_secret); } + // finish printing "this" node if necessary if (print_this && !cfg->isLeafTypeless()) { _diff_print_indent(cfg1, cfg2, level, pfx_diff); printf("}\n"); @@ -382,12 +348,8 @@ _show_diff(CfgNode *cfg1, CfgNode *cfg2, int level, bool show_def, if (_diff_check_and_show_leaf(cfg1, cfg2, level, show_def, hide_secret)) { // leaf node has been shown. done. return; - } else if (_diff_check_and_show_tag(cfg1, cfg2, level, show_def, - hide_secret)) { - // tag node has been shown. done. - return; } else { - // intermediate node or tag value + // intermediate node, tag node, or tag value _diff_show_other(cfg1, cfg2, level, show_def, hide_secret); } } diff --git a/src/cnode/cnode.cpp b/src/cnode/cnode.cpp index 4ebbb2b..2948a14 100644 --- a/src/cnode/cnode.cpp +++ b/src/cnode/cnode.cpp @@ -111,13 +111,7 @@ CfgNode::CfgNode(Cstore& cstore, vector& path_comps, for (size_t i = 0; i < cnodes.size(); i++) { path_comps.push_back(cnodes[i]); CfgNode *cn = new CfgNode(cstore, path_comps, active, recursive); - if (_is_tag && !_is_value) { - // tag node - _tag_values.push_back(cn); - } else { - // intermediate node or tag value - _child_nodes.push_back(cn); - } + _child_nodes.push_back(cn); path_comps.pop_back(); } } diff --git a/src/cnode/cnode.hpp b/src/cnode/cnode.hpp index 3143370..0be02ca 100644 --- a/src/cnode/cnode.hpp +++ b/src/cnode/cnode.hpp @@ -43,7 +43,6 @@ public: const std::string& getValue() const { return _value; } const std::vector& getValues() const { return _values; } const std::string& getComment() const { return _comment; } - const std::vector& getTagValues() const { return _tag_values; } const std::vector& getChildNodes() const { return _child_nodes; } private: @@ -60,7 +59,6 @@ private: std::string _value; std::vector _values; std::string _comment; - std::vector _tag_values; std::vector _child_nodes; }; -- cgit v1.2.3