regex: moved it to a seperate file

This commit is contained in:
Kaley, Fischer 2024-10-19 02:43:34 +02:00
parent 532e3e408f
commit 9e4bec6b79
5 changed files with 82 additions and 63 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

@ -9,9 +9,9 @@
#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 +24,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;
@ -177,58 +177,7 @@ 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;
// 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();
}
regex({collectionid}, {gameid}, {cacheid}, {cachesc});
//checks if a "\" is needed or not
std::ifstream idscount{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();
}
}