diff options
Diffstat (limited to 'src/MuxElements.cpp')
-rw-r--r-- | src/MuxElements.cpp | 80 |
1 files changed, 35 insertions, 45 deletions
diff --git a/src/MuxElements.cpp b/src/MuxElements.cpp index e8871d1..4bd82cd 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> @@ -43,29 +44,26 @@ const unsigned short Sub_Channel_SizeTable[64] = { using namespace std; -int DabLabel::setLabel(const std::string& text) +int DabLabel::setLabel(const std::string& label) { - int len = text.length(); - if (len > 16) + size_t len = label.length(); + if (len > DABLABEL_LENGTH) return -3; - memset(m_text, 0, 17); - memcpy(m_text, text.c_str(), len); - m_flag = 0xFF00; // truncate the label to the eight first characters + m_label = label; + return 0; } -int DabLabel::setLabel(const std::string& text, const std::string& short_label) +int DabLabel::setLabel(const std::string& label, const std::string& short_label) { DabLabel newlabel; - memset(newlabel.m_text, 0, 17); - int len = text.length(); - if (len > 16) - return -3; - memcpy(newlabel.m_text, text.c_str(), len); + 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); @@ -73,8 +71,8 @@ int DabLabel::setLabel(const std::string& text, const std::string& short_label) return flag; // short label is valid. - memcpy(this->m_text, newlabel.m_text, 17); - this->m_flag = flag & 0xFFFF; + m_flag = flag & 0xFFFF; + m_label = newlabel.m_label; return 0; } @@ -104,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 == this->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; @@ -117,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; } @@ -138,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) @@ -186,19 +191,19 @@ vector<DabComponent*>::iterator getComponent( return getComponent(components, serviceId, components.end()); } -vector<DabService*>::iterator getService( +std::vector<std::shared_ptr<DabService> >::iterator getService( DabComponent* component, - vector<DabService*>& services) + std::vector<std::shared_ptr<DabService> >& services) { - vector<DabService*>::iterator service; - - for (service = services.begin(); service != services.end(); ++service) { - if ((*service)->id == component->serviceId) { - break; + size_t i = 0; + for (auto service : services) { + if (service->id == component->serviceId) { + return services.begin() + i; } + i++; } - return service; + throw std::runtime_error("Service not included in any component"); } bool DabComponent::isPacketComponent(vector<dabSubchannel*>& subchannels) @@ -224,9 +229,6 @@ bool DabComponent::isPacketComponent(vector<dabSubchannel*>& subchannels) void DabComponent::set_parameter(const string& parameter, const string& value) { - stringstream ss(value); - ss.exceptions ( stringstream::failbit | stringstream::badbit ); - if (parameter == "label") { vector<string> fields; boost::split(fields, value, boost::is_any_of(",")); @@ -274,10 +276,7 @@ const string DabComponent::get_parameter(const string& parameter) const { stringstream ss; if (parameter == "label") { - char l[17]; - l[16] = '\0'; - memcpy(l, label.text(), 16); - ss << l << "," << label.short_label(); + ss << label.long_label() << "," << label.short_label(); } else { ss << "Parameter '" << parameter << @@ -289,7 +288,7 @@ const string DabComponent::get_parameter(const string& parameter) const } -unsigned char DabService::getType(dabEnsemble* ensemble) +unsigned char DabService::getType(boost::shared_ptr<dabEnsemble> ensemble) { vector<dabSubchannel*>::iterator subchannel; vector<DabComponent*>::iterator component = @@ -322,9 +321,6 @@ unsigned char DabService::nbComponent(vector<DabComponent*>& components) void DabService::set_parameter(const string& parameter, const string& value) { - stringstream ss(value); - ss.exceptions ( stringstream::failbit | stringstream::badbit ); - if (parameter == "label") { vector<string> fields; boost::split(fields, value, boost::is_any_of(",")); @@ -372,10 +368,7 @@ const string DabService::get_parameter(const string& parameter) const { stringstream ss; if (parameter == "label") { - char l[17]; - l[16] = '\0'; - memcpy(l, label.text(), 16); - ss << l << "," << label.short_label(); + ss << label.long_label() << "," << label.short_label(); } else { ss << "Parameter '" << parameter << @@ -388,9 +381,6 @@ const string DabService::get_parameter(const string& parameter) const void dabEnsemble::set_parameter(const string& parameter, const string& value) { - stringstream ss(value); - ss.exceptions ( stringstream::failbit | stringstream::badbit ); - if (parameter == "localtimeoffset") { if (value == "auto") { lto_auto = true; |