Skip to content

Commit f57983e

Browse files
committed
changed to be a working downloader
1 parent b80c8c2 commit f57983e

File tree

5 files changed

+111
-47
lines changed

5 files changed

+111
-47
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ coverage/
5656
# === Ninja build system ===
5757
.ninja_deps
5858
.ninja_log
59+
/download

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.31)
2+
project(modpack-updater)
3+
4+
set(CMAKE_CXX_STANDARD 26)
5+
6+
add_executable(modpack-updater main.cpp download.cpp download.h)

download.cpp

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,81 @@
1-
#ifndef DOWNLOAD_H
2-
#define DOWNLOAD_H
1+
#include "download.h"
2+
#include <iostream>
3+
#include <fstream>
4+
#include <curl/curl.h>
5+
#include <sys/stat.h>
6+
#include <sys/types.h>
7+
#ifdef _WIN32
8+
#include <direct.h>
9+
#define MKDIR(dir) _mkdir(dir)
10+
#else
11+
#include <errno.h>
12+
#define MKDIR(dir) mkdir(dir, 0755)
13+
#endif
314

4-
#include <string>
15+
// Helper: Create directory if it does not exist
16+
bool ensure_directory(const std::string& filepath) {
17+
size_t pos = filepath.find_last_of("/\\");
18+
if (pos == std::string::npos) return true; // No directory part
519

6-
bool download(const std::string& url, const std::string& outputPath);
20+
std::string dir = filepath.substr(0, pos);
21+
struct stat info;
22+
if (stat(dir.c_str(), &info) != 0) {
23+
// Directory does not exist, try to create it
24+
if (MKDIR(dir.c_str()) != 0) {
25+
std::cerr << "[download] Failed to create directory: " << dir << std::endl;
26+
return false;
27+
}
28+
} else if (!(info.st_mode & S_IFDIR)) {
29+
std::cerr << "[download] Path exists but is not a directory: " << dir << std::endl;
30+
return false;
31+
}
32+
return true;
33+
}
734

8-
#endif
35+
// Callback for libcurl to write data to file
36+
size_t write_data(void* ptr, size_t size, size_t nmemb, void* stream) {
37+
std::ofstream* out = static_cast<std::ofstream*>(stream);
38+
const size_t totalSize = size * nmemb;
39+
out->write(static_cast<char*>(ptr), totalSize);
40+
return totalSize;
41+
}
42+
43+
Download::Download(const std::string &url, const std::string &path)
44+
: url(url), path(path) {}
45+
46+
bool Download::download() const {
47+
if (!ensure_directory(path)) {
48+
return false;
49+
}
50+
51+
CURL* curl = curl_easy_init();
52+
if (!curl) {
53+
std::cerr << "[curl] Initialization failed" << std::endl;
54+
return false;
55+
}
56+
57+
std::ofstream outFile(path, std::ios::binary);
58+
if (!outFile.is_open()) {
59+
std::cerr << "[download] Could not open file: " << path << std::endl;
60+
curl_easy_cleanup(curl);
61+
return false;
62+
}
63+
64+
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
65+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
66+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &outFile);
67+
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // Follow redirects
68+
69+
CURLcode res = curl_easy_perform(curl);
70+
71+
curl_easy_cleanup(curl);
72+
outFile.close();
73+
74+
if (res != CURLE_OK) {
75+
std::cerr << "[curl] Download error: " << curl_easy_strerror(res) << std::endl;
76+
return false;
77+
}
78+
79+
std::cout << "[download] Successfully saved as: " << path << std::endl;
80+
return true;
81+
}

download.h

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,14 @@
1-
#include "download.h"
2-
#include <iostream>
3-
#include <fstream>
4-
#include <curl/curl.h>
1+
#ifndef DOWNLOAD_H
2+
#define DOWNLOAD_H
53

6-
// Callback zum Schreiben der Daten in die Datei
7-
size_t write_data(void* ptr, size_t size, size_t nmemb, void* stream) {
8-
auto* out = static_cast<std::ofstream*>(stream);
9-
const size_t totalSize = size * nmemb;
10-
out->write(static_cast<char*>(ptr), totalSize);
11-
return totalSize;
12-
}
4+
#include <string>
135

14-
bool download(const std::string& url, const std::string& path) {
15-
CURL* curl = curl_easy_init();
16-
if (!curl) {
17-
std::cerr << "[curl] Initialisierung fehlgeschlagen\n";
18-
return false;
19-
}
6+
class Download {
7+
std::string url;
8+
std::string path;
9+
public:
10+
Download(const std::string &url, const std::string &path);
11+
bool download() const;
12+
};
2013

21-
std::ofstream outFile(path, std::ios::binary);
22-
if (!outFile.is_open()) {
23-
std::cerr << "[download] Konnte Datei nicht öffnen: " << path << "\n";
24-
curl_easy_cleanup(curl);
25-
return false;
26-
}
27-
28-
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
29-
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
30-
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &outFile);
31-
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // Redirects folgen
32-
33-
const CURLcode res = curl_easy_perform(curl);
34-
35-
curl_easy_cleanup(curl);
36-
outFile.close();
37-
38-
if (res != CURLE_OK) {
39-
std::cerr << "[curl] Fehler beim Download: " << curl_easy_strerror(res) << "\n";
40-
return false;
41-
}
42-
43-
std::cout << "[download] Erfolgreich gespeichert als: " << path << "\n";
44-
return true;
45-
}
14+
#endif

main.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
#include "download.h"
3+
4+
int main() {
5+
const Download downloader(
6+
"https://eztxm.de/assets/images/logo.png",
7+
"download/logo.png"
8+
);
9+
if (downloader.download()) {
10+
std::cout << "Logo loaded successfully!" << std::endl;
11+
return 0;
12+
}
13+
std::cerr << "Error loading logo" << std::endl;
14+
return 1;
15+
}

0 commit comments

Comments
 (0)