From c7bb69e20a1b789aee8b0ba6a0bdef2af0c60a90 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sun, 11 Aug 2013 18:19:04 +0200 Subject: [PATCH] Added a function to get sorted available languages. --- src/WordReference.cpp | 50 ++++++++++++++++++++++++++++++------------- src/WordReference.hpp | 2 ++ 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/WordReference.cpp b/src/WordReference.cpp index 91f6a6b..f1c9348 100644 --- a/src/WordReference.cpp +++ b/src/WordReference.cpp @@ -4,21 +4,22 @@ #include namespace { - std::map SupportedLanguages { - std::make_pair("ar", "Arabic"), - std::make_pair("zh", "Chinese"), - std::make_pair("cz", "Czech"), - std::make_pair("en", "English"), - std::make_pair("fr", "French"), - std::make_pair("gr", "Greek"), - std::make_pair("it", "Italian"), - std::make_pair("ja", "Japanese"), - std::make_pair("ko", "Korean"), - std::make_pair("pl", "Polish"), - std::make_pair("pt", "Portuguese"), - std::make_pair("ro", "Romanian"), - std::make_pair("es", "Spanish"), - std::make_pair("tr", "Turkish") + typedef std::map LanguageMapType; + const LanguageMapType SupportedLanguages { + std::pair("ar", "Arabic"), + std::pair("zh", "Chinese"), + std::pair("cz", "Czech"), + std::pair("en", "English"), + std::pair("fr", "French"), + std::pair("gr", "Greek"), + std::pair("it", "Italian"), + std::pair("ja", "Japanese"), + std::pair("ko", "Korean"), + std::pair("pl", "Polish"), + std::pair("pt", "Portuguese"), + std::pair("ro", "Romanian"), + std::pair("es", "Spanish"), + std::pair("tr", "Turkish") }; std::string GetCleanWord (std::string parWord) __attribute__((pure)); @@ -30,6 +31,25 @@ namespace { } } //unnamed namespace +///----------------------------------------------------------------------------- +///----------------------------------------------------------------------------- +void GetAvailableLanguages (std::vector& parCodes, std::vector& parNames) { + typedef std::vector > SortedListType; + SortedListType sorted; + sorted.reserve(SupportedLanguages.size()); + parCodes.reserve(SupportedLanguages.size()); + parNames.reserve(SupportedLanguages.size()); + + for (LanguageMapType::const_iterator itCurr = SupportedLanguages.begin(), itEND = SupportedLanguages.end(); itCurr != itEND; ++itCurr) { + sorted.push_back(std::make_pair(&itCurr->first, &itCurr->second)); + } + std::sort(sorted.begin(), sorted.end(), [](const SortedListType::value_type& parA, const SortedListType::value_type& parB) { return *(parA.second) < *(parB.second); }); + for (SortedListType::const_iterator itCurr = sorted.begin(), itEND = sorted.end(); itCurr != itEND; ++itCurr) { + parCodes.push_back(itCurr->first); + parNames.push_back(itCurr->second); + } +} + ///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------- ErrBadLanguage::ErrBadLanguage (std::string&& parMessage) : diff --git a/src/WordReference.hpp b/src/WordReference.hpp index a879bb9..f2357a1 100644 --- a/src/WordReference.hpp +++ b/src/WordReference.hpp @@ -6,6 +6,8 @@ enum WordReferenceLangDirection { WordRefLangTo }; +void GetAvailableLanguages ( std::vector& parCodes, std::vector& parNames ); + class ErrBadLanguage : public std::runtime_error { public: ErrBadLanguage ( std::string&& parMessage );