/*
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 .
*/
#include "main.hpp"
#include "WordReference.hpp"
#include
#include
namespace {
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));
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& 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) :
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();
}