@@ -193,10 +193,11 @@ class InventoryItem
193
193
ledState (LedState::UNKNOWN)
194
194
{
195
195
// Set inventory item name to last node of object path
196
- auto pos = objectPath.rfind (' /' );
197
- if ((pos != std::string::npos) && ((pos + 1 ) < objectPath.size ()))
196
+ sdbusplus::message::object_path path (objectPath);
197
+ name = path.filename ();
198
+ if (name.empty ())
198
199
{
199
- name = objectPath. substr (pos + 1 ) ;
200
+ BMCWEB_LOG_ERROR << " Failed to find '/' in " << objectPath ;
200
201
}
201
202
}
202
203
@@ -388,13 +389,13 @@ void getValidChassisPath(const std::shared_ptr<SensorsAsyncResp>& asyncResp,
388
389
std::string chassisName;
389
390
for (const std::string& chassis : chassisPaths)
390
391
{
391
- std::size_t lastPos = chassis.rfind (' /' );
392
- if (lastPos == std::string::npos)
392
+ sdbusplus::message::object_path path (chassis);
393
+ chassisName = path.filename ();
394
+ if (chassisName.empty ())
393
395
{
394
396
BMCWEB_LOG_ERROR << " Failed to find '/' in " << chassis;
395
397
continue ;
396
398
}
397
- chassisName = chassis.substr (lastPos + 1 );
398
399
if (chassisName == asyncResp->chassisId )
399
400
{
400
401
chassisPath = chassis;
@@ -441,13 +442,13 @@ void getChassis(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
441
442
std::string chassisName;
442
443
for (const std::string& chassis : chassisPaths)
443
444
{
444
- std::size_t lastPos = chassis.rfind (' /' );
445
- if (lastPos == std::string::npos)
445
+ sdbusplus::message::object_path path (chassis);
446
+ chassisName = path.filename ();
447
+ if (chassisName.empty ())
446
448
{
447
449
BMCWEB_LOG_ERROR << " Failed to find '/' in " << chassis;
448
450
continue ;
449
451
}
450
- chassisName = chassis.substr (lastPos + 1 );
451
452
if (chassisName == sensorsAsyncResp->chassisId )
452
453
{
453
454
chassisPath = &chassis;
@@ -1149,15 +1150,16 @@ inline void populateFanRedundancy(
1149
1150
sensorsAsyncResp->res );
1150
1151
return ;
1151
1152
}
1152
- size_t lastSlash = path.rfind (' /' );
1153
- if (lastSlash == std::string::npos)
1153
+ sdbusplus::message::object_path objectPath (
1154
+ path);
1155
+ std::string name = objectPath.filename ();
1156
+ if (name.empty ())
1154
1157
{
1155
1158
// this should be impossible
1156
1159
messages::internalError (
1157
1160
sensorsAsyncResp->res );
1158
1161
return ;
1159
1162
}
1160
- std::string name = path.substr (lastSlash + 1 );
1161
1163
std::replace (name.begin (), name.end (), ' _' ,
1162
1164
' ' );
1163
1165
@@ -1180,10 +1182,12 @@ inline void populateFanRedundancy(
1180
1182
sensorsAsyncResp->res .jsonValue [" Fans" ];
1181
1183
for (const std::string& item : *collection)
1182
1184
{
1183
- lastSlash = item.rfind (' /' );
1184
- // make a copy as collection is const
1185
- std::string itemName =
1186
- item.substr (lastSlash + 1 );
1185
+ sdbusplus::message::object_path path (item);
1186
+ std::string itemName = path.filename ();
1187
+ if (itemName.empty ())
1188
+ {
1189
+ continue ;
1190
+ }
1187
1191
/*
1188
1192
todo(ed): merge patch that fixes the names
1189
1193
std::replace(itemName.begin(),
@@ -2684,14 +2688,14 @@ inline bool findSensorNameUsingSensorPath(
2684
2688
boost::container::flat_set<std::string>& sensorsList,
2685
2689
boost::container::flat_set<std::string>& sensorsModified)
2686
2690
{
2687
- for (std::string_view chassisSensor : sensorsList)
2691
+ for (auto & chassisSensor : sensorsList)
2688
2692
{
2689
- std::size_t pos = chassisSensor.rfind (' /' );
2690
- if (pos >= (chassisSensor.size () - 1 ))
2693
+ sdbusplus::message::object_path path (chassisSensor);
2694
+ std::string_view thisSensorName = path.filename ();
2695
+ if (thisSensorName.empty ())
2691
2696
{
2692
2697
continue ;
2693
2698
}
2694
- std::string_view thisSensorName = chassisSensor.substr (pos + 1 );
2695
2699
if (thisSensorName == sensorName)
2696
2700
{
2697
2701
sensorsModified.emplace (chassisSensor);
@@ -2790,14 +2794,13 @@ inline void setSensorsOverride(
2790
2794
}
2791
2795
for (const auto & item : objectsWithConnection)
2792
2796
{
2793
-
2794
- auto lastPos = item. first . rfind ( ' / ' );
2795
- if (lastPos == std::string::npos )
2797
+ sdbusplus::message::object_path path (item. first );
2798
+ std::string sensorName = path. filename ( );
2799
+ if (sensorName. empty () )
2796
2800
{
2797
2801
messages::internalError (sensorAsyncResp->res );
2798
2802
return ;
2799
2803
}
2800
- std::string sensorName = item.first .substr (lastPos + 1 );
2801
2804
2802
2805
const auto & iterator = overrideMap.find (sensorName);
2803
2806
if (iterator == overrideMap.end ())
@@ -3042,15 +3045,14 @@ class SensorCollection : public Node
3042
3045
{
3043
3046
BMCWEB_LOG_DEBUG << " Adding sensor: " << sensor;
3044
3047
3045
- std:: size_t lastPos = sensor. rfind ( ' / ' );
3046
- if (lastPos == std::string::npos ||
3047
- lastPos + 1 >= sensor. size ())
3048
+ sdbusplus::message::object_path path (sensor );
3049
+ std::string sensorName = path. filename ();
3050
+ if (sensorName. empty ())
3048
3051
{
3049
3052
BMCWEB_LOG_ERROR << " Invalid sensor path: " << sensor;
3050
3053
messages::internalError (asyncResp->res );
3051
3054
return ;
3052
3055
}
3053
- std::string sensorName = sensor.substr (lastPos + 1 );
3054
3056
entriesArray.push_back (
3055
3057
{{" @odata.id" ,
3056
3058
" /redfish/v1/Chassis/" + asyncResp->chassisId + " /" +
@@ -3128,16 +3130,14 @@ class Sensor : public Node
3128
3130
std::vector<std::pair<std::string,
3129
3131
std::vector<std::string>>>>&
3130
3132
object) {
3131
- std::string_view sensor = object.first ;
3132
- std::size_t lastPos = sensor.rfind (' /' );
3133
- if (lastPos == std::string::npos ||
3134
- lastPos + 1 >= sensor.size ())
3133
+ sdbusplus::message::object_path path (object.first );
3134
+ std::string name = path.filename ();
3135
+ if (name.empty ())
3135
3136
{
3136
3137
BMCWEB_LOG_ERROR << " Invalid sensor path: "
3137
- << sensor ;
3138
+ << object. first ;
3138
3139
return false ;
3139
3140
}
3140
- std::string_view name = sensor.substr (lastPos + 1 );
3141
3141
3142
3142
return name == sensorName;
3143
3143
});
0 commit comments