-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue #28
- Loading branch information
Valentin Noel
committed
Jul 19, 2013
1 parent
5be8cda
commit ccbc9c4
Showing
4 changed files
with
277 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#include "SpecNode.hpp" | ||
#include <boost/foreach.hpp> | ||
|
||
SpecNode::SpecNode() | ||
{ | ||
} | ||
|
||
SpecNode::SpecNode( const bpt::ptree::value_type& node ) | ||
: _node( node.second ) | ||
{ | ||
} | ||
|
||
SpecNode::~SpecNode() | ||
{ | ||
} | ||
|
||
void SpecNode::setNode( const bpt::ptree::value_type& node ) | ||
{ | ||
_node = node.second; | ||
} | ||
|
||
|
||
|
||
std::string SpecNode::getId() | ||
{ | ||
return _node.get< std::string >( kId ); | ||
} | ||
|
||
std::string SpecNode::getLabel() | ||
{ | ||
return _node.get< std::string >( kLabel ); | ||
} | ||
|
||
std::string SpecNode::getType() | ||
{ | ||
return _node.get< std::string >( kType ); | ||
} | ||
|
||
std::string SpecNode::getDisplayType() | ||
{ | ||
return _node.get< std::string >( kDisplayType, "" ); | ||
} | ||
|
||
|
||
std::string SpecNode::getCount() | ||
{ | ||
return _node.get< std::string >( kCount, "" ); | ||
} | ||
|
||
std::string SpecNode::getRequired() | ||
{ | ||
return _node.get< std::string >( kRequired, "" ); | ||
} | ||
|
||
|
||
std::vector< std::string > SpecNode::getValues() | ||
{ | ||
std::vector< std::string > values; | ||
if( boost::optional< bpt::ptree& > valuesNode = _node.get_child_optional( kValues ) ) | ||
{ | ||
std::string stringValue = _node.get< std::string >( kValues ); | ||
if( stringValue.empty() ) | ||
{ | ||
BOOST_FOREACH( bpt::ptree::value_type& value, valuesNode.get( ) ) | ||
values.push_back( value.second.get_value< std::string >() ); | ||
} | ||
else | ||
{ | ||
values.push_back( stringValue ); | ||
} | ||
} | ||
return values; | ||
} | ||
|
||
|
||
|
||
bool SpecNode::isBigEndian() | ||
{ | ||
return ( _node.get<std::string>( kEndian, kEndianBig ) == kEndianBig ); | ||
} | ||
|
||
bool SpecNode::isOptional() | ||
{ | ||
return ( _node.get<std::string>( kOptional, kOptionalFalse ) == kOptionalTrue ); | ||
} | ||
|
||
bool SpecNode::isOrdered() | ||
{ | ||
return ( _node.get<std::string>( kOrdered, kOrderedTrue ) == kOrderedTrue ); | ||
} | ||
|
||
bool SpecNode::hasGroup() | ||
{ | ||
return _node.get_child_optional( kGroup ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#ifndef _SPECREADER_SPECNODE_HPP_ | ||
#define _SPECREADER_SPECNODE_HPP_ | ||
|
||
#include <common/global.hpp> | ||
|
||
#include <boost/property_tree/ptree.hpp> | ||
|
||
namespace bpt = boost::property_tree; | ||
|
||
const std::string kId = "id"; | ||
const std::string kLabel = "label"; | ||
|
||
const std::string kAscii = "ascii"; | ||
const std::string kHexa = "hexa"; | ||
const std::string kType = "type"; | ||
const std::string kCount = "count"; | ||
const std::string kValues = "values"; | ||
const std::string kMap = "map"; | ||
const std::string kRange = "range"; | ||
const std::string kMin = "min"; | ||
const std::string kMax = "max"; | ||
|
||
const std::string kGroup = "group"; | ||
const std::string kGroupSize = "groupSize"; | ||
|
||
const std::string kRepetition = "repeated"; | ||
const std::string kRequired = "required"; | ||
|
||
const std::string kEndian = "endian"; | ||
const std::string kEndianBig = "big"; | ||
const std::string kEndianLittle = "little"; | ||
|
||
const std::string kOptional = "optional"; | ||
const std::string kOptionalTrue = "true"; | ||
const std::string kOptionalFalse = "false"; | ||
|
||
const std::string kOrdered = "ordered"; | ||
const std::string kOrderedTrue = "true"; | ||
const std::string kOrderedFalse = "false"; | ||
|
||
const std::string kData = "data"; | ||
const std::string kDisplayType = "displayType"; | ||
|
||
class SpecNode | ||
{ | ||
public: | ||
SpecNode(); | ||
SpecNode( const bpt::ptree::value_type& node ); | ||
~SpecNode(); | ||
|
||
void setNode( const bpt::ptree::value_type& node ); | ||
|
||
std::string getId(); | ||
std::string getLabel(); | ||
std::string getType(); | ||
std::string getDisplayType(); | ||
|
||
std::string getCount(); | ||
std::string getRequired(); | ||
|
||
std::vector< std::string > getValues(); | ||
|
||
// std::vector< std::pair< std::string > > getRange(); // @todo | ||
// std::vector< std::pair< std::string > > getRepetition(); // @todo | ||
|
||
// std::map< std::string, std::string > getMap(); // @todo | ||
|
||
bool isBigEndian(); | ||
bool isOptional(); | ||
bool isOrdered(); | ||
|
||
bool hasGroup(); | ||
// std::string getGroupSize(); // @todo | ||
|
||
private: | ||
bpt::ptree _node; | ||
}; | ||
|
||
|
||
|
||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#include <boost/foreach.hpp> | ||
|
||
BOOST_AUTO_TEST_SUITE( spec_reader_test_specNode ) | ||
|
||
BOOST_AUTO_TEST_CASE( spec_reader_specNode ) | ||
{ | ||
LOG_INFO( "\n>>> spec_reader_specNode <<<" ); | ||
{ | ||
std::string jsonString = " { \"header\": [ "; | ||
jsonString += " { \"id\": \"node\", \"label\": \"Node\", \"type\": \"type\" },"; | ||
jsonString += " { \"id\": \"node\", \"label\": \"Node\", \"type\": \"type\" }"; | ||
jsonString += " ] } "; | ||
|
||
std::istringstream isstream( jsonString ); | ||
bpt::ptree tree; | ||
|
||
bpt::read_json( isstream, tree ); | ||
size_t i = 0; | ||
BOOST_FOREACH( bpt::ptree::value_type n, tree.get_child( "header" ) ) | ||
{ | ||
SpecNode node; | ||
node.setNode( n ); | ||
BOOST_CHECK_EQUAL( node.getId(), "node" ); | ||
i++; | ||
} | ||
BOOST_CHECK_EQUAL( i, 2 ); | ||
|
||
i = 0; | ||
BOOST_FOREACH( bpt::ptree::value_type n, tree.get_child( "header" ) ) | ||
{ | ||
SpecNode node( n ); | ||
BOOST_CHECK_EQUAL( node.getLabel(), "Node" ); | ||
BOOST_CHECK_EQUAL( node.getType(), "type" ); | ||
i++; | ||
} | ||
BOOST_CHECK_EQUAL( i, 2 ); | ||
} | ||
{ | ||
std::string jsonString = " { \"header\": [ "; | ||
jsonString += " { \"displayType\": \"display\", \"count\": \"123\", \"required\": true }"; | ||
jsonString += " ] } "; | ||
|
||
std::istringstream isstream( jsonString ); | ||
bpt::ptree tree; | ||
|
||
bpt::read_json( isstream, tree ); | ||
size_t i = 0; | ||
BOOST_FOREACH( bpt::ptree::value_type n, tree.get_child( "header" ) ) | ||
{ | ||
SpecNode node( n ); | ||
BOOST_CHECK_EQUAL( node.getDisplayType(), "display" ); | ||
BOOST_CHECK_EQUAL( node.getCount(), "123" ); | ||
BOOST_CHECK_EQUAL( node.getRequired(), "true" ); | ||
i++; | ||
} | ||
BOOST_CHECK_EQUAL( i, 1 ); | ||
} | ||
{ | ||
std::string jsonString = " { \"header\": [ "; | ||
jsonString += " { \"values\": [ \"value1\", \"value2\", \"value3\" ] }"; | ||
jsonString += " ] } "; | ||
|
||
std::istringstream isstream( jsonString ); | ||
bpt::ptree tree; | ||
|
||
bpt::read_json( isstream, tree ); | ||
size_t i = 0; | ||
BOOST_FOREACH( bpt::ptree::value_type n, tree.get_child( "header" ) ) | ||
{ | ||
SpecNode node( n ); | ||
BOOST_CHECK_EQUAL( node.getValues().at(0), "value1" ); | ||
BOOST_CHECK_EQUAL( node.getValues().at(1), "value2" ); | ||
BOOST_CHECK_EQUAL( node.getValues().at(2), "value3" ); | ||
i++; | ||
} | ||
BOOST_CHECK_EQUAL( i, 1 ); | ||
} | ||
{ | ||
std::string jsonString = " { \"header\": [ "; | ||
jsonString += " { \"values\": \"value\" }"; | ||
jsonString += " ] } "; | ||
|
||
std::istringstream isstream( jsonString ); | ||
bpt::ptree tree; | ||
|
||
bpt::read_json( isstream, tree ); | ||
size_t i = 0; | ||
BOOST_FOREACH( bpt::ptree::value_type n, tree.get_child( "header" ) ) | ||
{ | ||
SpecNode node( n ); | ||
BOOST_CHECK_EQUAL( node.getValues().at(0), "value" ); | ||
i++; | ||
} | ||
BOOST_CHECK_EQUAL( i, 1 ); | ||
} | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters