Skip to content

Commit ccd324a

Browse files
General: Replace "std:stod" with string streams and use imbue to
associate a locale to the stream and stream buffer. std::stod behaviour changes based on the system locale, we made these changes to avoid wrong parameter conversion. Signed-off-by: AlexandraTrifan <[email protected]>
1 parent 9ec96a2 commit ccd324a

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

include/libm2k/utils/utils.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ namespace utils {
7171
static std::vector<std::string> split(std::string, std::string);
7272
static int compareVersions(std::string v1, std::string v2);
7373
static bool compareNatural(const std::string &a, const std::string &b);
74+
static double safeStod(const std::string& to_convert);
7475
private:
7576
static std::string parseIniSection(std::string line);
7677
static std::pair<std::string, std::vector<std::string>>

src/analog/m2kpowersupply_impl.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <libm2k/m2kexceptions.hpp>
2525
#include <libm2k/logger.hpp>
2626
#include "utils/channel.hpp"
27+
#include <libm2k/utils/utils.hpp>
2728
#include <iio.h>
2829

2930
using namespace libm2k::analog;
@@ -194,7 +195,8 @@ void M2kPowerSupplyImpl::loadCalibrationCoefficients()
194195
__try {
195196
auto pair = m_generic_device->getContextAttr(i);
196197
calib_pair.first = std::string(pair.first.c_str() + 4);
197-
calib_pair.second = std::stod(pair.second);
198+
calib_pair.second = Utils::safeStod(pair.second);
199+
198200
m_calib_coefficients.push_back(calib_pair);
199201
} __catch (exception_type &) {
200202
continue;

src/m2k_impl.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -559,24 +559,24 @@ bool M2kImpl::hasContextCalibration()
559559
auto it = splitValues.begin();
560560
while (it != splitValues.end()) {
561561
try {
562-
temperature = std::stod(*it);
562+
temperature = Utils::safeStod(*it);
563563
auto parameters = std::make_shared<struct CALIBRATION_PARAMETERS>();
564564
it = std::next(it);
565565
parameters->adc_offset_ch_1 = std::stoi(*it);
566566
it = std::next(it);
567567
parameters->adc_offset_ch_2 = std::stoi(*it);
568568
it = std::next(it);
569-
parameters->adc_gain_ch_1 = std::stod(*it);
569+
parameters->adc_gain_ch_1 = Utils::safeStod(*it);
570570
it = std::next(it);
571-
parameters->adc_gain_ch_2 = std::stod(*it);
571+
parameters->adc_gain_ch_2 = Utils::safeStod(*it);
572572
it = std::next(it);
573573
parameters->dac_a_offset = std::stoi(*it);
574574
it = std::next(it);
575575
parameters->dac_b_offset = std::stoi(*it);
576576
it = std::next(it);
577-
parameters->dac_a_gain = std::stod(*it);
577+
parameters->dac_a_gain = Utils::safeStod(*it);
578578
it = std::next(it);
579-
parameters->dac_b_gain = std::stod(*it);
579+
parameters->dac_b_gain = Utils::safeStod(*it);
580580
it = std::next(it);
581581

582582
m_calibration_lut.insert({temperature, parameters});

src/utils/utils.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
#include <algorithm>
2929
#include <thread>
3030
#include <chrono>
31-
#include <iostream>
3231
#include <cctype>
3332
#include <sstream>
33+
#include <locale>
3434

3535
using namespace std;
3636
using namespace libm2k::utils;
@@ -283,3 +283,12 @@ bool Utils::compareNatural(const std::string& a, const std::string& b){
283283
std::getline(string_stream_b, b_new);
284284
return (compareNatural(a_new, b_new));
285285
}
286+
287+
double Utils::safeStod(const std::string& to_convert)
288+
{
289+
double converted_value = 0.0;
290+
std::istringstream in_s(to_convert);
291+
in_s.imbue(std::locale("C"));
292+
in_s >> converted_value;
293+
return converted_value;
294+
}

0 commit comments

Comments
 (0)