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

View file

@ -1,12 +1,7 @@
#include <cctype>
#include <cpptoml.h>
#include <cstdio>
#include <cstdlib>
#include <fstream> #include <fstream>
#include <ios>
#include <iostream> #include <iostream>
#include <ostream>
#include <string> #include <string>
#include <sstream>
#include "regex" #include "regex"
#include "includes/Regex.hpp" #include "includes/Regex.hpp"
@ -14,27 +9,24 @@
void Regex(cmd *inputCmd) { void Regex(cmd *inputCmd) {
if (!inputCmd->collectionid.empty()) { if (!inputCmd->collectionid.empty()) {
// Input and output file paths // Input and output file paths
std::string inputFilePath = inputCmd->cachesc; std::string inputFilePath = inputCmd->cachesc; // Path to the input file
std::string outputFilePath = inputCmd->cacheid;
// Open the input file (source.html) // Open the input file
std::ifstream inputFile(inputFilePath); std::ifstream inputFile(inputFilePath);
std::ofstream outputFile(outputFilePath, std::ios::app);
if (!inputFile.is_open() && !outputFile.is_open()) { // Check if the input file is open
throw("Unable to open file\n"); if (!inputFile.is_open()) {
throw std::runtime_error("Unable to open file: " + inputFilePath);
} }
std::regex grepRegex(R"(<div class="workshopItemPreviewHolder ")"); // Regex to search for the desired pattern
std::regex grepRegex(R"(<div class="workshopItemPreviewHolder )");
std::string line; std::string line;
// Process each line // Process each line from the input file
while (std::getline(inputFile, line)) { while (std::getline(inputFile, line)) {
// grep-like behavior (only process lines containing the pattern with two // grep-like behavior (only process lines containing the pattern with two spaces)
// spaces)
if (std::regex_search(line, grepRegex)) { if (std::regex_search(line, grepRegex)) {
// sed 's/"><div class=.*//' // sed 's/"><div class=.*//'
std::size_t divPos = line.find("\"><div class="); std::size_t divPos = line.find("\"><div class=");
if (divPos != std::string::npos) { if (divPos != std::string::npos) {
@ -51,27 +43,31 @@ void Regex(cmd *inputCmd) {
line += " \\"; line += " \\";
outputFile << line << std::endl; inputCmd->ids += line + "\n"; // Write to output buffer
} }
} }
// Step 6: Write "+quit" at the end of the output file // Step 6: Write "+quit" at the end of the output buffer
outputFile << "+quit" << std::endl; inputCmd->ids += "+quit\n";
// Close the input and output files // 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(); inputFile.close();
outputFile.close();
// Optionally print the output for verification
// std::cout << inputCmd->ids; // Print output buffer content
} }
} }
void slashing(cmd *inputCmd) { void slashing(cmd *inputCmd) {
//checks if a "\" is needed or not std::istringstream inputStream(inputCmd->ids);
std::ifstream idscount{inputCmd->cacheid}; std::string line;
for (std::string line; std::getline(idscount, line); ) { // Count the number of lines
while (std::getline(inputStream, line)) {
inputCmd->step++; inputCmd->step++;
} }
inputCmd->slash = (inputCmd->step == 2) ? R"( )": R"( \ )"; 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 cacheid = std::string(usercache) + "ids.txt";
std::string cachesc = std::string(usercache) + "sources.html"; std::string cachesc = std::string(usercache) + "sources.html";
std::string ids;
std::string collectionid; std::string collectionid;
std::string modid; std::string modid;
std::string user = "anonymous"; std::string user = "anonymous";
@ -21,9 +23,13 @@ int ab = 0;
int step = 0; int step = 0;
std::string slash; 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 Regex(cmd *inputCmd);
void slashing(cmd *inputCmd); void slashing(cmd *inputCmd);
#endif #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/getHttp.hpp"
#include "includes/Strings.hpp" #include "includes/Strings.hpp"
#include "includes/Regex.hpp" #include "includes/Regex.hpp"
#include "includes/maincommand.hpp"
std::string woof(std::ifstream &meow) { std::string woof(std::ifstream &meow) {
std::ostringstream nya; std::ostringstream nya;
@ -83,7 +84,6 @@ int main(int argc, char **argv, char **envp) {
return 1; return 1;
} }
std::cout << "success\n";
break; break;
} }
@ -104,7 +104,6 @@ int main(int argc, char **argv, char **envp) {
return 1; return 1;
} }
std::cout << "success\n";
break; break;
} else { } else {
std::cerr << HELP; std::cerr << HELP;
@ -131,7 +130,6 @@ int main(int argc, char **argv, char **envp) {
} }
inputCmd.ab = 1; inputCmd.ab = 1;
std::cout << "success\n";
break; break;
} }
@ -145,7 +143,6 @@ int main(int argc, char **argv, char **envp) {
} }
inputCmd.ab = 1; inputCmd.ab = 1;
std::cout << "success\n";
break; break;
} else { } else {
std::cerr << HELP; std::cerr << HELP;
@ -161,21 +158,9 @@ int main(int argc, char **argv, char **envp) {
} }
} }
// regex function
Regex(&inputCmd); Regex(&inputCmd);
slashing(&inputCmd); slashing(&inputCmd);
maincommand(&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";
return 1; 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";
}