Joel Grunbaum
2022-01-11 0594ac9ba0f38944ef810b5ac48a2067b1ce5a09
commit | author | age
0594ac 1 #include "bom.hpp"
JG 2
3 #include "book.hpp"
4 #include <cstddef>
5 #include <cstdio>
6 #include <curl/curl.h>
7 #include <stdio.h>
8 #include <string>
9 #include <unordered_map>
10
11 namespace bom
12 {
13
14 static size_t my_fwrite(void* buffer, size_t size, size_t nmemb, void* stream)
15 {
16     FILE* out = (FILE*)stream;
17     if (!out) {
18         out = tmpfile();
19         if (!out) {
20             return -1;
21         }
22     }
23     return fwrite(buffer, size, nmemb, out);
24 }
25 void updateBom(std::unordered_map<std::string, book::Book>& bs)
26 {
27     CURL* curl;
28     CURLcode res;
29     FILE* f{NULL};
30     curl_global_init(CURL_GLOBAL_DEFAULT);
31     curl = curl_easy_init();
32     if (curl) {
33         curl_easy_setopt(curl, CURLOPT_URL,
34                          "ftp://ftp.bom.gov.au/anon/gen/fwo/IDN60920.xml");
35         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
36         curl_easy_setopt(curl, CURLOPT_WRITEDATA, &f);
37         curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
38         res = curl_easy_perform(curl);
39         curl_easy_cleanup(curl);
40         if (CURLE_OK != res) {
41             fprintf(stderr, "Curl failed: %d\n", res);
42         }
43     }
44     if (f) {
45         fclose(f);
46     }
47     curl_global_cleanup();
48 }
49 } // namespace bom