Skip to content

Commit

Permalink
Fix for #21. On systems with "non-standard" LOCALE settings std::stoX…
Browse files Browse the repository at this point in the history
… functions may produce wrong results (#22)

Signed-off-by: Remigiusz Kołłątaj <[email protected]>
  • Loading branch information
Remigiusz Kołłątaj authored Mar 11, 2020
1 parent 8541596 commit 0200156
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,27 @@

#include "cantypes.hpp"
#include <string>
#include <clocale>

namespace CANdb {

template <typename Derived> struct Parser {

bool parse(const std::string& data) noexcept
{
// Store current LOCALE
std::string prevLocale = std::setlocale(LC_ALL, nullptr);
// To assure that std::stoX functions will produced the same results
// on systems with different LOCALE settings
std::setlocale(LC_ALL, "C");

Derived* d = static_cast<Derived*>(this);
return d->parse(data);
auto ret = d->parse(data);

// Restore previous LOCALE for the application
std::setlocale(LC_ALL, prevLocale.c_str());

return ret;
}

CANdb_t getDb() const noexcept { return can_db; }
Expand Down

0 comments on commit 0200156

Please sign in to comment.