Search URL now correctly appears: removed a std::cout that got mixed with std::wcout.
Also changed some comments that spanned over 79 characters.
This commit is contained in:
parent
402f90b5df
commit
3c334595c8
6 changed files with 99 additions and 55 deletions
|
@ -7,35 +7,35 @@
|
|||
#include <clocale>
|
||||
|
||||
namespace cconv {
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
ErrCantConvert::ErrCantConvert (const std::string& parFrom, const std::string& parTo) :
|
||||
std::domain_error(std::string("Can't convert from \"") + parFrom + "\" to \"" + parTo + "\"")
|
||||
{
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
ErrCantConvert::ErrCantConvert (const char* parFrom, const char* parTo) :
|
||||
std::domain_error(std::string("Can't convert from \"") + parFrom + "\" to \"" + parTo + "\"")
|
||||
{
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
std::wstring MultibyteToWide (const std::string& parMultiByte) {
|
||||
return MultibyteToWide(parMultiByte.c_str(), parMultiByte.size());
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
std::wstring MultibyteToWide (const char* parMultiByte) {
|
||||
return MultibyteToWide(parMultiByte, std::strlen(parMultiByte));
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
///See: http://www.lemoda.net/c/iconv-example/iconv-example.html
|
||||
///-------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
std::wstring MultibyteToWide (const char* parMultiByte, size_t parLen) {
|
||||
const size_t maxCount = parLen + 1;
|
||||
|
||||
|
@ -72,20 +72,20 @@ namespace cconv {
|
|||
return std::wstring(buff.get());
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
std::string WideToMultibyte (const std::wstring& parWide) {
|
||||
return WideToMultibyte(parWide.c_str(), parWide.size());
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
std::string WideToMultibyte (const wchar_t* parWide) {
|
||||
return WideToMultibyte(parWide, std::wcslen(parWide));
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
std::string WideToMultibyte (const wchar_t* parWide, size_t parLen) {
|
||||
const size_t maxMultibSize = parLen * 4 + 1;
|
||||
const std::unique_ptr<char[]> memForMultib(new char[maxMultibSize]);
|
||||
|
|
|
@ -37,8 +37,8 @@ namespace {
|
|||
|
||||
std::string GetCleanWord (CURL* parCurl, std::wstring parWord) __attribute__((pure));
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
std::string GetCleanWord (CURL* parCurl, std::wstring parWord) {
|
||||
class CurlFree {
|
||||
public:
|
||||
|
@ -105,20 +105,32 @@ std::string HttpReader::GetPage (const std::string& parAddress) const {
|
|||
return localString;
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
std::wstring HttpReader::GetPageW (const std::string& parAddress) const {
|
||||
return cconv::MultibyteToWide(this->GetPage(parAddress));
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
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);
|
||||
}
|
||||
|
||||
///----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
std::wstring HttpReader::UrlEncodeW (const wchar_t* parWord) {
|
||||
return cconv::MultibyteToWide(this->UrlEncode(parWord));
|
||||
}
|
||||
|
||||
///----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
std::wstring HttpReader::UrlEncodeW (const std::wstring& parWord) {
|
||||
return cconv::MultibyteToWide(this->UrlEncode(parWord));
|
||||
}
|
||||
|
|
|
@ -10,6 +10,8 @@ public:
|
|||
std::wstring GetPageW ( const std::string& parAddress ) const;
|
||||
std::string UrlEncode ( const wchar_t* parWord ) __attribute__((pure));
|
||||
std::string UrlEncode ( const std::wstring& parWord ) __attribute__((pure));
|
||||
std::wstring UrlEncodeW ( const wchar_t* parWord ) __attribute__((pure));
|
||||
std::wstring UrlEncodeW ( const std::wstring& parWord ) __attribute__((pure));
|
||||
|
||||
private:
|
||||
struct LocalData;
|
||||
|
|
|
@ -43,8 +43,16 @@ namespace {
|
|||
std::pair<std::string, std::string>("tr", "Turkish")
|
||||
};
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------
|
||||
std::wstring CastToWcharStr (const std::string& parIn) {
|
||||
std::wstring retVal;
|
||||
retVal.assign(parIn.begin(), parIn.end());
|
||||
return retVal;
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------
|
||||
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 << "/"
|
||||
|
@ -54,8 +62,8 @@ namespace {
|
|||
return libjson::parse(libjson::to_json_string(jsonResponse));
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------
|
||||
void PrintTranslation (std::wostream& parStream, const JSONNode& parNode) {
|
||||
for (JSONNode::const_iterator itTr = parNode.begin(), itTrEND = parNode.end(); itTr != itTrEND; ++itTr) {
|
||||
JSONNode::const_iterator originalTerm = itTr->find(json_string(L"OriginalTerm"));
|
||||
|
@ -71,8 +79,8 @@ namespace {
|
|||
}
|
||||
} //unnamed namespace
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
void GetAvailableLanguages (std::vector<const std::string*>& parCodes, std::vector<const std::string*>& parNames) {
|
||||
typedef std::vector<std::pair<const std::string*, const std::string*> > SortedListType;
|
||||
SortedListType sorted;
|
||||
|
@ -90,15 +98,15 @@ void GetAvailableLanguages (std::vector<const std::string*>& parCodes, std::vect
|
|||
}
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
ErrBadLanguage::ErrBadLanguage (std::string&& parMessage) :
|
||||
std::runtime_error(parMessage)
|
||||
{
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
WordReference::WordReference (const std::string& parFrom, const std::string& parTo, const char* parApiKey) :
|
||||
m_httpReader(new HttpReader),
|
||||
m_langFrom(parFrom),
|
||||
|
@ -117,13 +125,13 @@ WordReference::WordReference (const std::string& parFrom, const std::string& par
|
|||
}
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
WordReference::~WordReference() {
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
const std::string& WordReference::GetLanguageCode (WordReferenceLangDirection parDir) const {
|
||||
if (WordRefLangFrom == parDir)
|
||||
return m_langFrom;
|
||||
|
@ -131,20 +139,20 @@ const std::string& WordReference::GetLanguageCode (WordReferenceLangDirection pa
|
|||
return m_langTo;
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
const std::string& WordReference::GetLanguageName (WordReferenceLangDirection parDir) const {
|
||||
return SupportedLanguages.at(GetLanguageCode(parDir));
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
std::string WordReference::GetApiVersion() {
|
||||
return std::string(ApiVersion);
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
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) {
|
||||
|
@ -158,8 +166,8 @@ void WordReference::Translate (const std::wstring& parWord, std::wostream& parSt
|
|||
}
|
||||
}
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
std::string WordReference::GetHttpLink (const wchar_t* parWord) {
|
||||
std::ostringstream oss;
|
||||
oss << "http://www.wordreference.com/redirect/translation.aspx?w=";
|
||||
|
@ -167,11 +175,31 @@ std::string WordReference::GetHttpLink (const wchar_t* parWord) {
|
|||
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();
|
||||
}
|
||||
|
||||
///----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
std::wstring WordReference::GetHttpLinkW (const wchar_t* parWord) {
|
||||
std::wostringstream oss;
|
||||
oss << L"http://www.wordreference.com/redirect/translation.aspx?w=";
|
||||
oss << m_httpReader->UrlEncodeW(parWord) << L"&dict=";
|
||||
oss << CastToWcharStr(m_langFrom) << CastToWcharStr(m_langTo);
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
///----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
std::wstring WordReference::GetHttpLinkW (const std::wstring& parWord) {
|
||||
std::wostringstream oss;
|
||||
oss << L"http://www.wordreference.com/redirect/translation.aspx?w=";
|
||||
oss << m_httpReader->UrlEncodeW(parWord) << L"&dict=";
|
||||
oss << CastToWcharStr(m_langFrom) << CastToWcharStr(m_langTo);
|
||||
return oss.str();
|
||||
}
|
||||
|
|
|
@ -45,6 +45,8 @@ public:
|
|||
static std::string GetApiVersion ( void );
|
||||
std::string GetHttpLink ( const wchar_t* parWord );
|
||||
std::string GetHttpLink ( const std::wstring& parWord );
|
||||
std::wstring GetHttpLinkW ( const wchar_t* parWord );
|
||||
std::wstring GetHttpLinkW ( const std::wstring& parWord );
|
||||
void Translate ( const std::wstring& parWord, std::wostream& parStream );
|
||||
|
||||
private:
|
||||
|
|
|
@ -30,8 +30,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
namespace {
|
||||
const char* DefApiKey = "58327";
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
const char* GetBaseName (const char* parPath) {
|
||||
const size_t len = std::strlen(parPath);
|
||||
const char* found;
|
||||
|
@ -44,8 +44,8 @@ namespace {
|
|||
return found;
|
||||
}
|
||||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
bool GetCommandLine (boost::program_options::variables_map& parVarMap, int parArgc, const char* const parArgv[]) {
|
||||
const char* const programName = parArgv[0];
|
||||
std::ostringstream oss;
|
||||
|
@ -82,8 +82,8 @@ namespace {
|
|||
}
|
||||
} //unnamed namespace
|
||||
|
||||
///-----------------------------------------------------------------------------
|
||||
///-----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
int main (int parArgc, const char* const parArgv[]) {
|
||||
//std::setlocale(LC_CTYPE, "UTF-8");
|
||||
std::setlocale(LC_CTYPE, "");
|
||||
|
@ -113,7 +113,7 @@ int main (int parArgc, const char* const parArgv[]) {
|
|||
WordReference wref(langFrom, langTo, DefApiKey);
|
||||
wref.Translate(searchWord, std::wcout);
|
||||
|
||||
std::cout << wref.GetHttpLink(searchWord) << "\n";
|
||||
std::wcout << wref.GetHttpLinkW(searchWord) << "\n";
|
||||
}
|
||||
std::wcout << L"Written by King_DuckZ; © WordReference.com" << std::endl;
|
||||
return 0;
|
||||
|
|
Loading…
Reference in a new issue