diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2015-05-25 19:04:57 +0200 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2015-05-25 19:04:57 +0200 |
commit | 0b647385bb6e379c253099b32692f867fe1bd428 (patch) | |
tree | a5355f61bf8a91c7e52f2a1fcb0e50e70bf522df /src | |
parent | f470de09c4215c39387fbe6d85330c90fecea10d (diff) | |
download | dabmux-0b647385bb6e379c253099b32692f867fe1bd428.tar.gz dabmux-0b647385bb6e379c253099b32692f867fe1bd428.tar.bz2 dabmux-0b647385bb6e379c253099b32692f867fe1bd428.zip |
Simplify DAB label handling
The DabLabel object only save the label in the unpadded form,
and handle padding with spaces at the time of usage.
Diffstat (limited to 'src')
-rw-r--r-- | src/DabMux.cpp | 6 | ||||
-rw-r--r-- | src/MuxElements.cpp | 24 | ||||
-rw-r--r-- | src/MuxElements.h | 17 |
3 files changed, 30 insertions, 17 deletions
diff --git a/src/DabMux.cpp b/src/DabMux.cpp index ecbedc4..d83dbce 100644 --- a/src/DabMux.cpp +++ b/src/DabMux.cpp @@ -1661,7 +1661,7 @@ int main(int argc, char *argv[]) fig1_0->EId = htons(ensemble->id); index = index + 4; - memcpy(&etiFrame[index], ensemble->label.text(), 16); + ensemble->label.writeLabel(&etiFrame[index]); index = index + 16; etiFrame[index++] = ensemble->label.flag() >> 8; @@ -1892,7 +1892,7 @@ int main(int argc, char *argv[]) index += 6; figSize += 6; } - memcpy(&etiFrame[index], (*service)->label.text(), 16); + (*service)->label.writeLabel(&etiFrame[index]); index += 16; figSize += 16; etiFrame[index++] = (*service)->label.flag() >> 8; @@ -1943,7 +1943,7 @@ int main(int argc, char *argv[]) index += 7; figSize += 7; } - memcpy(&etiFrame[index], (*component)->label.text(), 16); + (*component)->label.writeLabel(&etiFrame[index]); index += 16; figSize += 16; diff --git a/src/MuxElements.cpp b/src/MuxElements.cpp index 65045db..a452220 100644 --- a/src/MuxElements.cpp +++ b/src/MuxElements.cpp @@ -24,6 +24,7 @@ */ #include <vector> +#include <algorithm> #include "MuxElements.h" #include <boost/algorithm/string.hpp> @@ -46,12 +47,9 @@ using namespace std; int DabLabel::setLabel(const std::string& label) { size_t len = label.length(); - if (len > sizeof(m_text)) + if (len > DABLABEL_LENGTH) return -3; - memcpy(m_text, label.c_str(), len); - memset(m_text + len, 0x20, sizeof(m_text) - len); - m_flag = 0xFF00; // truncate the label to the eight first characters m_label = label; @@ -73,7 +71,6 @@ int DabLabel::setLabel(const std::string& label, const std::string& short_label) return flag; // short label is valid. - memcpy(m_text, newlabel.m_text, sizeof(m_text)); m_flag = flag & 0xFFFF; m_label = newlabel.m_label; @@ -105,8 +102,8 @@ int DabLabel::setShortLabel(const std::string& slabel) /* Iterate over the label and set the bits in the flag * according to the characters in the slabel */ - for (int i = 0; i < 32; ++i) { - if (*slab == m_text[i]) { + for (size_t i = 0; i < m_label.size(); ++i) { + if (*slab == m_label[i]) { flag |= 0x8000 >> i; if (*(++slab) == 0) { break; @@ -118,7 +115,7 @@ int DabLabel::setShortLabel(const std::string& slabel) * we went through the whole label, the short label * cannot be represented */ - if (*slab != 0) { + if (*slab != '\0') { return -1; } @@ -139,15 +136,22 @@ int DabLabel::setShortLabel(const std::string& slabel) const string DabLabel::short_label() const { stringstream shortlabel; - for (int i = 0; i < 32; ++i) { + for (size_t i = 0; i < m_label.size(); ++i) { if (m_flag & 0x8000 >> i) { - shortlabel << m_text[i]; + shortlabel << m_label[i]; } } return shortlabel.str(); } +void DabLabel::writeLabel(uint8_t* buf) const +{ + memset(buf, ' ', DABLABEL_LENGTH); + if (m_label.size() <= DABLABEL_LENGTH) { + std::copy(m_label.begin(), m_label.end(), (char*)buf); + } +} vector<dabSubchannel*>::iterator getSubchannel( vector<dabSubchannel*>& subchannels, int id) diff --git a/src/MuxElements.h b/src/MuxElements.h index 22112db..a116427 100644 --- a/src/MuxElements.h +++ b/src/MuxElements.h @@ -51,6 +51,7 @@ struct dabOutput { DabOutput* output; }; +#define DABLABEL_LENGTH 16 class DabLabel { @@ -71,18 +72,26 @@ class DabLabel */ int setLabel(const std::string& label); - const char* text() const { return m_text; } + /* Write the label to the 16-byte buffer given in buf + * In the DAB standard, the label is 16 bytes long, and is + * padded using spaces. + */ + 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 short_label() const; private: - // In the DAB standard, the label is 16 bytes long. - // We use spaces for padding at the end if needed. - char m_text[16]; + /* The flag field selects which label characters make + * up the short label + */ uint16_t m_flag; + + /* The m_label is not padded in any way */ std::string m_label; + /* Checks and calculates the flag */ int setShortLabel(const std::string& slabel); }; |