replacing atof with boost::lexical_casts #3391

This commit is contained in:
hsu 2010-02-26 19:09:02 +00:00
parent 9efcb7c56f
commit a57bd32cd2
4 changed files with 247 additions and 29 deletions

View File

@ -42,6 +42,7 @@
#include <math.h> #include <math.h>
#include <ros/ros.h> #include <ros/ros.h>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
namespace urdf namespace urdf
{ {
@ -70,8 +71,15 @@ public:
{ {
if (!pieces[i].empty()) if (!pieces[i].empty())
{ {
///@todo: do better atof check if string is a number try
rgba.push_back(atof(pieces[i].c_str())); {
rgba.push_back(boost::lexical_cast<double>(pieces[i].c_str()));
}
catch (boost::bad_lexical_cast &e)
{
ROS_ERROR("color rgba element (%s) is not a valid float",pieces[i].c_str());
return false;
}
} }
} }

View File

@ -42,6 +42,7 @@
#include <math.h> #include <math.h>
#include <ros/ros.h> #include <ros/ros.h>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
namespace urdf{ namespace urdf{
@ -63,8 +64,15 @@ public:
boost::split( pieces, vector_str, boost::is_any_of(" ")); boost::split( pieces, vector_str, boost::is_any_of(" "));
for (unsigned int i = 0; i < pieces.size(); ++i){ for (unsigned int i = 0; i < pieces.size(); ++i){
if (pieces[i] != ""){ if (pieces[i] != ""){
///@todo: do better atof check if string is a number try
xyz.push_back(atof(pieces[i].c_str())); {
xyz.push_back(boost::lexical_cast<double>(pieces[i].c_str()));
}
catch (boost::bad_lexical_cast &e)
{
ROS_ERROR("Vector3 xyz element (%s) is not a valid float",pieces[i].c_str());
return false;
}
} }
} }

View File

@ -36,6 +36,7 @@
#include <urdf/joint.h> #include <urdf/joint.h>
#include <ros/ros.h> #include <ros/ros.h>
#include <boost/lexical_cast.hpp>
namespace urdf{ namespace urdf{
@ -50,7 +51,17 @@ bool JointDynamics::initXml(TiXmlElement* config)
this->damping = 0; this->damping = 0;
} }
else else
this->damping = atof(damping_str); {
try
{
this->damping = boost::lexical_cast<double>(damping_str);
}
catch (boost::bad_lexical_cast &e)
{
ROS_ERROR("damping value (%s) is not a float",damping_str);
return false;
}
}
// Get joint friction // Get joint friction
const char* friction_str = config->Attribute("friction"); const char* friction_str = config->Attribute("friction");
@ -59,7 +70,17 @@ bool JointDynamics::initXml(TiXmlElement* config)
this->friction = 0; this->friction = 0;
} }
else else
this->friction = atof(friction_str); {
try
{
this->friction = boost::lexical_cast<double>(friction_str);
}
catch (boost::bad_lexical_cast &e)
{
ROS_ERROR("friction value (%s) is not a float",friction_str);
return false;
}
}
if (damping_str == NULL && friction_str == NULL) if (damping_str == NULL && friction_str == NULL)
{ {
@ -83,7 +104,17 @@ bool JointLimits::initXml(TiXmlElement* config)
this->lower = 0; this->lower = 0;
} }
else else
this->lower = atof(lower_str); {
try
{
this->lower = boost::lexical_cast<double>(lower_str);
}
catch (boost::bad_lexical_cast &e)
{
ROS_ERROR("lower value (%s) is not a float",lower_str);
return false;
}
}
// Get upper joint limit // Get upper joint limit
const char* upper_str = config->Attribute("upper"); const char* upper_str = config->Attribute("upper");
@ -92,7 +123,17 @@ bool JointLimits::initXml(TiXmlElement* config)
this->upper = 0; this->upper = 0;
} }
else else
this->upper = atof(upper_str); {
try
{
this->upper = boost::lexical_cast<double>(upper_str);
}
catch (boost::bad_lexical_cast &e)
{
ROS_ERROR("upper value (%s) is not a float",upper_str);
return false;
}
}
// Get joint effort limit // Get joint effort limit
const char* effort_str = config->Attribute("effort"); const char* effort_str = config->Attribute("effort");
@ -101,7 +142,17 @@ bool JointLimits::initXml(TiXmlElement* config)
return false; return false;
} }
else else
this->effort = atof(effort_str); {
try
{
this->effort = boost::lexical_cast<double>(effort_str);
}
catch (boost::bad_lexical_cast &e)
{
ROS_ERROR("effort value (%s) is not a float",effort_str);
return false;
}
}
// Get joint velocity limit // Get joint velocity limit
const char* velocity_str = config->Attribute("velocity"); const char* velocity_str = config->Attribute("velocity");
@ -110,7 +161,17 @@ bool JointLimits::initXml(TiXmlElement* config)
return false; return false;
} }
else else
this->velocity = atof(velocity_str); {
try
{
this->velocity = boost::lexical_cast<double>(velocity_str);
}
catch (boost::bad_lexical_cast &e)
{
ROS_ERROR("velocity value (%s) is not a float",velocity_str);
return false;
}
}
return true; return true;
} }
@ -127,7 +188,17 @@ bool JointSafety::initXml(TiXmlElement* config)
this->soft_lower_limit = 0; this->soft_lower_limit = 0;
} }
else else
this->soft_lower_limit = atof(soft_lower_limit_str); {
try
{
this->soft_lower_limit = boost::lexical_cast<double>(soft_lower_limit_str);
}
catch (boost::bad_lexical_cast &e)
{
ROS_ERROR("soft_lower_limit value (%s) is not a float",soft_lower_limit_str);
return false;
}
}
// Get soft_upper_limit joint limit // Get soft_upper_limit joint limit
const char* soft_upper_limit_str = config->Attribute("soft_upper_limit"); const char* soft_upper_limit_str = config->Attribute("soft_upper_limit");
@ -137,7 +208,17 @@ bool JointSafety::initXml(TiXmlElement* config)
this->soft_upper_limit = 0; this->soft_upper_limit = 0;
} }
else else
this->soft_upper_limit = atof(soft_upper_limit_str); {
try
{
this->soft_upper_limit = boost::lexical_cast<double>(soft_upper_limit_str);
}
catch (boost::bad_lexical_cast &e)
{
ROS_ERROR("soft_upper_limit value (%s) is not a float",soft_upper_limit_str);
return false;
}
}
// Get k_position_ safety "position" gain - not exactly position gain // Get k_position_ safety "position" gain - not exactly position gain
const char* k_position_str = config->Attribute("k_position"); const char* k_position_str = config->Attribute("k_position");
@ -147,7 +228,17 @@ bool JointSafety::initXml(TiXmlElement* config)
this->k_position = 0; this->k_position = 0;
} }
else else
this->k_position = atof(k_position_str); {
try
{
this->k_position = boost::lexical_cast<double>(k_position_str);
}
catch (boost::bad_lexical_cast &e)
{
ROS_ERROR("k_position value (%s) is not a float",k_position_str);
return false;
}
}
// Get k_velocity_ safety velocity gain // Get k_velocity_ safety velocity gain
const char* k_velocity_str = config->Attribute("k_velocity"); const char* k_velocity_str = config->Attribute("k_velocity");
if (k_velocity_str == NULL) if (k_velocity_str == NULL)
@ -156,7 +247,17 @@ bool JointSafety::initXml(TiXmlElement* config)
return false; return false;
} }
else else
this->k_velocity = atof(k_velocity_str); {
try
{
this->k_velocity = boost::lexical_cast<double>(k_velocity_str);
}
catch (boost::bad_lexical_cast &e)
{
ROS_ERROR("k_velocity value (%s) is not a float",k_velocity_str);
return false;
}
}
return true; return true;
} }
@ -173,7 +274,17 @@ bool JointCalibration::initXml(TiXmlElement* config)
this->reference_position = 0; this->reference_position = 0;
} }
else else
this->reference_position = atof(reference_position_str); {
try
{
this->reference_position = boost::lexical_cast<double>(reference_position_str);
}
catch (boost::bad_lexical_cast &e)
{
ROS_ERROR("reference_position value (%s) is not a float",reference_position_str);
return false;
}
}
// Get rising edge position // Get rising edge position
const char* rising_position_str = config->Attribute("rising"); const char* rising_position_str = config->Attribute("rising");
@ -183,7 +294,17 @@ bool JointCalibration::initXml(TiXmlElement* config)
this->rising.reset(); this->rising.reset();
} }
else else
this->rising.reset(new double(atof(rising_position_str))); {
try
{
this->rising.reset(new double(boost::lexical_cast<double>(rising_position_str)));
}
catch (boost::bad_lexical_cast &e)
{
ROS_ERROR("risingvalue (%s) is not a float",rising_position_str);
return false;
}
}
// Get falling edge position // Get falling edge position
const char* falling_position_str = config->Attribute("falling"); const char* falling_position_str = config->Attribute("falling");
@ -193,7 +314,17 @@ bool JointCalibration::initXml(TiXmlElement* config)
this->falling.reset(); this->falling.reset();
} }
else else
this->falling.reset(new double(atof(falling_position_str))); {
try
{
this->falling.reset(new double(boost::lexical_cast<double>(falling_position_str)));
}
catch (boost::bad_lexical_cast &e)
{
ROS_ERROR("fallingvalue (%s) is not a float",falling_position_str);
return false;
}
}
return true; return true;
} }
@ -220,7 +351,17 @@ bool JointMimic::initXml(TiXmlElement* config)
this->multiplier = 1; this->multiplier = 1;
} }
else else
this->multiplier = atof(multiplier_str); {
try
{
this->multiplier = boost::lexical_cast<double>(multiplier_str);
}
catch (boost::bad_lexical_cast &e)
{
ROS_ERROR("multiplier value (%s) is not a float",multiplier_str);
return false;
}
}
// Get mimic offset // Get mimic offset
const char* offset_str = config->Attribute("offset"); const char* offset_str = config->Attribute("offset");
@ -230,7 +371,17 @@ bool JointMimic::initXml(TiXmlElement* config)
this->offset = 0; this->offset = 0;
} }
else else
this->offset = atof(offset_str); {
try
{
this->offset = boost::lexical_cast<double>(offset_str);
}
catch (boost::bad_lexical_cast &e)
{
ROS_ERROR("offset value (%s) is not a float",offset_str);
return false;
}
}
return true; return true;
} }

View File

@ -37,6 +37,7 @@
#include "urdf/link.h" #include "urdf/link.h"
#include <ros/ros.h> #include <ros/ros.h>
#include <boost/lexical_cast.hpp>
namespace urdf{ namespace urdf{
@ -162,7 +163,16 @@ bool Inertial::initXml(TiXmlElement *config)
ROS_ERROR("Inertial: mass element must have value attributes"); ROS_ERROR("Inertial: mass element must have value attributes");
return false; return false;
} }
mass = atof(mass_xml->Attribute("value"));
try
{
mass = boost::lexical_cast<double>(mass_xml->Attribute("value"));
}
catch (boost::bad_lexical_cast &e)
{
ROS_ERROR("mass (%s) is not a float",mass_xml->Attribute("value"));
return false;
}
TiXmlElement *inertia_xml = config->FirstChildElement("inertia"); TiXmlElement *inertia_xml = config->FirstChildElement("inertia");
if (!inertia_xml) if (!inertia_xml)
@ -177,12 +187,26 @@ bool Inertial::initXml(TiXmlElement *config)
ROS_ERROR("Inertial: inertia element must have ixx,ixy,ixz,iyy,iyz,izz attributes"); ROS_ERROR("Inertial: inertia element must have ixx,ixy,ixz,iyy,iyz,izz attributes");
return false; return false;
} }
ixx = atof(inertia_xml->Attribute("ixx")); try
ixy = atof(inertia_xml->Attribute("ixy")); {
ixz = atof(inertia_xml->Attribute("ixz")); ixx = boost::lexical_cast<double>(inertia_xml->Attribute("ixx"));
iyy = atof(inertia_xml->Attribute("iyy")); ixy = boost::lexical_cast<double>(inertia_xml->Attribute("ixy"));
iyz = atof(inertia_xml->Attribute("iyz")); ixz = boost::lexical_cast<double>(inertia_xml->Attribute("ixz"));
izz = atof(inertia_xml->Attribute("izz")); iyy = boost::lexical_cast<double>(inertia_xml->Attribute("iyy"));
iyz = boost::lexical_cast<double>(inertia_xml->Attribute("iyz"));
izz = boost::lexical_cast<double>(inertia_xml->Attribute("izz"));
}
catch (boost::bad_lexical_cast &e)
{
ROS_ERROR("one of the inertia elements: ixx (%s) ixy (%s) ixz (%s) iyy (%s) iyz (%s) izz (%s) is not a valid double",
inertia_xml->Attribute("ixx"),
inertia_xml->Attribute("ixy"),
inertia_xml->Attribute("ixz"),
inertia_xml->Attribute("iyy"),
inertia_xml->Attribute("iyz"),
inertia_xml->Attribute("izz"));
return false;
}
return true; return true;
} }
@ -287,7 +311,16 @@ bool Sphere::initXml(TiXmlElement *c)
return false; return false;
} }
radius = atof(c->Attribute("radius")); try
{
radius = boost::lexical_cast<double>(c->Attribute("radius"));
}
catch (boost::bad_lexical_cast &e)
{
ROS_ERROR("radius (%s) is not a valid float",c->Attribute("radius"));
return false;
}
return true; return true;
} }
@ -322,8 +355,26 @@ bool Cylinder::initXml(TiXmlElement *c)
return false; return false;
} }
length = atof(c->Attribute("length")); try
radius = atof(c->Attribute("radius")); {
length = boost::lexical_cast<double>(c->Attribute("length"));
}
catch (boost::bad_lexical_cast &e)
{
ROS_ERROR("length (%s) is not a valid float",c->Attribute("length"));
return false;
}
try
{
radius = boost::lexical_cast<double>(c->Attribute("radius"));
}
catch (boost::bad_lexical_cast &e)
{
ROS_ERROR("radius (%s) is not a valid float",c->Attribute("radius"));
return false;
}
return true; return true;
} }