1 #include "includes.h"
2 
prepare_logdir()3 void prepare_logdir()
4 {
5     spdlog::drop_all();
6 #ifdef _WIN32
7     system("if not exist logs mkdir logs");
8     system("del /F /Q logs\\*");
9 #else
10     auto rv = system("mkdir -p logs");
11     rv = system("rm -f logs/*");
12     (void)rv;
13 #endif
14 }
15 
file_contents(const std::string &filename)16 std::string file_contents(const std::string &filename)
17 {
18     std::ifstream ifs(filename);
19     if (!ifs)
20         throw std::runtime_error("Failed open file ");
21     return std::string((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
22 }
23 
count_lines(const std::string &filename)24 std::size_t count_lines(const std::string &filename)
25 {
26     std::ifstream ifs(filename);
27     if (!ifs)
28         throw std::runtime_error("Failed open file ");
29 
30     std::string line;
31     size_t counter = 0;
32     while (std::getline(ifs, line))
33         counter++;
34     return counter;
35 }
36 
get_filesize(const std::string &filename)37 std::size_t get_filesize(const std::string &filename)
38 {
39     std::ifstream ifs(filename, std::ifstream::ate | std::ifstream::binary);
40     if (!ifs)
41         throw std::runtime_error("Failed open file ");
42 
43     return static_cast<std::size_t>(ifs.tellg());
44 }
45 
46 // source: https://stackoverflow.com/a/2072890/192001
ends_with(std::string const &value, std::string const &ending)47 bool ends_with(std::string const &value, std::string const &ending)
48 {
49     if (ending.size() > value.size())
50         return false;
51     return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
52 }
53