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:
King_DuckZ 2013-08-21 16:02:39 +02:00
parent 402f90b5df
commit 3c334595c8
6 changed files with 99 additions and 55 deletions

View file

@ -7,35 +7,35 @@
#include <clocale> #include <clocale>
namespace cconv { namespace cconv {
///------------------------------------------------------------------------- ///------------------------------------------------------------------------
///------------------------------------------------------------------------- ///------------------------------------------------------------------------
ErrCantConvert::ErrCantConvert (const std::string& parFrom, const std::string& parTo) : ErrCantConvert::ErrCantConvert (const std::string& parFrom, const std::string& parTo) :
std::domain_error(std::string("Can't convert from \"") + parFrom + "\" to \"" + parTo + "\"") std::domain_error(std::string("Can't convert from \"") + parFrom + "\" to \"" + parTo + "\"")
{ {
} }
///------------------------------------------------------------------------- ///------------------------------------------------------------------------
///------------------------------------------------------------------------- ///------------------------------------------------------------------------
ErrCantConvert::ErrCantConvert (const char* parFrom, const char* parTo) : ErrCantConvert::ErrCantConvert (const char* parFrom, const char* parTo) :
std::domain_error(std::string("Can't convert from \"") + parFrom + "\" to \"" + parTo + "\"") std::domain_error(std::string("Can't convert from \"") + parFrom + "\" to \"" + parTo + "\"")
{ {
} }
///------------------------------------------------------------------------- ///------------------------------------------------------------------------
///------------------------------------------------------------------------- ///------------------------------------------------------------------------
std::wstring MultibyteToWide (const std::string& parMultiByte) { std::wstring MultibyteToWide (const std::string& parMultiByte) {
return MultibyteToWide(parMultiByte.c_str(), parMultiByte.size()); return MultibyteToWide(parMultiByte.c_str(), parMultiByte.size());
} }
///------------------------------------------------------------------------- ///------------------------------------------------------------------------
///------------------------------------------------------------------------- ///------------------------------------------------------------------------
std::wstring MultibyteToWide (const char* parMultiByte) { std::wstring MultibyteToWide (const char* parMultiByte) {
return MultibyteToWide(parMultiByte, std::strlen(parMultiByte)); return MultibyteToWide(parMultiByte, std::strlen(parMultiByte));
} }
///------------------------------------------------------------------------- ///------------------------------------------------------------------------
///See: http://www.lemoda.net/c/iconv-example/iconv-example.html ///See: http://www.lemoda.net/c/iconv-example/iconv-example.html
///------------------------------------------------------------------------- ///------------------------------------------------------------------------
std::wstring MultibyteToWide (const char* parMultiByte, size_t parLen) { std::wstring MultibyteToWide (const char* parMultiByte, size_t parLen) {
const size_t maxCount = parLen + 1; const size_t maxCount = parLen + 1;
@ -72,20 +72,20 @@ namespace cconv {
return std::wstring(buff.get()); return std::wstring(buff.get());
} }
///------------------------------------------------------------------------- ///------------------------------------------------------------------------
///------------------------------------------------------------------------- ///------------------------------------------------------------------------
std::string WideToMultibyte (const std::wstring& parWide) { std::string WideToMultibyte (const std::wstring& parWide) {
return WideToMultibyte(parWide.c_str(), parWide.size()); return WideToMultibyte(parWide.c_str(), parWide.size());
} }
///------------------------------------------------------------------------- ///------------------------------------------------------------------------
///------------------------------------------------------------------------- ///------------------------------------------------------------------------
std::string WideToMultibyte (const wchar_t* parWide) { std::string WideToMultibyte (const wchar_t* parWide) {
return WideToMultibyte(parWide, std::wcslen(parWide)); return WideToMultibyte(parWide, std::wcslen(parWide));
} }
///------------------------------------------------------------------------- ///------------------------------------------------------------------------
///------------------------------------------------------------------------- ///------------------------------------------------------------------------
std::string WideToMultibyte (const wchar_t* parWide, size_t parLen) { std::string WideToMultibyte (const wchar_t* parWide, size_t parLen) {
const size_t maxMultibSize = parLen * 4 + 1; const size_t maxMultibSize = parLen * 4 + 1;
const std::unique_ptr<char[]> memForMultib(new char[maxMultibSize]); const std::unique_ptr<char[]> memForMultib(new char[maxMultibSize]);

View file

@ -37,8 +37,8 @@ namespace {
std::string GetCleanWord (CURL* parCurl, std::wstring parWord) __attribute__((pure)); std::string GetCleanWord (CURL* parCurl, std::wstring parWord) __attribute__((pure));
///------------------------------------------------------------------------- ///------------------------------------------------------------------------
///------------------------------------------------------------------------- ///------------------------------------------------------------------------
std::string GetCleanWord (CURL* parCurl, std::wstring parWord) { std::string GetCleanWord (CURL* parCurl, std::wstring parWord) {
class CurlFree { class CurlFree {
public: public:
@ -105,20 +105,32 @@ std::string HttpReader::GetPage (const std::string& parAddress) const {
return localString; return localString;
} }
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
std::wstring HttpReader::GetPageW (const std::string& parAddress) const { std::wstring HttpReader::GetPageW (const std::string& parAddress) const {
return cconv::MultibyteToWide(this->GetPage(parAddress)); return cconv::MultibyteToWide(this->GetPage(parAddress));
} }
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
std::string HttpReader::UrlEncode (const wchar_t* parWord) { std::string HttpReader::UrlEncode (const wchar_t* parWord) {
return GetCleanWord(m_localData->curl, parWord); return GetCleanWord(m_localData->curl, parWord);
} }
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
std::string HttpReader::UrlEncode (const std::wstring& parWord) { std::string HttpReader::UrlEncode (const std::wstring& parWord) {
return GetCleanWord(m_localData->curl, 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));
}

View file

@ -10,6 +10,8 @@ public:
std::wstring GetPageW ( const std::string& parAddress ) const; std::wstring GetPageW ( const std::string& parAddress ) const;
std::string UrlEncode ( const wchar_t* parWord ) __attribute__((pure)); std::string UrlEncode ( const wchar_t* parWord ) __attribute__((pure));
std::string UrlEncode ( const std::wstring& 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: private:
struct LocalData; struct LocalData;

View file

@ -43,8 +43,16 @@ namespace {
std::pair<std::string, std::string>("tr", "Turkish") 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) { JSONNode QueryJSon (HttpReader& parHttp, const std::string& parFrom, const std::string& parTo, const std::string& parKey, const std::wstring& parWord) {
std::ostringstream oss; std::ostringstream oss;
oss << "http://api.wordreference.com/" << ApiVersion << "/" oss << "http://api.wordreference.com/" << ApiVersion << "/"
@ -54,8 +62,8 @@ namespace {
return libjson::parse(libjson::to_json_string(jsonResponse)); return libjson::parse(libjson::to_json_string(jsonResponse));
} }
///------------------------------------------------------------------------- ///-----------------------------------------------------------------------
///------------------------------------------------------------------------- ///-----------------------------------------------------------------------
void PrintTranslation (std::wostream& parStream, const JSONNode& parNode) { void PrintTranslation (std::wostream& parStream, const JSONNode& parNode) {
for (JSONNode::const_iterator itTr = parNode.begin(), itTrEND = parNode.end(); itTr != itTrEND; ++itTr) { for (JSONNode::const_iterator itTr = parNode.begin(), itTrEND = parNode.end(); itTr != itTrEND; ++itTr) {
JSONNode::const_iterator originalTerm = itTr->find(json_string(L"OriginalTerm")); JSONNode::const_iterator originalTerm = itTr->find(json_string(L"OriginalTerm"));
@ -71,8 +79,8 @@ namespace {
} }
} //unnamed namespace } //unnamed namespace
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
void GetAvailableLanguages (std::vector<const std::string*>& parCodes, std::vector<const std::string*>& parNames) { 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; typedef std::vector<std::pair<const std::string*, const std::string*> > SortedListType;
SortedListType sorted; SortedListType sorted;
@ -90,15 +98,15 @@ void GetAvailableLanguages (std::vector<const std::string*>& parCodes, std::vect
} }
} }
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
ErrBadLanguage::ErrBadLanguage (std::string&& parMessage) : ErrBadLanguage::ErrBadLanguage (std::string&& parMessage) :
std::runtime_error(parMessage) std::runtime_error(parMessage)
{ {
} }
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
WordReference::WordReference (const std::string& parFrom, const std::string& parTo, const char* parApiKey) : WordReference::WordReference (const std::string& parFrom, const std::string& parTo, const char* parApiKey) :
m_httpReader(new HttpReader), m_httpReader(new HttpReader),
m_langFrom(parFrom), m_langFrom(parFrom),
@ -117,13 +125,13 @@ WordReference::WordReference (const std::string& parFrom, const std::string& par
} }
} }
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
WordReference::~WordReference() { WordReference::~WordReference() {
} }
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
const std::string& WordReference::GetLanguageCode (WordReferenceLangDirection parDir) const { const std::string& WordReference::GetLanguageCode (WordReferenceLangDirection parDir) const {
if (WordRefLangFrom == parDir) if (WordRefLangFrom == parDir)
return m_langFrom; return m_langFrom;
@ -131,20 +139,20 @@ const std::string& WordReference::GetLanguageCode (WordReferenceLangDirection pa
return m_langTo; return m_langTo;
} }
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
const std::string& WordReference::GetLanguageName (WordReferenceLangDirection parDir) const { const std::string& WordReference::GetLanguageName (WordReferenceLangDirection parDir) const {
return SupportedLanguages.at(GetLanguageCode(parDir)); return SupportedLanguages.at(GetLanguageCode(parDir));
} }
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
std::string WordReference::GetApiVersion() { std::string WordReference::GetApiVersion() {
return std::string(ApiVersion); return std::string(ApiVersion);
} }
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
void WordReference::Translate (const std::wstring& parWord, std::wostream& parStream) { void WordReference::Translate (const std::wstring& parWord, std::wostream& parStream) {
JSONNode root = QueryJSon(*m_httpReader, m_langFrom, m_langTo, m_apiKey, parWord); 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) { 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::string WordReference::GetHttpLink (const wchar_t* parWord) {
std::ostringstream oss; std::ostringstream oss;
oss << "http://www.wordreference.com/redirect/translation.aspx?w="; oss << "http://www.wordreference.com/redirect/translation.aspx?w=";
@ -167,11 +175,31 @@ std::string WordReference::GetHttpLink (const wchar_t* parWord) {
return oss.str(); return oss.str();
} }
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
std::string WordReference::GetHttpLink (const std::wstring& parWord) { std::string WordReference::GetHttpLink (const std::wstring& parWord) {
std::ostringstream oss; std::ostringstream oss;
oss << "http://www.wordreference.com/redirect/translation.aspx?w="; oss << "http://www.wordreference.com/redirect/translation.aspx?w=";
oss << m_httpReader->UrlEncode(parWord) << "&dict=" << m_langFrom << m_langTo; oss << m_httpReader->UrlEncode(parWord) << "&dict=" << m_langFrom << m_langTo;
return oss.str(); 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();
}

View file

@ -45,6 +45,8 @@ public:
static std::string GetApiVersion ( void ); static std::string GetApiVersion ( void );
std::string GetHttpLink ( const wchar_t* parWord ); std::string GetHttpLink ( const wchar_t* parWord );
std::string GetHttpLink ( const std::wstring& 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 ); void Translate ( const std::wstring& parWord, std::wostream& parStream );
private: private:

View file

@ -30,8 +30,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
namespace { namespace {
const char* DefApiKey = "58327"; const char* DefApiKey = "58327";
///------------------------------------------------------------------------- ///------------------------------------------------------------------------
///------------------------------------------------------------------------- ///------------------------------------------------------------------------
const char* GetBaseName (const char* parPath) { const char* GetBaseName (const char* parPath) {
const size_t len = std::strlen(parPath); const size_t len = std::strlen(parPath);
const char* found; const char* found;
@ -44,8 +44,8 @@ namespace {
return found; return found;
} }
///------------------------------------------------------------------------- ///------------------------------------------------------------------------
///------------------------------------------------------------------------- ///------------------------------------------------------------------------
bool GetCommandLine (boost::program_options::variables_map& parVarMap, int parArgc, const char* const parArgv[]) { bool GetCommandLine (boost::program_options::variables_map& parVarMap, int parArgc, const char* const parArgv[]) {
const char* const programName = parArgv[0]; const char* const programName = parArgv[0];
std::ostringstream oss; std::ostringstream oss;
@ -82,8 +82,8 @@ namespace {
} }
} //unnamed namespace } //unnamed namespace
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
///----------------------------------------------------------------------------- ///----------------------------------------------------------------------------
int main (int parArgc, const char* const parArgv[]) { int main (int parArgc, const char* const parArgv[]) {
//std::setlocale(LC_CTYPE, "UTF-8"); //std::setlocale(LC_CTYPE, "UTF-8");
std::setlocale(LC_CTYPE, ""); std::setlocale(LC_CTYPE, "");
@ -113,7 +113,7 @@ int main (int parArgc, const char* const parArgv[]) {
WordReference wref(langFrom, langTo, DefApiKey); WordReference wref(langFrom, langTo, DefApiKey);
wref.Translate(searchWord, std::wcout); 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; std::wcout << L"Written by King_DuckZ; © WordReference.com" << std::endl;
return 0; return 0;