diff options
| author | Lexi Winter <lexi@le-fay.org> | 2025-06-28 20:40:25 +0100 |
|---|---|---|
| committer | Lexi Winter <lexi@le-fay.org> | 2025-06-28 20:40:25 +0100 |
| commit | c54ff48ac3abb62a40eb1a438da8e3e7ef139797 (patch) | |
| tree | 4a78c556c7cbc6d3d7e364ca0c52c57ac0f5094b /nihil.posix/posix.argv.cc | |
| parent | a2d7181700ac64b8e7a4472ec26dfa253b38f188 (diff) | |
| download | nihil-c54ff48ac3abb62a40eb1a438da8e3e7ef139797.tar.gz nihil-c54ff48ac3abb62a40eb1a438da8e3e7ef139797.tar.bz2 | |
posix: add tempfile()
Diffstat (limited to 'nihil.posix/posix.argv.cc')
| -rw-r--r-- | nihil.posix/posix.argv.cc | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/nihil.posix/posix.argv.cc b/nihil.posix/posix.argv.cc new file mode 100644 index 0000000..e6b1389 --- /dev/null +++ b/nihil.posix/posix.argv.cc @@ -0,0 +1,65 @@ +/* + * This source code is released into the public domain. + */ + +module; + +#include <memory> +#include <ranges> +#include <string> +#include <vector> + +module nihil.posix; + +namespace nihil { + +argv::argv() = default; +argv::argv(argv &&) noexcept = default; +auto argv::operator=(this argv &, argv &&) -> argv & = default; + +argv::~argv() +{ + for (auto *arg : m_args) + delete[] arg; +} + +auto argv::data(this argv const &self) -> char const * const * +{ + return self.m_args.data(); +} + +auto argv::data(this argv &self) -> char * const * +{ + return self.m_args.data(); +} + +auto argv::size(this argv const &self) +{ + return self.m_args.size(); +} + +auto argv::begin(this argv const &self) +{ + return self.m_args.begin(); +} + +auto argv::end(this argv const &self) +{ + return self.m_args.end(); +} + + +auto argv::add_arg(this argv &self, std::string_view arg) -> void +{ + // Create a nul-terminated C string. + auto ptr = std::make_unique<char[]>(arg.size() + 1); + std::ranges::copy(arg, ptr.get()); + ptr[arg.size()] = '\0'; + + // Ensure we won't throw when emplacing the pointer. + self.m_args.reserve(self.m_args.size() + 1); + self.m_args.emplace_back(ptr.release()); +} + +} // namespace nihil + |
