Skip to content
Snippets Groups Projects
Commit d4c1f252 authored by Alexandre MALFREYT's avatar Alexandre MALFREYT
Browse files

refactor: enhance extractNumber function to validate prefixes and improve error handling

parent 2973a71d
No related branches found
No related tags found
2 merge requests!4develop -> main,!2[CTRLRFC-1594] Apply memorized dynamic attributes at init
......@@ -368,6 +368,7 @@ void SingleShotAO::init_device()
return;
}
// add dynamic attributes: channel, speed & initial for each channel
std::vector<yat4tango::DynamicAttributeInfo> l_dynAttrList;
for (unsigned int l_cpt = 0; l_cpt < m_nb_chan; l_cpt++)
......@@ -476,9 +477,9 @@ void SingleShotAO::init_device()
l_dynAttrList.push_back(dai_initial);
}
m_dyn_attr_manager->add_attributes(l_dynAttrList);
// Get memorized values from database
for (unsigned int l_cpt = 0; l_cpt < m_nb_chan; l_cpt++)
{
......@@ -757,21 +758,37 @@ Tango::ConstDevString SingleShotAO::dev_status()
//+------------------------------------------------------------------
/**
* Extract the first number found in a string.
* Used for extracting the channel number from dynamic attribute names
* like "channelX", "speedX", "initialX" where X is the channel number.
* Extract the number from a dynamic attribute name with a specific prefix.
* Used for parsing dynamic attribute names like "channelX", "speedX", "initialX"
* where X is the channel number.
*
* @param str The input string to search for numbers
* @return The first number found in the string
* @throws DevFailed if no number is found in the string
* @param prefix The expected prefix (e.g., "channel", "speed", "initial")
* @return The number found after the prefix
* @throws DevFailed if string doesn't start with prefix or no number is found
*/
int extractNumber(const std::string &str)
int extractNumber(const std::string &str, const char* prefix)
{
size_t pos = str.find_first_of("0123456789");
if (pos == std::string::npos)
raise_error("No number found in string", "extractNumber");
// Validate string starts with the given prefix
if (str.find(prefix) != 0)
{
THROW_DEVFAILED("DEVICE_ERROR",
"String must start with the expected prefix",
"SingleShotAO::extractNumber");
}
// Find first digit after the prefix
size_t pos = strlen(prefix);
// Extract and convert the number
std::string numberStr = str.substr(pos);
if (numberStr.empty() || !isdigit(numberStr[0]))
{
THROW_DEVFAILED("DEVICE_ERROR",
"No number found after prefix",
"SingleShotAO::extractNumber");
}
return std::stoi(numberStr);
}
......@@ -787,7 +804,7 @@ void SingleShotAO::read_channel(yat4tango::DynamicAttributeReadCallbackData & cb
{
yat::AutoMutex<> guard(m_lock);
std::string l_attr_name = cbd.dya->get_name();
yat::uint16 l_idx = extractNumber(l_attr_name); // extract channel nb
yat::uint16 l_idx = extractNumber(l_attr_name, kCHANNEL); // extract channel nb
CHECK_MANAGER();
double l_val = m_manager->get_channel(l_idx);
......@@ -809,7 +826,7 @@ void SingleShotAO::write_channel(yat4tango::DynamicAttributeWriteCallbackData &
cbd.tga->get_write_value(l_val);
std::string l_attr_name = cbd.dya->get_name();
yat::uint16 l_idx = extractNumber(l_attr_name); // extract channel nb
yat::uint16 l_idx = extractNumber(l_attr_name, kCHANNEL); // extract channel nb
CHECK_MANAGER();
try
......@@ -846,7 +863,7 @@ void SingleShotAO::read_speed(yat4tango::DynamicAttributeReadCallbackData & cbd)
{
yat::AutoMutex<> guard(m_lock);
std::string l_attr_name = cbd.dya->get_name();
yat::uint16 l_idx = extractNumber(l_attr_name); // extract channel nb
yat::uint16 l_idx = extractNumber(l_attr_name, kSPEED); // extract channel nb
CHECK_MANAGER();
double l_val = m_manager->get_speed(l_idx);
......@@ -870,7 +887,7 @@ void SingleShotAO::write_speed(yat4tango::DynamicAttributeWriteCallbackData & cb
cbd.tga->get_write_value(l_val);
std::string l_attr_name = cbd.dya->get_name();
yat::uint16 l_idx = extractNumber(l_attr_name); // extract channel nb
yat::uint16 l_idx = extractNumber(l_attr_name, kSPEED); // extract channel nb
if (l_val < 0)
{
l_val = -l_val;
......@@ -912,7 +929,7 @@ void SingleShotAO::read_initial(yat4tango::DynamicAttributeReadCallbackData & cb
{
yat::AutoMutex<> guard(m_lock);
std::string l_attr_name = cbd.dya->get_name();
yat::uint16 l_idx = extractNumber(l_attr_name); // extract channel nb
yat::uint16 l_idx = extractNumber(l_attr_name, kINITIAL); // extract channel nb
CHECK_MANAGER();
double l_val = m_manager->get_initial(l_idx);
......@@ -936,7 +953,7 @@ void SingleShotAO::write_initial(yat4tango::DynamicAttributeWriteCallbackData &
cbd.tga->get_write_value(l_val);
std::string l_attr_name = cbd.dya->get_name();
yat::uint16 l_idx = extractNumber(l_attr_name); // extract channel nb
yat::uint16 l_idx = extractNumber(l_attr_name, kINITIAL); // extract channel nb
CHECK_MANAGER();
try
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment