diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/pointerlist.cc | 123 | ||||
-rw-r--r-- | src/pointerlist.h | 74 |
2 files changed, 197 insertions, 0 deletions
diff --git a/src/pointerlist.cc b/src/pointerlist.cc new file mode 100644 index 0000000..c0242f7 --- /dev/null +++ b/src/pointerlist.cc @@ -0,0 +1,123 @@ +// -*- c++ -*- +// Distributed under the BSD 2-Clause License. +// See accompanying file LICENSE for details. +#include "pointerlist.h" + +#include <cstring> + +PointerList::PointerList(int argc, const char* const argv[]) +{ + for(int i = 0; i < argc; ++i) + { + push_back(argv[i]); + } +} + +std::pair<int, const char* const*> PointerList::get() +{ + argptrs.clear(); + for(const auto& arg : *this) + { + argptrs.push_back(arg.data()); + } + argptrs.push_back(nullptr); + + return {argptrs.size() - 1, // size not counting the nullptr at the end + argptrs.data()}; +} + +EnvMap::EnvMap(const char* const env[]) +{ + if(env == nullptr) + { + return; + } + + auto ptr = env; + while(*ptr) + { + insert(std::string_view(*ptr)); + ++ptr; + } +} + +EnvMap::EnvMap(const char* env) +{ + if(env == nullptr) + { + return; + } + + auto ptr = env; + while(*ptr) + { + insert(ptr); + ptr += strlen(ptr) + 1; + } +} + +std::string EnvMap::operator[](const std::string& key) const +{ + try + { + return data.at(key); + } + catch(...) + { + return {}; + } +} + +void EnvMap::clear() +{ + data.clear(); +} + +void EnvMap::insert(std::string_view key_value) +{ + auto equals_sign = key_value.find('='); + if(equals_sign == std::string::npos) + { + insert({key_value, ""}); + return; + } + std::string key{key_value.substr(0, equals_sign)}; + std::string value{key_value.substr(equals_sign + 1)}; // skip '=' + insert({key, value}); +} + +void EnvMap::insert(const std::pair<std::string_view, std::string_view>& item) +{ + data[std::string(item.first)] = item.second; +} + +bool EnvMap::contains(std::string_view key) const +{ + return data.contains(std::string{key}); +} + +std::size_t EnvMap::size() const +{ + return data.size(); +} + +std::string EnvMap::stringify() const +{ + std::string str; + for(const auto& [key, value] : data) + { + str += key + "=" + value + '\0'; + } + str += '\0'; + return str; +} + +std::pair<int, const char* const*> EnvMap::get() +{ + pointerlist.clear(); + for(const auto& [key, value] : data) + { + pointerlist.push_back(key + "=" + value + '\0'); + } + return pointerlist.get(); +} diff --git a/src/pointerlist.h b/src/pointerlist.h new file mode 100644 index 0000000..988bb26 --- /dev/null +++ b/src/pointerlist.h @@ -0,0 +1,74 @@ +// -*- c++ -*- +// Distributed under the BSD 2-Clause License. +// See accompanying file LICENSE for details. +#pragma once + +#include <string> +#include <vector> +#include <deque> +#include <utility> +#include <map> + +//! Maintains an (owning) list of string args and converts them to argc/argv +//! compatible arguments on request. +//! The returned pointers are guaranteed to be valid as long as the PointerList +//! object lifetime is not exceeded. +class PointerList + : public std::deque<std::string> +{ +public: + PointerList() = default; + PointerList(int argc, const char* const argv[]); + + //! Returns argc/argv pair from the current list of args + //! The argv entry after the last is a nullptr (not included in the argc) + std::pair<int, const char* const*> get(); + +private: + std::vector<const char*> argptrs; +}; + + +//! Maintains an owning map of strings representing the env. +class EnvMap +{ +public: + EnvMap() = default; + + //! Initialize from an array of pointers to key=value\0 strings terminated + //! by \0 + EnvMap(const char* const env[]); + + //! Initialize from a string of the format + //! key1=val\0key2=val\0...keyN=val\0\0 + EnvMap(const char* env); + + std::string operator[](const std::string& key) const; + + //! Clear all items in the map + void clear(); + + //! Insert string from format: key=value + void insert(std::string_view key_value); + + //! Regular map insert + void insert(const std::pair<std::string_view, std::string_view>& item); + + //! Checks if the container contains element with specific key + bool contains(std::string_view key) const; + + std::size_t size() const; + + //! Return string with the following format: + //! key1=val\0key2=val\0...keyN=val\0\0 + std::string stringify() const; + + //! Returns the map as argc/argv pair where each pointer points to a string + //! of the format key=value\0 and is terminated with a nullptr + std::pair<int, const char* const*> get(); + +private: + std::map<std::string, std::string> data{}; + + PointerList pointerlist; +}; |