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
|
@ -20,6 +20,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
#include "HttpReader.hpp"
|
||||
#include <curl/curl.h>
|
||||
#include <sstream>
|
||||
#include <boost/algorithm/string/trim_all.hpp>
|
||||
#include <cstdlib>
|
||||
|
||||
namespace {
|
||||
///------------------------------------------------------------------------
|
||||
|
@ -32,6 +34,30 @@ namespace {
|
|||
that->append(parData, effectiveWrite);
|
||||
return effectiveWrite;
|
||||
}
|
||||
|
||||
std::string GetCleanWord (CURL* parCurl, std::wstring parWord) __attribute__((pure));
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
std::string GetCleanWord (CURL* parCurl, std::wstring parWord) {
|
||||
class CurlFree {
|
||||
public:
|
||||
CurlFree ( void ) noexcept = default;
|
||||
void operator() ( char* parDele ) const { curl_free(parDele); }
|
||||
};
|
||||
|
||||
boost::algorithm::trim_all(parWord);
|
||||
const size_t maxMultibSize = parWord.size() * 4 + 1;
|
||||
const std::unique_ptr<char[]> memForMultib(new char[maxMultibSize]);
|
||||
|
||||
const size_t wctombsRet = std::wcstombs(memForMultib.get(), parWord.c_str(), maxMultibSize);
|
||||
if (EILSEQ == wctombsRet)
|
||||
throw std::runtime_error("Can't convert received string to multibyte, wcstombs() failed");
|
||||
|
||||
const std::unique_ptr<char, CurlFree> urlEncoded(curl_easy_escape(parCurl, memForMultib.get(), static_cast<int>(wctombsRet)));
|
||||
std::string retVal(urlEncoded.get());
|
||||
return retVal;
|
||||
}
|
||||
} //unnamed namespace
|
||||
|
||||
struct HttpReader::LocalData {
|
||||
|
@ -84,3 +110,15 @@ std::string HttpReader::GetPage (const std::string& parAddress) const {
|
|||
}
|
||||
return localString;
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
std::string HttpReader::UrlEncode (const wchar_t* parWord) {
|
||||
return GetCleanWord(m_localData->curl, parWord);
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
std::string HttpReader::UrlEncode (const std::wstring& parWord) {
|
||||
return GetCleanWord(m_localData->curl, parWord);
|
||||
}
|
||||
|
|
|
@ -7,6 +7,8 @@ public:
|
|||
~HttpReader ( void ) noexcept;
|
||||
|
||||
std::string GetPage ( const std::string& parAddress ) const;
|
||||
std::string UrlEncode ( const wchar_t* parWord ) __attribute__((pure));
|
||||
std::string UrlEncode ( const std::wstring& parWord ) __attribute__((pure));
|
||||
|
||||
private:
|
||||
struct LocalData;
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -19,6 +19,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
#ifndef id75A4E59ADE4649F7A4A56F91C5886405
|
||||
#define id75A4E59ADE4649F7A4A56F91C5886405
|
||||
|
||||
class HttpReader;
|
||||
|
||||
enum WordReferenceLangDirection {
|
||||
WordRefLangFrom,
|
||||
WordRefLangTo
|
||||
|
@ -34,17 +36,19 @@ public:
|
|||
|
||||
class WordReference {
|
||||
public:
|
||||
WordReference ( void ) = delete;
|
||||
WordReference ( const char* parFrom, const char* parTo, const char* parApiKey );
|
||||
~WordReference ( void ) = default;
|
||||
~WordReference ( void );
|
||||
|
||||
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 );
|
||||
static std::string GetApiVersion ( void );
|
||||
void Translate ( const std::string& parWord, std::wostream& parStream );
|
||||
std::string GetHttpLink ( const wchar_t* parWord );
|
||||
std::string GetHttpLink ( const std::wstring& parWord );
|
||||
void Translate ( const std::wstring& parWord, std::wostream& parStream );
|
||||
|
||||
private:
|
||||
const std::unique_ptr<HttpReader> m_httpReader;
|
||||
std::string m_langFrom;
|
||||
std::string m_langTo;
|
||||
std::string m_apiKey;
|
||||
|
|
|
@ -102,8 +102,9 @@ int main (int parArgc, const char* const parArgv[]) {
|
|||
}
|
||||
|
||||
WordReference wref("en", "it", DefApiKey);
|
||||
wref.Translate("house", std::wcout);
|
||||
wref.Translate(L"house", std::wcout);
|
||||
|
||||
std::wcout << wref.GetHttpLink("north face") << L"\nWritten by King_DuckZ; © WordReference.com" << std::endl;
|
||||
std::cout << wref.GetHttpLink(L"north face") << "\n";
|
||||
std::wcout << L"Written by King_DuckZ; © WordReference.com" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue