a lot of fixes

This commit is contained in:
2024-10-05 13:52:44 +03:00
parent 640337d420
commit d2afa8c9dd
32 changed files with 909 additions and 104 deletions

63
net/net.cpp Normal file
View File

@@ -0,0 +1,63 @@
#include "net.h"
#include <curl/curl.h>
#include <iostream>
#include <vector>
#include <regex>
struct data {};
size_t write_data(void *buffer, size_t size, size_t nmemb, void *filename) {
FILE *f = fopen(((std::string *)filename)->c_str(), "w");
size_t written = fwrite(buffer, size, nmemb, f);
fclose(f);
return written;
}
Net::Net() {}
void write_modules(void *buffer, size_t size, size_t nmemb, void *modules) {
std::vector<std::string> *modules_vector =
(std::vector<std::string> *)modules;
std::string to_parse = std::string((char*)buffer);
std::regex r("(?!\\\")\\w+\\.json(?!\\\")", std::regex::collate);
std::smatch res;
std::string::const_iterator search(to_parse.cbegin());
while (std::regex_search(search, to_parse.cend(), res, r)) {
modules_vector->push_back(res[0]);
search = res.suffix().first;
}
}
std::vector<std::string> Net::get_all_modules(std::string url) {
CURL *handle = curl_easy_init();
curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_modules);
std::vector<std::string> modules {};
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &modules);
curl_easy_perform(handle);
return modules;
}
std::string Net::get_file(std::string url, std::string filename) {
CURL *handle = curl_easy_init();
curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &filename);
auto success = curl_easy_perform(handle);
curl_easy_cleanup(handle);
return "";
}

17
net/net.h Normal file
View File

@@ -0,0 +1,17 @@
#ifndef NET_H
#define NET_H
#include <string>
#include <vector>
size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp);
class Net
{
public:
Net();
std::vector<std::string> get_all_modules(std::string url);
std::string get_file(std::string url, std::string filename);
};
#endif // NET_H