Skip to content

Commit 7451032

Browse files
committed
feat: Enhance string splitting and error handling in Dynamixel model file processing
- Updated the split_string function to include an optional trim parameter for whitespace removal. - Improved error handling in ReadDxlModelFile by adding try-catch blocks for invalid arguments and out-of-range errors during data parsing. - Ensured that empty tokens are skipped after trimming, enhancing robustness in data processing.
1 parent 19d95c8 commit 7451032

File tree

1 file changed

+46
-20
lines changed

1 file changed

+46
-20
lines changed

src/dynamixel/dynamixel_info.cpp

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,22 @@ namespace dynamixel_hardware_interface
2424
{
2525

2626
// Helper function to split string by delimiter
27-
std::vector<std::string> split_string(const std::string & str, char delimiter)
27+
std::vector<std::string> split_string(const std::string & str, char delimiter, bool trim = false)
2828
{
2929
std::vector<std::string> tokens;
3030
std::string token;
3131
std::istringstream token_stream(str);
3232
while (std::getline(token_stream, token, delimiter)) {
33+
if (trim) {
34+
// Trim leading and trailing whitespace
35+
size_t first = token.find_first_not_of(" \t\r\n");
36+
size_t last = token.find_last_not_of(" \t\r\n");
37+
if (first != std::string::npos && last != std::string::npos) {
38+
token = token.substr(first, (last - first + 1));
39+
} else {
40+
token.clear();
41+
}
42+
}
3343
if (!token.empty()) { // Skip empty tokens
3444
tokens.push_back(token);
3545
}
@@ -95,21 +105,29 @@ void DynamixelInfo::ReadDxlModelFile(uint8_t id, uint16_t model_num)
95105
break;
96106
}
97107

98-
std::vector<std::string> strs = split_string(line, '\t');
108+
std::vector<std::string> strs = split_string(line, '\t', true);
99109

100110
if (!strs.empty()) {
101-
if (strs.at(0) == "value_of_zero_radian_position") {
102-
temp_dxl_info.value_of_zero_radian_position = static_cast<int32_t>(stoi(strs.at(1)));
103-
} else if (strs.at(0) == "value_of_max_radian_position") {
104-
temp_dxl_info.value_of_max_radian_position = static_cast<int32_t>(stoi(strs.at(1)));
105-
} else if (strs.at(0) == "value_of_min_radian_position") {
106-
temp_dxl_info.value_of_min_radian_position = static_cast<int32_t>(stoi(strs.at(1)));
107-
} else if (strs.at(0) == "min_radian") {
108-
temp_dxl_info.min_radian = static_cast<double>(stod(strs.at(1)));
109-
} else if (strs.at(0) == "max_radian") {
110-
temp_dxl_info.max_radian = static_cast<double>(stod(strs.at(1)));
111-
} else if (strs.at(0) == "torque_constant") {
112-
temp_dxl_info.torque_constant = static_cast<double>(stod(strs.at(1)));
111+
try {
112+
if (strs.at(0) == "value_of_zero_radian_position") {
113+
temp_dxl_info.value_of_zero_radian_position = static_cast<int32_t>(std::stoi(strs.at(1)));
114+
} else if (strs.at(0) == "value_of_max_radian_position") {
115+
temp_dxl_info.value_of_max_radian_position = static_cast<int32_t>(std::stoi(strs.at(1)));
116+
} else if (strs.at(0) == "value_of_min_radian_position") {
117+
temp_dxl_info.value_of_min_radian_position = static_cast<int32_t>(std::stoi(strs.at(1)));
118+
} else if (strs.at(0) == "min_radian") {
119+
temp_dxl_info.min_radian = static_cast<double>(std::stod(strs.at(1)));
120+
} else if (strs.at(0) == "max_radian") {
121+
temp_dxl_info.max_radian = static_cast<double>(std::stod(strs.at(1)));
122+
} else if (strs.at(0) == "torque_constant") {
123+
temp_dxl_info.torque_constant = static_cast<double>(std::stod(strs.at(1)));
124+
}
125+
} catch (const std::invalid_argument & e) {
126+
fprintf(stderr, "[ERROR] Invalid argument in model file: %s\n", e.what());
127+
continue;
128+
} catch (const std::out_of_range & e) {
129+
fprintf(stderr, "[ERROR] Out of range in model file: %s\n", e.what());
130+
continue;
113131
}
114132
}
115133
}
@@ -121,14 +139,22 @@ void DynamixelInfo::ReadDxlModelFile(uint8_t id, uint16_t model_num)
121139
break;
122140
}
123141

124-
std::vector<std::string> strs = split_string(line, '\t');
142+
std::vector<std::string> strs = split_string(line, '\t', true);
125143

126144
if (!strs.empty()) {
127-
ControlItem temp;
128-
temp.address = static_cast<uint16_t>(stoi(strs.at(0)));
129-
temp.size = static_cast<uint8_t>(stoi(strs.at(1)));
130-
temp.item_name = strs.at(2);
131-
temp_dxl_info.item.push_back(temp);
145+
try {
146+
ControlItem temp;
147+
temp.address = static_cast<uint16_t>(std::stoi(strs.at(0)));
148+
temp.size = static_cast<uint8_t>(std::stoi(strs.at(1)));
149+
temp.item_name = strs.at(2);
150+
temp_dxl_info.item.push_back(temp);
151+
} catch (const std::invalid_argument & e) {
152+
fprintf(stderr, "[ERROR] Invalid argument in control table: %s\n", e.what());
153+
continue;
154+
} catch (const std::out_of_range & e) {
155+
fprintf(stderr, "[ERROR] Out of range in control table: %s\n", e.what());
156+
continue;
157+
}
132158
}
133159
}
134160

0 commit comments

Comments
 (0)