From a5eb97229002d6a5f7bcc284e0452e35563a290e Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 22 Aug 2013 11:13:15 +0200 Subject: [PATCH] Translation finally added to the output --- wordref/src/WordReference.cpp | 27 ++++++++++++++++++++++----- wordref/src/main.cpp | 2 +- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/wordref/src/WordReference.cpp b/wordref/src/WordReference.cpp index 100409d..910c77d 100644 --- a/wordref/src/WordReference.cpp +++ b/wordref/src/WordReference.cpp @@ -62,17 +62,34 @@ namespace { return libjson::parse(libjson::to_json_string(jsonResponse)); } + ///----------------------------------------------------------------------- + ///----------------------------------------------------------------------- + void PrintSingloNodeIfNotEmpty (std::wostream& parStream, JSONNode::const_iterator parNode, const wchar_t* parName, const wchar_t* parPre, const wchar_t* parPost) { + JSONNode::const_iterator found(parNode->find(parName)); + if (found != parNode->end() and not found->as_string().empty()) + parStream << parPre << found->as_string() << parPost; + } + ///----------------------------------------------------------------------- ///----------------------------------------------------------------------- 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")); if (originalTerm != itTr->end()) { - JSONNode::const_iterator term(originalTerm->find(L"term")), - sense(originalTerm->find(L"sense")); - if (term != originalTerm->end() and sense != originalTerm->end()) { - parStream << originalTerm->find(L"term")->as_string(); - parStream << L": (" << originalTerm->find(L"sense")->as_string() << L")\n"; + + JSONNode::const_iterator term(originalTerm->find(L"term")); + if (term != originalTerm->end()) { + JSONNode::const_iterator firstTranslation = itTr->find(json_string(L"FirstTranslation")); + parStream << originalTerm->find(L"term")->as_string() << L": "; + PrintSingloNodeIfNotEmpty(parStream, originalTerm, L"POS", L"", L" "); + PrintSingloNodeIfNotEmpty(parStream, originalTerm, L"sense", L"(", L")"); + if (firstTranslation != itTr->end()) { + PrintSingloNodeIfNotEmpty(parStream, firstTranslation, L"term", L"\t- ", L""); + PrintSingloNodeIfNotEmpty(parStream, firstTranslation, L"POS", L" ", L""); + } + parStream << L"\n"; + PrintSingloNodeIfNotEmpty(parStream, originalTerm, L"usage", L"", L"\n"); + PrintSingloNodeIfNotEmpty(parStream, itTr, L"Note", L"Note: ", L"\n"); } } } diff --git a/wordref/src/main.cpp b/wordref/src/main.cpp index 36fcdb6..7f9a457 100644 --- a/wordref/src/main.cpp +++ b/wordref/src/main.cpp @@ -113,7 +113,7 @@ int main (int parArgc, const char* const parArgv[]) { WordReference wref(langFrom, langTo, DefApiKey); wref.Translate(searchWord, std::wcout); - std::wcout << wref.GetHttpLinkW(searchWord) << "\n"; + std::wcout << L"\n" << wref.GetHttpLinkW(searchWord) << L"\n"; } std::wcout << L"Written by King_DuckZ; © WordReference.com" << std::endl; return 0;