File: configuration.h

package info (click to toggle)
rust-rust-apt 0.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 620 kB
  • sloc: cpp: 703; makefile: 8
file content (116 lines) | stat: -rw-r--r-- 3,481 bytes parent folder | download
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#pragma once
#include <apt-pkg/aptconfiguration.h>
#include <apt-pkg/configuration.h>
#include <apt-pkg/init.h>
#include <apt-pkg/pkgsystem.h>
#include <sstream>
#include "rust/cxx.h"

#include "types.h"

/// The configuration pointer is global.
/// We do not need to make a new unique one.

/// Initialize the apt configuration.
void init_config() { pkgInitConfig(*_config); }
/// Initialize the apt system.

void init_system() { pkgInitSystem(*_config, _system); }

struct ConfigTree {
	const Configuration::Item* ptr;

	bool end() const { return ptr == 0; }

	UniquePtr<ConfigTree> raw_next() const { return std::make_unique<ConfigTree>(ptr->Next); }

	UniquePtr<ConfigTree> unique() const { return std::make_unique<ConfigTree>(ptr); }

	UniquePtr<ConfigTree> parent() const { return std::make_unique<ConfigTree>(ptr->Parent); }

	UniquePtr<ConfigTree> child() const { return std::make_unique<ConfigTree>(ptr->Child); }

	String tag() const { return ptr->Tag; }

	String value() const { return ptr->Value; }

	ConfigTree(const Configuration::Item* base) : ptr(base){};
};

UniquePtr<ConfigTree> root_tree() { return std::make_unique<ConfigTree>(_config->Tree(0)); }

UniquePtr<ConfigTree> tree(String key) {
	return std::make_unique<ConfigTree>(_config->Tree(key.c_str()));
}

/// Returns a String dump of configuration options separated by `\n`
String dump() {
	std::stringstream String_stream;
	_config->Dump(String_stream);
	return String_stream.str();
}

/// Find a key and return it's value as a String.
String find(String key, String default_value) {
	return _config->Find(key.c_str(), default_value.c_str());
}

/// Find a file and return it's value as a String.
String find_file(String key, String default_value) {
	return _config->FindFile(key.c_str(), default_value.c_str());
}

/// Find a directory and return it's value as a String.
String find_dir(String key, String default_value) {
	return _config->FindDir(key.c_str(), default_value.c_str());
}

/// Same as find, but for boolean values.
bool find_bool(String key, bool default_value) {
	return _config->FindB(key.c_str(), default_value);
}

/// Same as find, but for i32 values.
int find_int(String key, i32 default_value) { return _config->FindI(key.c_str(), default_value); }

/// Return a vector for an Apt configuration list.
Vec<String> find_vector(String key) {
	std::vector<std::string> vector = _config->FindVector(key.c_str());
	Vec<String> rust_vector;

	for (const std::string& str : vector) {
		rust_vector.push_back(str);
	}

	return rust_vector;
}

/// Return a vector of supported architectures on this system.
/// The main architecture is the first in the list.
Vec<String> get_architectures() {
	Vec<String> rust_vector;

	for (const std::string& str : APT::Configuration::getArchitectures()) {
		rust_vector.push_back(str);
	}

	return rust_vector;
}

/// Set the given key to the specified value.
void set(String key, String value) { _config->Set(key.c_str(), value.c_str()); }

/// Simply check if a key exists.
bool exists(String key) { return _config->Exists(key.c_str()); }

/// Clears all values from a key.
///
/// If the value is a list, the entire list is cleared.
/// If you need to clear 1 value from a list see `clear_value`
void clear(String key) { _config->Clear(key.c_str()); }

/// Clear all configurations.
void clear_all() { _config->Clear(); }

/// Clear a single value from a list.
void clear_value(String key, String value) { _config->Clear(key.c_str(), value.c_str()); }