# HG changeset patch # User František Kučera # Date 1650493090 -7200 # Node ID 0890135ff1f7ccc28ddcfd92c9274a31ec736554 # Parent 9e16e31fa756b8cd2a6de07f59d7f58b9b9e191f use common hex function diff -r 9e16e31fa756 -r 0890135ff1f7 src/Hex.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Hex.h Thu Apr 21 00:18:10 2022 +0200 @@ -0,0 +1,85 @@ +/** + * Relational pipes + * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#include +#include +#include +#include +#include + +namespace relpipe { +namespace tr { +namespace serialize { + +class Hex { +private: + + Hex() { + } + + static char fromHex(wchar_t ch) { + if (L'0' <= ch && ch <= L'9') return ch - L'0'; + else if (L'a' <= ch && ch <= L'f') return ch - L'a' + 10; + else throw std::invalid_argument("Unable to decode hexadeximal string."); + } + +public: + + static std::stringstream fromHex(const std::wstring& hex) { + std::stringstream octets; + + char octet = 0; + + for (size_t i = 0, limit = hex.size(); i < limit; i++) { + if (i % 2 == 0) { + octet = fromHex(hex[i]) << 4; + } else { + octet += fromHex(hex[i]); + octets.put(octet); + } + } + + return octets; + } + + static std::wstring toHex(const std::string& octets) { + std::wstring_convert < codecvt_utf8> convertor; // TODO: do not create converter each time + std::stringstream hex; + hex << std::hex << std::setfill('0') << std::hex; + for (size_t i = 0, size = octets.size(); i < size; i++) hex << std::setw(2) << (0xff & octets[i]); + return convertor.from_bytes(hex.str()); + } + + static std::wstring toTxt(const std::string& octets, bool* validEncoding = nullptr) { + std::wstring_convert < codecvt_utf8> convertor; // TODO: do not create converter each time + try { + if (validEncoding) *validEncoding = true; + // TODO: use encoding from the HTTP response headers instead of the constant one? + return convertor.from_bytes(octets); + } catch (...) { + if (validEncoding) *validEncoding = false; + std::stringstream filtered; + for (char ch : octets) filtered << (ch >= ' ' && ch < 127 ? ch : '.'); + return convertor.from_bytes(filtered.str()); + } + } +}; + +} +} +} diff -r 9e16e31fa756 -r 0890135ff1f7 src/SerializeHandler.h --- a/src/SerializeHandler.h Tue Apr 19 21:00:07 2022 +0200 +++ b/src/SerializeHandler.h Thu Apr 21 00:18:10 2022 +0200 @@ -32,6 +32,7 @@ #include #include "Configuration.h" +#include "Hex.h" namespace relpipe { namespace tr { @@ -57,13 +58,6 @@ size_t attributeIndex = 0; } recordContext; - relpipe::common::type::StringX toHex(const std::string& octets) { - std::stringstream hex; - hex << std::hex << std::setfill('0') << std::hex; - for (size_t i = 0, size = octets.size(); i < size; i++) hex << std::setw(2) << (0xff & octets[i]); - return convertor.from_bytes(hex.str()); - } - public: SerializeHandler(shared_ptr writer, Configuration configuration) : writer(writer), configuration(configuration) { @@ -103,7 +97,7 @@ recordContext.attributeIndex++; if (recordContext.attributeIndex % relationContext.readerMetadata.size() == 0) { - writer->writeAttribute(toHex(recordContext.buffer.str())); + writer->writeAttribute(Hex::toHex(recordContext.buffer.str())); recordContext.attributeIndex = 0; } }