aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.posix/argv.cc
diff options
context:
space:
mode:
Diffstat (limited to 'nihil.posix/argv.cc')
-rw-r--r--nihil.posix/argv.cc65
1 files changed, 65 insertions, 0 deletions
diff --git a/nihil.posix/argv.cc b/nihil.posix/argv.cc
new file mode 100644
index 0000000..e6b1389
--- /dev/null
+++ b/nihil.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
+