Text converted to wchar_t where necessary
This commit is contained in:
parent
f861039851
commit
2299247e77
5 changed files with 80 additions and 41 deletions
|
@ -21,7 +21,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
#include "HttpReader.hpp"
|
||||
#include "libjson.h"
|
||||
#include <sstream>
|
||||
#include <boost/algorithm/string/trim_all.hpp>
|
||||
|
||||
namespace {
|
||||
const char* ApiVersion = "0.8";
|
||||
|
@ -44,25 +43,14 @@ namespace {
|
|||
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;
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
JSONNode QueryJSon (const std::string& parFrom, const std::string& parTo, const std::string& parKey, const std::string& parWord) {
|
||||
JSONNode QueryJSon (HttpReader& parHttp, const std::string& parFrom, const std::string& parTo, const std::string& parKey, const std::wstring& parWord) {
|
||||
std::ostringstream oss;
|
||||
oss << "http://api.wordreference.com/" << ApiVersion << "/"
|
||||
<< parKey << "/json/" << parFrom << parTo << "/" << GetCleanWord(parWord);
|
||||
<< parKey << "/json/" << parFrom << parTo << "/" << parHttp.UrlEncode(parWord);
|
||||
|
||||
HttpReader http;
|
||||
std::string jsonResponse(http.GetPage(oss.str()));
|
||||
std::string jsonResponse(parHttp.GetPage(oss.str()));
|
||||
return libjson::parse(libjson::to_json_string(jsonResponse));
|
||||
}
|
||||
|
||||
|
@ -112,6 +100,7 @@ ErrBadLanguage::ErrBadLanguage (std::string&& parMessage) :
|
|||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
WordReference::WordReference (const char* parFrom, const char* parTo, const char* parApiKey) :
|
||||
m_httpReader(new HttpReader),
|
||||
m_langFrom(parFrom),
|
||||
m_langTo(parTo),
|
||||
m_apiKey(parApiKey)
|
||||
|
@ -128,6 +117,11 @@ WordReference::WordReference (const char* parFrom, const char* parTo, const char
|
|||
}
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
WordReference::~WordReference() {
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
const std::string& WordReference::GetLanguageCode (WordReferenceLangDirection parDir) const {
|
||||
|
@ -143,24 +137,6 @@ const std::string& WordReference::GetLanguageName (WordReferenceLangDirection pa
|
|||
return SupportedLanguages.at(GetLanguageCode(parDir));
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
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();
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
std::string WordReference::GetApiVersion() {
|
||||
|
@ -169,8 +145,8 @@ std::string WordReference::GetApiVersion() {
|
|||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
void WordReference::Translate (const std::string& parWord, std::wostream& parStream) {
|
||||
JSONNode root = QueryJSon(m_langFrom, m_langTo, m_apiKey, parWord);
|
||||
void WordReference::Translate (const std::wstring& parWord, std::wostream& parStream) {
|
||||
JSONNode root = QueryJSon(*m_httpReader, m_langFrom, m_langTo, m_apiKey, parWord);
|
||||
for (JSONNode::const_iterator itCur = root.begin(), itCurEND = root.end(); itCur != itCurEND; ++itCur) {
|
||||
const std::wstring nodeName(libjson::to_std_wstring(itCur->name()));
|
||||
if (itCur->type() == JSON_NODE and nodeName.compare(0, 4, L"term") == 0) {
|
||||
|
@ -181,3 +157,21 @@ void WordReference::Translate (const std::string& parWord, std::wostream& parStr
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
std::string WordReference::GetHttpLink (const wchar_t* parWord) {
|
||||
std::ostringstream oss;
|
||||
oss << "http://www.wordreference.com/redirect/translation.aspx?w=";
|
||||
oss << m_httpReader->UrlEncode(parWord) << "&dict=" << m_langFrom << m_langTo;
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
std::string WordReference::GetHttpLink (const std::wstring& parWord) {
|
||||
std::ostringstream oss;
|
||||
oss << "http://www.wordreference.com/redirect/translation.aspx?w=";
|
||||
oss << m_httpReader->UrlEncode(parWord) << "&dict=" << m_langFrom << m_langTo;
|
||||
return oss.str();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue