From 3937847025eab9af4c35000232983c67f108e9ae Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sun, 11 Aug 2013 14:39:03 +0200 Subject: [PATCH] Added new file. --- CMakeLists.txt | 1 + WordReference.cpp | 84 +++++++++++++++++++++++++++++++++++++++++++++++ WordReference.hpp | 30 +++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 WordReference.cpp create mode 100644 WordReference.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ae3800e..eb53a08 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,6 +57,7 @@ add_dependencies(json build_json) add_executable(${PROJECT_NAME} main.cpp + WordReference.cpp ) target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES} diff --git a/WordReference.cpp b/WordReference.cpp new file mode 100644 index 0000000..91f6a6b --- /dev/null +++ b/WordReference.cpp @@ -0,0 +1,84 @@ +#include "main.hpp" +#include "WordReference.hpp" +#include +#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") + }; + + 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(); +} diff --git a/WordReference.hpp b/WordReference.hpp new file mode 100644 index 0000000..a879bb9 --- /dev/null +++ b/WordReference.hpp @@ -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