spdlog/tests/utils.cpp

120 lines
2.7 KiB
C++
Raw Normal View History

2016-04-20 04:57:49 -04:00
#include "includes.h"
2019-09-15 12:09:57 -04:00
#ifndef _WIN32
2019-09-15 11:34:29 -04:00
#include <sys/types.h>
#include <dirent.h>
2019-09-15 12:09:57 -04:00
#endif
2016-04-20 04:57:49 -04:00
void prepare_logdir()
{
2017-03-27 19:08:18 -04:00
spdlog::drop_all();
2016-04-20 04:57:49 -04:00
#ifdef _WIN32
2017-03-27 19:08:18 -04:00
system("if not exist logs mkdir logs");
system("del /F /Q logs\\*");
2016-04-20 04:57:49 -04:00
#else
2017-03-27 19:08:18 -04:00
auto rv = system("mkdir -p logs");
2018-10-05 08:21:05 -04:00
if (rv != 0)
2018-10-05 08:20:14 -04:00
{
throw std::runtime_error("Failed to mkdir -p logs");
}
2017-03-27 19:05:59 -04:00
rv = system("rm -f logs/*");
2018-10-05 08:21:05 -04:00
if (rv != 0)
2018-10-05 08:20:14 -04:00
{
throw std::runtime_error("Failed to rm -f logs/*");
}
2017-03-27 19:08:18 -04:00
#endif
2016-04-20 04:57:49 -04:00
}
2018-03-09 08:26:33 -05:00
std::string file_contents(const std::string &filename)
2016-04-20 04:57:49 -04:00
{
std::ifstream ifs(filename);
if (!ifs)
2018-10-05 08:20:14 -04:00
{
2016-04-20 04:57:49 -04:00
throw std::runtime_error("Failed open file ");
2018-10-05 08:20:14 -04:00
}
2018-03-09 08:26:33 -05:00
return std::string((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
2016-04-20 04:57:49 -04:00
}
2018-03-09 08:26:33 -05:00
std::size_t count_lines(const std::string &filename)
2016-04-20 04:57:49 -04:00
{
std::ifstream ifs(filename);
if (!ifs)
2018-10-05 08:20:14 -04:00
{
2016-04-20 04:57:49 -04:00
throw std::runtime_error("Failed open file ");
2018-10-05 08:20:14 -04:00
}
2016-04-20 04:57:49 -04:00
std::string line;
size_t counter = 0;
2018-03-09 08:26:33 -05:00
while (std::getline(ifs, line))
2016-04-20 04:57:49 -04:00
counter++;
return counter;
}
2018-03-09 08:26:33 -05:00
std::size_t get_filesize(const std::string &filename)
2016-04-20 04:57:49 -04:00
{
std::ifstream ifs(filename, std::ifstream::ate | std::ifstream::binary);
if (!ifs)
2018-10-05 08:20:14 -04:00
{
2016-04-20 04:57:49 -04:00
throw std::runtime_error("Failed open file ");
2018-10-05 08:20:14 -04:00
}
2016-04-20 04:57:49 -04:00
return static_cast<std::size_t>(ifs.tellg());
2016-04-20 04:57:49 -04:00
}
// source: https://stackoverflow.com/a/2072890/192001
2018-03-09 08:26:33 -05:00
bool ends_with(std::string const &value, std::string const &ending)
2017-11-06 05:39:04 -05:00
{
2018-03-09 08:26:33 -05:00
if (ending.size() > value.size())
2018-10-05 08:20:14 -04:00
{
2018-03-09 08:26:33 -05:00
return false;
2018-10-05 08:20:14 -04:00
}
2017-11-06 05:39:04 -05:00
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
}
2019-09-15 11:34:29 -04:00
#ifdef _WIN32
2019-09-15 14:01:15 -04:00
// Based on: https://stackoverflow.com/a/37416569/192001
2019-09-15 11:38:31 -04:00
std::size_t count_files(const std::string &folder)
{
2019-09-15 11:49:19 -04:00
size_t counter = 0;
2019-09-15 11:38:31 -04:00
WIN32_FIND_DATA ffd;
2019-09-16 04:58:26 -04:00
2019-09-16 04:58:51 -04:00
// Start iterating over the files in the folder directory.
2019-09-16 04:58:26 -04:00
HANDLE hFind = ::FindFirstFileA((folder + "\\*").c_str(), &ffd);
2019-09-15 11:38:31 -04:00
if (hFind != INVALID_HANDLE_VALUE)
{
do // Managed to locate and create an handle to that folder.
{
2019-09-15 13:56:20 -04:00
if (ffd.cFileName[0] != '.')
counter++;
2019-09-16 04:56:56 -04:00
} while (::FindNextFile(hFind, &ffd) != 0);
2019-09-15 11:38:31 -04:00
::FindClose(hFind);
}
else
{
throw std::runtime_error("Failed open folder " + folder);
}
return counter;
}
2019-09-15 11:34:29 -04:00
#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;
2019-09-16 16:50:56 -04:00
while ((ep = readdir(dp)) != nullptr)
2019-09-15 11:34:29 -04:00
{
if (ep->d_name[0] != '.')
counter++;
}
(void)closedir(dp);
return counter;
}
#endif