#include "baza.h"
#include "baza_inter.h"

#include <errno.h>
#include <string.h>

namespace een9 {
    ServerError::ServerError(const std::string &err, const std::string &file, const std::string &func, int line): err(err),
       FILE(file),
       func(func),
       LINE(line) {
        char buf[4096];
        snprintf(buf, 4096, "Error occured in function %s (line %d of %s)\n"
                 "Error: %s",
                 func.c_str(), LINE, FILE.c_str(), err.c_str());
        WHAT = buf;
    }

    const char * ServerError::what() const noexcept {
        return WHAT.c_str();
    }

    std::string prettyprint_errno(const std::string &pref) {
        const char* d = strerrorname_np(errno);
        return pref.empty() ? std::string(d) : std::string(pref) + ": " + d;
    }

    /* This function is VITAL */
    bool strIn(const std::string &str, const char *arr[]) {
        for (const char** elPtr = arr; *elPtr != NULL; elPtr += 1) {
            if (str == (*elPtr))
                return true;
        }
        return false;
    }

    bool endsIn(const std::string &a, const std::string &b) {
        if (b.size() > a.size())
            return false;
        return std::equal(a.end() - (ssize_t)b.size(), a.end(), b.begin());
    }

    std::string getSubstring(const std::string &str, size_t A, size_t B) {
        ASSERT(A <= B && B <= str.size(), "Incorrect substring segment");
        return str.substr(A, B - A);
    }
}