-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tools.cpp
247 lines (194 loc) · 5.73 KB
/
Tools.cpp
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
libmimeapps, an implementation of
http://standards.freedesktop.org/mime-apps-spec/mime-apps-spec-latest.html
Copyright (c) 2015, Piotr Wójcik
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Tools.h"
#include <dirent.h>
#include <sys/stat.h>
#include <algorithm>
#include <cstring>
#include <map>
#include <sstream>
namespace LibMimeApps
{
bool startsWith(const std::string &str, const std::string &prefix)
{
if (str.size() < prefix.size())
{
return false;
}
return std::equal(prefix.begin(), prefix.end(), str.begin());
}
bool endsWith(std::string const &str, std::string const &suffix)
{
if (str.size() < suffix.size())
{
return false;
}
return std::equal(suffix.rbegin(), suffix.rend(), str.rbegin());
}
std::vector<std::string> split(const std::string &str, char delimiter)
{
std::vector<std::string> chunks;
std::stringstream stream(str);
std::string item;
while (std::getline(stream, item, delimiter))
{
chunks.push_back(item);
}
return chunks;
}
bool match(std::string const& text, std::string const& pattern)
{
return std::search(text.begin(), text.end(), pattern.begin(), pattern.end()) != text.end();
}
std::vector<file> directoryEntries(const std::string &directory)
{
std::vector<file> result;
DIR *stream = opendir(directory.c_str());
if (stream == NULL)
{
return result;
}
struct dirent *entry;
struct stat info;
std::string name;
std::string path;
while ((entry = readdir(stream)) != NULL)
{
name = entry->d_name;
path = directory + name;
stat(path.c_str(), &info);
if (name.size() > 0 && name.at(0) != '.')
{
if (S_ISREG(info.st_mode))
{
result.push_back(file(name, FileType::File));
}
else if (S_ISDIR(info.st_mode))
{
result.push_back(file(name, FileType::Directory));
}
}
}
closedir(stream);
return result;
}
std::vector<std::string> unfoldVariable(const std::string &string)
{
std::vector<std::string> result;
size_t begin = string.find('$');
if (begin == std::string::npos)
{
result.push_back(string);
return result;
}
size_t end = begin+1;
while (end < string.size() && (std::isalnum(string.at(end)) || string.at(end) == '_'))
{
++end;
}
std::string name = string.substr(begin+1, end-(begin+1));
std::vector<std::string> values = getVariableValues(name);
for (std::vector<std::string>::iterator value = values.begin(); value != values.end(); ++value)
{
result.push_back(string.substr(0, begin) + (*value) + string.substr(end));
}
return result;
}
std::vector<std::string> getVariableValues(const std::string &name)
{
std::string value;
char *env = std::getenv(name.c_str());
if (env && std::strlen(env) > 0)
{
value = std::string(env);
}
else
{
std::map<std::string, std::string> defaultVariableValues;
defaultVariableValues["XDG_DATA_HOME"] = std::string("$HOME/.local/share");
defaultVariableValues["XDG_CONFIG_HOME"] = std::string("$HOME/.config");
defaultVariableValues["XDG_DATA_DIRS"] = std::string("/usr/local/share/:/usr/share/");
defaultVariableValues["XDG_CONFIG_DIRS"] = std::string("/etc/xdg");
if (defaultVariableValues.find(name) != defaultVariableValues.end())
{
value = defaultVariableValues.at(name);
}
}
return split(value, ':');
}
std::string getLocaleValue(const ConfigReader &config, const std::string &group, const std::string &key, const std::string &language)
{
lang wanted(language);
std::vector<std::string> localeKeys;
if (wanted.country.size() > 0 && wanted.modifier.size() > 0)
{
localeKeys.push_back(wanted.language + "_" + wanted.country + "@" + wanted.modifier);
}
if (wanted.country.size() > 0)
{
localeKeys.push_back(wanted.language + "_" + wanted.country);
}
if (wanted.modifier.size() > 0)
{
localeKeys.push_back(wanted.language + "@" + wanted.modifier);
}
localeKeys.push_back(wanted.language);
for (std::vector<std::string>::size_type i = 0; i < localeKeys.size(); ++i)
{
std::string localeKey = key + "[" + localeKeys[i] + "]";
if (config.hasKey(group, localeKey))
{
return config.value(group, localeKey);
}
}
return config.value(group, key);
}
std::string alnums(const std::string &string, size_t begin)
{
if (begin == std::string::npos || begin >= string.size())
{
return std::string();
}
size_t end = begin;
while (end < string.size() && std::isalnum(string.at(end)))
{
++end;
}
return string.substr(begin, end-begin);
}
lang::lang(const std::string &string):
language(alnums(string, 0))
{
if (match(string, "_"))
{
country = alnums(string, string.find('_')+1);
}
if (match(string, "@"))
{
modifier = alnums(string, string.find('@')+1);
}
}
}