Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "ros_type_introspection/ros_field.hpp"
00036 #include <boost/regex.hpp>
00037 #include <boost/algorithm/string/regex.hpp>
00038 #include <boost/algorithm/string/trim.hpp>
00039
00040 namespace RosIntrospection{
00041
00042 ROSField::ROSField(const std::string &definition):
00043 _array_size(1)
00044 {
00045 static const boost::regex type_regex("[a-zA-Z][a-zA-Z0-9_]*"
00046 "(/[a-zA-Z][a-zA-Z0-9_]*){0,1}"
00047 "(\\[[0-9]*\\]){0,1}");
00048
00049 static const boost::regex field_regex("[a-zA-Z][a-zA-Z0-9_]*");
00050
00051 static const boost::regex array_regex("(.+)(\\[([0-9]*)\\])");
00052
00053 using boost::regex;
00054 std::string::const_iterator begin = definition.begin();
00055 std::string::const_iterator end = definition.end();
00056 boost::match_results<std::string::const_iterator> what;
00057
00058
00059 std::string type, value;
00060
00061
00062
00063 if( regex_search(begin, end, what, type_regex)) {
00064 type = what[0];
00065 begin = what[0].second;
00066 }
00067 else {
00068 throw std::runtime_error("Bad type when parsing field: " + definition);
00069 }
00070
00071 if (regex_search(begin, end, what, field_regex))
00072 {
00073 _fieldname = what[0];
00074 begin = what[0].second;
00075 }
00076 else {
00077 throw std::runtime_error("Bad field when parsing field: " + definition);
00078 }
00079
00080 std::string temp_type = type;
00081 if (regex_search(temp_type, what, array_regex))
00082 {
00083 type = what[1];
00084
00085 if (what.size() == 3) {
00086 _array_size = -1;
00087 }
00088 else if (what.size() == 4) {
00089 std::string size(what[3].first, what[3].second);
00090 _array_size = size.empty() ? -1 : atoi(size.c_str());
00091 }
00092 else {
00093 throw std::runtime_error("Bad array size when parsing field: " + definition);
00094 }
00095 }
00096
00097
00098
00099
00100
00101
00102 if (regex_search(begin, end, what, boost::regex("\\S")))
00103 {
00104 if (what[0] == "=")
00105 {
00106 begin = what[0].second;
00107
00108 if (type == "string") {
00109 value.assign(begin, end);
00110 }
00111 else {
00112 if (regex_search(begin, end, what, boost::regex("\\s*#")))
00113 {
00114 value.assign(begin, what[0].first);
00115 }
00116 else {
00117 value.assign(begin, end);
00118 }
00119
00120 }
00121
00122 boost::algorithm::trim(value);
00123 } else if (what[0] == "#") {
00124
00125 } else {
00126
00127 throw std::runtime_error("Unexpected character after type and field: " +
00128 definition);
00129 }
00130 }
00131 _type = ROSType( type );
00132 _value = value;
00133 }
00134
00135 }