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 <iomanip>
27#include <iostream>
28#include <fstream>
29#include <sstream>
30#include <string.h>
31#include <sys/stat.h>
32
33#ifdef __MSVC__
34#include <io.h>
35#include <direct.h>
36#include <stdlib.h>
37#else
38#include <unistd.h>
39#endif
40
41#include "model/ocpn_utils.h"
42
43namespace ocpn {
44
45bool endswith(const std::string& str, const std::string& suffix) {
46 return str.size() >= suffix.size() &&
47 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix);
48}
49
50bool startswith(const std::string& str, const std::string& prefix) {
51 return prefix.size() <= str.size() &&
52 strncmp(str.c_str(), prefix.c_str(), prefix.size()) == 0;
53}
54
55std::vector<std::string> split(const char* token_string,
56 const std::string& delimiter) {
57 std::vector<std::string> tokens;
58 std::string s = std::string(token_string);
59 size_t pos = 0;
60 std::string token;
61 while ((pos = s.find(delimiter)) != std::string::npos) {
62 token = s.substr(0, pos);
63 tokens.push_back(token);
64 s.erase(0, pos + delimiter.length());
65 }
66 tokens.push_back(s);
67 return tokens;
68}
69
70std::vector<std::string> split(const std::string& string,
71 const std::string& delimiter) {
72 return split(string.c_str(), delimiter);
73}
74
75bool exists(const std::string& name) {
76#ifdef __MSVC__
77 return (_access(name.c_str(), 0) != -1);
78#else
79 return (access(name.c_str(), F_OK) != -1);
80#endif
81}
82
83void mkdir(const std::string path) {
84#if defined(_WIN32) && !defined(__MINGW32__)
85 _mkdir(path.c_str());
86#elif defined(__MINGW32__)
87 ::mkdir(path.c_str());
88#else
89 ::mkdir(path.c_str(), 0755);
90#endif
91}
92
93std::string ltrim(std::string s) {
94 using namespace std;
95
96 s.erase(s.begin(),
97 find_if(s.begin(), s.end(), [](int ch) { return !isspace(ch); }));
98 return s;
99}
100
101std::string rtrim(std::string s) {
102 using namespace std;
103
104 s.erase(
105 find_if(s.rbegin(), s.rend(), [](int ch) { return !isspace(ch); }).base(),
106 s.end());
107 return s;
108}
109
110std::string trim(std::string s) {
111 s = ltrim(s);
112 s = rtrim(s);
113 return s;
114}
115
116std::string join(std::vector<std::string> v, char c) {
117 std::string s;
118 for (auto p = v.begin(); p != v.end(); p++) {
119 s += *p;
120 if (p != v.end() - 1) {
121 s += c;
122 }
123 }
124 return s;
125}
126
127std::string tolower(const std::string& input) {
128 std::string s(input);
129 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
130 return s;
131}
132
133bool replace(std::string& str, const std::string& from, const std::string& to) {
134 size_t start_pos = str.find(from);
135 if (start_pos == std::string::npos) return false;
136 str.replace(start_pos, from.length(), to);
137 return true;
138}
139
140void copy_file(const std::string& src_path, const std::string& dest_path) {
141 std::ifstream source(src_path, std::ios::binary);
142 std::ofstream dest(dest_path, std::ios::binary);
143
144 dest << source.rdbuf();
145
146 source.close();
147 dest.close();
148}
149
150bool N0183CheckSumOk(const std::string& sentence) {
151 size_t check_start = sentence.find('*');
152 if (check_start == std::string::npos || check_start > sentence.size() - 3)
153 return false; // * not found, or it didn't have 2 characters following it.
154
155 std::string check_str = sentence.substr(check_start + 1, 2);
156 unsigned long checksum = strtol(check_str.c_str(), 0, 16);
157 if (checksum == 0L && check_str != "00") return false;
158
159 unsigned char calculated_checksum = 0;
160 for (std::string::const_iterator i = sentence.begin() + 1;
161 i != sentence.end() && *i != '*'; ++i)
162 calculated_checksum ^= static_cast<unsigned char>(*i);
163
164 return calculated_checksum == checksum;
165}
166
167std::string printable(const std::string str) {
168 std::stringstream ss;
169 for (auto it = str.begin(); it != str.end(); it++) {
170 if (std::isprint(*it) && *it != '\r' && *it != '\n') {
171 ss << *it;
172 } else {
173 std::stringstream ss2;
174 ss2 << std::setw(2) << std::setfill('0') << std::uppercase << std::hex
175 << static_cast<int>(*it);
176 ss << "<" << ss2.str() << ">";
177 }
178 }
179 return ss.str();
180}
181
182} // namespace ocpn
Standard, mostly strings utilities.
std::string printable(const std::string str)
Return copy of str with non-printable chars replaced by hex like "<0D>".
bool N0183CheckSumOk(const std::string &sentence)
Check if checksum in a NMEA0183 sentence is correct.