wordreference/WordReference.cpp
2013-08-11 14:39:03 +02:00

84 lines
3.5 KiB
C++

#include "main.hpp"
#include "WordReference.hpp"
#include <sstream>
#include <boost/algorithm/string/trim_all.hpp>
namespace {
std::map<std::string, std::string> SupportedLanguages {
std::make_pair<std::string, std::string>("ar", "Arabic"),
std::make_pair<std::string, std::string>("zh", "Chinese"),
std::make_pair<std::string, std::string>("cz", "Czech"),
std::make_pair<std::string, std::string>("en", "English"),
std::make_pair<std::string, std::string>("fr", "French"),
std::make_pair<std::string, std::string>("gr", "Greek"),
std::make_pair<std::string, std::string>("it", "Italian"),
std::make_pair<std::string, std::string>("ja", "Japanese"),
std::make_pair<std::string, std::string>("ko", "Korean"),
std::make_pair<std::string, std::string>("pl", "Polish"),
std::make_pair<std::string, std::string>("pt", "Portuguese"),
std::make_pair<std::string, std::string>("ro", "Romanian"),
std::make_pair<std::string, std::string>("es", "Spanish"),
std::make_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
///-----------------------------------------------------------------------------
///-----------------------------------------------------------------------------
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();
}