diff options
| author | BossCode45 <boss@tehbox.org> | 2025-05-06 18:00:13 +1200 |
|---|---|---|
| committer | BossCode45 <boss@tehbox.org> | 2025-05-06 18:00:13 +1200 |
| commit | ecc333d03abe308ce47c05345f8f90ae6a01b215 (patch) | |
| tree | 6aa4cc449bf9289c2ac8414931f8a536299c4490 /src/commands.cpp | |
| parent | d1d4a63d4473cd4910b678cf5b385f622186fbd3 (diff) | |
| parent | bc9c5f43f5c8108c3d617716b2c3aeaf553a03e3 (diff) | |
| download | YATwm-ecc333d03abe308ce47c05345f8f90ae6a01b215.tar.gz YATwm-ecc333d03abe308ce47c05345f8f90ae6a01b215.zip | |
Merge branch 'nix'
Diffstat (limited to 'src/commands.cpp')
| -rw-r--r-- | src/commands.cpp | 43 |
1 files changed, 40 insertions, 3 deletions
diff --git a/src/commands.cpp b/src/commands.cpp index 5688da4..caa82eb 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -3,15 +3,14 @@ #include "util.h" #include <cctype> +#include <filesystem> #include <iostream> #include <algorithm> -#include <iterator> #include <stdexcept> #include <string> -#include <utility> #include <vector> -#include <regex> #include <cstring> +#include <fstream> using std::cout, std::endl, std::string, std::vector; @@ -20,9 +19,47 @@ const void CommandsModule::echo(const CommandArg* argv) cout << argv[0].str << endl; } +const void CommandsModule::loadFile(const CommandArg* argv) +{ + std::vector<Err> errs; + + string path = cwd + "/" + std::string(argv[0].str); + + std::ifstream file(path); + if(!file.good()) + throw Err(CMD_ERR_NON_FATAL, "File '" + std::string(argv[0].str) + "' doesn't exist"); + + string prevCWD = cwd; + cwd = std::filesystem::path(path).parent_path(); + + cout << "Loading file '" << argv[0].str << "'" << endl; + + int line = 0; + for(string cmd; std::getline(file, cmd);) + { + line++; + if(cmd.size() == 0) + continue; + if(cmd.at(0) == '#') + continue; + try + { + this->runCommand(cmd); + } + catch (Err e) + { + throw Err(e.code, "Error in file '" + std::string(argv[0].str) + "' (line " + std::to_string(line) + "): " + std::to_string(e.code) + "\n\tMessage: " + e.message); + } + } + + cwd = prevCWD; +} + CommandsModule::CommandsModule() { addCommand("echo", &CommandsModule::echo, 1, {STR_REST}, this); + addCommand("loadFile", &CommandsModule::loadFile, 1, {STR_REST}, this); + cwd = std::filesystem::current_path(); } CommandsModule::~CommandsModule() { |
