From 9cb5eb04bf0700bef1c061261ffbd6dc893eb7e8 Mon Sep 17 00:00:00 2001 From: An-Cheng Huang Date: Thu, 20 Jan 2011 10:28:06 -0800 Subject: export config output algorithms through shell API --- src/cnode/cnode-algorithm.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++ src/cnode/cnode-algorithm.hpp | 8 ++++++ 2 files changed, 67 insertions(+) (limited to 'src/cnode') diff --git a/src/cnode/cnode-algorithm.cpp b/src/cnode/cnode-algorithm.cpp index 844b145..3722d13 100644 --- a/src/cnode/cnode-algorithm.cpp +++ b/src/cnode/cnode-algorithm.cpp @@ -17,9 +17,11 @@ #include #include #include +#include #include #include +#include #include using namespace std; @@ -33,6 +35,8 @@ static const string PFX_DIFF_UPD = ">"; // changed static const string PFX_DIFF_NONE = " "; static const string PFX_DIFF_NULL = ""; +const string cnode::ACTIVE_CFG = "@ACTIVE"; +const string cnode::WORKING_CFG = "@WORKING"; ////// static (internal) functions static void @@ -893,3 +897,58 @@ cnode::get_cmds(const CfgNode& cfg, vector >& set_list, _get_cmds_diff(&cfg, &cfg, cur_path, del_list, set_list, com_list); } +void +cnode::showConfig(const string& cfg1, const string& cfg2, + const vector& path, bool show_def, bool hide_secret, + bool context_diff, bool show_cmds) +{ + tr1::shared_ptr aroot, wroot, croot1, croot2; + tr1::shared_ptr cstore; + vector rpath(path); + vector cur_path; + + if ((cfg1 == ACTIVE_CFG || cfg1 == WORKING_CFG) + && (cfg2 == ACTIVE_CFG || cfg2 == WORKING_CFG)) { + // active/working config only => use edit level and path + cstore.reset(Cstore::createCstore(true)); + cstore->getEditLevel(cur_path); + } else { + // at least one config file => don't use edit level and path + cstore.reset(Cstore::createCstore(false)); + rpath.clear(); + } + if (cfg1 == ACTIVE_CFG || cfg2 == ACTIVE_CFG) { + aroot.reset(new CfgNode(*cstore, rpath, true, true)); + } + if (cfg1 == WORKING_CFG || cfg2 == WORKING_CFG) { + // note: if there is no config session, this will abort + wroot.reset(new CfgNode(*cstore, rpath, false, true)); + } + + if (cfg1 == ACTIVE_CFG) { + croot1 = aroot; + } else if (cfg1 == WORKING_CFG) { + croot1 = wroot; + } else { + croot1.reset(cparse::parse_file(cfg1.c_str(), *cstore)); + } + if (cfg2 == ACTIVE_CFG) { + croot2 = aroot; + } else if (cfg2 == WORKING_CFG) { + croot2 = wroot; + } else { + croot2.reset(cparse::parse_file(cfg2.c_str(), *cstore)); + } + if (!croot1.get() || !croot2.get()) { + printf("Cannot parse specified config file(s)\n"); + return; + } + + if (show_cmds) { + show_cmds_diff(*croot1, *croot2); + } else { + show_cfg_diff(*croot1, *croot2, cur_path, show_def, hide_secret, + context_diff); + } +} + diff --git a/src/cnode/cnode-algorithm.hpp b/src/cnode/cnode-algorithm.hpp index 4b1ec2f..c473d6a 100644 --- a/src/cnode/cnode-algorithm.hpp +++ b/src/cnode/cnode-algorithm.hpp @@ -38,6 +38,14 @@ void get_cmds_diff(const CfgNode& cfg1, const CfgNode& cfg2, void get_cmds(const CfgNode& cfg, vector >& set_list, vector >& com_list); +extern const string ACTIVE_CFG; +extern const string WORKING_CFG; + +void showConfig(const string& cfg1, const string& cfg2, + const vector& path, bool show_def = false, + bool hide_secret = false, bool context_diff = false, + bool show_cmds = false); + } // namespace cnode #endif /* _CNODE_ALGORITHM_HPP_ */ -- cgit v1.2.3