#include "utils.hpp" void throw_test() { static int i = 0; ++i; if (i % 8 == 0) throw std::bad_alloc(); } std::vector split(std::string input, char delimiter) { std::vector answer; std::stringstream ss(input); std::string temp; while (getline(ss, temp, delimiter)) answer.push_back(temp); return answer; } std::vector split_trim(std::string input, std::string delim = "\n", char ctrim = '\0') { std::vector split_str; std::string tmp; int start = 0; int end; end = input.find(delim); while (end != -1) { tmp = input.substr(start, end - start); if (ctrim != '\0') tmp = trim(tmp, ctrim); if (tmp.size() != 0) split_str.push_back( tmp ); start = end + delim.size(); end = input.find(delim, start); } split_str.push_back( input.substr(start, end - start) ); return split_str; } std::string trim(std::string str, char del) { size_t pos; // delete leadings del pos = str.find_first_not_of(del); if (pos == std::string::npos) pos = str.size(); str = str.substr(pos); // delete trailing del pos = str.find_last_not_of(del); if (pos != std::string::npos) str = str.substr(0, pos + 1); return str; } std::string itos(int n) { std::stringstream strs; strs << n; return ( strs.str() ); } bool isNumeric(std::string str) { for (size_t i = 0; i < str.length(); i++) { if (std::isdigit(str[i]) == false) return false; } return true; } bool isNumeric_btw(int low, int high, std::string str) { for (size_t i = 0; i < str.length(); i++) { if (std::isdigit(str[i]) == false) return false; } int n = std::atoi(str.c_str()); if (n < low || n > high) return false; return true; } http_method str_to_http_method(std::string &str) { if (str == "GET") return GET; else if (str == "POST") return POST; else if (str == "DELETE") return DELETE; return UNKNOWN; } std::string http_methods_to_str(unsigned int methods) { std::string str; if (methods & GET) str.append("GET"); if (methods & POST) { if (!str.empty()) str.append(", "); str.append("POST"); } if (methods & DELETE) { if (!str.empty()) str.append(", "); str.append("DELETE"); } return (str); } # include // you could make this &path... int path_is_valid(std::string path) { const char *tmp_path = path.c_str(); struct stat s; if (stat(tmp_path, &s) == 0) { if (S_ISREG(s.st_mode)) { // std::cout << "is a file\n"; return (2); } else if (S_ISDIR(s.st_mode)) { // std::cout << "is a Dir\n"; return (1); } } // std::cout << "path is neither dir nor file\n"; return (0); } void replace_all_substr(std::string &str, const std::string &ori_substr, const std::string &new_substr) { if (ori_substr.empty()) return; size_t pos = 0; while (1) { pos = str.find(ori_substr, pos); if (pos == std::string::npos) break; str.replace(pos, ori_substr.size(), new_substr); pos += new_substr.size(); } } std::string str_tolower(std::string str) { std::transform(str.begin(), str.end(), str.begin(), ::tolower); return str; } // identify a line in a string, by delim (ex. '\n') // delete this line from the string // and return the deleted line std::string extract_line(std::string * str, size_t pos, std::string delim) { std::string del_str; size_t begin; size_t end; begin = (*str).rfind(delim, pos); if (begin == std::string::npos) begin = 0; else begin += delim.size(); end = (*str).find(delim, pos); if (end == std::string::npos) end = 0; else end += delim.size(); del_str = (*str).substr(begin, end - begin); (*str).erase(begin, end - begin); return del_str; } bool operator==(const listen_socket& lhs, int fd) { return lhs.fd == fd; } bool operator==(int fd, const listen_socket& rhs) { return fd == rhs.fd; }