Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XmlRpcValue::_doubleFormat should be used during write. #2003

Merged
merged 9 commits into from
Sep 23, 2020
47 changes: 42 additions & 5 deletions 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,16 +611,52 @@ 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;
int ret, required_size;
fujitatomoya marked this conversation as resolved.
Show resolved Hide resolved
char buf[128]; // Should be long enough
ret = std::snprintf(buf, sizeof(buf)-1,
getDoubleFormat().c_str(), _value.asDouble);
if (ret < 0) {
std::call_once(once,
[](){XmlRpcUtil::error("Failed to format with %s, using default %%.16g format",
getDoubleFormat().c_str());});
fujitatomoya marked this conversation as resolved.
Show resolved Hide resolved
required_size = std::snprintf(buf, sizeof(buf)-1, "%.16g", _value.asDouble);
fujitatomoya marked this conversation as resolved.
Show resolved Hide resolved
} else {
required_size = ret;
}
if (required_size >= 0 && required_size < (int) sizeof(buf)) {
fujitatomoya marked this conversation as resolved.
Show resolved Hide resolved
buf[sizeof(buf)-1] = 0;
os << buf;
} else if (required_size >= (int) sizeof(buf)) {
int ret;
fujitatomoya marked this conversation as resolved.
Show resolved Hide resolved
char required_buf[required_size+1];
ret = std::snprintf(required_buf, required_size,
getDoubleFormat().c_str(), _value.asDouble);
if (ret == required_size) {
required_buf[required_size] = 0;
os << required_buf;
}
} else {
XmlRpcUtil::error("Unexpected error to format %s", getDoubleFormat().c_str());
fujitatomoya marked this conversation as resolved.
Show resolved Hide resolved
}
break;
}
case TypeString: os << *_value.asString; break;
case TypeDateTime:
{
struct tm* t = _value.asTime;
int ret;
char buf[20];
std::snprintf(buf, sizeof(buf)-1, "%4d%02d%02dT%02d:%02d:%02d",
t->tm_year,t->tm_mon,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
buf[sizeof(buf)-1] = 0;
os << buf;
ret = std::snprintf(buf, sizeof(buf)-1, "%4d%02d%02dT%02d:%02d:%02d",
t->tm_year,t->tm_mon,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
if (ret > 0) {
buf[sizeof(buf)-1] = 0;
os << buf;
} else {
XmlRpcUtil::error("Unexpected error to format TypeDateTime");
}
fujitatomoya marked this conversation as resolved.
Show resolved Hide resolved
break;
}
case TypeBase64:
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