aboutsummaryrefslogtreecommitdiff
path: root/src/filepath.cpp
blob: caaf7da5a65bd1e0a53659438fddffaca2439bfc (plain) (blame)
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
117
118
119
120
121
122
123
124
#include "filepath.h"

namespace newsboat {

inline namespace {

rust::Vec<std::uint8_t> string_to_vec(const std::string& input)
{
	rust::Vec<std::uint8_t> result;
	result.reserve(input.length());
	for (const auto byte : input) {
		result.push_back(byte);
	}
	return result;
}

}

Filepath::Filepath()
	: rs_object(filepath::bridged::create_empty())
{
}

Filepath Filepath::from_locale_string(const std::string& filepath)
{
	Filepath result;
	result.rs_object = filepath::bridged::create(string_to_vec(filepath));
	return result;
}

Filepath::Filepath(const Filepath& filepath)
	: rs_object(filepath::bridged::clone(*filepath.rs_object))
{
}

Filepath& Filepath::operator=(const Filepath& filepath)
{
	if (this == &filepath) {
		return *this;
	}
	rs_object = filepath::bridged::clone(*filepath.rs_object);
	return *this;
}

std::string Filepath::to_locale_string() const
{
	const auto bytes = filepath::bridged::into_bytes(*rs_object);
	return std::string(std::begin(bytes), std::end(bytes));
}

std::string Filepath::display() const
{
	return std::string(filepath::bridged::display(*rs_object));
}

bool Filepath::operator==(const Filepath& other) const
{
	return filepath::bridged::equals(*rs_object, *other.rs_object);
}

bool Filepath::operator!=(const Filepath& other) const
{
	return !(*this == other);
}

bool Filepath::operator<(const Filepath& other) const
{
	return filepath::bridged::less_than(*rs_object, *other.rs_object);
}

bool Filepath::operator<=(const Filepath& other) const
{
	return !(*this > other);
}

bool Filepath::operator>(const Filepath& other) const
{
	return !(*this < other) && (*this != other);
}

bool Filepath::operator>=(const Filepath& other) const
{
	return !(*this < other);
}

void Filepath::push(const Filepath& component)
{
	filepath::bridged::push(*rs_object, *component.rs_object);
}

Filepath Filepath::join(const Filepath& component) const
{
	auto result = *this;
	result.push(component);
	return result;
}

bool Filepath::is_absolute() const
{
	return filepath::bridged::is_absolute(*rs_object);
}

bool Filepath::set_extension(const std::string& ext)
{
	return filepath::bridged::set_extension(*rs_object, string_to_vec(ext));
}

bool Filepath::starts_with(const Filepath& base) const
{
	return filepath::bridged::starts_with(*rs_object, *base.rs_object);
}

nonstd::optional<Filepath> Filepath::file_name() const
{
	auto str = filepath::bridged::file_name(*rs_object);
	auto res = std::string(str.begin(), str.end());
	if (res.empty()) {
		return nonstd::nullopt;
	} else {
		return from_locale_string(res);
	}
}

} // namespace newsboat