2017-03-19 22:06:22 +00:00
|
|
|
#include "platform.h"
|
|
|
|
|
2017-12-02 01:04:39 +00:00
|
|
|
#include <doctest/doctest.h>
|
|
|
|
#include <loguru.hpp>
|
|
|
|
|
2017-04-18 02:59:48 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <iterator>
|
|
|
|
#include <sstream>
|
2017-09-22 01:14:57 +00:00
|
|
|
#include <string>
|
2017-03-19 22:06:22 +00:00
|
|
|
#include <thread>
|
2017-04-18 02:59:48 +00:00
|
|
|
#include <vector>
|
2017-03-19 22:06:22 +00:00
|
|
|
|
2017-04-18 02:59:48 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// See http://stackoverflow.com/a/236803
|
2017-09-22 01:14:57 +00:00
|
|
|
template <typename Out>
|
|
|
|
void Split(const std::string& s, char delim, Out result) {
|
2017-04-18 02:59:48 +00:00
|
|
|
std::stringstream ss;
|
|
|
|
ss.str(s);
|
|
|
|
std::string item;
|
|
|
|
while (std::getline(ss, item, delim)) {
|
|
|
|
if (!item.empty())
|
|
|
|
*(result++) = item;
|
|
|
|
}
|
|
|
|
}
|
2017-09-22 01:14:57 +00:00
|
|
|
std::vector<std::string> Split(const std::string& s, char delim) {
|
2017-04-18 02:59:48 +00:00
|
|
|
std::vector<std::string> elems;
|
|
|
|
Split(s, delim, std::back_inserter(elems));
|
|
|
|
return elems;
|
|
|
|
}
|
|
|
|
|
2017-11-19 22:11:54 +00:00
|
|
|
std::string Join(const std::vector<std::string>& entries,
|
|
|
|
char delim,
|
2017-11-18 19:13:48 +00:00
|
|
|
size_t end) {
|
2017-04-18 02:59:48 +00:00
|
|
|
std::string result;
|
|
|
|
bool first = true;
|
2017-11-18 19:13:48 +00:00
|
|
|
for (size_t i = 0; i < end; ++i) {
|
2017-04-18 02:59:48 +00:00
|
|
|
if (!first)
|
|
|
|
result += delim;
|
|
|
|
first = false;
|
|
|
|
result += entries[i];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-03-22 17:03:45 +00:00
|
|
|
PlatformMutex::~PlatformMutex() = default;
|
|
|
|
|
|
|
|
PlatformScopedMutexLock::~PlatformScopedMutexLock() = default;
|
|
|
|
|
|
|
|
PlatformSharedMemory::~PlatformSharedMemory() = default;
|
|
|
|
|
2017-04-18 02:59:48 +00:00
|
|
|
void MakeDirectoryRecursive(std::string path) {
|
|
|
|
path = NormalizePath(path);
|
|
|
|
|
|
|
|
if (TryMakeDirectory(path))
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::string prefix = "";
|
|
|
|
if (path[0] == '/')
|
|
|
|
prefix = "/";
|
|
|
|
|
|
|
|
std::vector<std::string> components = Split(path, '/');
|
|
|
|
|
|
|
|
// Find first parent directory which doesn't exist.
|
|
|
|
int first_success = -1;
|
2017-11-18 19:13:48 +00:00
|
|
|
for (size_t j = 0; j < components.size(); ++j) {
|
|
|
|
size_t i = components.size() - j;
|
2017-04-18 02:59:48 +00:00
|
|
|
if (TryMakeDirectory(prefix + Join(components, '/', i))) {
|
|
|
|
first_success = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first_success == -1) {
|
2017-12-02 01:04:39 +00:00
|
|
|
LOG_S(FATAL) << "Failed to make any parent directory for " << path;
|
2017-04-18 02:59:48 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make all child directories.
|
2017-11-18 19:13:48 +00:00
|
|
|
for (size_t i = first_success + 1; i <= components.size(); ++i) {
|
2017-04-18 02:59:48 +00:00
|
|
|
if (TryMakeDirectory(prefix + Join(components, '/', i)) == false) {
|
2017-12-02 01:04:39 +00:00
|
|
|
LOG_S(FATAL) << "Failed making directory for " << path
|
|
|
|
<< " even after creating parent directories";
|
2017-04-18 02:59:48 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-19 18:05:06 +00:00
|
|
|
TEST_SUITE("Platform") {
|
|
|
|
TEST_CASE("Split strings") {
|
|
|
|
std::vector<std::string> actual = Split("/a/b/c/", '/');
|
|
|
|
std::vector<std::string> expected{"a", "b", "c"};
|
|
|
|
REQUIRE(actual == expected);
|
|
|
|
}
|
2017-03-19 22:06:22 +00:00
|
|
|
}
|