Regex: removed output file and instead used a string

This commit is contained in:
Kaley, Fischer 2024-10-21 23:02:11 +02:00
parent 3079cb8422
commit 1bb8e7ee49
6 changed files with 175 additions and 81 deletions

View file

@ -1,8 +1,8 @@
===============================================================================
Language Files Lines Code Comments Blanks
===============================================================================
C++ 3 299 225 26 48
C++ Header 3 51 43 1 7
C++ 4 383 308 26 49
C++ Header 4 67 56 1 10
===============================================================================
Total 6 350 268 27 55
Total 8 450 364 27 59
===============================================================================

View file

@ -1,77 +1,73 @@
#include <cctype>
#include <cpptoml.h>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <ios>
#include <iostream>
#include <ostream>
#include <string>
#include <sstream>
#include "regex"
#include "includes/Regex.hpp"
// regex and stuff (collectionid)
void Regex(cmd *inputCmd) {
if (!inputCmd->collectionid.empty()) {
// Input and output file paths
std::string inputFilePath = inputCmd->cachesc;
std::string outputFilePath = inputCmd->cacheid;
if (!inputCmd->collectionid.empty()) {
// Input and output file paths
std::string inputFilePath = inputCmd->cachesc; // Path to the input file
// Open the input file (source.html)
std::ifstream inputFile(inputFilePath);
std::ofstream outputFile(outputFilePath, std::ios::app);
// Open the input file
std::ifstream inputFile(inputFilePath);
if (!inputFile.is_open() && !outputFile.is_open()) {
throw("Unable to open file\n");
// Check if the input file is open
if (!inputFile.is_open()) {
throw std::runtime_error("Unable to open file: " + inputFilePath);
}
// Regex to search for the desired pattern
std::regex grepRegex(R"(<div class="workshopItemPreviewHolder )");
std::string line;
// Process each line from the input file
while (std::getline(inputFile, line)) {
// grep-like behavior (only process lines containing the pattern with two spaces)
if (std::regex_search(line, grepRegex)) {
// sed 's/"><div class=.*//'
std::size_t divPos = line.find("\"><div class=");
if (divPos != std::string::npos) {
line = line.substr(0, divPos); // Trim everything after '"><div class='
}
// sed 's/.*id=//'
std::size_t idPos = line.find("id=");
if (idPos != std::string::npos) {
line = line.substr(idPos + 3); // Trim everything before 'id=' and keep the ID
}
line = "+workshop_download_item " + inputCmd->gameid + " " + line;
line += " \\";
inputCmd->ids += line + "\n"; // Write to output buffer
}
}
// Step 6: Write "+quit" at the end of the output buffer
inputCmd->ids += "+quit\n";
// Store the output back in the cmd structure (or handle it as needed)
// inputCmd->ids = outputBuffer.str(); // Store output in cacheid
// Close the input file
inputFile.close();
// Optionally print the output for verification
// std::cout << inputCmd->ids; // Print output buffer content
}
}
std::regex grepRegex(R"(<div class="workshopItemPreviewHolder ")");
void slashing(cmd *inputCmd) {
std::istringstream inputStream(inputCmd->ids);
std::string line;
// Process each line
while (std::getline(inputFile, line)) {
// grep-like behavior (only process lines containing the pattern with two
// spaces)
if (std::regex_search(line, grepRegex)) {
// sed 's/"><div class=.*//'
std::size_t divPos = line.find("\"><div class=");
if (divPos != std::string::npos) {
line = line.substr(0, divPos); // Trim everything after '"><div class='
}
// sed 's/.*id=//'
std::size_t idPos = line.find("id=");
if (idPos != std::string::npos) {
line = line.substr(idPos + 3); // Trim everything before 'id=' and keep the ID
}
line = "+workshop_download_item " + inputCmd->gameid + " " + line;
line += " \\";
outputFile << line << std::endl;
}
// Count the number of lines
while (std::getline(inputStream, line)) {
inputCmd->step++;
}
// Step 6: Write "+quit" at the end of the output file
outputFile << "+quit" << std::endl;
// Close the input and output files
inputFile.close();
outputFile.close();
}
}
void slashing(cmd *inputCmd) {
//checks if a "\" is needed or not
std::ifstream idscount{inputCmd->cacheid};
for (std::string line; std::getline(idscount, line); ) {
inputCmd->step++;
}
inputCmd->slash = (inputCmd->step == 2) ? R"( )": R"( \ )";
idscount.close();
}

View file

@ -10,6 +10,8 @@ std::string usercache = std::string(userHome) + "/.cache/";
std::string cacheid = std::string(usercache) + "ids.txt";
std::string cachesc = std::string(usercache) + "sources.html";
std::string ids;
std::string collectionid;
std::string modid;
std::string user = "anonymous";
@ -21,9 +23,13 @@ int ab = 0;
int step = 0;
std::string slash;
int successes = 0;
int timedout = 0;
int errors = 0;
int totalmeow = 0;
};
// void Regex(const std::string& collectionid, const std::string& gameid, const std::string& cacheid, const std::string& cachesc);
void Regex(cmd *inputCmd);
void slashing(cmd *inputCmd);
#endif

View file

@ -0,0 +1,5 @@
#ifndef MAINCOMMAND_HPP
#define MAINCOMMAND_HPP
#include "Regex.hpp"
void maincommand(cmd *inputCmd);
#endif

View file

@ -11,6 +11,7 @@
#include "includes/getHttp.hpp"
#include "includes/Strings.hpp"
#include "includes/Regex.hpp"
#include "includes/maincommand.hpp"
std::string woof(std::ifstream &meow) {
std::ostringstream nya;
@ -83,7 +84,6 @@ int main(int argc, char **argv, char **envp) {
return 1;
}
std::cout << "success\n";
break;
}
@ -104,7 +104,6 @@ int main(int argc, char **argv, char **envp) {
return 1;
}
std::cout << "success\n";
break;
} else {
std::cerr << HELP;
@ -131,7 +130,6 @@ int main(int argc, char **argv, char **envp) {
}
inputCmd.ab = 1;
std::cout << "success\n";
break;
}
@ -145,7 +143,6 @@ int main(int argc, char **argv, char **envp) {
}
inputCmd.ab = 1;
std::cout << "success\n";
break;
} else {
std::cerr << HELP;
@ -161,21 +158,9 @@ int main(int argc, char **argv, char **envp) {
}
}
// regex function
Regex(&inputCmd);
slashing(&inputCmd);
//gets the ids
std::ifstream ids{inputCmd.cacheid};
std::string idsm = (inputCmd.ab == 1) ? R"( +workshop_download_item )" + inputCmd.gameid + " " + inputCmd.modid + " +quit": R"()";
// main command
system(std::string{"sh ~/Steam/steamcmd.sh +force_install_dir " + inputCmd.dir + " +login " + inputCmd.user + inputCmd.pass + inputCmd.slash + idsm + woof(ids)}.c_str());
// shows how much and what has downloaded
std::string mods = (!inputCmd.modid.empty()) ? "" : R"(Mods: )" + std::to_string(inputCmd.step -1) + "\n";
std::string colm = (inputCmd.ab == 1) ? R"(Mod)": R"(Collection)";
std::cout << "\n\n" + mods + colm + " has been downloaded too: " + inputCmd.dir + "\n";
maincommand(&inputCmd);
return 1;
}

102
src/maincommand.cpp Normal file
View file

@ -0,0 +1,102 @@
#include <atomic>
#include <iostream>
#include <string>
#include <thread>
#include "includes/Regex.hpp"
#include <cctype>
#include <cpptoml.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <ostream>
#include <string>
#include <iostream>
#include <cstdio>
#include <memory>
#include <stdexcept>
#include <string>
#include <unistd.h>
#include <thread>
#include <chrono>
#include <atomic>
#include "includes/Regex.hpp"
void execAndDisplay(cmd *inputCmd, const std::string& cmd, std::atomic<bool>& running) {
char temp[128];
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(temp, sizeof(temp), pipe.get()) != nullptr) {
std::string line(temp);
// checks for success.
if (line.find("Success") != std::string::npos) {
inputCmd->successes++;
}
// checks for timed out ones.
if (line.find("Time") != std::string::npos) {
inputCmd->timedout++;
}
// checks for errors.
if (line.find("ERROR!") != std::string::npos) {
inputCmd->errors++;
}
inputCmd->totalmeow++;
}
running = false;
}
// loading cursor
void showLoadingCursor(std::atomic<bool>& running) {
const char* cursor = "|/-\\";
int i = 0;
while (running) {
std::cout << "\r" << cursor[i++ % 4] << " Getting Mods...";
std::cout.flush();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
void maincommand(cmd *inputCmd) {
std::string idsm = (inputCmd->ab == 1) ? R"( +workshop_download_item )" + inputCmd->gameid + " " + inputCmd->modid + " +quit": R"()";
std::string maincommand2 = std::string{"sh ~/Steam/steamcmd.sh +force_install_dir "
+ inputCmd->dir
+ " +login "
+ inputCmd->user
+ inputCmd->pass
+ inputCmd->slash
+ idsm
+ inputCmd->ids}.c_str();
try {
std::atomic<bool> running(true);
std::thread cursorThread(showLoadingCursor, std::ref(running));
execAndDisplay(inputCmd, maincommand2, running);
running = false;
cursorThread.join();
} catch (const std::runtime_error& e) {
std::cerr << "\nError: " << e.what() << std::endl;
}
// shows how much and what has downloaded
std::string total = (!inputCmd->modid.empty()) ? "Total: 1" : "Total: " + std::to_string(inputCmd->step -1) + "\n";
std::string mods = total + "\n"
+ "Finished: " + std::to_string(inputCmd->successes +1) + "\n"
+ "Timed out: " + std::to_string(inputCmd->timedout) + "\n"
+ "Errored: " + std::to_string(inputCmd->errors) + "\n" + "\n";
std::string colm = (inputCmd->ab == 1) ? R"(Mod)": R"(Collection)";
std::cout << "\n\n" + mods + colm + " has been downloaded too: " + inputCmd->dir + "\n";
}