From a2ebbbe45057acd2ca72b2bb7cc2a54fc0474990 Mon Sep 17 00:00:00 2001 From: winckler Date: Mon, 15 Jun 2015 15:55:11 +0200 Subject: [PATCH] move fairmq json parser to fairmq/options --- fairmq/CMakeLists.txt | 6 +- .../{ProgOptionTest/lib => }/FairMQParser.cxx | 90 +---------------- fairmq/options/FairMQParser.h | 38 ++++++++ fairmq/options/ProgOptionTest/CMakeLists.txt | 2 +- .../lib/FairMQParserExample.cxx | 97 +++++++++++++++++++ .../{FairMQParser.h => FairMQParserExample.h} | 23 ++--- 6 files changed, 150 insertions(+), 106 deletions(-) rename fairmq/options/{ProgOptionTest/lib => }/FairMQParser.cxx (67%) create mode 100644 fairmq/options/FairMQParser.h create mode 100644 fairmq/options/ProgOptionTest/lib/FairMQParserExample.cxx rename fairmq/options/ProgOptionTest/lib/{FairMQParser.h => FairMQParserExample.h} (72%) diff --git a/fairmq/CMakeLists.txt b/fairmq/CMakeLists.txt index 7092f6ba..239ff5ac 100644 --- a/fairmq/CMakeLists.txt +++ b/fairmq/CMakeLists.txt @@ -6,7 +6,9 @@ # copied verbatim in the file "LICENSE" # ################################################################################ #Temporary for test -add_subdirectory (options/ProgOptionTest) +#add_subdirectory (options/ProgOptionTest) +configure_file(${CMAKE_SOURCE_DIR}/fairmq/options/ProgOptionTest/macro/bsampler-sink.json ${CMAKE_BINARY_DIR}/bin/bsampler-sink.json) + Set(INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR}/fairmq ${CMAKE_SOURCE_DIR}/fairmq/devices @@ -76,7 +78,7 @@ set(SRCS "devices/FairMQMerger.cxx" "options/FairProgOptions.cxx" "options/FairMQProgOptions.cxx" - "options/FairMQChannel.cxx" + "options/FairMQParser.cxx" "FairMQPoller.cxx" "examples/req-rep/FairMQExampleClient.cxx" "examples/req-rep/FairMQExampleServer.cxx" diff --git a/fairmq/options/ProgOptionTest/lib/FairMQParser.cxx b/fairmq/options/FairMQParser.cxx similarity index 67% rename from fairmq/options/ProgOptionTest/lib/FairMQParser.cxx rename to fairmq/options/FairMQParser.cxx index 8e2e184f..b3b8ef5d 100644 --- a/fairmq/options/ProgOptionTest/lib/FairMQParser.cxx +++ b/fairmq/options/FairMQParser.cxx @@ -7,7 +7,6 @@ #include "FairMQParser.h" #include "FairMQLogger.h" -#include // WARNING : pragma commands to hide boost (1.54.0) warning // TODO : remove these pragma commands when boost will fix this issue in future release @@ -18,11 +17,10 @@ namespace FairMQParser { - no_id_exception NoIdErr; // TODO : add key-value map parameter for replacing/updating values from keys // function that convert property tree (given the xml or json structure) to FairMQMap - FairMQMap boost_ptree_to_MQMap(const boost::property_tree::ptree& pt, const std::string& device_id, const std::string& root_node, const std::string& format_flag) + FairMQMap ptreeToMQMap(const boost::property_tree::ptree& pt, const std::string& device_id, const std::string& root_node, const std::string& format_flag) { // Create fair mq map FairMQMap MQChannelMap; @@ -30,10 +28,6 @@ namespace FairMQParser // variables to create key for the mq map. Note: maybe device name and id useless here std::string kdevice_id; std::string kchannel; - - if(device_id.empty()) - throw NoIdErr; - // do a first loop just to print the device-id in xml/json input for(const auto& p : pt.get_child(root_node)) @@ -156,97 +150,19 @@ namespace FairMQParser - //////////////////////////////////////////////////////////////////////////// - //----------- filename version - FairMQMap XML::UserParser(const std::string& filename, const std::string& device_id, const std::string& root_node) - { - boost::property_tree::ptree pt; - boost::property_tree::read_xml(filename, pt); - return boost_ptree_to_MQMap(pt,device_id,root_node,"xml"); - } - - - - //----------- stringstream version - FairMQMap XML::UserParser(std::stringstream& input_ss, const std::string& device_id, const std::string& root_node) - { - boost::property_tree::ptree pt; - boost::property_tree::read_xml(input_ss, pt); - return boost_ptree_to_MQMap(pt,device_id,root_node,"xml"); - } - - //////////////////////////////////////////////////////////////////////////// FairMQMap JSON::UserParser(const std::string& filename, const std::string& device_id, const std::string& root_node) { boost::property_tree::ptree pt; boost::property_tree::read_json(filename, pt); - return boost_ptree_to_MQMap(pt,device_id,root_node,"json"); + return ptreeToMQMap(pt,device_id,root_node,"json"); } FairMQMap JSON::UserParser(std::stringstream& input_ss, const std::string& device_id, const std::string& root_node) { boost::property_tree::ptree pt; boost::property_tree::read_json(input_ss, pt); - return boost_ptree_to_MQMap(pt,device_id,root_node,"json"); + return ptreeToMQMap(pt,device_id,root_node,"json"); } - // other xml examples - //////////////////////////////////////////////////////////////////////////// - boost::property_tree::ptree MQXML2::UserParser(const std::string& filename) - { - boost::property_tree::ptree pt; - boost::property_tree::read_xml(filename, pt); - return pt; - } - - - - - // TODO : finish implementation - //////////////////////////////////////////////////////////////////////////// - boost::property_tree::ptree MQXML3::UserParser(const std::string& filename, const std::string& devicename) - { - // Create an empty property tree object - boost::property_tree::ptree pt; - boost::property_tree::read_xml(filename, pt); - - - // Create fair mq map - - auto xml = pt.get_child(""); - std::vector> match; - std::pair device_match; - - ProcessTree(xml.begin (), xml.end (), std::back_inserter(match), - [] (const std::string& key) { return key == "device"; }); - - - // for each device - for (const auto& pair: match) - { - if(pair.second.get(".name") == devicename) - { - device_match=pair; - - } - else - { - //match.erase(std::remove(match.begin(), match.end(), pair), match.end()); - continue; - } - - //std::cout << "pair.first " << pair.first << std::endl;//device - //std::cout << "\t node = " << pair.first - // << "\t name = " << pair.second.get(".name") - // << "\t id = " << pair.second.get(".id"); - //std::cout< + +// std +#include +#include +#include + + +namespace FairMQParser +{ + + typedef std::map > FairMQMap; + FairMQMap ptreeToMQMap(const boost::property_tree::ptree& pt, const std::string& device_id, const std::string& root_node, const std::string& format_flag="json"); + + struct JSON + { + FairMQMap UserParser(const std::string& filename, const std::string& device_id, const std::string& root_node="fairMQOptions"); + FairMQMap UserParser(std::stringstream& input_ss, const std::string& device_id, const std::string& root_node="fairMQOptions"); + }; + + +} // end FairMQParser namespace +#endif /* FAIRMQPARSER_H */ + diff --git a/fairmq/options/ProgOptionTest/CMakeLists.txt b/fairmq/options/ProgOptionTest/CMakeLists.txt index f70f41fe..101bbf5e 100644 --- a/fairmq/options/ProgOptionTest/CMakeLists.txt +++ b/fairmq/options/ProgOptionTest/CMakeLists.txt @@ -52,7 +52,7 @@ ${Boost_LIBRARY_DIRS} link_directories(${LINK_DIRECTORIES}) Set(SRCS - lib/FairMQParser.cxx + lib/FairMQParserExample.cxx ) Set(LIBRARY_NAME ProgOptionTest) diff --git a/fairmq/options/ProgOptionTest/lib/FairMQParserExample.cxx b/fairmq/options/ProgOptionTest/lib/FairMQParserExample.cxx new file mode 100644 index 00000000..3fdca457 --- /dev/null +++ b/fairmq/options/ProgOptionTest/lib/FairMQParserExample.cxx @@ -0,0 +1,97 @@ +/* + * File: FairMQParserExample.cxx + * Author: winckler + * + * Created on May 14, 2015, 5:01 PM + */ + +#include "FairMQParserExample.h" +#include "FairMQLogger.h" +#include + + +namespace FairMQParser +{ + + + //////////////////////////////////////////////////////////////////////////// + //----------- filename version + FairMQMap XML::UserParser(const std::string& filename, const std::string& device_id, const std::string& root_node) + { + boost::property_tree::ptree pt; + boost::property_tree::read_xml(filename, pt); + return ptreeToMQMap(pt,device_id,root_node,"xml"); + } + + + + //----------- stringstream version + FairMQMap XML::UserParser(std::stringstream& input_ss, const std::string& device_id, const std::string& root_node) + { + boost::property_tree::ptree pt; + boost::property_tree::read_xml(input_ss, pt); + return ptreeToMQMap(pt,device_id,root_node,"xml"); + } + + + + + // other xml examples + //////////////////////////////////////////////////////////////////////////// + boost::property_tree::ptree MQXML2::UserParser(const std::string& filename) + { + boost::property_tree::ptree pt; + boost::property_tree::read_xml(filename, pt); + return pt; + } + + + + + // TODO : finish implementation + //////////////////////////////////////////////////////////////////////////// + boost::property_tree::ptree MQXML3::UserParser(const std::string& filename, const std::string& devicename) + { + // Create an empty property tree object + boost::property_tree::ptree pt; + boost::property_tree::read_xml(filename, pt); + + + // Create fair mq map + + auto xml = pt.get_child(""); + std::vector> match; + std::pair device_match; + + ProcessTree(xml.begin (), xml.end (), std::back_inserter(match), + [] (const std::string& key) { return key == "device"; }); + + + // for each device + for (const auto& pair: match) + { + if(pair.second.get(".name") == devicename) + { + device_match=pair; + + } + else + { + //match.erase(std::remove(match.begin(), match.end(), pair), match.end()); + continue; + } + + //std::cout << "pair.first " << pair.first << std::endl;//device + //std::cout << "\t node = " << pair.first + // << "\t name = " << pair.second.get(".name") + // << "\t id = " << pair.second.get(".id"); + //std::cout< @@ -23,9 +24,6 @@ namespace FairMQParser { - typedef std::map > FairMQMap; - FairMQMap boost_ptree_to_MQMap(const boost::property_tree::ptree& pt, const std::string& device_id, const std::string& root_node, const std::string& format_flag="json"); - //////////////////////////////////////////////////////////////////////////// /////////////////////////////////// XML //////////////////////////////////// //////////////////////////////////////////////////////////////////////////// @@ -52,14 +50,7 @@ namespace FairMQParser boost::property_tree::ptree UserParser(const std::string& filename, const std::string& root_node); }; - //////////////////////////////////////////////////////////////////////////// - /////////////////////////////////// JSON /////////////////////////////////// - //////////////////////////////////////////////////////////////////////////// - struct JSON - { - FairMQMap UserParser(const std::string& filename, const std::string& device_id, const std::string& root_node="fairMQOptions"); - FairMQMap UserParser(std::stringstream& input_ss, const std::string& device_id, const std::string& root_node="fairMQOptions"); - }; + //////////////////////////////////////////////////////////////////////////// @@ -94,10 +85,10 @@ namespace FairMQParser { virtual const char* what() const throw() { - return "Empty string for the device-id in FairMQParser::boost_ptree_to_MQMap(...) function"; + return "Empty string for the device-id in FairMQParser::ptreeToMQMap(...) function"; } }; } // end FairMQParser namespace -#endif /* FAIRMQPARSER_H */ +#endif /* FAIRMQPARSEREXAMPLE_H */