utils.cpp 1.3 KB
Newer Older
gabime's avatar
gabime committed
1 2 3 4
#include "includes.h"

void prepare_logdir()
{
gabime's avatar
gabime committed
5
    spdlog::drop_all();
gabime's avatar
gabime committed
6
#ifdef _WIN32
gabime's avatar
gabime committed
7
    system("if not exist logs mkdir logs");
8
    system("del /F /Q logs\\*");
gabime's avatar
gabime committed
9
#else
gabime's avatar
gabime committed
10
    auto rv = system("mkdir -p logs");
11
    (void)rv;
gabime's avatar
gabime committed
12 13
    rv = system("rm -f logs/*");
    (void)rv;
gabime's avatar
gabime committed
14
#endif
gabime's avatar
gabime committed
15 16
}

gabime's avatar
gabime committed
17
std::string file_contents(const std::string &filename)
gabime's avatar
gabime committed
18 19 20 21
{
    std::ifstream ifs(filename);
    if (!ifs)
        throw std::runtime_error("Failed open file ");
gabime's avatar
gabime committed
22
    return std::string((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
gabime's avatar
gabime committed
23 24
}

gabime's avatar
gabime committed
25
std::size_t count_lines(const std::string &filename)
gabime's avatar
gabime committed
26 27 28 29 30 31 32
{
    std::ifstream ifs(filename);
    if (!ifs)
        throw std::runtime_error("Failed open file ");

    std::string line;
    size_t counter = 0;
gabime's avatar
gabime committed
33
    while (std::getline(ifs, line))
gabime's avatar
gabime committed
34 35 36 37
        counter++;
    return counter;
}

gabime's avatar
gabime committed
38
std::size_t get_filesize(const std::string &filename)
gabime's avatar
gabime committed
39 40 41 42 43
{
    std::ifstream ifs(filename, std::ifstream::ate | std::ifstream::binary);
    if (!ifs)
        throw std::runtime_error("Failed open file ");

44
    return static_cast<std::size_t>(ifs.tellg());
gabime's avatar
gabime committed
45
}
46 47

// source: https://stackoverflow.com/a/2072890/192001
gabime's avatar
gabime committed
48
bool ends_with(std::string const &value, std::string const &ending)
gabime's avatar
gabime committed
49
{
gabime's avatar
gabime committed
50 51
    if (ending.size() > value.size())
        return false;
gabime's avatar
gabime committed
52
    return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
53
}