:-'Generic Commit'

This commit is contained in:
Your Name 2025-05-28 17:14:37 +12:00
parent 8597cfb723
commit b361e009a7
2 changed files with 84 additions and 4 deletions

View File

@ -1,18 +1,95 @@
#include "BashrcEditor.hpp"
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
std::string removeWhitespace(const std::string& s) {
std::string out;
for (char c : s) {
if (!isspace(static_cast<unsigned char>(c))) out += c;
}
return out;
}
static constexpr const char* BLOCK_START = "#---DROPSHELL-TOOL-START---";
static constexpr const char* BLOCK_END = "#---DROPSHELL-TOOL-END---";
BashrcEditor::BashrcEditor(const std::string& bashrcPath) : bashrcPath(bashrcPath) {}
bool BashrcEditor::hasSourceLine(const std::string& scriptPath) const {
// TODO: Implement file reading to check for source line
std::ifstream infile(bashrcPath);
if (!infile) return false;
std::string line;
bool inBlock = false;
const std::string blockStart = removeWhitespace(BLOCK_START);
const std::string blockEnd = removeWhitespace(BLOCK_END);
const std::string target = "source \"" + scriptPath + "\"";
while (std::getline(infile, line)) {
std::string trimmed = removeWhitespace(line);
if (trimmed == blockStart) {
return true;
}
}
return false;
}
void BashrcEditor::addSourceLine(const std::string& scriptPath) {
// TODO: Implement file append logic
std::ifstream infile(bashrcPath);
std::vector<std::string> lines;
std::string line;
bool inBlock = false;
const std::string blockStart = removeWhitespace(BLOCK_START);
const std::string blockEnd = removeWhitespace(BLOCK_END);
while (std::getline(infile, line)) {
std::string trimmed = removeWhitespace(line);
if (trimmed == blockStart) {
inBlock = true;
continue;
}
if (trimmed == blockEnd) {
inBlock = false;
continue;
}
if (!inBlock) {
lines.push_back(line);
}
}
infile.close();
lines.push_back(BLOCK_START);
lines.push_back("source \"" + scriptPath + "\"");
lines.push_back(BLOCK_END);
std::ofstream outfile(bashrcPath, std::ios::trunc);
for (const auto& l : lines) {
outfile << l << "\n";
}
}
void BashrcEditor::removeSourceLine(const std::string& scriptPath) {
// TODO: Implement file removal logic
void BashrcEditor::removeSourceLine(const std::string& /*scriptPath*/) {
std::ifstream infile(bashrcPath);
std::vector<std::string> lines;
std::string line;
bool inBlock = false;
const std::string blockStart = removeWhitespace(BLOCK_START);
const std::string blockEnd = removeWhitespace(BLOCK_END);
while (std::getline(infile, line)) {
std::string trimmed = removeWhitespace(line);
if (trimmed == blockStart) {
inBlock = true;
continue;
}
if (trimmed == blockEnd) {
inBlock = false;
continue;
}
if (!inBlock) {
lines.push_back(line);
}
}
infile.close();
std::ofstream outfile(bashrcPath, std::ios::trunc);
for (const auto& l : lines) {
outfile << l << "\n";
}
}

View File

@ -4,8 +4,11 @@
class BashrcEditor {
public:
BashrcEditor(const std::string& bashrcPath);
// Checks if the unique block with the source line exists
bool hasSourceLine(const std::string& scriptPath) const;
// Adds or updates the unique block with the given scriptPath
void addSourceLine(const std::string& scriptPath);
// Removes the unique block
void removeSourceLine(const std::string& scriptPath);
private:
std::string bashrcPath;