Doing work to make the query print something useful, but this breaks the build. WiP

This commit is contained in:
King_DuckZ 2013-08-17 03:58:03 +02:00
parent fb49c84899
commit f861039851
4 changed files with 30 additions and 4 deletions

View file

@ -65,6 +65,22 @@ namespace {
std::string jsonResponse(http.GetPage(oss.str()));
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"));
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";
}
}
}
}
} //unnamed namespace
///-----------------------------------------------------------------------------
@ -153,6 +169,15 @@ std::string WordReference::GetApiVersion() {
///-----------------------------------------------------------------------------
///-----------------------------------------------------------------------------
void WordReference::Translate (const std::string& parWord) {
void WordReference::Translate (const std::string& parWord, std::wostream& parStream) {
JSONNode root = QueryJSon(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) {
JSONNode::const_iterator principTranslations(itCur->find(L"PrincipalTranslations"));
if (principTranslations != itCur->end()) {
PrintTranslation(parStream, *principTranslations);
}
}
}
}

View file

@ -42,7 +42,7 @@ public:
std::string GetHttpLink ( const char* parWord );
std::string GetHttpLink ( const std::string& parWord );
static std::string GetApiVersion ( void );
void Translate ( const std::string& parWord );
void Translate ( const std::string& parWord, std::wostream& parStream );
private:
std::string m_langFrom;

View file

@ -102,8 +102,8 @@ int main (int parArgc, const char* const parArgv[]) {
}
WordReference wref("en", "it", DefApiKey);
wref.Translate("house");
wref.Translate("house", std::wcout);
std::cout << wref.GetHttpLink("north face") << "\nWritten by King_DuckZ; © WordReference.com" << std::endl;
std::wcout << wref.GetHttpLink("north face") << L"\nWritten by King_DuckZ; © WordReference.com" << std::endl;
return 0;
}

View file

@ -24,5 +24,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <stdexcept>
#include <algorithm>
#include <memory>
#include <ostream>
#endif