Advertisement
Guest User

Simple example of using the GW2 API for C++ novices.

a guest
Nov 17th, 2013
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.17 KB | None | 0 0
  1. /// Simple example of using the GW2 API for C++ novices.
  2. /*!
  3.   The example has been created in the most lazy way possible using
  4.   copy/paste/modify from stackexchange.com and code.google.com/p/rapidjson/.
  5.  
  6.   The example has been tested with Visual Studio 2010 and Windows XP SP3.
  7. --------------------------------------------------------------------------------
  8.   To play with the code:
  9.  
  10.   1) Create a new Win32 Console Application and copy the code into the main
  11.      project file.
  12.   2) Download rapidjson and add the rapidjson include files to your projects
  13.      include path.
  14.  
  15.   3) Compile and run
  16.  */
  17.  
  18. #include "stdafx.h"
  19.  
  20. #include <Windows.h>
  21. #include <Wininet.h>
  22. #include <iostream>
  23. #include <vector>
  24.  
  25. // Rapid json include files
  26. #include "document.h"
  27.  
  28. using namespace rapidjson;
  29.  
  30. void neterror(const char *id)
  31. {
  32.     const DWORD error = GetLastError();
  33.     std::cerr << id << ": " << error << "." << std::endl;
  34. }
  35.  
  36. HINTERNET netstart ()
  37. {
  38.     DWORD atype = INTERNET_OPEN_TYPE_DIRECT;
  39.     const HINTERNET handle = InternetOpenW(0, atype, 0, 0, 0);
  40.     if ( handle == 0 )
  41.         neterror("InternetOpen()");
  42.     return (handle);
  43. }
  44.  
  45. void netstop ( HINTERNET object )
  46. {
  47.     const BOOL result = InternetCloseHandle(object);
  48.     if ( result == FALSE )
  49.         neterror("InternetCloseHandle()");
  50. }
  51.  
  52. void (*conclose)( HINTERNET ) = netstop;
  53.  
  54. HINTERNET conopen ( HINTERNET session, LPCWSTR url )
  55. {
  56.     const HINTERNET handle =
  57.         InternetOpenUrlW(session, url, 0, 0, 0, 0);
  58.     if ( handle == 0 )
  59.         neterror("InternetOpenUrl()");
  60.     return (handle);
  61. }
  62.  
  63. int netfetch ( HINTERNET handle, std::vector<char>& vect )
  64. {
  65.     static const DWORD SIZE = BUFSIZ * 10;
  66.     DWORD bytes_read = 0;
  67.     do {
  68.         const DWORD old_size = vect.size();
  69.         // todo: we need to make sure that the allocation succeeds. we don't like exceptions.
  70.         // todo: we need to make sure that the old_size + SIZE is less than max_size.
  71.         vect.resize(old_size + SIZE);
  72.         BOOL result = InternetReadFile(handle, &vect[old_size], SIZE, &bytes_read);
  73.         if ( result == FALSE )
  74.         {
  75.             neterror("InternetReadFile()");
  76.             return EXIT_FAILURE;
  77.         }
  78.         vect.resize(old_size + bytes_read);
  79.     }
  80.     while (bytes_read > 0);
  81.     return EXIT_SUCCESS;
  82. }
  83.  
  84.  
  85. int check_object(const Value &v, Value::ConstMemberIterator& itr, SizeType i) {
  86.     if(itr == v.MemberEnd()) {
  87.         std::cerr << "Error: Array element " << i << " is an empty object." << std::endl;
  88.         return (0);
  89.     }
  90.     if(!itr->value.IsString()) {
  91.         std::cerr << "Error: Array element " << i << " has an object with a value that is not a string." << std::endl;
  92.         return (0);
  93.     }
  94.     return (1);
  95. }
  96.  
  97. // Fetch an url and write the results to standard output
  98. int dowork(HINTERNET session)
  99. {
  100.     //const WCHAR URL[] = L"https://render.guildwars2.com/file/02EFB1C5E11B2FF4B4AC25A84E2302D244C82AA3/66958.png";
  101.     //const WCHAR URL[] = L"https://api.guildwars2.com/v1/event_names.json";
  102.     const WCHAR URL[] = L"https://api.guildwars2.com/v1/world_names.json";
  103.  
  104.     const HINTERNET connection = conopen(session, URL);
  105.     if ( connection == 0 )
  106.         return (EXIT_FAILURE);
  107.     std::vector<char> vect;
  108.  
  109.     // Read the data and nul-terminate the data.
  110.     int res = netfetch(connection, vect);
  111.     vect.push_back(0);
  112.  
  113.     if(res == EXIT_SUCCESS)
  114.     {
  115.         Document d;
  116.         d.Parse<0>(vect.data());
  117.         if(d.IsArray()) {
  118.             for (SizeType i = 0; i < d.Size(); i++) { // rapidjson uses SizeType instead of size_t.
  119.                 const Value &v = d[i];
  120.                 if(v.IsObject()) {
  121.                     Value::ConstMemberIterator itr = v.MemberBegin();
  122.                     if(!check_object(v, itr, i))
  123.                         break;
  124.                     std::cout << itr->name.GetString() << " = " << itr->value.GetString();
  125.                     ++itr;
  126.                     if(!check_object(v, itr, i))
  127.                         break;
  128.                     std::cout << itr->name.GetString() << " = " << itr->value.GetString();
  129.                 } else {
  130.                     std::cerr << "Error: Array element " << i << " is not an object." << std::endl;
  131.                     break;
  132.                 }
  133.             }
  134.         } else
  135.             std::cerr << "Error: JSON returned is not an array." << std::endl;
  136.     }
  137.     conclose(connection);
  138.     return (res);
  139. }
  140.  
  141. int _tmain(int argc, _TCHAR* argv[])
  142. {
  143.     const HINTERNET session = netstart();
  144.     if ( session == 0 )
  145.         return (EXIT_FAILURE);
  146.     int res = dowork(session);
  147.     netstop(session);
  148.     return (res);
  149. }
  150.  
  151. #pragma comment ( lib, "Wininet.lib" )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement