Skip to content
Snippets Groups Projects
Commit 769fa2ee authored by Zsombor Szabó-Antalovszky's avatar Zsombor Szabó-Antalovszky
Browse files

added the cpp file for handling the snatch command

parent 1e938748
Branches
No related tags found
No related merge requests found
#include <iostream>
#include <fstream>
#include <curl/curl.h>
#include "snatch.h"
using namespace std;
// Write callback to write data into a file
size_t write_data(void* ptr, size_t size, size_t nmemb, FILE* stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
bool download_file(const string& url, const string& output_filename) {
CURL* curl;
FILE* fp;
CURLcode res;
curl = curl_easy_init();
if (curl) {
fp = fopen(output_filename.c_str(), "wb"); // "wb" = write binary
if (!fp) {
cerr << "Failed to open file: " << output_filename << endl;
curl_easy_cleanup(curl);
return false;
}
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // Set the URL
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); // How to write received data
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); // Where to write
res = curl_easy_perform(curl); // Perform download
curl_easy_cleanup(curl); // Clean up
fclose(fp); // Close file
if (res != CURLE_OK) {
cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << endl;
return false;
}
return true;
}
return false;
}
#ifndef __SNATCH_H
#define __SNATCH_H
#include <iostream>
size_t write_data(void* ptr, size_t size, size_t nmemb, FILE* stream);
bool download_file(const std::string& url, const std::string& output_filename);
#endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment