2017-02-17 09:57:44 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2018-04-22 17:01:44 +00:00
|
|
|
#include "filesystem.hh"
|
2017-05-23 07:24:14 +00:00
|
|
|
#include "platform.h"
|
|
|
|
|
2017-12-22 16:48:12 +00:00
|
|
|
#include <doctest/doctest.h>
|
2018-01-30 00:27:43 +00:00
|
|
|
#include <siphash.h>
|
2017-11-18 19:02:09 +00:00
|
|
|
#include <loguru/loguru.hpp>
|
2017-05-23 07:24:14 +00:00
|
|
|
|
2018-03-31 18:32:28 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
2018-04-07 17:43:56 +00:00
|
|
|
#include <errno.h>
|
2018-03-31 18:32:28 +00:00
|
|
|
#include <string.h>
|
2017-05-09 01:21:21 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <functional>
|
2017-04-08 22:54:36 +00:00
|
|
|
#include <unordered_map>
|
2018-03-31 18:32:28 +00:00
|
|
|
using namespace std::placeholders;
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-12-19 00:35:43 +00:00
|
|
|
// DEFAULT_RESOURCE_DIRECTORY is passed with quotes for non-MSVC compilers, ie,
|
|
|
|
// foo vs "foo".
|
2017-12-22 16:48:12 +00:00
|
|
|
#if defined(_MSC_VER)
|
2017-12-19 00:35:43 +00:00
|
|
|
#define _STRINGIFY(x) #x
|
|
|
|
#define ENSURE_STRING_MACRO_ARGUMENT(x) _STRINGIFY(x)
|
|
|
|
#else
|
2017-12-18 19:47:58 +00:00
|
|
|
#define ENSURE_STRING_MACRO_ARGUMENT(x) x
|
2017-12-19 00:35:43 +00:00
|
|
|
#endif
|
2017-12-16 05:18:49 +00:00
|
|
|
|
2018-03-31 17:37:03 +00:00
|
|
|
void TrimInPlace(std::string& s) {
|
2018-03-31 18:32:28 +00:00
|
|
|
auto f = [](char c) { return !isspace(c); };
|
|
|
|
s.erase(s.begin(), std::find_if(s.begin(), s.end(), f));
|
|
|
|
s.erase(std::find_if(s.rbegin(), s.rend(), f).base(), s.end());
|
2017-04-16 08:09:12 +00:00
|
|
|
}
|
2017-12-22 16:48:12 +00:00
|
|
|
std::string Trim(std::string s) {
|
|
|
|
TrimInPlace(s);
|
|
|
|
return s;
|
2017-04-16 08:09:12 +00:00
|
|
|
}
|
2018-01-13 19:39:06 +00:00
|
|
|
|
2018-03-31 18:32:28 +00:00
|
|
|
uint64_t HashUsr(std::string_view s) {
|
2018-01-13 19:39:06 +00:00
|
|
|
union {
|
|
|
|
uint64_t ret;
|
|
|
|
uint8_t out[8];
|
|
|
|
};
|
2018-01-14 21:18:12 +00:00
|
|
|
// k is an arbitrary key. Don't change it.
|
2018-01-13 19:39:06 +00:00
|
|
|
const uint8_t k[16] = {0xd0, 0xe5, 0x4d, 0x61, 0x74, 0x63, 0x68, 0x52,
|
|
|
|
0x61, 0x79, 0xea, 0x70, 0xca, 0x70, 0xf0, 0x0d};
|
2018-03-31 18:32:28 +00:00
|
|
|
(void)siphash(reinterpret_cast<const uint8_t*>(s.data()), s.size(), k, out, 8);
|
2018-01-13 19:39:06 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-03-31 18:32:28 +00:00
|
|
|
bool EndsWith(std::string_view s, std::string_view suffix) {
|
|
|
|
return s.size() >= suffix.size() &&
|
|
|
|
std::equal(suffix.rbegin(), suffix.rend(), s.rbegin());
|
2017-03-31 04:15:42 +00:00
|
|
|
}
|
|
|
|
|
2018-03-31 18:32:28 +00:00
|
|
|
bool StartsWith(std::string_view s, std::string_view prefix) {
|
|
|
|
return s.size() >= prefix.size() &&
|
|
|
|
std::equal(prefix.begin(), prefix.end(), s.begin());
|
2017-03-31 04:15:42 +00:00
|
|
|
}
|
|
|
|
|
2018-03-31 18:32:28 +00:00
|
|
|
bool AnyStartsWith(const std::vector<std::string>& xs,
|
|
|
|
std::string_view prefix) {
|
|
|
|
return std::any_of(xs.begin(), xs.end(), std::bind(StartsWith, _1, prefix));
|
2017-05-21 19:51:15 +00:00
|
|
|
}
|
|
|
|
|
2018-03-31 18:32:28 +00:00
|
|
|
bool StartsWithAny(std::string_view s, const std::vector<std::string>& ps) {
|
|
|
|
return std::any_of(ps.begin(), ps.end(), std::bind(StartsWith, s, _1));
|
2017-10-18 08:24:52 +00:00
|
|
|
}
|
|
|
|
|
2018-03-31 18:32:28 +00:00
|
|
|
bool EndsWithAny(std::string_view s, const std::vector<std::string>& ss) {
|
|
|
|
return std::any_of(ss.begin(), ss.end(), std::bind(EndsWith, s, _1));
|
2017-05-21 19:51:15 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 17:37:33 +00:00
|
|
|
bool FindAnyPartial(const std::string& value,
|
|
|
|
const std::vector<std::string>& values) {
|
2018-01-30 00:27:43 +00:00
|
|
|
return std::any_of(std::begin(values), std::end(values),
|
|
|
|
[&value](const std::string& v) {
|
|
|
|
return value.find(v) != std::string::npos;
|
|
|
|
});
|
2018-01-12 17:37:33 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
std::vector<std::string> SplitString(const std::string& str,
|
|
|
|
const std::string& delimiter) {
|
2017-05-19 01:14:53 +00:00
|
|
|
// http://stackoverflow.com/a/13172514
|
|
|
|
std::vector<std::string> strings;
|
|
|
|
|
|
|
|
std::string::size_type pos = 0;
|
|
|
|
std::string::size_type prev = 0;
|
|
|
|
while ((pos = str.find(delimiter, prev)) != std::string::npos) {
|
|
|
|
strings.push_back(str.substr(prev, pos - prev));
|
|
|
|
prev = pos + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// To get the last substring (or only, if delimiter is not found)
|
|
|
|
strings.push_back(str.substr(prev));
|
|
|
|
|
|
|
|
return strings;
|
|
|
|
}
|
|
|
|
|
2018-03-31 18:32:28 +00:00
|
|
|
std::string LowerPathIfInsensitive(const std::string& path) {
|
2017-05-22 07:14:11 +00:00
|
|
|
#if defined(_WIN32)
|
2018-03-31 18:32:28 +00:00
|
|
|
std::string ret = path;
|
|
|
|
for (char& c : ret)
|
|
|
|
c = tolower(c);
|
|
|
|
return ret;
|
|
|
|
#else
|
|
|
|
return path;
|
2017-05-22 07:14:11 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-05-21 19:51:15 +00:00
|
|
|
void EnsureEndsInSlash(std::string& path) {
|
|
|
|
if (path.empty() || path[path.size() - 1] != '/')
|
|
|
|
path += '/';
|
|
|
|
}
|
|
|
|
|
2017-12-02 05:07:30 +00:00
|
|
|
std::string EscapeFileName(std::string path) {
|
2018-03-31 18:32:28 +00:00
|
|
|
bool slash = path.size() && path.back() == '/';
|
|
|
|
for (char& c : path)
|
|
|
|
if (c == '\\' || c == '/' || c == ':')
|
|
|
|
c = '@';
|
|
|
|
if (slash)
|
|
|
|
path += '/';
|
2017-12-02 05:07:30 +00:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2018-03-31 03:16:33 +00:00
|
|
|
std::optional<std::string> ReadContent(const std::string& filename) {
|
2018-01-30 05:34:28 +00:00
|
|
|
LOG_S(INFO) << "Reading " << filename;
|
2018-03-31 18:32:28 +00:00
|
|
|
char buf[4096];
|
|
|
|
std::string ret;
|
|
|
|
FILE* f = fopen(filename.c_str(), "rb");
|
|
|
|
if (!f) return {};
|
|
|
|
size_t n;
|
|
|
|
while ((n = fread(buf, 1, sizeof buf, f)) > 0)
|
|
|
|
ret.append(buf, n);
|
2018-04-22 17:01:44 +00:00
|
|
|
fclose(f);
|
2018-03-31 18:32:28 +00:00
|
|
|
return ret;
|
2017-04-17 07:06:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 08:36:11 +00:00
|
|
|
void WriteToFile(const std::string& filename, const std::string& content) {
|
2018-03-31 18:32:28 +00:00
|
|
|
FILE* f = fopen(filename.c_str(), "wb");
|
|
|
|
if (!f || fwrite(content.c_str(), content.size(), 1, f) != 1) {
|
2018-04-07 17:43:56 +00:00
|
|
|
LOG_S(ERROR) << "Failed to write to " << filename << ' ' << strerror(errno);
|
2018-01-04 17:33:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-03-31 18:32:28 +00:00
|
|
|
fclose(f);
|
2017-03-09 18:02:55 +00:00
|
|
|
}
|
2017-05-25 02:04:19 +00:00
|
|
|
|
2018-04-22 17:01:44 +00:00
|
|
|
std::optional<int64_t> LastWriteTime(const std::string& filename) {
|
|
|
|
std::error_code ec;
|
|
|
|
auto ftime = fs::last_write_time(filename, ec);
|
|
|
|
if (ec) return std::nullopt;
|
|
|
|
return std::chrono::time_point_cast<std::chrono::nanoseconds>(ftime)
|
|
|
|
.time_since_epoch()
|
|
|
|
.count();
|
|
|
|
}
|
|
|
|
|
2017-12-16 05:18:49 +00:00
|
|
|
std::string GetDefaultResourceDirectory() {
|
|
|
|
std::string result;
|
|
|
|
|
2017-12-22 16:48:12 +00:00
|
|
|
std::string resource_directory =
|
|
|
|
std::string(ENSURE_STRING_MACRO_ARGUMENT(DEFAULT_RESOURCE_DIRECTORY));
|
2018-01-03 12:38:01 +00:00
|
|
|
// Remove double quoted resource dir if it was passed with quotes
|
|
|
|
// by the build system.
|
|
|
|
if (resource_directory.size() >= 2 && resource_directory[0] == '"' &&
|
|
|
|
resource_directory[resource_directory.size() - 1] == '"') {
|
|
|
|
resource_directory =
|
|
|
|
resource_directory.substr(1, resource_directory.size() - 2);
|
|
|
|
}
|
2018-02-13 07:20:08 +00:00
|
|
|
if (resource_directory.compare(0, 2, "..") == 0) {
|
2017-12-16 05:18:49 +00:00
|
|
|
std::string executable_path = GetExecutablePath();
|
|
|
|
size_t pos = executable_path.find_last_of('/');
|
|
|
|
result = executable_path.substr(0, pos + 1);
|
|
|
|
result += resource_directory;
|
|
|
|
} else {
|
|
|
|
result = resource_directory;
|
|
|
|
}
|
|
|
|
|
2018-01-04 02:32:15 +00:00
|
|
|
return NormalizePath(result);
|
2017-12-18 19:47:58 +00:00
|
|
|
}
|