Joel Grunbaum
2022-01-11 0594ac9ba0f38944ef810b5ac48a2067b1ce5a09
Copied libcurl's ftp example
2 files added
59 ■■■■■ changed files
bom.cpp 49 ●●●●● patch | view | raw | blame | history
bom.hpp 10 ●●●●● patch | view | raw | blame | history
bom.cpp
New file
@@ -0,0 +1,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
bom.hpp
New file
@@ -0,0 +1,10 @@
#pragma once
#include "book.hpp"
#include <string>
#include <unordered_map>
namespace bom
{
void updateBom(std::unordered_map<std::string, book::Book>& bs);
}