Copied libcurl's ftp example
New file |
| | |
| | | #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 |
New file |
| | |
| | | #pragma once |
| | | |
| | | #include "book.hpp" |
| | | #include <string> |
| | | #include <unordered_map> |
| | | |
| | | namespace bom |
| | | { |
| | | void updateBom(std::unordered_map<std::string, book::Book>& bs); |
| | | } |