OpenCPN Partial API docs
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 "model/ocpn_utils.h"
40 
41 namespace ocpn {
42 
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);
46 }
47 
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;
51 }
52 
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);
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 
68 std::vector<std::string> split(const std::string& string,
69  const std::string& delimiter) {
70  return split(string.c_str(), delimiter);
71 }
72 
73 bool exists(const std::string& name) {
74 #ifdef __MSVC__
75  return (_access(name.c_str(), 0) != -1);
76 #else
77  return (access(name.c_str(), F_OK) != -1);
78 #endif
79 }
80 
81 void mkdir(const std::string path) {
82 #if defined(_WIN32) && !defined(__MINGW32__)
83  _mkdir(path.c_str());
84 #elif defined(__MINGW32__)
85  ::mkdir(path.c_str());
86 #else
87  ::mkdir(path.c_str(), 0755);
88 #endif
89 }
90 
91 std::string ltrim(std::string s) {
92  using namespace std;
93 
94  s.erase(s.begin(),
95  find_if(s.begin(), s.end(), [](int ch) { return !isspace(ch); }));
96  return s;
97 }
98 
99 std::string rtrim(std::string s) {
100  using namespace std;
101 
102  s.erase(
103  find_if(s.rbegin(), s.rend(), [](int ch) { return !isspace(ch); }).base(),
104  s.end());
105  return s;
106 }
107 
108 std::string trim(std::string s) {
109  s = ltrim(s);
110  s = rtrim(s);
111  return s;
112 }
113 
114 std::string join(std::vector<std::string> v, char c) {
115  std::string s;
116  for (auto p = v.begin(); p != v.end(); p++) {
117  s += *p;
118  if (p != v.end() - 1) {
119  s += c;
120  }
121  }
122  return s;
123 }
124 
125 std::string tolower(const std::string& input) {
126  std::string s(input);
127  std::transform(s.begin(), s.end(), s.begin(), ::tolower);
128  return s;
129 }
130 
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);
135  return true;
136 }
137 
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);
141 
142  dest << source.rdbuf();
143 
144  source.close();
145  dest.close();
146 }
147 
148 } // namespace ocpn
Standard, mostly strings utilities.
Definition: ocpn_utils.cpp:41