added some code

wrote some code to test some functionallity
This commit is contained in:
jakani24
2023-11-02 16:41:35 +01:00
parent 89e911488e
commit b931c524a1
41 changed files with 299 additions and 10 deletions

View File

@@ -32,4 +32,50 @@ int connect_to_srv(const char*url,char*out,int max_len, bool ignore_insecure) {
}
return 2;
}
size_t write_callback(void* contents, size_t size, size_t nmemb, void* userp) {
size_t totalSize = size * nmemb;
FILE* file = (FILE*)userp;
if (file) {
fwrite(contents, 1, totalSize, file);
}
return totalSize;
}
int download_file_from_srv(const char* url, const char* outputFileName) {
//use curl to download a file from a server
CURL* curl;
CURLcode res;
FILE* output_file;
curl = curl_easy_init();
if (!curl) {
return 1;
}
// Set the URL to download
curl_easy_setopt(curl, CURLOPT_URL, url);
// Create a file to write the downloaded data
output_file = fopen(outputFileName, "wb");
if (!output_file) {
curl_easy_cleanup(curl);
return 1;
}
// Set the write callback function
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, output_file);
// Perform the download
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
return 1;
}
// Cleanup and close the file
curl_easy_cleanup(curl);
fclose(output_file);
return 0;
}
#endif