Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// Simple example of using the GW2 API for C++ novices.
- /*!
- The example has been created in the most lazy way possible using
- copy/paste/modify from stackexchange.com and code.google.com/p/rapidjson/.
- The example has been tested with Visual Studio 2010 and Windows XP SP3.
- --------------------------------------------------------------------------------
- To play with the code:
- 1) Create a new Win32 Console Application and copy the code into the main
- project file.
- 2) Download rapidjson and add the rapidjson include files to your projects
- include path.
- 3) Compile and run
- */
- #include "stdafx.h"
- #include <Windows.h>
- #include <Wininet.h>
- #include <iostream>
- #include <vector>
- // Rapid json include files
- #include "document.h"
- using namespace rapidjson;
- void neterror(const char *id)
- {
- const DWORD error = GetLastError();
- std::cerr << id << ": " << error << "." << std::endl;
- }
- HINTERNET netstart ()
- {
- DWORD atype = INTERNET_OPEN_TYPE_DIRECT;
- const HINTERNET handle = InternetOpenW(0, atype, 0, 0, 0);
- if ( handle == 0 )
- neterror("InternetOpen()");
- return (handle);
- }
- void netstop ( HINTERNET object )
- {
- const BOOL result = InternetCloseHandle(object);
- if ( result == FALSE )
- neterror("InternetCloseHandle()");
- }
- void (*conclose)( HINTERNET ) = netstop;
- HINTERNET conopen ( HINTERNET session, LPCWSTR url )
- {
- const HINTERNET handle =
- InternetOpenUrlW(session, url, 0, 0, 0, 0);
- if ( handle == 0 )
- neterror("InternetOpenUrl()");
- return (handle);
- }
- int netfetch ( HINTERNET handle, std::vector<char>& vect )
- {
- static const DWORD SIZE = BUFSIZ * 10;
- DWORD bytes_read = 0;
- do {
- const DWORD old_size = vect.size();
- // todo: we need to make sure that the allocation succeeds. we don't like exceptions.
- // todo: we need to make sure that the old_size + SIZE is less than max_size.
- vect.resize(old_size + SIZE);
- BOOL result = InternetReadFile(handle, &vect[old_size], SIZE, &bytes_read);
- if ( result == FALSE )
- {
- neterror("InternetReadFile()");
- return EXIT_FAILURE;
- }
- vect.resize(old_size + bytes_read);
- }
- while (bytes_read > 0);
- return EXIT_SUCCESS;
- }
- int check_object(const Value &v, Value::ConstMemberIterator& itr, SizeType i) {
- if(itr == v.MemberEnd()) {
- std::cerr << "Error: Array element " << i << " is an empty object." << std::endl;
- return (0);
- }
- if(!itr->value.IsString()) {
- std::cerr << "Error: Array element " << i << " has an object with a value that is not a string." << std::endl;
- return (0);
- }
- return (1);
- }
- // Fetch an url and write the results to standard output
- int dowork(HINTERNET session)
- {
- //const WCHAR URL[] = L"https://render.guildwars2.com/file/02EFB1C5E11B2FF4B4AC25A84E2302D244C82AA3/66958.png";
- //const WCHAR URL[] = L"https://api.guildwars2.com/v1/event_names.json";
- const WCHAR URL[] = L"https://api.guildwars2.com/v1/world_names.json";
- const HINTERNET connection = conopen(session, URL);
- if ( connection == 0 )
- return (EXIT_FAILURE);
- std::vector<char> vect;
- // Read the data and nul-terminate the data.
- int res = netfetch(connection, vect);
- vect.push_back(0);
- if(res == EXIT_SUCCESS)
- {
- Document d;
- d.Parse<0>(vect.data());
- if(d.IsArray()) {
- for (SizeType i = 0; i < d.Size(); i++) { // rapidjson uses SizeType instead of size_t.
- const Value &v = d[i];
- if(v.IsObject()) {
- Value::ConstMemberIterator itr = v.MemberBegin();
- if(!check_object(v, itr, i))
- break;
- std::cout << itr->name.GetString() << " = " << itr->value.GetString();
- ++itr;
- if(!check_object(v, itr, i))
- break;
- std::cout << itr->name.GetString() << " = " << itr->value.GetString();
- } else {
- std::cerr << "Error: Array element " << i << " is not an object." << std::endl;
- break;
- }
- }
- } else
- std::cerr << "Error: JSON returned is not an array." << std::endl;
- }
- conclose(connection);
- return (res);
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- const HINTERNET session = netstart();
- if ( session == 0 )
- return (EXIT_FAILURE);
- int res = dowork(session);
- netstop(session);
- return (res);
- }
- #pragma comment ( lib, "Wininet.lib" )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement