From 0509d16de9ecabf2d8bd10ea8d3103838560926f Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 6 Oct 2024 20:27:24 +0300 Subject: [PATCH 1/3] added getting the http file with libcurl and updated cmake --- CMakeLists.txt | 16 ++++++++++++---- src/getHttp.cpp | 39 +++++++++++++++++++++++++++++++++++++++ src/includes/getHttp.h | 6 ++++++ src/main.cpp | 9 +++------ 4 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 src/getHttp.cpp create mode 100644 src/includes/getHttp.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 12c8185..e670e30 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,20 +3,28 @@ cmake_minimum_required(VERSION 3.28.2) # Get version file(READ ${CMAKE_CURRENT_SOURCE_DIR}/props.json PROPS) string(JSON VER GET ${PROPS} version) - +# find curl +find_package(CURL REQUIRED) project(stc DESCRIPTION "Easily download collections and mods from steam." VERSION ${VER}) set(CMAKE_CXX_STANDARD 26) - +# check if build type is set if it isnt set it to release +if(NOT CMAKE_BUILD_TYPE) + set(DCKMAKE_BUILD_TYPE Release) +endif() # Add the path to the source files for stc file(GLOB_RECURSE STC_SOURCE_FILES "src/*.cpp") - +# add compiler flags +set(CMAKE_CXX_FLAGS_DEBUG "-g") +set(CMAKE_CXX_FLAGS_RELEASE "-03") # Add the executable target for stc add_executable(stc ${STC_SOURCE_FILES}) install(TARGETS stc DESTINATION bin) - +# add curl libraries +target_link_libraries(stc PRIVATE ${CURL_LIBRARIES}) +target_include_directories(stc PRIVATE ${CURL_INCLUDE_DIRS}) # DEBIAN set(CPACK_DEBIAN_PACKAGE_MAINTAINER "DRAGONTOS") set(CPACK_DEBIAN_PACKAGE_LICENSE "GPLv3") diff --git a/src/getHttp.cpp b/src/getHttp.cpp new file mode 100644 index 0000000..b1df58f --- /dev/null +++ b/src/getHttp.cpp @@ -0,0 +1,39 @@ +#include +#include +#include + +size_t appendCurlOutputToString(void *ptr, size_t size, size_t nmemb, std::string *woof){ + // append the output from curl to a string + woof->append(static_cast(ptr), size*nmemb); + return size*nmemb; +} + +void writeHtmlFile(std::string* woof, std::string *outputfile){ + // check for nullptrs + if (!woof || !outputfile){ + throw("string is null\n"); + } + // open file and write to it + std::ofstream meow{*outputfile}; + meow << *woof; +} + +void getHttp(std::string url, std::string *outputfile){ + //initialize curl object and curlcode + CURL *meow = curl_easy_init(); + CURLcode res; + std::string woof; + // check for nullptr and if nullptr throw an exception + if (!outputfile){ + curl_easy_cleanup(meow); + throw("outputfile is null\n"); + } + // set the options + curl_easy_setopt(meow, CURLOPT_URL, url.c_str()); + curl_easy_setopt(meow, CURLOPT_WRITEFUNCTION, appendCurlOutputToString); + curl_easy_setopt(meow, CURLOPT_WRITEDATA, &woof); + // perform the request + curl_easy_perform(meow); + writeHtmlFile(&woof, outputfile); + curl_easy_cleanup(meow); +} diff --git a/src/includes/getHttp.h b/src/includes/getHttp.h new file mode 100644 index 0000000..41b876f --- /dev/null +++ b/src/includes/getHttp.h @@ -0,0 +1,6 @@ +#ifndef GETHTTP_H +#define GETHTTP_H +#include +void getHttp(std::string url, std::string *outputfile); +#endif + diff --git a/src/main.cpp b/src/main.cpp index 4746423..b6763fe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,7 @@ #include #include #include +#include "includes/getHttp.h" #include #include #include @@ -85,9 +86,7 @@ int main(int argc, char **argv, char **envp) { dir = ARGS[1+5]; } - // should be done with libcurl in the future ata - system(std::string {"curl https://steamcommunity.com/sharedfiles/filedetails/?id=" + collectionid + " -o " + cachesc}.c_str()); - + getHttp(std::string {"https://steamcommunity.com/sharedfiles/filedetails/?id=" + collectionid}, &cachesc); std::cout << collectionid << user << pass << gameid << dir; std::cout << "success1\n"; break; @@ -102,9 +101,7 @@ int main(int argc, char **argv, char **envp) { dir = ARGS[1+3]; } - // should be done with libcurl in the future ata - system(std::string {"curl https://steamcommunity.com/sharedfiles/filedetails/?id=" + collectionid + " -o " + cachesc}.c_str()); - + getHttp(std::string {"https://steamcommunity.com/sharedfiles/filedetails/?id=" + collectionid}, &cachesc); std::cout << collectionid << gameid << dir; std::cout << "success\n"; From 89c280a7856f7971147146c7e40b8695d99276c7 Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 6 Oct 2024 20:55:16 +0300 Subject: [PATCH 2/3] forgot try catch statement --- src/getHttp.cpp | 11 +++++++---- src/main.cpp | 21 ++++++++++++++++----- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/getHttp.cpp b/src/getHttp.cpp index b1df58f..2d6a774 100644 --- a/src/getHttp.cpp +++ b/src/getHttp.cpp @@ -2,13 +2,13 @@ #include #include -size_t appendCurlOutputToString(void *ptr, size_t size, size_t nmemb, std::string *woof){ +size_t appendCurlOutputToString(void *ptr, size_t size, size_t nmemb, std::string *woof) { // append the output from curl to a string woof->append(static_cast(ptr), size*nmemb); return size*nmemb; } -void writeHtmlFile(std::string* woof, std::string *outputfile){ +void writeHtmlFile(std::string* woof, std::string *outputfile) { // check for nullptrs if (!woof || !outputfile){ throw("string is null\n"); @@ -18,7 +18,7 @@ void writeHtmlFile(std::string* woof, std::string *outputfile){ meow << *woof; } -void getHttp(std::string url, std::string *outputfile){ +void getHttp(std::string url, std::string *outputfile) { //initialize curl object and curlcode CURL *meow = curl_easy_init(); CURLcode res; @@ -33,7 +33,10 @@ void getHttp(std::string url, std::string *outputfile){ curl_easy_setopt(meow, CURLOPT_WRITEFUNCTION, appendCurlOutputToString); curl_easy_setopt(meow, CURLOPT_WRITEDATA, &woof); // perform the request - curl_easy_perform(meow); + res = curl_easy_perform(meow); + if (res != CURLE_OK){ + throw("failed to perform the request\n"); + } writeHtmlFile(&woof, outputfile); curl_easy_cleanup(meow); } diff --git a/src/main.cpp b/src/main.cpp index b6763fe..239df08 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -85,8 +85,13 @@ int main(int argc, char **argv, char **envp) { if (argc == 7) { dir = ARGS[1+5]; } - - getHttp(std::string {"https://steamcommunity.com/sharedfiles/filedetails/?id=" + collectionid}, &cachesc); + try { + getHttp(std::string {"https://steamcommunity.com/sharedfiles/filedetails/?id=" + collectionid}, &cachesc); + } + catch(std::string& meow) { + std::cout << meow; + return 1; + } std::cout << collectionid << user << pass << gameid << dir; std::cout << "success1\n"; break; @@ -100,9 +105,15 @@ int main(int argc, char **argv, char **envp) { if (argc == 5) { dir = ARGS[1+3]; } - - getHttp(std::string {"https://steamcommunity.com/sharedfiles/filedetails/?id=" + collectionid}, &cachesc); - std::cout << collectionid << gameid << dir; + try { + getHttp(std::string {"https://steamcommunity.com/sharedfiles/filedetails/?id=" + collectionid}, &cachesc); + } + catch(std::string& meow) { + std::cout << meow; + return 1; + } + + std::cout << collectionid << gameid << dir << '\n'; std::cout << "success\n"; break; From 32f6abaf521480e724cf38a5e320cd220da24aa7 Mon Sep 17 00:00:00 2001 From: DRAGONTOS Date: Sun, 6 Oct 2024 23:01:22 +0200 Subject: [PATCH 3/3] pull: 89c280a7856f7971147146c7e40b8695d99276c7 --- CMakeLists.txt | 8 ++++++++ src/main.cpp | 6 +++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e670e30..c3e1243 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.28.2) # Get version file(READ ${CMAKE_CURRENT_SOURCE_DIR}/props.json PROPS) string(JSON VER GET ${PROPS} version) + # find curl find_package(CURL REQUIRED) project(stc @@ -10,21 +11,27 @@ project(stc VERSION ${VER}) set(CMAKE_CXX_STANDARD 26) + # check if build type is set if it isnt set it to release if(NOT CMAKE_BUILD_TYPE) set(DCKMAKE_BUILD_TYPE Release) endif() + # Add the path to the source files for stc file(GLOB_RECURSE STC_SOURCE_FILES "src/*.cpp") + # add compiler flags set(CMAKE_CXX_FLAGS_DEBUG "-g") set(CMAKE_CXX_FLAGS_RELEASE "-03") + # Add the executable target for stc add_executable(stc ${STC_SOURCE_FILES}) install(TARGETS stc DESTINATION bin) + # add curl libraries target_link_libraries(stc PRIVATE ${CURL_LIBRARIES}) target_include_directories(stc PRIVATE ${CURL_INCLUDE_DIRS}) + # DEBIAN set(CPACK_DEBIAN_PACKAGE_MAINTAINER "DRAGONTOS") set(CPACK_DEBIAN_PACKAGE_LICENSE "GPLv3") @@ -32,6 +39,7 @@ set(CPACK_DEBIAN_PACKAGE_DESCRIPTION ${DESCRIPTION}) set(CPACK_DEBIAN_PACKAGE_RELEASE_DIST ON) set(CPACK_GENERATOR DEBIAN) set(CPACK_PACKAGING_INSTALL_PREFIX "/usr") + # RPM set(CPACK_PACKAGE_VENDOR "DRAGONTOS") set(CPACK_RPM_PACKAGE_LICENSE "GPLv3") diff --git a/src/main.cpp b/src/main.cpp index 239df08..cdc2100 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -85,6 +85,7 @@ int main(int argc, char **argv, char **envp) { if (argc == 7) { dir = ARGS[1+5]; } + try { getHttp(std::string {"https://steamcommunity.com/sharedfiles/filedetails/?id=" + collectionid}, &cachesc); } @@ -92,7 +93,7 @@ int main(int argc, char **argv, char **envp) { std::cout << meow; return 1; } - std::cout << collectionid << user << pass << gameid << dir; + std::cout << "success1\n"; break; } @@ -105,6 +106,7 @@ int main(int argc, char **argv, char **envp) { if (argc == 5) { dir = ARGS[1+3]; } + try { getHttp(std::string {"https://steamcommunity.com/sharedfiles/filedetails/?id=" + collectionid}, &cachesc); } @@ -113,8 +115,6 @@ int main(int argc, char **argv, char **envp) { return 1; } - std::cout << collectionid << gameid << dir << '\n'; - std::cout << "success\n"; break; } else {