Loading ccsrc/Framework/converter.hh +1 −0 Original line number Original line Diff line number Diff line Loading @@ -353,4 +353,5 @@ public: */ */ std::vector<std::string> split_arguments_line(const std::string & p_value); std::vector<std::string> split_arguments_line(const std::string & p_value); static const std::string lut; }; // End of class converter }; // End of class converter ccsrc/Framework/src/converter.cc +32 −46 Original line number Original line Diff line number Diff line #include "converter.hh" #include "converter.hh" #include <stdexcept> converter * converter::instance = NULL; converter * converter::instance = NULL; uint16_t converter::swap(const uint16_t p_value) { uint16_t converter::swap(const uint16_t p_value) { Loading @@ -12,8 +12,8 @@ uint32_t converter::swap(const uint32_t p_value) { return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]; return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]; } } const std::string converter::lut = "0123456789ABCDEF"; std::string converter::string_to_hexa(const std::string & p_value) { std::string converter::string_to_hexa(const std::string & p_value) { static const char * const lut = "0123456789ABCDEF"; std::string input(p_value); std::string input(p_value); std::for_each( std::for_each( Loading @@ -37,55 +37,41 @@ std::string converter::string_to_hexa(const std::string & p_value) { } } std::string converter::bytes_to_hexa(const std::vector<uint8_t> & p_value) { std::string converter::bytes_to_hexa(const std::vector<uint8_t> & p_value) { std::ostringstream oss; std::string ret; oss << std::setfill('0'); ret.assign(p_value.size()*2, ' '); std::for_each( for(size_t i=0; i<p_value.size(); i++){ p_value.begin(), uint8_t c = p_value[i]; p_value.end(), ret[i*2] = lut[c>>4]; [&oss](uint8_t ch) { ret[i*2+1] = lut[c&0xF]; oss << std::hex << std::setw(2) << static_cast<int>(ch); } } ); return ret; return oss.str(); } } std::vector<uint8_t> converter::hexa_to_bytes(const std::string & p_value) { inline uint8_t char2byte(const char ch) { static const char * const lut = "0123456789ABCDEF"; size_t s = converter::lut.find(ch); if(s == std::string::npos) throw (std::length_error("")); return s; } std::vector<uint8_t> converter::hexa_to_bytes(const std::string & p_value) { // Sanity check // Sanity check std::vector<uint8_t> output; std::vector<uint8_t> output; if (p_value.length() & 1) { size_t i=0, idx = 0, outlen=(p_value.length()+1) / 2; return output; } std::string input(p_value); output.assign(outlen, 0x00); std::for_each( try{ input.begin(), if (p_value.length() & 1) input.end(), output[idx++] = char2byte(p_value[i++]); [](char & c) { for(;idx<outlen;idx++){ c = std::toupper(c); uint8_t b0 = char2byte(p_value[i++]); uint8_t b1 = char2byte(p_value[i++]); output[idx] = (b0<<4)|b1; } } ); output.reserve(p_value.length() / 2); for (uint32_t i = 0; i < p_value.length(); i += 2) { char msbDigit = input[i]; const char *msbIndex = std::lower_bound(lut, lut + 16, msbDigit); if (*msbIndex != msbDigit) { output.clear(); return output; } } char lsbDigit = input[i + 1]; catch (const std::length_error& le) { const char* lsbIndex = std::lower_bound(lut, lut + 16, lsbDigit); if (*lsbIndex != lsbDigit) { output.clear(); output.clear(); return output; } } output.push_back(((msbIndex - lut) << 4) | (lsbIndex - lut)); } // End of 'for' statement return output; return output; } } Loading Loading
ccsrc/Framework/converter.hh +1 −0 Original line number Original line Diff line number Diff line Loading @@ -353,4 +353,5 @@ public: */ */ std::vector<std::string> split_arguments_line(const std::string & p_value); std::vector<std::string> split_arguments_line(const std::string & p_value); static const std::string lut; }; // End of class converter }; // End of class converter
ccsrc/Framework/src/converter.cc +32 −46 Original line number Original line Diff line number Diff line #include "converter.hh" #include "converter.hh" #include <stdexcept> converter * converter::instance = NULL; converter * converter::instance = NULL; uint16_t converter::swap(const uint16_t p_value) { uint16_t converter::swap(const uint16_t p_value) { Loading @@ -12,8 +12,8 @@ uint32_t converter::swap(const uint32_t p_value) { return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]; return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]; } } const std::string converter::lut = "0123456789ABCDEF"; std::string converter::string_to_hexa(const std::string & p_value) { std::string converter::string_to_hexa(const std::string & p_value) { static const char * const lut = "0123456789ABCDEF"; std::string input(p_value); std::string input(p_value); std::for_each( std::for_each( Loading @@ -37,55 +37,41 @@ std::string converter::string_to_hexa(const std::string & p_value) { } } std::string converter::bytes_to_hexa(const std::vector<uint8_t> & p_value) { std::string converter::bytes_to_hexa(const std::vector<uint8_t> & p_value) { std::ostringstream oss; std::string ret; oss << std::setfill('0'); ret.assign(p_value.size()*2, ' '); std::for_each( for(size_t i=0; i<p_value.size(); i++){ p_value.begin(), uint8_t c = p_value[i]; p_value.end(), ret[i*2] = lut[c>>4]; [&oss](uint8_t ch) { ret[i*2+1] = lut[c&0xF]; oss << std::hex << std::setw(2) << static_cast<int>(ch); } } ); return ret; return oss.str(); } } std::vector<uint8_t> converter::hexa_to_bytes(const std::string & p_value) { inline uint8_t char2byte(const char ch) { static const char * const lut = "0123456789ABCDEF"; size_t s = converter::lut.find(ch); if(s == std::string::npos) throw (std::length_error("")); return s; } std::vector<uint8_t> converter::hexa_to_bytes(const std::string & p_value) { // Sanity check // Sanity check std::vector<uint8_t> output; std::vector<uint8_t> output; if (p_value.length() & 1) { size_t i=0, idx = 0, outlen=(p_value.length()+1) / 2; return output; } std::string input(p_value); output.assign(outlen, 0x00); std::for_each( try{ input.begin(), if (p_value.length() & 1) input.end(), output[idx++] = char2byte(p_value[i++]); [](char & c) { for(;idx<outlen;idx++){ c = std::toupper(c); uint8_t b0 = char2byte(p_value[i++]); uint8_t b1 = char2byte(p_value[i++]); output[idx] = (b0<<4)|b1; } } ); output.reserve(p_value.length() / 2); for (uint32_t i = 0; i < p_value.length(); i += 2) { char msbDigit = input[i]; const char *msbIndex = std::lower_bound(lut, lut + 16, msbDigit); if (*msbIndex != msbDigit) { output.clear(); return output; } } char lsbDigit = input[i + 1]; catch (const std::length_error& le) { const char* lsbIndex = std::lower_bound(lut, lut + 16, lsbDigit); if (*lsbIndex != lsbDigit) { output.clear(); output.clear(); return output; } } output.push_back(((msbIndex - lut) << 4) | (lsbIndex - lut)); } // End of 'for' statement return output; return output; } } Loading