diff --git a/dropshell-tool/src/BashrcEditor.cpp b/dropshell-tool/src/BashrcEditor.cpp index 1b617da..e6fd836 100644 --- a/dropshell-tool/src/BashrcEditor.cpp +++ b/dropshell-tool/src/BashrcEditor.cpp @@ -1,18 +1,95 @@ #include "BashrcEditor.hpp" #include +#include +#include #include +#include + +std::string removeWhitespace(const std::string& s) { + std::string out; + for (char c : s) { + if (!isspace(static_cast(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 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 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"; + } } \ No newline at end of file diff --git a/dropshell-tool/src/BashrcEditor.hpp b/dropshell-tool/src/BashrcEditor.hpp index 259acfb..ce27280 100644 --- a/dropshell-tool/src/BashrcEditor.hpp +++ b/dropshell-tool/src/BashrcEditor.hpp @@ -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;