From 966d13519a38e1cd69cb19e62c69870529bcb523 Mon Sep 17 00:00:00 2001 From: Stefan Pöschel Date: Wed, 15 Feb 2017 10:16:17 +0100 Subject: Small refactorings --- src/dls.cpp | 22 +++++++++------------ src/dls.h | 12 ++++++++++- src/odr-padenc.cpp | 58 +++++++++++++++++++++++------------------------------- src/pad_common.cpp | 4 ++++ src/pad_common.h | 12 ++++++----- src/sls.cpp | 8 ++++---- src/sls.h | 2 +- 7 files changed, 61 insertions(+), 57 deletions(-) (limited to 'src') diff --git a/src/dls.cpp b/src/dls.cpp index 8e946ca..08297cb 100644 --- a/src/dls.cpp +++ b/src/dls.cpp @@ -186,17 +186,16 @@ void DLSManager::parse_dl_params(std::ifstream &dls_fstream, DL_STATE &dl_state) } -void DLSManager::writeDLS(const std::string& dls_file, DABCharset charset, bool raw_dls, bool remove_dls) { +void DLSManager::writeDLS(const std::string& dls_file, const DL_PARAMS& dl_params) { + DL_STATE dl_state; + std::vector dls_lines; + std::ifstream dls_fstream(dls_file); if (!dls_fstream.is_open()) { std::cerr << "Could not open " << dls_file << std::endl; return; } - DL_STATE dl_state; - - std::vector dls_lines; - std::string line; // Read and convert lines one by one because the converter doesn't understand // line endings @@ -206,7 +205,7 @@ void DLSManager::writeDLS(const std::string& dls_file, DABCharset charset, bool if (line == DL_PARAMS_OPEN) { parse_dl_params(dls_fstream, dl_state); } else { - if (not raw_dls && charset == DABCharset::UTF8) { + if (not dl_params.raw_dls && dl_params.charset == DABCharset::UTF8) { dls_lines.push_back(charset_converter.convert(line)); } else { @@ -219,14 +218,14 @@ void DLSManager::writeDLS(const std::string& dls_file, DABCharset charset, bool std::stringstream ss; for (size_t i = 0; i < dls_lines.size(); i++) { if (i != 0) { - if (charset == DABCharset::UCS2_BE) + if (dl_params.charset == DABCharset::UCS2_BE) ss << '\0' << '\n'; else ss << '\n'; } // UCS-2 BE: if from file the first byte of \0\n remains, remove it - if (charset == DABCharset::UCS2_BE && dls_lines[i].size() % 2) { + if (dl_params.charset == DABCharset::UCS2_BE && dls_lines[i].size() % 2) { dls_lines[i].resize(dls_lines[i].size() - 1); } @@ -242,9 +241,6 @@ void DLSManager::writeDLS(const std::string& dls_file, DABCharset charset, bool if (dl_state.dl_plus_enabled && dl_state.dl_plus_tags.empty()) dl_state.dl_plus_tags.push_back(DL_PLUS_TAG()); - if (not raw_dls) - charset = DABCharset::COMPLETE_EBU_LATIN; - // toggle the toggle bit only on new DL state bool dl_state_is_new = dl_state != dl_state_prev; @@ -267,7 +263,7 @@ void DLSManager::writeDLS(const std::string& dls_file, DABCharset charset, bool DATA_GROUP *remove_label_dg = NULL; if (dl_state_is_new) { - if (remove_dls) + if (dl_params.remove_dls) remove_label_dg = createDynamicLabelCommand(DLS_CMD_REMOVE_LABEL); dls_toggle = !dls_toggle; // indicate changed text @@ -275,7 +271,7 @@ void DLSManager::writeDLS(const std::string& dls_file, DABCharset charset, bool dl_state_prev = dl_state; } - prepend_dl_dgs(dl_state, charset); + prepend_dl_dgs(dl_state, dl_params.raw_dls ? dl_params.charset : DABCharset::COMPLETE_EBU_LATIN); if (remove_label_dg) pad_packetizer->AddDG(remove_label_dg, true); } diff --git a/src/dls.h b/src/dls.h index 547c1df..5b56ffa 100644 --- a/src/dls.h +++ b/src/dls.h @@ -47,6 +47,16 @@ enum { }; +// --- DL_PARAMS ----------------------------------------------------------------- +struct DL_PARAMS { + DABCharset charset; + bool raw_dls; + bool remove_dls; + + DL_PARAMS() : charset(DABCharset::UTF8), raw_dls(false), remove_dls(false) {} +}; + + // --- DL_PLUS_TAG ----------------------------------------------------------------- struct DL_PLUS_TAG { int content_type; @@ -140,7 +150,7 @@ public: static const std::string DL_PARAMS_CLOSE; DLSManager(PADPacketizer* pad_packetizer) : pad_packetizer(pad_packetizer), dls_toggle(false) {} - void writeDLS(const std::string& dls_file, DABCharset charset, bool raw_dls, bool remove_dls); + void writeDLS(const std::string& dls_file, const DL_PARAMS& dl_params); }; #endif /* DLS_H_ */ diff --git a/src/odr-padenc.cpp b/src/odr-padenc.cpp index e085e0b..b3fe1b2 100644 --- a/src/odr-padenc.cpp +++ b/src/odr-padenc.cpp @@ -20,7 +20,7 @@ */ /*! \file odr-padenc.cpp - \brief Generete PAD data for MOT Slideshow and DLS + \brief Generate PAD data for MOT Slideshow and DLS \author Sergio Sagliocco \author Matthias P. Braendli @@ -43,12 +43,11 @@ #include "sls.h" -static const int SLEEPDELAY_DEFAULT = 10; // seconds +static const int SLEEPDELAY_DEFAULT = 10; // seconds -void usage(char* name) -{ +static void usage(const char* name) { fprintf(stderr, "DAB PAD encoder %s for MOT Slideshow and DLS\n\n" "By CSP Innovazione nelle ICT s.c.a r.l. (http://rd.csp.it/) and\n" "Opendigitalradio.org\n\n" @@ -91,7 +90,7 @@ void usage(char* name) } -std::string list_dls_files(std::vector dls_files) { +static std::string list_dls_files(std::vector dls_files) { std::string result = ""; for(std::string dls_file : dls_files) { if(!result.empty()) @@ -102,17 +101,12 @@ std::string list_dls_files(std::vector dls_files) { } -int main(int argc, char *argv[]) -{ - int ret; - struct dirent *pDirent; +int main(int argc, char *argv[]) { size_t padlen = 58; bool erase_after_tx = false; int sleepdelay = SLEEPDELAY_DEFAULT; bool raw_slides = false; - DABCharset charset = DABCharset::UTF8; - bool raw_dls = false; - bool remove_dls = false; + DL_PARAMS dl_params; const char* sls_dir = NULL; const char* output = "/tmp/pad.fifo"; @@ -135,19 +129,17 @@ int main(int argc, char *argv[]) {0,0,0,0}, }; - int ch=0; - int index; - while(ch != -1) { - ch = getopt_long(argc, argv, "eChRrc:d:o:p:s:t:v", longopts, &index); + int ch; + while((ch = getopt_long(argc, argv, "eChRrc:d:o:p:s:t:v", longopts, NULL)) != -1) { switch (ch) { case 'c': - charset = (DABCharset) atoi(optarg); + dl_params.charset = (DABCharset) atoi(optarg); break; case 'C': - raw_dls = true; + dl_params.raw_dls = true; break; case 'r': - remove_dls = true; + dl_params.remove_dls = true; break; case 'd': sls_dir = optarg; @@ -161,7 +153,7 @@ int main(int argc, char *argv[]) case 's': sleepdelay = atoi(optarg); break; - case 't': + case 't': // can be used more than once! dls_files.push_back(optarg); break; case 'p': @@ -180,8 +172,8 @@ int main(int argc, char *argv[]) } } - if (padlen != PADPacketizer::SHORT_PAD && (padlen < PADPacketizer::VARSIZE_PAD_MIN || padlen > PADPacketizer::VARSIZE_PAD_MAX)) { - fprintf(stderr, "ODR-PadEnc Error: pad length %zu invalid: Possible values: %s\n", + if (!PADPacketizer::CheckPADLen(padlen)) { + fprintf(stderr, "ODR-PadEnc Error: PAD length %zu invalid: Possible values: %s\n", padlen, PADPacketizer::ALLOWED_PADLEN.c_str()); return 2; } @@ -199,13 +191,13 @@ int main(int argc, char *argv[]) list_dls_files(dls_files).c_str(), output); } else { - fprintf(stderr, "ODR-PadEnc Error: No DLS nor slideshow to encode !\n"); + fprintf(stderr, "ODR-PadEnc Error: Neither DLS nor Slideshow to encode !\n"); usage(argv[0]); return 1; } const char* user_charset; - switch (charset) { + switch (dl_params.charset) { case DABCharset::COMPLETE_EBU_LATIN: user_charset = "Complete EBU Latin"; break; @@ -231,10 +223,10 @@ int main(int argc, char *argv[]) } fprintf(stderr, "ODR-PadEnc using charset %s (%d)\n", - user_charset, charset); + user_charset, dl_params.charset); - if (not raw_dls) { - switch (charset) { + if (not dl_params.raw_dls) { + switch (dl_params.charset) { case DABCharset::COMPLETE_EBU_LATIN: // no conversion needed break; @@ -273,6 +265,7 @@ int main(int argc, char *argv[]) } // Add new slides to transmit to list + struct dirent *pDirent; while ((pDirent = readdir(pDir)) != NULL) { std::string slide = pDirent->d_name; @@ -308,7 +301,7 @@ int main(int argc, char *argv[]) // if ATM no slides, transmit at least DLS if (slides_to_transmit.empty()) { if (not dls_files.empty()) { - dls_manager.writeDLS(dls_files[curr_dls_file], charset, raw_dls, remove_dls); + dls_manager.writeDLS(dls_files[curr_dls_file], dl_params); pad_packetizer.WriteAllPADs(output_fd); curr_dls_file = (curr_dls_file + 1) % dls_files.size(); @@ -324,8 +317,7 @@ int main(int argc, char *argv[]) it != slides_to_transmit.cend(); ++it) { - ret = sls_manager.encodeFile(it->filepath, it->fidx, raw_slides); - if (ret != 1) + if (!sls_manager.encodeFile(it->filepath, it->fidx, raw_slides)) fprintf(stderr, "ODR-PadEnc Error: cannot encode file '%s'\n", it->filepath.c_str()); if (erase_after_tx) { @@ -338,14 +330,14 @@ int main(int argc, char *argv[]) // while flushing, insert DLS after a certain PAD amout while (pad_packetizer.QueueFilled()) { if (not dls_files.empty()) - dls_manager.writeDLS(dls_files[curr_dls_file], charset, raw_dls, remove_dls); + dls_manager.writeDLS(dls_files[curr_dls_file], dl_params); pad_packetizer.WriteAllPADs(output_fd, DLSManager::DLS_REPETITION_WHILE_SLS); } // after the slide, output a last DLS if (not dls_files.empty()) - dls_manager.writeDLS(dls_files[curr_dls_file], charset, raw_dls, remove_dls); + dls_manager.writeDLS(dls_files[curr_dls_file], dl_params); pad_packetizer.WriteAllPADs(output_fd); curr_dls_file = (curr_dls_file + 1) % dls_files.size(); @@ -356,7 +348,7 @@ int main(int argc, char *argv[]) } } else { // only DLS // Always retransmit DLS, we want it to be updated frequently - dls_manager.writeDLS(dls_files[curr_dls_file], charset, raw_dls, remove_dls); + dls_manager.writeDLS(dls_files[curr_dls_file], dl_params); pad_packetizer.WriteAllPADs(output_fd); curr_dls_file = (curr_dls_file + 1) % dls_files.size(); diff --git a/src/pad_common.cpp b/src/pad_common.cpp index e29d548..4d2d9af 100644 --- a/src/pad_common.cpp +++ b/src/pad_common.cpp @@ -311,3 +311,7 @@ DATA_GROUP* PADPacketizer::CreateDataGroupLengthIndicator(size_t len) { return dg; } + +bool PADPacketizer::CheckPADLen(size_t len) { + return len == PADPacketizer::SHORT_PAD || (len >= PADPacketizer::VARSIZE_PAD_MIN && len <= PADPacketizer::VARSIZE_PAD_MAX); +} diff --git a/src/pad_common.h b/src/pad_common.h index a2f836c..6aba19b 100644 --- a/src/pad_common.h +++ b/src/pad_common.h @@ -73,6 +73,12 @@ struct DATA_GROUP { // --- PADPacketizer ----------------------------------------------------------------- class PADPacketizer { private: + static const size_t SUBFIELD_LENS[]; + static const size_t FPAD_LEN; + static const size_t SHORT_PAD; + static const size_t VARSIZE_PAD_MIN; + static const size_t VARSIZE_PAD_MAX; + const size_t xpad_size_max; const bool short_xpad; const size_t max_cis; @@ -105,11 +111,6 @@ private: void ResetPAD(); pad_t* FlushPAD(); public: - static const size_t SUBFIELD_LENS[]; - static const size_t FPAD_LEN; - static const size_t SHORT_PAD; - static const size_t VARSIZE_PAD_MIN; - static const size_t VARSIZE_PAD_MAX; static const std::string ALLOWED_PADLEN; PADPacketizer(size_t pad_size); @@ -125,6 +126,7 @@ public: void WriteAllPADs(int output_fd, int limit = -1); static DATA_GROUP* CreateDataGroupLengthIndicator(size_t len); + static bool CheckPADLen(size_t len); }; #endif /* PAD_COMMON_H_ */ diff --git a/src/sls.cpp b/src/sls.cpp index a51b376..0ad3123 100644 --- a/src/sls.cpp +++ b/src/sls.cpp @@ -286,9 +286,9 @@ size_t SLSManager::resizeImage(MagickWand* m_wand, unsigned char** blob, const s #endif -int SLSManager::encodeFile(const std::string& fname, int fidx, bool raw_slides) +bool SLSManager::encodeFile(const std::string& fname, int fidx, bool raw_slides) { - int ret = 0; + bool result = false; int nseg, lastseglen, i, last, curseglen; #if HAVE_MAGICKWAND MagickWand *m_wand = NULL; @@ -509,7 +509,7 @@ int SLSManager::encodeFile(const std::string& fname, int fidx, bool raw_slides) pad_packetizer->AddDG(mscdg, false); } - ret = 1; + result = true; } encodefile_out: @@ -522,7 +522,7 @@ encodefile_out: if (blob) { free(blob); } - return ret; + return result; } diff --git a/src/sls.h b/src/sls.h index 44ca39b..8dc49c8 100644 --- a/src/sls.h +++ b/src/sls.h @@ -225,7 +225,7 @@ public: SLSManager(PADPacketizer* pad_packetizer) : pad_packetizer(pad_packetizer), cindex_header(0), cindex_body(0) {} - int encodeFile(const std::string& fname, int fidx, bool raw_slides); + bool encodeFile(const std::string& fname, int fidx, bool raw_slides); }; #endif /* SLS_H_ */ -- cgit v1.2.3