You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
spdlog/tests/utils.cpp

120 lines
2.7 KiB
C++

9 years ago
#include "includes.h"
6 years ago
#ifndef _WIN32
#include <sys/types.h>
#include <dirent.h>
6 years ago
#endif
9 years ago
void prepare_logdir()
{
9 years ago
spdlog::drop_all();
9 years ago
#ifdef _WIN32
9 years ago
system("if not exist logs mkdir logs");
system("del /F /Q logs\\*");
9 years ago
#else
9 years ago
auto rv = system("mkdir -p logs");
7 years ago
if (rv != 0)
{
throw std::runtime_error("Failed to mkdir -p logs");
}
rv = system("rm -f logs/*");
7 years ago
if (rv != 0)
{
throw std::runtime_error("Failed to rm -f logs/*");
}
9 years ago
#endif
9 years ago
}
8 years ago
std::string file_contents(const std::string &filename)
9 years ago
{
std::ifstream ifs(filename);
if (!ifs)
{
9 years ago
throw std::runtime_error("Failed open file ");
}
8 years ago
return std::string((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
9 years ago
}
8 years ago
std::size_t count_lines(const std::string &filename)
9 years ago
{
std::ifstream ifs(filename);
if (!ifs)
{
9 years ago
throw std::runtime_error("Failed open file ");
}
9 years ago
std::string line;
size_t counter = 0;
8 years ago
while (std::getline(ifs, line))
9 years ago
counter++;
return counter;
}
8 years ago
std::size_t get_filesize(const std::string &filename)
9 years ago
{
std::ifstream ifs(filename, std::ifstream::ate | std::ifstream::binary);
if (!ifs)
{
9 years ago
throw std::runtime_error("Failed open file ");
}
9 years ago
return static_cast<std::size_t>(ifs.tellg());
9 years ago
}
// source: https://stackoverflow.com/a/2072890/192001
8 years ago
bool ends_with(std::string const &value, std::string const &ending)
8 years ago
{
8 years ago
if (ending.size() > value.size())
{
8 years ago
return false;
}
8 years ago
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
}
#ifdef _WIN32
6 years ago
// Based on: https://stackoverflow.com/a/37416569/192001
std::size_t count_files(const std::string &folder)
{
6 years ago
size_t counter = 0;
WIN32_FIND_DATA ffd;
6 years ago
// Start iterating over the files in the path directory.
6 years ago
HANDLE hFind = ::FindFirstFileA((folder + "\\*").c_str(), &ffd);
if (hFind != INVALID_HANDLE_VALUE)
{
do // Managed to locate and create an handle to that folder.
{
if (ffd.cFileName[0] != '.')
counter++;
} while (::FindNextFile(hFind, &ffd) != 0);
::FindClose(hFind);
}
else
{
throw std::runtime_error("Failed open folder " + folder);
}
return counter;
}
#else
// Based on: https://stackoverflow.com/a/2802255/192001
std::size_t count_files(const std::string &folder)
{
size_t counter = 0;
DIR *dp = opendir(folder.c_str());
if (dp == nullptr)
{
throw std::runtime_error("Failed open folder " + folder);
}
struct dirent *ep;
while (ep = readdir(dp))
{
if (ep->d_name[0] != '.')
counter++;
}
(void)closedir(dp);
return counter;
}
#endif