Joel Grunbaum
2022-01-12 ae7d00edf2f02912ec9c04919ea7535c93d2d1e0
commit | author | age
0594ac 1 #include "bom.hpp"
JG 2
3 #include "book.hpp"
ae7d00 4 #include "rapidxml/rapidxml.hpp"
0594ac 5 #include <cstddef>
JG 6 #include <cstdio>
7 #include <curl/curl.h>
ae7d00 8 #include <fstream>
JG 9 #include <iterator>
0594ac 10 #include <stdio.h>
JG 11 #include <string>
12 #include <unordered_map>
ae7d00 13 #include <vector>
0594ac 14
JG 15 namespace bom
16 {
ae7d00 17 CURL* curl{NULL};
JG 18 std::string result;
0594ac 19
ae7d00 20 static size_t my_write(void* buffer, std::size_t size, std::size_t nmemb,
JG 21                        void* stream)
0594ac 22 {
ae7d00 23     std::string& text = *static_cast<std::string*>(stream);
JG 24     std::size_t totalsize = size * nmemb;
25     text.append(static_cast<char*>(buffer), totalsize);
26     return totalsize;
0594ac 27 }
ae7d00 28
JG 29 void initialise()
0594ac 30 {
JG 31     curl_global_init(CURL_GLOBAL_DEFAULT);
32     curl = curl_easy_init();
33     if (curl) {
34         curl_easy_setopt(curl, CURLOPT_URL,
35                          "ftp://ftp.bom.gov.au/anon/gen/fwo/IDN60920.xml");
ae7d00 36         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write);
JG 37         curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result);
0594ac 38         curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
JG 39     }
ae7d00 40 }
JG 41
42 void destroy() { curl_global_cleanup(); }
43
44 void updateBom(std::unordered_map<std::string, book::Book>& bs)
45 {
46     CURLcode res;
47     res = curl_easy_perform(curl);
48     curl_easy_cleanup(curl);
49     if (CURLE_OK != res) {
50         fprintf(stderr, "Curl failed: %d\n", res);
0594ac 51     }
ae7d00 52     rapidxml::xml_document<> d;
JG 53     d.parse<0>(&result[0]);
54     rapidxml::xml_node<>* n = d.first_node();
55     for (rapidxml::xml_attribute<>* a = n->first_attribute(); a;
56          a = a->next_attribute()) {
57         std::cout << a->name() << ": " << a->value() << std::endl;
58     }
0594ac 59 }
JG 60 } // namespace bom