Added new file.

This commit is contained in:
King_DuckZ 2013-08-11 14:39:03 +02:00
parent 02e7aedc08
commit 3937847025
3 changed files with 115 additions and 0 deletions

View file

@ -57,6 +57,7 @@ add_dependencies(json build_json)
add_executable(${PROJECT_NAME} add_executable(${PROJECT_NAME}
main.cpp main.cpp
WordReference.cpp
) )
target_link_libraries(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME}
${Boost_LIBRARIES} ${Boost_LIBRARIES}

84
WordReference.cpp Normal file
View file

@ -0,0 +1,84 @@
#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();
}

30
WordReference.hpp Normal file
View file

@ -0,0 +1,30 @@
#ifndef id75A4E59ADE4649F7A4A56F91C5886405
#define id75A4E59ADE4649F7A4A56F91C5886405
enum WordReferenceLangDirection {
WordRefLangFrom,
WordRefLangTo
};
class ErrBadLanguage : public std::runtime_error {
public:
ErrBadLanguage ( std::string&& parMessage );
~ErrBadLanguage ( void ) noexcept = default;
};
class WordReference {
public:
WordReference ( const char* parFrom, const char* parTo );
~WordReference ( void ) = default;
const std::string& GetLanguageCode ( WordReferenceLangDirection parDir ) const;
const std::string& GetLanguageName ( WordReferenceLangDirection parDir ) const;
std::string GetHttpLink ( const char* parWord );
std::string GetHttpLink ( const std::string& parWord );
private:
std::string m_langFrom;
std::string m_langTo;
};
#endif