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