Skip to content

Commit

Permalink
XmlRpcValue::_doubleFormat should be used during write. (#2003)
Browse files Browse the repository at this point in the history
* XmlRpcValue::_doubleFormat should be used during write.

Signed-off-by: Tomoya.Fujita <Tomoya.Fujita@sony.com>

* allocate buffer dynamically for XmlRpcValue::_doubleFormat if necessary.

Signed-off-by: Tomoya.Fujita <Tomoya.Fujita@sony.com>

* add test for XmlRpcValue::_doubleFormat.

Signed-off-by: Tomoya.Fujita <Tomoya.Fujita@sony.com>

* check return code from std::snprintf, save/restore DoubleFormat for test.

Signed-off-by: Tomoya.Fujita <Tomoya.Fujita@sony.com>

* add one time warning message for DoubleFormat.

Signed-off-by: Tomoya.Fujita <Tomoya.Fujita@sony.com>

* use XmlRpcUtil::error instead of ROS_ERROR.

Signed-off-by: Tomoya.Fujita <Tomoya.Fujita@sony.com>

* use static_cast and minor fixes.

Signed-off-by: Tomoya.Fujita <Tomoya.Fujita@sony.com>

* delete unrelated change and fix invalid format case.

Signed-off-by: Tomoya.Fujita <Tomoya.Fujita@sony.com>

* get rid of redundant condition from if statement.

Signed-off-by: Tomoya.Fujita <Tomoya.Fujita@sony.com>
  • Loading branch information
fujitatomoya authored Sep 23, 2020
1 parent 84d413f commit 44fa5cf
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
24 changes: 23 additions & 1 deletion utilities/xmlrpcpp/src/XmlRpcValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#endif

#include <sstream>
#include <mutex>

namespace XmlRpc {

Expand Down Expand Up @@ -610,7 +611,28 @@ namespace XmlRpc {
default: break;
case TypeBoolean: os << _value.asBool; break;
case TypeInt: os << _value.asInt; break;
case TypeDouble: os << _value.asDouble; break;
case TypeDouble:
{
static std::once_flag once;
char buf[128]; // Should be long enough
int required_size = std::snprintf(buf, sizeof(buf)-1,
getDoubleFormat().c_str(), _value.asDouble);
if (required_size < 0) {
std::call_once(once,
[](){XmlRpcUtil::error("Failed to format with %s", getDoubleFormat().c_str());});
os << _value.asDouble;
} else if (required_size < static_cast<int>(sizeof(buf))) {
buf[sizeof(buf)-1] = 0;
os << buf;
} else { // required_size >= static_cast<int>(sizeof(buf)
char required_buf[required_size+1];
std::snprintf(required_buf, required_size,
getDoubleFormat().c_str(), _value.asDouble);
required_buf[required_size] = 0;
os << required_buf;
}
break;
}
case TypeString: os << *_value.asString; break;
case TypeDateTime:
{
Expand Down
34 changes: 34 additions & 0 deletions utilities/xmlrpcpp/test/TestValues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,40 @@ TEST(XmlRpc, testDouble) {
std::stringstream ss;
ss << d;
EXPECT_EQ("43.7", ss.str());
ss.str("");

// Test format
const XmlRpc::XmlRpcValue a(2.0);
ASSERT_EQ(XmlRpcValue::TypeDouble, d.getType());
const std::string save_format = XmlRpc::XmlRpcValue::getDoubleFormat();

XmlRpc::XmlRpcValue::setDoubleFormat("%32.10f");
ss << a;
EXPECT_EQ(" 2.0000000000", ss.str());
ss.str("");

XmlRpc::XmlRpcValue::setDoubleFormat("%10.32f");
ss << a;
EXPECT_EQ("2.00000000000000000000000000000000", ss.str());
ss.str("");

XmlRpc::XmlRpcValue::setDoubleFormat("%128.10f");
ss << a;
EXPECT_EQ(" "
" "
" "
" 2.000000000", ss.str());
ss.str("");

XmlRpc::XmlRpcValue::setDoubleFormat("%10.128f");
ss << a;
EXPECT_EQ("2.000000000000000000000000000000"
"00000000000000000000000000000000"
"00000000000000000000000000000000"
"000000000000000000000000000000000", ss.str());
ss.str("");

XmlRpc::XmlRpcValue::setDoubleFormat(save_format.c_str());
}

TEST(XmlRpc, testString) {
Expand Down

0 comments on commit 44fa5cf

Please sign in to comment.