#include "utils.h" #include #include #include /** * Clean up the trailing ../ in __FILE__ * * This is used by the logging macro. * * @param filepath * @return const char* */ const char *trim_path(const char *filepath) { if (strncmp(filepath, "../", 3) == 0) { filepath += 3; } return filepath; } #include bool file_exists(const std::string &name) { std::ifstream f(name.c_str()); return f.good(); } bool replace(std::string &str, const std::string &from, const std::string &to) { size_t start_pos = str.find(from); if (start_pos == std::string::npos) return false; do { str.replace(start_pos, from.length(), to); } while ((start_pos = str.find(from)) != std::string::npos); return true; } bool replace(std::string &str, const char *from, const char *to) { size_t start_pos = str.find(from); if (start_pos == std::string::npos) return false; do { str.replace(start_pos, strlen(from), to); } while ((start_pos = str.find(from)) != std::string::npos); return true; } void ansi_clean(std::string &str) { static std::regex ansi_cleaner("\x1b\[[0-9;]*[A-Zmh]", std::regex_constants::ECMAScript); str = std::regex_replace(str, ansi_cleaner, ""); } void high_ascii(std::string &str) { // the + replaces all of them into one. I want each high ascii replaced with // #. static std::regex high_cleaner("[\x80-\xff]", std::regex_constants::ECMAScript); str = std::regex_replace(str, high_cleaner, "#"); } std::smatch ansi_newline(const std::string &str) { static std::regex ansi_nl("\x1b\[[0-9;]*[JK]", std::regex_constants::ECMAScript); std::smatch m; std::regex_search(str, m, ansi_nl); return m; } std::string repr(const std::string &source) { std::string output = source; replace(output, "\n", "\\n"); replace(output, "\r", "\\r"); replace(output, "\b", "\\b"); replace(output, "\x1b", "\\["); high_ascii(output); return output; } std::string clean_string(const std::string &source) { std::string clean = source; /* replace(clean, "\n", "\\n"); replace(clean, "\r", "\\r"); replace(clean, "\b", "\\b"); replace(clean, "\x1b", "\\["); */ replace(clean, "\n", ""); replace(clean, "\r", ""); // ANSI too ansi_clean(clean); // BUGZ_LOG(error) << "cleaned: " << clean; high_ascii(clean); // replace(clean, "\x1b", "^"); return clean; } std::vector split(const std::string &line) { static std::regex rx_split("[^\\s]+"); std::vector results; for (auto it = std::sregex_iterator(line.begin(), line.end(), rx_split); it != std::sregex_iterator(); ++it) { results.push_back(it->str()); } return results; } std::vector split(const std::string &line, const std::string &by) { std::string work = line; std::vector results; size_t pos; while ((pos = work.find(by)) != std::string::npos) { results.push_back(work.substr(0, pos)); work.erase(0, pos + by.length()); } if (!work.empty()) results.push_back(work); return results; } bool in(const std::string &line, const std::string &has) { return (line.find(has) != std::string::npos); } bool startswith(const std::string &line, const std::string &has) { return (line.substr(0, has.length()) == has); } bool endswith(const std::string &line, const std::string &has) { if (line.length() < has.length()) return false; return (line.substr(line.length() - has.length()) == has); } void trim(std::string &str) { while (str.substr(0, 1) == " ") str.erase(0, 1); while (str.substr(str.length() - 1) == " ") str.erase(str.length() - 1); } bool at_command_prompt(const std::string &prompt) { if (startswith(prompt, "Command [")) if (endswith(prompt, "] (?=Help)? : ")) return true; return false; }