122 lines
5.2 KiB
C++
122 lines
5.2 KiB
C++
/*
|
|
WordReferenceCLI, a c++ program to access WordReference.com.
|
|
Copyright (C) 2013 King_DuckZ
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "main.hpp"
|
|
#include "WordReference.hpp"
|
|
#include <sstream>
|
|
#include <boost/algorithm/string/trim_all.hpp>
|
|
|
|
namespace {
|
|
typedef std::map<std::string, std::string> LanguageMapType;
|
|
const LanguageMapType SupportedLanguages {
|
|
std::pair<std::string, std::string>("ar", "Arabic"),
|
|
std::pair<std::string, std::string>("zh", "Chinese"),
|
|
std::pair<std::string, std::string>("cz", "Czech"),
|
|
std::pair<std::string, std::string>("en", "English"),
|
|
std::pair<std::string, std::string>("fr", "French"),
|
|
std::pair<std::string, std::string>("gr", "Greek"),
|
|
std::pair<std::string, std::string>("it", "Italian"),
|
|
std::pair<std::string, std::string>("ja", "Japanese"),
|
|
std::pair<std::string, std::string>("ko", "Korean"),
|
|
std::pair<std::string, std::string>("pl", "Polish"),
|
|
std::pair<std::string, std::string>("pt", "Portuguese"),
|
|
std::pair<std::string, std::string>("ro", "Romanian"),
|
|
std::pair<std::string, std::string>("es", "Spanish"),
|
|
std::pair<std::string, std::string>("tr", "Turkish")
|
|
};
|
|
|
|
std::string GetCleanWord (std::string parWord) __attribute__((pure));
|
|
|
|
std::string GetCleanWord (std::string parWord) {
|
|
boost::algorithm::trim_all(parWord);
|
|
std::replace(parWord.begin(), parWord.end(), ' ', '+');
|
|
return parWord;
|
|
}
|
|
} //unnamed namespace
|
|
|
|
///-----------------------------------------------------------------------------
|
|
///-----------------------------------------------------------------------------
|
|
void GetAvailableLanguages (std::vector<const std::string*>& parCodes, std::vector<const std::string*>& parNames) {
|
|
typedef std::vector<std::pair<const std::string*, const std::string*> > 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) :
|
|
std::runtime_error(parMessage)
|
|
{
|
|
}
|
|
|
|
///-----------------------------------------------------------------------------
|
|
///-----------------------------------------------------------------------------
|
|
WordReference::WordReference (const char* parFrom, const char* parTo) :
|
|
m_langFrom(parFrom),
|
|
m_langTo(parTo)
|
|
{
|
|
if (SupportedLanguages.find(parFrom) == SupportedLanguages.end()) {
|
|
std::ostringstream oss;
|
|
oss << "Invalid source language: \"" << m_langFrom << "\"";
|
|
throw ErrBadLanguage(oss.str());
|
|
}
|
|
if (SupportedLanguages.find(parTo) == SupportedLanguages.end()) {
|
|
std::ostringstream oss;
|
|
oss << "Invalid destination language: \"" << m_langTo << "\"";
|
|
throw ErrBadLanguage(oss.str());
|
|
}
|
|
}
|
|
|
|
///-----------------------------------------------------------------------------
|
|
///-----------------------------------------------------------------------------
|
|
const std::string& WordReference::GetLanguageCode (WordReferenceLangDirection parDir) const {
|
|
}
|
|
|
|
///-----------------------------------------------------------------------------
|
|
///-----------------------------------------------------------------------------
|
|
const std::string& WordReference::GetLanguageName (WordReferenceLangDirection parDir) const {
|
|
}
|
|
|
|
///-----------------------------------------------------------------------------
|
|
///-----------------------------------------------------------------------------
|
|
std::string WordReference::GetHttpLink (const char* parWord) {
|
|
std::ostringstream oss;
|
|
oss << "http://www.wordreference.com/redirect/translation.aspx?w=";
|
|
oss << GetCleanWord(parWord) << "&dict=" << m_langFrom << m_langTo;
|
|
return oss.str();
|
|
}
|
|
|
|
///-----------------------------------------------------------------------------
|
|
///-----------------------------------------------------------------------------
|
|
std::string WordReference::GetHttpLink (const std::string& parWord) {
|
|
std::ostringstream oss;
|
|
oss << "http://www.wordreference.com/redirect/translation.aspx?w=";
|
|
oss << GetCleanWord(parWord) << "&dict=" << m_langFrom << m_langTo;
|
|
return oss.str();
|
|
}
|