From a57bd32cd2ceb6d25c83b6a7228b44fb6600ab03 Mon Sep 17 00:00:00 2001 From: hsu Date: Fri, 26 Feb 2010 19:09:02 +0000 Subject: [PATCH] replacing atof with boost::lexical_casts #3391 --- urdf/include/urdf/color.h | 12 ++- urdf/include/urdf/pose.h | 12 ++- urdf/src/joint.cpp | 181 ++++++++++++++++++++++++++++++++++---- urdf/src/link.cpp | 71 ++++++++++++--- 4 files changed, 247 insertions(+), 29 deletions(-) diff --git a/urdf/include/urdf/color.h b/urdf/include/urdf/color.h index e3a7b3e..c132da4 100644 --- a/urdf/include/urdf/color.h +++ b/urdf/include/urdf/color.h @@ -42,6 +42,7 @@ #include #include #include +#include namespace urdf { @@ -70,8 +71,15 @@ public: { if (!pieces[i].empty()) { - ///@todo: do better atof check if string is a number - rgba.push_back(atof(pieces[i].c_str())); + try + { + rgba.push_back(boost::lexical_cast(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; + } } } diff --git a/urdf/include/urdf/pose.h b/urdf/include/urdf/pose.h index 3a46a9d..48e57ab 100644 --- a/urdf/include/urdf/pose.h +++ b/urdf/include/urdf/pose.h @@ -42,6 +42,7 @@ #include #include #include +#include namespace urdf{ @@ -63,8 +64,15 @@ public: boost::split( pieces, vector_str, boost::is_any_of(" ")); for (unsigned int i = 0; i < pieces.size(); ++i){ if (pieces[i] != ""){ - ///@todo: do better atof check if string is a number - xyz.push_back(atof(pieces[i].c_str())); + try + { + xyz.push_back(boost::lexical_cast(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; + } } } diff --git a/urdf/src/joint.cpp b/urdf/src/joint.cpp index 5a1adab..09bc94e 100644 --- a/urdf/src/joint.cpp +++ b/urdf/src/joint.cpp @@ -36,6 +36,7 @@ #include #include +#include namespace urdf{ @@ -50,7 +51,17 @@ bool JointDynamics::initXml(TiXmlElement* config) this->damping = 0; } else - this->damping = atof(damping_str); + { + try + { + this->damping = boost::lexical_cast(damping_str); + } + catch (boost::bad_lexical_cast &e) + { + ROS_ERROR("damping value (%s) is not a float",damping_str); + return false; + } + } // Get joint friction const char* friction_str = config->Attribute("friction"); @@ -59,7 +70,17 @@ bool JointDynamics::initXml(TiXmlElement* config) this->friction = 0; } else - this->friction = atof(friction_str); + { + try + { + this->friction = boost::lexical_cast(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) { @@ -83,7 +104,17 @@ bool JointLimits::initXml(TiXmlElement* config) this->lower = 0; } else - this->lower = atof(lower_str); + { + try + { + this->lower = boost::lexical_cast(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 const char* upper_str = config->Attribute("upper"); @@ -92,7 +123,17 @@ bool JointLimits::initXml(TiXmlElement* config) this->upper = 0; } else - this->upper = atof(upper_str); + { + try + { + this->upper = boost::lexical_cast(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 const char* effort_str = config->Attribute("effort"); @@ -101,7 +142,17 @@ bool JointLimits::initXml(TiXmlElement* config) return false; } else - this->effort = atof(effort_str); + { + try + { + this->effort = boost::lexical_cast(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 const char* velocity_str = config->Attribute("velocity"); @@ -110,7 +161,17 @@ bool JointLimits::initXml(TiXmlElement* config) return false; } else - this->velocity = atof(velocity_str); + { + try + { + this->velocity = boost::lexical_cast(velocity_str); + } + catch (boost::bad_lexical_cast &e) + { + ROS_ERROR("velocity value (%s) is not a float",velocity_str); + return false; + } + } return true; } @@ -127,7 +188,17 @@ bool JointSafety::initXml(TiXmlElement* config) this->soft_lower_limit = 0; } else - this->soft_lower_limit = atof(soft_lower_limit_str); + { + try + { + this->soft_lower_limit = boost::lexical_cast(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 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; } else - this->soft_upper_limit = atof(soft_upper_limit_str); + { + try + { + this->soft_upper_limit = boost::lexical_cast(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 const char* k_position_str = config->Attribute("k_position"); @@ -147,7 +228,17 @@ bool JointSafety::initXml(TiXmlElement* config) this->k_position = 0; } else - this->k_position = atof(k_position_str); + { + try + { + this->k_position = boost::lexical_cast(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 const char* k_velocity_str = config->Attribute("k_velocity"); if (k_velocity_str == NULL) @@ -156,7 +247,17 @@ bool JointSafety::initXml(TiXmlElement* config) return false; } else - this->k_velocity = atof(k_velocity_str); + { + try + { + this->k_velocity = boost::lexical_cast(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; } @@ -173,7 +274,17 @@ bool JointCalibration::initXml(TiXmlElement* config) this->reference_position = 0; } else - this->reference_position = atof(reference_position_str); + { + try + { + this->reference_position = boost::lexical_cast(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 const char* rising_position_str = config->Attribute("rising"); @@ -183,7 +294,17 @@ bool JointCalibration::initXml(TiXmlElement* config) this->rising.reset(); } else - this->rising.reset(new double(atof(rising_position_str))); + { + try + { + this->rising.reset(new double(boost::lexical_cast(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 const char* falling_position_str = config->Attribute("falling"); @@ -193,7 +314,17 @@ bool JointCalibration::initXml(TiXmlElement* config) this->falling.reset(); } else - this->falling.reset(new double(atof(falling_position_str))); + { + try + { + this->falling.reset(new double(boost::lexical_cast(falling_position_str))); + } + catch (boost::bad_lexical_cast &e) + { + ROS_ERROR("fallingvalue (%s) is not a float",falling_position_str); + return false; + } + } return true; } @@ -220,7 +351,17 @@ bool JointMimic::initXml(TiXmlElement* config) this->multiplier = 1; } else - this->multiplier = atof(multiplier_str); + { + try + { + this->multiplier = boost::lexical_cast(multiplier_str); + } + catch (boost::bad_lexical_cast &e) + { + ROS_ERROR("multiplier value (%s) is not a float",multiplier_str); + return false; + } + } // Get mimic offset const char* offset_str = config->Attribute("offset"); @@ -230,7 +371,17 @@ bool JointMimic::initXml(TiXmlElement* config) this->offset = 0; } else - this->offset = atof(offset_str); + { + try + { + this->offset = boost::lexical_cast(offset_str); + } + catch (boost::bad_lexical_cast &e) + { + ROS_ERROR("offset value (%s) is not a float",offset_str); + return false; + } + } return true; } diff --git a/urdf/src/link.cpp b/urdf/src/link.cpp index 74ab9c9..ef057cc 100644 --- a/urdf/src/link.cpp +++ b/urdf/src/link.cpp @@ -37,6 +37,7 @@ #include "urdf/link.h" #include +#include namespace urdf{ @@ -162,7 +163,16 @@ bool Inertial::initXml(TiXmlElement *config) ROS_ERROR("Inertial: mass element must have value attributes"); return false; } - mass = atof(mass_xml->Attribute("value")); + + try + { + mass = boost::lexical_cast(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"); 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"); return false; } - ixx = atof(inertia_xml->Attribute("ixx")); - ixy = atof(inertia_xml->Attribute("ixy")); - ixz = atof(inertia_xml->Attribute("ixz")); - iyy = atof(inertia_xml->Attribute("iyy")); - iyz = atof(inertia_xml->Attribute("iyz")); - izz = atof(inertia_xml->Attribute("izz")); + try + { + ixx = boost::lexical_cast(inertia_xml->Attribute("ixx")); + ixy = boost::lexical_cast(inertia_xml->Attribute("ixy")); + ixz = boost::lexical_cast(inertia_xml->Attribute("ixz")); + iyy = boost::lexical_cast(inertia_xml->Attribute("iyy")); + iyz = boost::lexical_cast(inertia_xml->Attribute("iyz")); + izz = boost::lexical_cast(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; } @@ -287,7 +311,16 @@ bool Sphere::initXml(TiXmlElement *c) return false; } - radius = atof(c->Attribute("radius")); + try + { + radius = boost::lexical_cast(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; } @@ -322,8 +355,26 @@ bool Cylinder::initXml(TiXmlElement *c) return false; } - length = atof(c->Attribute("length")); - radius = atof(c->Attribute("radius")); + try + { + length = boost::lexical_cast(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(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; }