Skip to content

Commit

Permalink
Changes (read description 😄)
Browse files Browse the repository at this point in the history
[functions.cpp] implemented new function - installPackage
[functions.cpp] _getUpdates function now loads upd.json at each application start
[Programs] added new option --list-applications - list all available applications in the repository
  • Loading branch information
nixxoq committed May 12, 2024
1 parent 6e1df19 commit 4118d21
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 29 deletions.
18 changes: 7 additions & 11 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
#include <iostream>
#include "source/functions.h"
#include "source/constants.h"

int main(int argc, char **argv)
{
{
// if (!isWindowsXP()) {
// std::cout << "This program works only on Windows XP" << std::endl;
// return 0;
// }

// cpr::Response r = cpr::Get(cpr::Url{"https://google.com"});
// std::cout << r.status_code << std::endl;

getAllApplications(getUpdates());

// if (!checkFlags(argc, argv))
// {
// printHelp();
// return 0;
// };
if (!checkFlags(argc, argv))
{
printHelp();
return 0;
};


return 0;
Expand Down
3 changes: 3 additions & 0 deletions source/constants.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "constants.h"

json applicationsList = _getUpdates();
9 changes: 9 additions & 0 deletions source/constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// constants.h
#include "functions.h"

#ifndef CONSTANTS_H
#define CONSTANTS_H

#define PROGRAM_VERSION "0.1.0"

#endif
102 changes: 86 additions & 16 deletions source/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,38 @@ int checkFlags(int argc, char **argv)
{
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i], "-h") || strcmp(argv[i], "--help"))
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
{
printHelp();
break;
}
else if (strcmp(argv[i], "-i") == 0 || strcmp(argv[i], "--install") == 0)
{
if (i + 1 < argc)
{
installPackage(argv[i + 1]);
break;
}
else
{
std::cout << "Please input the application you want to install after the -i argument\n\nFor example:\nxp-apps.exe -i PyCharm2023" << std::endl;
break;
}
}
else if (strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--list") == 0 || strcmp(argv[i], "--list-applications") == 0)
{
getAllApplications(applicationsList);
break;
}
}
return 1;
}

return 0;
return 1;
}

void printHelp()
{
std::cout << R"(XP-Apps ver 0.1.0
List of available arguments:
[Option] [Description]
-h, --help Display this help message
-i, --install Install Application from XP-Apps repository
Example:
xp-apps.exe -i PyCharm2023 or xp-apps.exe --install PyCharm2023
)" << std::endl;
std::cout << "XP-Apps ver " << PROGRAM_VERSION << "\n\nList of available arguments:\n\n[Option]\t\t\t\t[Description]\n-h, --help\t\t\t\tDisplay this help message\n-i, --install\t\t\t\tInstall Application from XP-Apps repository\n-l, --list, --list-applications\t\tList all available applications in the repository\n\nExample:\n xp-apps.exe -i PyCharm2023 or xp-apps.exe --install PyCharm2023" << std::endl;
}

json parseJson(std::string jsonString)
Expand All @@ -58,10 +68,18 @@ std::string readJsonFile(std::string path)
return buffer.str();
}

json getUpdates()
json _getUpdates()
{
cpr::Response r = getResponse("https://raw.githubusercontent.com/Snaky1a/xp-apps/development/upd.json");
return (r.status_code == 200) ? parseJson(r.text) : parseJson("{\"Hello\": \"World\"}");
if (r.status_code == 200)
{
std::ofstream of("upd.json");
if (of.is_open())
{
of << r.text;
of.close();
}
}
return parseJson(readJsonFile("upd.json"));
}

Expand All @@ -76,6 +94,7 @@ void getAllApplications(json applications)
JSON structure:
category {
application_name {
filename: filename
url: download_url
}
}
Expand All @@ -93,4 +112,55 @@ void getAllApplications(json applications)
std::cout << category.key() << "\t\t" << application.key() << std::endl;
}
}
}

auto SearchPackage(std::string packageName)
{
/*
JSON structure:
category {
application_name {
filename: filename
url: download_url
}
}
*/

// TODO: Speed up searching
for (const auto &category : applicationsList.items())
{
for (const auto &application : category.value().items())
{
if (application.key() == packageName)
{
return application.value();
}
}
}
return parseJson("{}");
}

void installPackage(std::string packageName)
{
auto package = SearchPackage(packageName);
if (package.empty())
{
std::cout << "Application not found" << std::endl;
exit(0);
}
else
{
std::cout << "Found application: " << packageName << "\nDownloading..." << std::endl;
std::ofstream of(package["filename"], std::ios::binary);
cpr::Response r = cpr::Download(of, cpr::Url{package["url"]});
if (r.status_code == 200)
{
std::cout << "Download complete." << std::endl;
}
else
{
std::cout << "Download failed.\nError:" << r.error.message << std::endl;
}
}
return;
}
8 changes: 6 additions & 2 deletions source/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <windows.h>
#include <fstream>
#include <cpr/cpr.h>
// #include <curl/curl.h>

// third-party libraries
#include "nlohmann/json.hpp"
Expand All @@ -20,11 +19,16 @@ void printHelp();
bool isWindowsXP();
int checkFlags(int argc, char **argv);
void getAllApplications(json applications);
void installPackage(std::string packageName);

// json helpers
json parseJson(std::string jsonString);
std::string readJsonFile(std::string path);
json getUpdates();
json _getUpdates();
cpr::Response getResponse(std::string url);

extern json applicationsList;

#include "constants.h"

#endif // FUNCTIONS_H
1 change: 1 addition & 0 deletions upd.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"test": {
"app": {
"filename": "AIMP.zip",
"url": "https://github.com/Snaky1a/xp-apps/releases/download/2023_10_11_17_52/AIMP.zip"
}
}
Expand Down

0 comments on commit 4118d21

Please sign in to comment.