39 #include "model/ocpn_utils.h"
43 bool 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);
48 bool 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;
53 std::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());
68 std::vector<std::string> split(
const std::string&
string,
69 const std::string& delimiter) {
70 return split(
string.c_str(), delimiter);
73 bool exists(
const std::string& name) {
75 return (_access(name.c_str(), 0) != -1);
77 return (access(name.c_str(), F_OK) != -1);
81 void mkdir(
const std::string path) {
82 #if defined(_WIN32) && !defined(__MINGW32__)
84 #elif defined(__MINGW32__)
85 ::mkdir(path.c_str());
87 ::mkdir(path.c_str(), 0755);
91 std::string ltrim(std::string s) {
95 find_if(s.begin(), s.end(), [](
int ch) { return !isspace(ch); }));
99 std::string rtrim(std::string s) {
103 find_if(s.rbegin(), s.rend(), [](
int ch) { return !isspace(ch); }).base(),
108 std::string trim(std::string s) {
114 std::string join(std::vector<std::string> v,
char c) {
116 for (
auto p = v.begin(); p != v.end(); p++) {
118 if (p != v.end() - 1) {
125 std::string tolower(
const std::string& input) {
126 std::string s(input);
127 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
131 bool replace(std::string& str,
const std::string& from,
const std::string& to) {
132 size_t start_pos = str.find(from);
133 if (start_pos == std::string::npos)
return false;
134 str.replace(start_pos, from.length(), to);
138 void copy_file(
const std::string& src_path,
const std::string& dest_path) {
139 std::ifstream source(src_path, std::ios::binary);
140 std::ofstream dest(dest_path, std::ios::binary);
142 dest << source.rdbuf();
Standard, mostly strings utilities.