2017-02-17 09:57:44 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
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-05-23 07:24:14 +00:00
|
|
|
#include <tinydir.h>
|
2017-11-18 19:02:09 +00:00
|
|
|
#include <loguru/loguru.hpp>
|
2017-05-23 07:24:14 +00:00
|
|
|
|
2017-05-09 01:21:21 +00:00
|
|
|
#include <algorithm>
|
2017-03-06 07:19:00 +00:00
|
|
|
#include <cassert>
|
2017-04-16 08:09:12 +00:00
|
|
|
#include <cctype>
|
2018-01-13 19:39:06 +00:00
|
|
|
#include <cstring>
|
2017-09-22 01:14:57 +00:00
|
|
|
#include <fstream>
|
2017-05-09 01:21:21 +00:00
|
|
|
#include <functional>
|
2018-02-07 05:26:38 +00:00
|
|
|
#include <queue>
|
2017-04-16 08:09:12 +00:00
|
|
|
#include <sstream>
|
2018-01-03 12:38:01 +00:00
|
|
|
#include <string>
|
2017-04-08 22:54:36 +00:00
|
|
|
#include <unordered_map>
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2017-05-31 00:54:19 +00:00
|
|
|
#if !defined(__APPLE__)
|
|
|
|
#include <sparsepp/spp_memory.h>
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
2017-04-16 08:09:12 +00:00
|
|
|
// See http://stackoverflow.com/a/217605
|
2017-12-22 16:48:12 +00:00
|
|
|
void TrimStartInPlace(std::string& s) {
|
2017-09-22 01:14:57 +00:00
|
|
|
s.erase(s.begin(),
|
|
|
|
std::find_if(s.begin(), s.end(),
|
|
|
|
std::not1(std::ptr_fun<int, int>(std::isspace))));
|
2017-04-16 08:09:12 +00:00
|
|
|
}
|
2017-12-22 16:48:12 +00:00
|
|
|
void TrimEndInPlace(std::string& s) {
|
2017-04-16 08:09:12 +00:00
|
|
|
s.erase(std::find_if(s.rbegin(), s.rend(),
|
2017-09-22 01:14:57 +00:00
|
|
|
std::not1(std::ptr_fun<int, int>(std::isspace)))
|
|
|
|
.base(),
|
|
|
|
s.end());
|
2017-04-16 08:09:12 +00:00
|
|
|
}
|
2017-12-22 16:48:12 +00:00
|
|
|
void TrimInPlace(std::string& s) {
|
|
|
|
TrimStartInPlace(s);
|
|
|
|
TrimEndInPlace(s);
|
|
|
|
}
|
|
|
|
std::string Trim(std::string s) {
|
|
|
|
TrimInPlace(s);
|
|
|
|
return s;
|
2017-04-16 08:09:12 +00:00
|
|
|
}
|
2018-02-01 09:12:36 +00:00
|
|
|
void RemoveLastCR(std::string& s) {
|
|
|
|
if (!s.empty() && *s.rbegin() == '\r')
|
|
|
|
s.pop_back();
|
|
|
|
}
|
2017-04-16 08:09:12 +00:00
|
|
|
|
2018-01-14 21:18:12 +00:00
|
|
|
uint64_t HashUsr(const std::string& s) {
|
|
|
|
return HashUsr(s.c_str(), s.size());
|
2018-01-13 19:39:06 +00:00
|
|
|
}
|
|
|
|
|
2018-01-14 21:18:12 +00:00
|
|
|
uint64_t HashUsr(const char* s) {
|
|
|
|
return HashUsr(s, strlen(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t HashUsr(const char* s, size_t n) {
|
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};
|
|
|
|
(void)siphash(reinterpret_cast<const uint8_t*>(s), n, k, out, 8);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-31 04:15:42 +00:00
|
|
|
// See http://stackoverflow.com/a/2072890
|
|
|
|
bool EndsWith(const std::string& value, const std::string& ending) {
|
|
|
|
if (ending.size() > value.size())
|
|
|
|
return false;
|
|
|
|
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StartsWith(const std::string& value, const std::string& start) {
|
|
|
|
if (start.size() > value.size())
|
|
|
|
return false;
|
|
|
|
return std::equal(start.begin(), start.end(), value.begin());
|
|
|
|
}
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
bool AnyStartsWith(const std::vector<std::string>& values,
|
|
|
|
const std::string& start) {
|
|
|
|
return std::any_of(
|
|
|
|
std::begin(values), std::end(values),
|
|
|
|
[&start](const std::string& value) { return StartsWith(value, start); });
|
2017-05-21 19:51:15 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 08:24:52 +00:00
|
|
|
bool StartsWithAny(const std::string& value,
|
|
|
|
const std::vector<std::string>& startings) {
|
2017-10-25 01:28:23 +00:00
|
|
|
return std::any_of(std::begin(startings), std::end(startings),
|
|
|
|
[&value](const std::string& starting) {
|
|
|
|
return StartsWith(value, starting);
|
|
|
|
});
|
2017-10-18 08:24:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
bool EndsWithAny(const std::string& value,
|
|
|
|
const std::vector<std::string>& endings) {
|
|
|
|
return std::any_of(
|
|
|
|
std::begin(endings), std::end(endings),
|
|
|
|
[&value](const std::string& ending) { return EndsWith(value, ending); });
|
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
|
|
|
}
|
|
|
|
|
2018-02-03 21:16:38 +00:00
|
|
|
std::string GetDirName(std::string path) {
|
|
|
|
if (path.size() && path.back() == '/')
|
|
|
|
path.pop_back();
|
|
|
|
size_t last_slash = path.find_last_of('/');
|
2018-02-22 07:34:32 +00:00
|
|
|
if (last_slash == std::string::npos)
|
|
|
|
return ".";
|
|
|
|
if (last_slash == 0)
|
|
|
|
return "/";
|
2018-02-03 21:16:38 +00:00
|
|
|
return path.substr(0, last_slash);
|
|
|
|
}
|
|
|
|
|
2018-01-11 02:33:20 +00:00
|
|
|
std::string GetBaseName(const std::string& path) {
|
|
|
|
size_t last_slash = path.find_last_of('/');
|
|
|
|
if (last_slash != std::string::npos && (last_slash + 1) < path.size())
|
|
|
|
return path.substr(last_slash + 1);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2018-01-12 17:37:33 +00:00
|
|
|
std::string StripFileType(const std::string& path) {
|
|
|
|
size_t last_period = path.find_last_of('.');
|
|
|
|
if (last_period != std::string::npos)
|
|
|
|
return path.substr(0, last_period);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2017-04-13 06:01:42 +00:00
|
|
|
// See http://stackoverflow.com/a/29752943
|
2017-09-22 01:14:57 +00:00
|
|
|
std::string ReplaceAll(const std::string& source,
|
|
|
|
const std::string& from,
|
|
|
|
const std::string& to) {
|
2017-04-13 06:01:42 +00:00
|
|
|
std::string result;
|
|
|
|
result.reserve(source.length()); // avoids a few memory allocations
|
|
|
|
|
|
|
|
std::string::size_type last_pos = 0;
|
|
|
|
std::string::size_type find_pos;
|
|
|
|
|
|
|
|
while (std::string::npos != (find_pos = source.find(from, last_pos))) {
|
|
|
|
result.append(source, last_pos, find_pos - last_pos);
|
|
|
|
result += to;
|
|
|
|
last_pos = find_pos + from.length();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Care for the rest after last occurrence
|
|
|
|
result += source.substr(last_pos);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-05-22 07:14:11 +00:00
|
|
|
std::string LowerPathIfCaseInsensitive(const std::string& path) {
|
|
|
|
std::string result = path;
|
|
|
|
#if defined(_WIN32)
|
|
|
|
for (size_t i = 0; i < result.size(); ++i)
|
|
|
|
result[i] = (char)tolower(result[i]);
|
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-05-21 19:51:15 +00:00
|
|
|
static void GetFilesInFolderHelper(
|
2017-09-22 01:14:57 +00:00
|
|
|
std::string folder,
|
|
|
|
bool recursive,
|
|
|
|
std::string output_prefix,
|
|
|
|
const std::function<void(const std::string&)>& handler) {
|
2018-01-08 17:09:19 +00:00
|
|
|
std::queue<std::pair<std::string, std::string>> q;
|
|
|
|
q.push(make_pair(folder, output_prefix));
|
2018-01-11 02:43:01 +00:00
|
|
|
while (!q.empty()) {
|
2018-01-08 17:09:19 +00:00
|
|
|
tinydir_dir dir;
|
|
|
|
if (tinydir_open(&dir, q.front().first.c_str()) == -1) {
|
|
|
|
LOG_S(WARNING) << "Unable to open directory " << folder;
|
2017-02-17 09:57:44 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2018-01-08 17:09:19 +00:00
|
|
|
while (dir.has_next) {
|
|
|
|
tinydir_file file;
|
|
|
|
if (tinydir_readfile(&dir, &file) == -1) {
|
|
|
|
LOG_S(WARNING) << "Unable to read file " << file.name
|
|
|
|
<< " when reading directory " << folder;
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2018-02-03 21:16:38 +00:00
|
|
|
// Skip all dot files except .cquery.
|
2018-01-08 17:09:19 +00:00
|
|
|
//
|
|
|
|
// The nested ifs are intentional, branching order is subtle here.
|
|
|
|
//
|
|
|
|
// Note that in the future if we do support dot directories/files, we must
|
|
|
|
// always ignore the '.' and '..' directories otherwise this will loop
|
|
|
|
// infinitely.
|
2018-02-03 21:16:38 +00:00
|
|
|
if (file.name[0] != '.' || strcmp(file.name, ".cquery") == 0) {
|
2018-01-08 17:09:19 +00:00
|
|
|
if (file.is_dir) {
|
|
|
|
if (recursive) {
|
2018-02-02 18:51:14 +00:00
|
|
|
std::string child_dir = q.front().second + file.name + "/";
|
|
|
|
if (!IsSymLink(file.path))
|
2018-01-08 17:09:19 +00:00
|
|
|
q.push(make_pair(file.path, child_dir));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
handler(q.front().second + file.name);
|
2017-03-11 02:24:51 +00:00
|
|
|
}
|
2017-03-06 07:19:00 +00:00
|
|
|
}
|
2017-02-17 09:57:44 +00:00
|
|
|
|
2018-01-08 17:09:19 +00:00
|
|
|
if (tinydir_next(&dir) == -1) {
|
|
|
|
LOG_S(WARNING) << "Unable to fetch next file when reading directory "
|
|
|
|
<< folder;
|
|
|
|
goto bail;
|
|
|
|
}
|
2017-02-17 09:57:44 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 17:09:19 +00:00
|
|
|
bail:
|
|
|
|
tinydir_close(&dir);
|
|
|
|
q.pop();
|
|
|
|
}
|
2017-02-17 09:57:44 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
std::vector<std::string> GetFilesInFolder(std::string folder,
|
|
|
|
bool recursive,
|
|
|
|
bool add_folder_to_path) {
|
2017-05-21 19:51:15 +00:00
|
|
|
EnsureEndsInSlash(folder);
|
|
|
|
std::vector<std::string> result;
|
2017-09-22 01:14:57 +00:00
|
|
|
GetFilesInFolderHelper(
|
|
|
|
folder, recursive, add_folder_to_path ? folder : "",
|
|
|
|
[&result](const std::string& path) { result.push_back(path); });
|
2017-05-21 19:51:15 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
void GetFilesInFolder(std::string folder,
|
|
|
|
bool recursive,
|
|
|
|
bool add_folder_to_path,
|
|
|
|
const std::function<void(const std::string&)>& handler) {
|
2017-05-21 19:51:15 +00:00
|
|
|
EnsureEndsInSlash(folder);
|
2017-09-22 01:14:57 +00:00
|
|
|
GetFilesInFolderHelper(folder, recursive, add_folder_to_path ? folder : "",
|
|
|
|
handler);
|
2017-03-06 07:19:00 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
if (path.size() && path.back() == '/')
|
|
|
|
path.pop_back();
|
2018-01-11 07:32:36 +00:00
|
|
|
std::replace(path.begin(), path.end(), '\\', '@');
|
|
|
|
std::replace(path.begin(), path.end(), '/', '@');
|
|
|
|
std::replace(path.begin(), path.end(), ':', '@');
|
2017-12-02 05:07:30 +00:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2017-03-09 18:02:55 +00:00
|
|
|
// http://stackoverflow.com/a/6089413
|
|
|
|
std::istream& SafeGetline(std::istream& is, std::string& t) {
|
2017-09-22 01:14:57 +00:00
|
|
|
t.clear();
|
|
|
|
|
|
|
|
// The characters in the stream are read one-by-one using a std::streambuf.
|
2017-11-28 01:53:14 +00:00
|
|
|
// That is faster than reading them one-by-one using the std::istream. Code
|
|
|
|
// that uses streambuf this way must be guarded by a sentry object. The sentry
|
|
|
|
// object performs various tasks, such as thread synchronization and updating
|
|
|
|
// the stream state.
|
2017-09-22 01:14:57 +00:00
|
|
|
|
|
|
|
std::istream::sentry se(is, true);
|
|
|
|
std::streambuf* sb = is.rdbuf();
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
int c = sb->sbumpc();
|
2017-11-28 02:49:57 +00:00
|
|
|
if (c == EOF) {
|
|
|
|
// Also handle the case when the last line has no line ending
|
|
|
|
if (t.empty())
|
|
|
|
is.setstate(std::ios::eofbit);
|
|
|
|
return is;
|
2017-03-09 18:02:55 +00:00
|
|
|
}
|
2017-11-28 02:49:57 +00:00
|
|
|
|
|
|
|
t += (char)c;
|
|
|
|
|
|
|
|
if (c == '\n')
|
|
|
|
return is;
|
2017-09-22 01:14:57 +00:00
|
|
|
}
|
2017-03-09 18:02:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-07 04:26:22 +00:00
|
|
|
bool FileExists(const std::string& filename) {
|
|
|
|
std::ifstream cache(filename);
|
|
|
|
return cache.is_open();
|
|
|
|
}
|
|
|
|
|
2017-04-17 07:06:01 +00:00
|
|
|
optional<std::string> ReadContent(const std::string& filename) {
|
2018-01-30 05:34:28 +00:00
|
|
|
LOG_S(INFO) << "Reading " << filename;
|
2017-04-17 07:06:01 +00:00
|
|
|
std::ifstream cache;
|
|
|
|
cache.open(filename);
|
|
|
|
|
2017-11-22 05:23:17 +00:00
|
|
|
try {
|
|
|
|
return std::string(std::istreambuf_iterator<char>(cache),
|
|
|
|
std::istreambuf_iterator<char>());
|
|
|
|
} catch (std::ios_base::failure&) {
|
|
|
|
return nullopt;
|
|
|
|
}
|
2017-04-17 07:06:01 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 02:17:36 +00:00
|
|
|
std::vector<std::string> ReadLinesWithEnding(std::string filename) {
|
2017-02-17 09:57:44 +00:00
|
|
|
std::vector<std::string> result;
|
|
|
|
|
|
|
|
std::ifstream input(filename);
|
2017-12-02 01:04:39 +00:00
|
|
|
for (std::string line; SafeGetline(input, line);)
|
2017-02-17 09:57:44 +00:00
|
|
|
result.push_back(line);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
std::vector<std::string> ToLines(const std::string& content,
|
|
|
|
bool trim_whitespace) {
|
2017-04-16 08:09:12 +00:00
|
|
|
std::vector<std::string> result;
|
|
|
|
|
|
|
|
std::istringstream lines(content);
|
|
|
|
|
|
|
|
std::string line;
|
|
|
|
while (getline(lines, line)) {
|
|
|
|
if (trim_whitespace)
|
2017-12-22 16:48:12 +00:00
|
|
|
TrimInPlace(line);
|
2018-02-01 09:12:36 +00:00
|
|
|
else
|
|
|
|
RemoveLastCR(line);
|
2017-04-16 08:09:12 +00:00
|
|
|
result.push_back(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-12-22 16:48:12 +00:00
|
|
|
std::string TextReplacer::Apply(const std::string& content) {
|
|
|
|
std::string result = content;
|
|
|
|
|
|
|
|
for (const Replacement& replacement : replacements) {
|
|
|
|
while (true) {
|
|
|
|
size_t idx = result.find(replacement.from);
|
|
|
|
if (idx == std::string::npos)
|
|
|
|
break;
|
|
|
|
|
|
|
|
result.replace(result.begin() + idx,
|
|
|
|
result.begin() + idx + replacement.from.size(),
|
|
|
|
replacement.to);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-03-01 08:36:11 +00:00
|
|
|
void WriteToFile(const std::string& filename, const std::string& content) {
|
2018-01-04 06:13:33 +00:00
|
|
|
std::ofstream file(filename,
|
|
|
|
std::ios::out | std::ios::trunc | std::ios::binary);
|
2018-01-04 17:33:35 +00:00
|
|
|
if (!file.good()) {
|
|
|
|
LOG_S(ERROR) << "Cannot write to " << filename;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-01 08:36:11 +00:00
|
|
|
file << content;
|
2017-03-09 18:02:55 +00:00
|
|
|
}
|
2017-05-25 02:04:19 +00:00
|
|
|
|
|
|
|
float GetProcessMemoryUsedInMb() {
|
2017-05-31 00:54:19 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
return 0.f;
|
|
|
|
#else
|
2017-05-25 02:04:19 +00:00
|
|
|
const float kBytesToMb = 1000000;
|
|
|
|
uint64_t memory_after = spp::GetProcessMemoryUsed();
|
|
|
|
return memory_after / kBytesToMb;
|
2017-05-31 00:54:19 +00:00
|
|
|
#endif
|
|
|
|
}
|
2017-06-14 06:59:40 +00:00
|
|
|
|
|
|
|
std::string FormatMicroseconds(long long microseconds) {
|
|
|
|
long long milliseconds = microseconds / 1000;
|
|
|
|
long long remaining = microseconds - milliseconds;
|
|
|
|
|
|
|
|
// Only show two digits after the dot.
|
|
|
|
while (remaining >= 100)
|
|
|
|
remaining /= 10;
|
|
|
|
|
|
|
|
return std::to_string(milliseconds) + "." + std::to_string(remaining) + "ms";
|
|
|
|
}
|
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
|
|
|
}
|
2017-12-22 16:48:12 +00:00
|
|
|
|
2017-12-23 15:51:34 +00:00
|
|
|
std::string UpdateToRnNewlines(std::string output) {
|
|
|
|
size_t idx = 0;
|
|
|
|
while (true) {
|
|
|
|
idx = output.find('\n', idx);
|
|
|
|
|
|
|
|
// No more matches.
|
|
|
|
if (idx == std::string::npos)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Skip an existing "\r\n" match.
|
|
|
|
if (idx > 0 && output[idx - 1] == '\r') {
|
|
|
|
++idx;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace "\n" with "\r|n".
|
|
|
|
output.replace(output.begin() + idx, output.begin() + idx + 1, "\r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_SUITE("Update \\n to \\r\\n") {
|
|
|
|
TEST_CASE("all") {
|
|
|
|
REQUIRE(UpdateToRnNewlines("\n") == "\r\n");
|
|
|
|
REQUIRE(UpdateToRnNewlines("\n\n") == "\r\n\r\n");
|
|
|
|
REQUIRE(UpdateToRnNewlines("\r\n\n") == "\r\n\r\n");
|
|
|
|
REQUIRE(UpdateToRnNewlines("\n\r\n") == "\r\n\r\n");
|
|
|
|
REQUIRE(UpdateToRnNewlines("\r\n\r\n") == "\r\n\r\n");
|
|
|
|
REQUIRE(UpdateToRnNewlines("f1\nfo2\nfoo3") == "f1\r\nfo2\r\nfoo3");
|
|
|
|
REQUIRE(UpdateToRnNewlines("f1\r\nfo2\r\nfoo3") == "f1\r\nfo2\r\nfoo3");
|
|
|
|
}
|
2018-01-03 12:38:01 +00:00
|
|
|
}
|
2018-01-12 17:37:33 +00:00
|
|
|
|
|
|
|
TEST_SUITE("StripFileType") {
|
|
|
|
TEST_CASE("all") {
|
|
|
|
REQUIRE(StripFileType("") == "");
|
|
|
|
REQUIRE(StripFileType("bar") == "bar");
|
|
|
|
REQUIRE(StripFileType("bar.cc") == "bar");
|
|
|
|
REQUIRE(StripFileType("foo/bar.cc") == "foo/bar");
|
|
|
|
}
|
|
|
|
}
|