OpenCPN Partial API docs
Loading...
Searching...
No Matches
ocpn_utils.cpp
1/******************************************************************************
2 *
3 * Project: OpenCPN
4 *
5 ***************************************************************************
6 * Copyright (C) 2019 Alec Leamas *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
22 ***************************************************************************
23 */
24#include <algorithm>
25#include <cstdio>
26#include <iostream>
27#include <fstream>
28#include <string.h>
29#include <sys/stat.h>
30
31#ifdef __MSVC__
32#include <io.h>
33#include <direct.h>
34#include <stdlib.h>
35#else
36#include <unistd.h>
37#endif
38
39#include "ocpn_utils.h"
40
41namespace ocpn {
42
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);
46}
47
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;
51}
52
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);
57 size_t pos = 0;
58 std::string token;
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());
63 }
64 tokens.push_back(s);
65 return tokens;
66}
67
68bool exists(const std::string& name) {
69#ifdef __MSVC__
70 return (_access(name.c_str(), 0) != -1);
71#else
72 return (access(name.c_str(), F_OK) != -1);
73#endif
74}
75
76void mkdir(const std::string path) {
77#if defined(_WIN32) && !defined(__MINGW32__)
78 _mkdir(path.c_str());
79#elif defined(__MINGW32__)
80 ::mkdir(path.c_str());
81#else
82 ::mkdir(path.c_str(), 0755);
83#endif
84}
85
86std::string ltrim(std::string s) {
87 using namespace std;
88
89 s.erase(s.begin(),
90 find_if(s.begin(), s.end(), [](int ch) { return !isspace(ch); }));
91 return s;
92}
93
94std::string rtrim(std::string s) {
95 using namespace std;
96
97 s.erase(
98 find_if(s.rbegin(), s.rend(), [](int ch) { return !isspace(ch); }).base(),
99 s.end());
100 return s;
101}
102
103std::string trim(std::string s) {
104 s = ltrim(s);
105 s = rtrim(s);
106 return s;
107}
108
109std::string join(std::vector<std::string> v, char c) {
110 std::string s;
111 for (auto p = v.begin(); p != v.end(); p++) {
112 s += *p;
113 if (p != v.end() - 1) {
114 s += c;
115 }
116 }
117 return s;
118}
119
120std::string tolower(const std::string& input) {
121 std::string s(input);
122 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
123 return s;
124}
125
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);
130 return true;
131}
132
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);
136
137 dest << source.rdbuf();
138
139 source.close();
140 dest.close();
141}
142
143} // namespace ocpn
Standard, mostly strings utilities.
Definition: ocpn_utils.cpp:41