39#include "ocpn_utils.h"
43bool endswith(
const std::string& str,
const std::string& suffix) {
44 return str.size() >= suffix.size() &&
45 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix);
48bool startswith(
const std::string& str,
const std::string& prefix) {
49 return prefix.size() <= str.size() &&
50 strncmp(str.c_str(), prefix.c_str(), prefix.size()) == 0;
53std::vector<std::string> split(
const char* token_string,
54 const std::string& delimiter) {
55 std::vector<std::string> tokens;
56 std::string s = std::string(token_string);
59 while ((pos = s.find(delimiter)) != std::string::npos) {
60 token = s.substr(0, pos);
61 tokens.push_back(token);
62 s.erase(0, pos + delimiter.length());
68bool exists(
const std::string& name) {
70 return (_access(name.c_str(), 0) != -1);
72 return (access(name.c_str(), F_OK) != -1);
76void mkdir(
const std::string path) {
77#if defined(_WIN32) && !defined(__MINGW32__)
79#elif defined(__MINGW32__)
80 ::mkdir(path.c_str());
82 ::mkdir(path.c_str(), 0755);
86std::string ltrim(std::string s) {
90 find_if(s.begin(), s.end(), [](
int ch) { return !isspace(ch); }));
94std::string rtrim(std::string s) {
98 find_if(s.rbegin(), s.rend(), [](
int ch) { return !isspace(ch); }).base(),
103std::string trim(std::string s) {
109std::string join(std::vector<std::string> v,
char c) {
111 for (
auto p = v.begin(); p != v.end(); p++) {
113 if (p != v.end() - 1) {
120std::string tolower(
const std::string& input) {
121 std::string s(input);
122 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
126bool replace(std::string& str,
const std::string& from,
const std::string& to) {
127 size_t start_pos = str.find(from);
128 if (start_pos == std::string::npos)
return false;
129 str.replace(start_pos, from.length(), to);
133void copy_file(
const std::string& src_path,
const std::string& dest_path) {
134 std::ifstream source(src_path, std::ios::binary);
135 std::ofstream dest(dest_path, std::ios::binary);
137 dest << source.rdbuf();
Standard, mostly strings utilities.