Joel Grunbaum
2022-01-12 ae7d00edf2f02912ec9c04919ea7535c93d2d1e0
Intermediate ftp, no libcurl on mac
5 files modified
1 files deleted
1 files added
87 ■■■■■ changed files
.gitmodules 3 ●●●●● patch | view | raw | blame | history
Makefile 6 ●●●●● patch | view | raw | blame | history
bom.cpp 57 ●●●●● patch | view | raw | blame | history
bom.hpp 4 ●●● patch | view | raw | blame | history
click patch | view | raw | blame | history
rapidxml @ 2ae4b2 1 ●●●● patch | view | raw | blame | history
test.cpp 16 ●●●●● patch | view | raw | blame | history
.gitmodules
@@ -10,3 +10,6 @@
[submodule "easywsclient"]
    path = easywsclient
    url = https://github.com/dhbaird/easywsclient.git
[submodule "rapidxml"]
    path = rapidxml
    url = https://github.com/discord/rapidxml.git
Makefile
@@ -12,15 +12,17 @@
ifneq ($(OS),Darwin)
CXXFLAGS += -static
else
CXXFLAGS += -I/usr/local/opt/curl/include
LDFLAGS += -L/usr/local/opt/curl/lib
endif
ifeq ($(TEST),1)
    CXXFLAGS += -DTEST_EXCHANGE
endif
json=json.o
ws=easywsclient/easywsclient.o
main=${json} ${ws} protocol.o book.o
main=${ws} json.o bom.o protocol.o book.o
default: test click bot
bom.cpp
@@ -1,49 +1,60 @@
#include "bom.hpp"
#include "book.hpp"
#include "rapidxml/rapidxml.hpp"
#include <cstddef>
#include <cstdio>
#include <curl/curl.h>
#include <fstream>
#include <iterator>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>
namespace bom
{
CURL* curl{NULL};
std::string result;
static size_t my_fwrite(void* buffer, size_t size, size_t nmemb, void* stream)
static size_t my_write(void* buffer, std::size_t size, std::size_t nmemb,
                       void* stream)
{
    FILE* out = (FILE*)stream;
    if (!out) {
        out = tmpfile();
        if (!out) {
            return -1;
        }
    }
    return fwrite(buffer, size, nmemb, out);
    std::string& text = *static_cast<std::string*>(stream);
    std::size_t totalsize = size * nmemb;
    text.append(static_cast<char*>(buffer), totalsize);
    return totalsize;
}
void updateBom(std::unordered_map<std::string, book::Book>& bs)
void initialise()
{
    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_WRITEFUNCTION, my_write);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result);
        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);
}
void destroy() { curl_global_cleanup(); }
void updateBom(std::unordered_map<std::string, book::Book>& bs)
{
    CURLcode res;
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    if (CURLE_OK != res) {
        fprintf(stderr, "Curl failed: %d\n", res);
    }
    curl_global_cleanup();
    rapidxml::xml_document<> d;
    d.parse<0>(&result[0]);
    rapidxml::xml_node<>* n = d.first_node();
    for (rapidxml::xml_attribute<>* a = n->first_attribute(); a;
         a = a->next_attribute()) {
        std::cout << a->name() << ": " << a->value() << std::endl;
    }
}
} // namespace bom
bom.hpp
@@ -6,5 +6,7 @@
namespace bom
{
void initialise();
void destroy();
void updateBom(std::unordered_map<std::string, book::Book>& bs);
}
} // namespace bom
click
Binary files differ
rapidxml
New file
@@ -0,0 +1 @@
Subproject commit 2ae4b2888165a393dfb6382168825fddf00c27b9
test.cpp
@@ -1,6 +1,7 @@
#include "book.hpp"
#include "json.hpp"
#include "protocol.hpp"
#include "bom.hpp"
#include <chrono>
#include <unistd.h>
#include <unordered_map>
@@ -9,10 +10,13 @@
{
    // book::Book b = book::testBook(10, true);
    auto bs = protocol::recoverBook();
    protocol::catchUp(bs);
    std::cout << bs.size() << std::endl;
    for (auto i : bs) {
        std::cout << i.first << std::endl;
        i.second.printBook();
    }
//     protocol::catchUp(bs);
//     std::cout << bs.size() << std::endl;
//     for (auto i : bs) {
//         std::cout << i.first << std::endl;
//         i.second.printBook();
//     }
    bom::initialise();
    bom::updateBom(bs);
    bom::destroy();
}