spdlog/tests/utils.cpp

57 lines
1.3 KiB
C++
Raw Normal View History

2016-04-20 04:57:49 -04:00
#include "includes.h"
2017-03-27 19:05:59 -04:00
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");
2017-03-27 19:05:59 -04:00
rv = system("rm -f logs/*");
(void)rv;
2017-03-27 19:08:18 -04:00
#endif
2016-04-20 04:57:49 -04:00
}
std::string file_contents(const std::string& filename)
{
std::ifstream ifs(filename);
if (!ifs)
throw std::runtime_error("Failed open file ");
return std::string((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));
}
std::size_t count_lines(const std::string& filename)
{
std::ifstream ifs(filename);
if (!ifs)
throw std::runtime_error("Failed open file ");
std::string line;
size_t counter = 0;
while(std::getline(ifs, line))
counter++;
return counter;
}
std::size_t get_filesize(const std::string& filename)
{
std::ifstream ifs(filename, std::ifstream::ate | std::ifstream::binary);
if (!ifs)
throw std::runtime_error("Failed open file ");
return static_cast<std::size_t>(ifs.tellg());
2016-04-20 04:57:49 -04:00
}
// source: https://stackoverflow.com/a/2072890/192001
bool ends_with(std::string const & value, std::string const & ending)
{
if (ending.size() > value.size()) return false;
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
}