diff options
| author | Hato <108814993+Hato1125@users.noreply.github.com> | 2025-08-18 04:18:51 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-17 20:18:51 +0100 |
| commit | d8901786109dba6af3eac03c1e723f807ed0117a (patch) | |
| tree | 05690a0ee6ad28f5b76a5b03a54b141a81167250 /src/main.cpp | |
| parent | bca96a5d3be8f3b02171a031511848d031ffa4b0 (diff) | |
internal: reference command-line arguments instead of copying them (#11422)
Eliminates the need to copy command-line arguments into a std::vector<std::string>, reducing memory usage and improving performance by referencing the original arguments directly.
Diffstat (limited to 'src/main.cpp')
| -rw-r--r-- | src/main.cpp | 144 |
1 files changed, 76 insertions, 68 deletions
diff --git a/src/main.cpp b/src/main.cpp index d7624c4e..8467ea6e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,8 +7,10 @@ #include <cstdio> #include <hyprutils/string/String.hpp> +#include <hyprutils/memory/Casts.hpp> #include <print> using namespace Hyprutils::String; +using namespace Hyprutils::Memory; #include <fcntl.h> #include <iostream> @@ -16,6 +18,8 @@ using namespace Hyprutils::String; #include <vector> #include <stdexcept> #include <string> +#include <string_view> +#include <span> #include <filesystem> static void help() { @@ -48,92 +52,96 @@ int main(int argc, char** argv) { setenv("MOZ_ENABLE_WAYLAND", "1", 1); // parse some args - std::string configPath; - std::string socketName; - int socketFd = -1; - bool ignoreSudo = false, verifyConfig = false; + std::string configPath; + std::string socketName; + int socketFd = -1; + bool ignoreSudo = false, verifyConfig = false; - std::vector<std::string> args{argv + 1, argv + argc}; + if (argc > 1) { + std::span<char*> args{argv + 1, sc<std::size_t>(argc - 1)}; - for (auto it = args.begin(); it != args.end(); it++) { - if (*it == "--i-am-really-stupid" && !ignoreSudo) { - std::println("[ WARNING ] Running Hyprland with superuser privileges might damage your system"); + for (auto it = args.begin(); it != args.end(); it++) { + std::string_view value = *it; - ignoreSudo = true; - } else if (*it == "--socket") { - if (std::next(it) == args.end()) { - help(); + if (value == "--i-am-really-stupid" && !ignoreSudo) { + std::println("[ WARNING ] Running Hyprland with superuser privileges might damage your system"); - return 1; - } + ignoreSudo = true; + } else if (value == "--socket") { + if (std::next(it) == args.end()) { + help(); - socketName = *std::next(it); - it++; - } else if (*it == "--wayland-fd") { - if (std::next(it) == args.end()) { - help(); + return 1; + } - return 1; - } + socketName = *std::next(it); + it++; + } else if (value == "--wayland-fd") { + if (std::next(it) == args.end()) { + help(); - try { - socketFd = std::stoi(*std::next(it)); + return 1; + } - // check if socketFd is a valid file descriptor - if (fcntl(socketFd, F_GETFD) == -1) - throw std::exception(); - } catch (...) { - std::println(stderr, "[ ERROR ] Invalid Wayland FD!"); - help(); + try { + socketFd = std::stoi(*std::next(it)); - return 1; - } + // check if socketFd is a valid file descriptor + if (fcntl(socketFd, F_GETFD) == -1) + throw std::exception(); + } catch (...) { + std::println(stderr, "[ ERROR ] Invalid Wayland FD!"); + help(); - it++; - } else if (*it == "-c" || *it == "--config") { - if (std::next(it) == args.end()) { - help(); + return 1; + } - return 1; - } - configPath = *std::next(it); + it++; + } else if (value == "-c" || value == "--config") { + if (std::next(it) == args.end()) { + help(); + + return 1; + } + configPath = *std::next(it); + + try { + configPath = std::filesystem::canonical(configPath); - try { - configPath = std::filesystem::canonical(configPath); + if (!std::filesystem::is_regular_file(configPath)) { + throw std::exception(); + } + } catch (...) { + std::println(stderr, "[ ERROR ] Config file '{}' doesn't exist!", configPath); + help(); - if (!std::filesystem::is_regular_file(configPath)) { - throw std::exception(); + return 1; } - } catch (...) { - std::println(stderr, "[ ERROR ] Config file '{}' doesn't exist!", configPath); + + Debug::log(LOG, "User-specified config location: '{}'", configPath); + + it++; + + continue; + } else if (value == "-h" || value == "--help") { + help(); + + return 0; + } else if (value == "-v" || value == "--version") { + std::println("{}", versionRequest(eHyprCtlOutputFormat::FORMAT_NORMAL, "")); + return 0; + } else if (value == "--systeminfo") { + std::println("{}", systemInfoRequest(eHyprCtlOutputFormat::FORMAT_NORMAL, "")); + return 0; + } else if (value == "--verify-config") { + verifyConfig = true; + continue; + } else { + std::println(stderr, "[ ERROR ] Unknown option '{}' !", value); help(); return 1; } - - Debug::log(LOG, "User-specified config location: '{}'", configPath); - - it++; - - continue; - } else if (*it == "-h" || *it == "--help") { - help(); - - return 0; - } else if (*it == "-v" || *it == "--version") { - std::println("{}", versionRequest(eHyprCtlOutputFormat::FORMAT_NORMAL, "")); - return 0; - } else if (*it == "--systeminfo") { - std::println("{}", systemInfoRequest(eHyprCtlOutputFormat::FORMAT_NORMAL, "")); - return 0; - } else if (*it == "--verify-config") { - verifyConfig = true; - continue; - } else { - std::println(stderr, "[ ERROR ] Unknown option '{}' !", *it); - help(); - - return 1; } } |
