using namespace snort;
-static void dump_value(JsonStream& json, const BaseConfigNode* node)
+static void dump_value(JsonStream& json, const char* node_name, const BaseConfigNode* node)
{
const Value* value = node->get_value();
if ( !value )
{
case Parameter::PT_BOOL:
case Parameter::PT_IMPLIED:
- value->get_bool() ? json.put_true(node->get_name().c_str()) :
- json.put_false(node->get_name().c_str());
+ value->get_bool() ? json.put_true(node_name) :
+ json.put_false(node_name);
break;
case Parameter::PT_INT:
- json.put(node->get_name().c_str(), value->get_long());
+ json.put(node_name, value->get_long());
break;
case Parameter::PT_REAL:
{
if ( pos != std::string::npos )
precision = value_str.size() - pos - 1;
- json.put(node->get_name().c_str(), value->get_real(), precision);
+ json.put(node_name, value->get_real(), precision);
break;
}
default:
- json.put(node->get_name().c_str(), value->get_origin_string());
+ json.put(node_name, value->get_origin_string());
break;
}
}
-static void dump_modules(JsonStream& json, const BaseConfigNode* node)
+static void dump_tree(JsonStream& json, const BaseConfigNode* node, bool list_node = false)
{
- Parameter::Type type = node->get_type();
- if ( type == Parameter::PT_LIST )
- json.open_array(node->get_name().c_str());
- else if ( type == Parameter::PT_TABLE )
- {
- std::string name = node->get_name();
- name.empty() ? json.open() : json.open(name.c_str());
- }
- else
- dump_value(json, node);
+ Parameter::Type node_type = node->get_type();
+ const std::string node_name = node->get_name();
+ const char* node_name_cstr = nullptr;
- for ( const auto n : node->get_children() )
- dump_modules(json, n);
+ if ( !list_node )
+ node_name_cstr = node_name.c_str();
- if ( type == Parameter::PT_LIST )
- json.close_array();
- else if ( type == Parameter::PT_TABLE )
+ if ( node_type == Parameter::PT_TABLE )
+ {
+ json.open(node_name_cstr);
+ for ( const auto n : node->get_children() )
+ dump_tree(json, n);
json.close();
+ }
+ else if ( node_type == Parameter::PT_LIST )
+ {
+ json.open_array(node_name_cstr);
+ for ( const auto n : node->get_children() )
+ dump_tree(json, n, true);
+ json.close_array();
+ }
+ else
+ dump_value(json, node_name_cstr, node);
}
JsonAllConfigOutput::JsonAllConfigOutput() :
json.open("config");
for ( const auto config_tree: config_data.config_trees )
- dump_modules(json, config_tree);
+ dump_tree(json, config_tree);
json.close();
json.close();
json.open();
for ( const auto config_tree: config_data.config_trees )
- dump_modules(json, config_tree);
+ dump_tree(json, config_tree);
json.close();
}
using namespace snort;
-void TextConfigOutput::dump_value(const BaseConfigNode* node, const std::string& config_name)
+static void dump_value(const BaseConfigNode* node, const std::string& config_name)
{
const Value* value = node->get_value();
if ( !value )
}
}
-void TextConfigOutput::dump_modules(const BaseConfigNode* parent, const std::string& config_name)
+static void dump_tree(const BaseConfigNode* node, const std::string& config_name)
{
- static char buf[16];
- int list_index = 0;
- for ( const auto node : parent->get_children() )
- {
- std::string full_config_name(config_name);
- std::string node_name = node->get_name();
+ Parameter::Type node_type = node->get_type();
- if ( !node_name.empty() )
- {
- full_config_name += ".";
- full_config_name += node_name;
- }
- else
+ if ( node_type == Parameter::PT_TABLE )
+ {
+ for ( const auto child : node->get_children() )
+ dump_tree(child, config_name + "." + child->get_name());
+ }
+ else if ( node_type == Parameter::PT_LIST )
+ {
+ char suffix[16];
+ int list_index = 0;
+ for ( const auto child : node->get_children() )
{
- sprintf(buf, "%i", list_index++);
- full_config_name += "[";
- full_config_name += buf;
- full_config_name += "]";
+ snprintf(suffix, 16, "[%i]", list_index);
+ const std::string full_config_name = config_name + suffix;
+ dump_tree(child, full_config_name);
+ list_index++;
}
-
- dump_modules(node, full_config_name);
}
-
- dump_value(parent, config_name);
+ else
+ dump_value(node, config_name);
}
void TextConfigOutput::dump(const ConfigData& config_data)
std::cout << output << std::endl;
for ( const auto config_tree: config_data.config_trees )
- dump_modules(config_tree, config_tree->get_name());
+ dump_tree(config_tree, config_tree->get_name());
}
if ( !s_config_output || !s_current_node )
return;
- // lua interpreter does not call open_table for simple list items like (string)
- // we have to add tree node for this item
+ // don't give names to list elements
if ( s_current_node->get_type() == Parameter::PT_LIST )
{
- add_config_child_node("", Parameter::PT_TABLE);
- s_current_node->add_child_node(new ValueConfigNode(s_current_node, value));
- s_current_node = s_current_node->get_parent_node();
-
+ s_current_node->add_child_node(new ValueConfigNode(s_current_node, value, ""));
return;
}