summaryrefslogtreecommitdiff
path: root/src/pointerlist.h
blob: 988bb26bc168dccb34f35bf92b38bc9f4896eb66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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;
};