diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2018-02-09 11:43:10 +0100 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2018-02-09 11:43:10 +0100 |
commit | 6902285dbc1c816f9c16d1d63a151b2022c094dc (patch) | |
tree | 4ea42146ffbcb54ad27526f4659099f77d435610 /src | |
parent | f4ef1284d754ecd907dd4ed3072f8be65b26f2de (diff) | |
download | dabmux-6902285dbc1c816f9c16d1d63a151b2022c094dc.tar.gz dabmux-6902285dbc1c816f9c16d1d63a151b2022c094dc.tar.bz2 dabmux-6902285dbc1c816f9c16d1d63a151b2022c094dc.zip |
Convert labels from utf-8 to EBU
Diffstat (limited to 'src')
-rw-r--r-- | src/MuxElements.cpp | 60 | ||||
-rw-r--r-- | src/MuxElements.h | 10 |
2 files changed, 55 insertions, 15 deletions
diff --git a/src/MuxElements.cpp b/src/MuxElements.cpp index cdfcc42..a31791a 100644 --- a/src/MuxElements.cpp +++ b/src/MuxElements.cpp @@ -30,6 +30,7 @@ #include <algorithm> #include "MuxElements.h" +#include "lib/charset/charset.h" #include <boost/algorithm/string.hpp> #include <boost/format.hpp> @@ -44,7 +45,7 @@ const unsigned short Sub_Channel_SizeTable[64] = { 232, 280, 160, 208, 280, 192, 280, 416 }; - +static CharsetConverter charset_converter; using namespace std; @@ -177,7 +178,14 @@ int DabLabel::setLabel(const std::string& label) m_flag = 0xFF00; // truncate the label to the eight first characters - m_label = label; + try { + m_label = charset_converter.convert(label); + } + catch (const utf8::exception& e) { + etiLog.level(warn) << "Failed to convert label '" << label << + "' to EBU Charset"; + m_label = label; + } return 0; } @@ -185,20 +193,43 @@ int DabLabel::setLabel(const std::string& label) int DabLabel::setLabel(const std::string& label, const std::string& short_label) { DabLabel newlabel; + newlabel.m_flag = 0xFF00; + + try { + newlabel.m_label = charset_converter.convert(label); + + int flag = newlabel.setShortLabel(charset_converter.convert(short_label)); + if (flag < 0) { + return flag; + } + + m_flag = flag & 0xFFFF; + } + catch (const utf8::exception& e) { + etiLog.level(warn) << "Failed to convert label '" << label << + " or short label '" << short_label << "' to EBU Charset"; + + // Use label as-is - int result = newlabel.setLabel(label); - if (result < 0) - return result; + newlabel.m_label = label; + newlabel.m_flag = 0xFF00; - /* First check if we can actually create the short label */ - int flag = newlabel.setShortLabel(short_label); - if (flag < 0) - return flag; + int result = newlabel.setLabel(label); + if (result < 0) { + return result; + } + + /* First check if we can actually create the short label */ + int flag = newlabel.setShortLabel(short_label); + if (flag < 0) { + return flag; + } + + m_flag = flag & 0xFFFF; + } // short label is valid. - m_flag = flag & 0xFFFF; m_label = newlabel.m_label; - return 0; } @@ -258,6 +289,11 @@ int DabLabel::setShortLabel(const std::string& slabel) return flag; } +const string DabLabel::long_label() const +{ + return charset_converter.convert_ebu_to_utf8(m_label); +} + const string DabLabel::short_label() const { stringstream shortlabel; @@ -267,7 +303,7 @@ const string DabLabel::short_label() const } } - return shortlabel.str(); + return charset_converter.convert_ebu_to_utf8(shortlabel.str()); } void DabLabel::writeLabel(uint8_t* buf) const diff --git a/src/MuxElements.h b/src/MuxElements.h index ba9941f..1f3f548 100644 --- a/src/MuxElements.h +++ b/src/MuxElements.h @@ -147,7 +147,11 @@ struct dabOutput { class DabLabel { public: - /* Set a new label and short label. + /* Set a new label and short label. If the label parses as valid UTF-8, it + * will be converted to EBU Latin. If utf-8 decoding fails, the label + * will be used as is. Characters that cannot be converted are replaced + * by a space. + * * returns: 0 on success * -1 if the short_label is not a representable * -2 if the short_label is too long @@ -170,7 +174,7 @@ class DabLabel void writeLabel(uint8_t* buf) const; uint16_t flag() const { return m_flag; } - const std::string long_label() const { return m_label; } + const std::string long_label() const; const std::string short_label() const; private: @@ -182,7 +186,7 @@ class DabLabel /* The m_label is not padded in any way */ std::string m_label; - /* Checks and calculates the flag */ + /* Checks and calculates the flag. slabel must be EBU Latin Charset */ int setShortLabel(const std::string& slabel); }; |