diff --git a/src/BookmarkMgr.cpp b/src/BookmarkMgr.cpp index edc13e6..e8ff07b 100644 --- a/src/BookmarkMgr.cpp +++ b/src/BookmarkMgr.cpp @@ -570,6 +570,38 @@ BookmarkEntryPtr BookmarkMgr::demodToBookmarkEntry(DemodulatorInstancePtr demod) return be; } +std::wstring BookmarkMgr::getSafeWstringValue(DataNode* node, const std::string& childNodeName) { + + std::wstring decodedWString = L""; + + if (node != nullptr) { + + DataNode* childNode = node->getNext(childNodeName.c_str()); + + //1) decode as encoded wstring: + try { + childNode->element()->get(decodedWString); + + } catch (DataTypeMismatchException* e) { + //2) wstring decode fail, try simple std::string + std::string decodedStdString; + try { + + childNode->element()->get(decodedStdString); + + //use wxString for a clean conversion to a wstring: + decodedWString = wxString(decodedStdString).ToStdWstring(); + + } catch (DataTypeMismatchException* e) { + //nothing works, return an empty string. + decodedWString = L""; + } + } + } + + return decodedWString; +} + BookmarkEntryPtr BookmarkMgr::nodeToBookmark(DataNode *node) { if (!node->hasAnother("frequency") || !node->hasAnother("type") || !node->hasAnother("bandwidth")) { return nullptr; @@ -582,7 +614,7 @@ BookmarkEntryPtr BookmarkMgr::nodeToBookmark(DataNode *node) { node->getNext("bandwidth")->element()->get(be->bandwidth); if (node->hasAnother("user_label")) { - node->getNext("user_label")->element()->get(be->label); + be->label = BookmarkMgr::getSafeWstringValue( node, "user_label"); } node->rewindAll(); diff --git a/src/BookmarkMgr.h b/src/BookmarkMgr.h index 3d2183f..4a96777 100644 --- a/src/BookmarkMgr.h +++ b/src/BookmarkMgr.h @@ -129,10 +129,15 @@ public: static std::wstring getBookmarkEntryDisplayName(BookmarkEntryPtr bmEnt); static std::wstring getActiveDisplayName(DemodulatorInstancePtr demod); + protected: void trimRecents(); void loadDefaultRanges(); + + //utility method that attemts to decode the childNodeName as std::wstring, else as std::string, else + //return an empty string. + static std::wstring getSafeWstringValue(DataNode* node, const std::string& childNodeName); BookmarkEntryPtr demodToBookmarkEntry(DemodulatorInstancePtr demod); BookmarkEntryPtr nodeToBookmark(DataNode *node); diff --git a/src/demod/DemodulatorMgr.cpp b/src/demod/DemodulatorMgr.cpp index 40978b9..cc3fa27 100644 --- a/src/demod/DemodulatorMgr.cpp +++ b/src/demod/DemodulatorMgr.cpp @@ -16,6 +16,7 @@ #endif #include "DataTree.h" +#include bool demodFreqCompare (DemodulatorInstancePtr i, DemodulatorInstancePtr j) { return (i->getFrequency() < j->getFrequency()); } bool inactiveCompare (DemodulatorInstancePtr i, DemodulatorInstancePtr j) { return (i->isActive() < j->isActive()); } @@ -432,6 +433,36 @@ void DemodulatorMgr::saveInstance(DataNode *node, DemodulatorInstancePtr inst) { } } +std::wstring DemodulatorMgr::getSafeWstringValue(DataNode* node) { + + std::wstring decodedWString = L""; + + if (node != nullptr) { + + //1) decode as encoded wstring: + try { + node->element()->get(decodedWString); + + } catch (DataTypeMismatchException* e) { + //2) wstring decode fail, try simple std::string + std::string decodedStdString; + try { + + node->element()->get(decodedStdString); + + //use wxString for a clean conversion to a wstring: + decodedWString = wxString(decodedStdString).ToStdWstring(); + + } catch (DataTypeMismatchException* e) { + //nothing works, return an empty string. + decodedWString = L""; + } + } + } + + return decodedWString; +} + DemodulatorInstancePtr DemodulatorMgr::loadInstance(DataNode *node) { std::lock_guard < std::recursive_mutex > lock(demods_busy); @@ -486,7 +517,8 @@ DemodulatorInstancePtr DemodulatorMgr::loadInstance(DataNode *node) { DataNode *demodUserLabel = node->hasAnother("user_label") ? node->getNext("user_label") : nullptr; if (demodUserLabel) { - demodUserLabel->element()->get(user_label); + + user_label = DemodulatorMgr::getSafeWstringValue(demodUserLabel); } ModemSettings mSettings; diff --git a/src/demod/DemodulatorMgr.h b/src/demod/DemodulatorMgr.h index b080e5f..027e1e3 100644 --- a/src/demod/DemodulatorMgr.h +++ b/src/demod/DemodulatorMgr.h @@ -77,8 +77,13 @@ public: DemodulatorInstancePtr loadInstance(DataNode *node); + private: + //utility method that attemts to decode node value as std::wstring, else as std::string, else + //return an empty string. + static std::wstring getSafeWstringValue(DataNode* node); + std::vector demods; DemodulatorInstancePtr activeDemodulator;