Compare commits

..

2 commits

Author SHA1 Message Date
8d4edf3cbf fix: removed some unnecessary c_str's 2024-10-19 02:44:34 +02:00
9e4bec6b79 regex: moved it to a seperate file 2024-10-19 02:43:34 +02:00
5 changed files with 86 additions and 68 deletions

View file

@ -1,8 +1,8 @@
===============================================================================
Language Files Lines Code Comments Blanks
===============================================================================
C Header 1 6 5 0 1
C++ 2 289 217 24 48
C++ 3 313 236 25 52
C++ Header 3 33 30 0 3
===============================================================================
Total 3 295 222 24 49
Total 6 346 266 25 55
===============================================================================

View file

@ -3,4 +3,3 @@
#include <string>
void getHttp(std::string url, std::string *outputfile);
#endif

6
src/includes/regex.hpp Normal file
View file

@ -0,0 +1,6 @@
#ifndef REGEX_H
#define REGEX_H
#include <string>
void regex(std::string collectionid, std::string gameid, std::string cacheid, std::string cachesc);
#endif

View file

@ -5,13 +5,12 @@
#include <cstring>
#include <filesystem>
#include <fstream>
#include <ios>
#include <iostream>
#include <ostream>
#include <string>
#include "includes/getHttp.h"
#include "includes/getHttp.hpp"
#include "includes/Strings.hpp"
#include "regex"
#include "includes/regex.hpp"
std::string woof(std::ifstream &meow) {
std::ostringstream nya;
@ -24,9 +23,9 @@ int main(int argc, char **argv, char **envp) {
// need some cleaning in the future ata
const char *userHome = getenv("HOME");
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 usercache = std::string(userHome) + "/.cache/";
std::string cacheid = std::string(usercache) + "ids.txt";
std::string cachesc = std::string(usercache) + "sources.html";
std::string collectionid;
std::string modid;
@ -42,11 +41,11 @@ int main(int argc, char **argv, char **envp) {
// Removes cache
if (std::filesystem::exists(cacheid) &&
std::filesystem::is_directory(cacheid)) {
int status = remove(std::string{cacheid}.c_str());
int status2 = remove(std::string{cachesc}.c_str());
std::filesystem::remove(cacheid);
std::filesystem::remove(cachesc);
} else {
int status = remove(std::string{cacheid}.c_str());
int status2 = remove(std::string{cachesc}.c_str());
std::filesystem::remove(cacheid);
std::filesystem::remove(cachesc);
}
std::vector<std::string> ARGS{argv, argv + argc};
@ -177,59 +176,8 @@ int main(int argc, char **argv, char **envp) {
}
}
// regex and stuff (collectionid)
if (!collectionid.empty()) {
// Input and output file paths
std::string inputFilePath = cachesc;
std::string outputFilePath = cacheid;
regex({collectionid}, {gameid}, {cacheid}, {cachesc});
// Open the input file (source.html)
std::ifstream inputFile(inputFilePath);
std::ofstream outputFile(outputFilePath, std::ios::app);
if (!inputFile.is_open() && !outputFile.is_open()) {
std::cerr << "Unable to open file";
return 1;
}
std::regex grepRegex(R"(<div class="workshopItemPreviewHolder ")");
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 " + gameid + " " + line;
line += " \\";
outputFile << line << std::endl;
}
}
// 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();
}
//checks if a "\" is needed or not
std::ifstream idscount{cacheid};
int step = 0;
@ -237,9 +185,9 @@ int main(int argc, char **argv, char **envp) {
for (std::string line; std::getline(idscount, line); ) {
step++;
}
std::string slash = (step == 2) ? R"( )": R"( \ )";
idscount.close();
idscount.close();
//gets the ids
std::ifstream ids{cacheid};

65
src/regex.cpp Normal file
View file

@ -0,0 +1,65 @@
#include <cctype>
#include <cpptoml.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <ios>
#include <iostream>
#include <ostream>
#include <string>
#include "regex"
// regex and stuff (collectionid)
void regex(std::string collectionid, std::string gameid, std::string cacheid, std::string cachesc) {
if (!collectionid.empty()) {
// Input and output file paths
std::string inputFilePath = cachesc;
std::string outputFilePath = cacheid;
// Open the input file (source.html)
std::ifstream inputFile(inputFilePath);
std::ofstream outputFile(outputFilePath, std::ios::app);
if (!inputFile.is_open() && !outputFile.is_open()) {
throw("Unable to open file\n");
}
std::regex grepRegex(R"(<div class="workshopItemPreviewHolder ")");
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 " + gameid + " " + line;
line += " \\";
outputFile << line << std::endl;
}
}
// 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();
}
}