OpenCPN Partial API docs
Loading...
Searching...
No Matches
ocpn_utils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2019 Alec Leamas *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
16 **************************************************************************/
17
25#include <algorithm>
26#include <cstdio>
27#include <iomanip>
28#include <iostream>
29#include <fstream>
30#include <sstream>
31#include <string.h>
32#include <sys/stat.h>
33
34#ifdef __MSVC__
35#include <io.h>
36#include <direct.h>
37#include <stdlib.h>
38#else
39#include <unistd.h>
40#endif
41
42#include "model/ocpn_utils.h"
43
44namespace ocpn {
45
46bool endswith(const std::string& str, const std::string& suffix) {
47 return str.size() >= suffix.size() &&
48 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix);
49}
50
51bool startswith(const std::string& str, const std::string& prefix) {
52 return prefix.size() <= str.size() &&
53 strncmp(str.c_str(), prefix.c_str(), prefix.size()) == 0;
54}
55
56std::vector<std::string> split(const char* token_string,
57 const std::string& delimiter) {
58 std::vector<std::string> tokens;
59 std::string s = std::string(token_string);
60 size_t pos = 0;
61 std::string token;
62 while ((pos = s.find(delimiter)) != std::string::npos) {
63 token = s.substr(0, pos);
64 tokens.push_back(token);
65 s.erase(0, pos + delimiter.length());
66 }
67 tokens.push_back(s);
68 return tokens;
69}
70
71std::vector<std::string> split(const std::string& string,
72 const std::string& delimiter) {
73 return split(string.c_str(), delimiter);
74}
75
76bool exists(const std::string& name) {
77#ifdef __MSVC__
78 return (_access(name.c_str(), 0) != -1);
79#else
80 return (access(name.c_str(), F_OK) != -1);
81#endif
82}
83
85void mkdir(const std::string path) {
86#if defined(_WIN32) && !defined(__MINGW32__)
87 _mkdir(path.c_str());
88#elif defined(__MINGW32__)
89 ::mkdir(path.c_str());
90#else
91 ::mkdir(path.c_str(), 0755);
92#endif
93}
94
95std::string ltrim(const std::string& arg) {
96 using namespace std;
97
98 string s(arg);
99 s.erase(s.begin(),
100 find_if(s.begin(), s.end(), [](int ch) { return !isspace(ch); }));
101 return s;
102}
103
104std::string rtrim(const std::string& arg) {
105 using namespace std;
106
107 string s(arg);
108 s.erase(
109 find_if(s.rbegin(), s.rend(), [](int ch) { return !isspace(ch); }).base(),
110 s.end());
111 return s;
112}
113
114std::string trim(std::string s) {
115 s = ltrim(s);
116 s = rtrim(s);
117 return s;
118}
119
120std::string join(std::vector<std::string> v, char c) {
121 std::string s;
122 for (auto p = v.begin(); p != v.end(); p++) {
123 s += *p;
124 if (p != v.end() - 1) {
125 s += c;
126 }
127 }
128 return s;
129}
130
131std::string tolower(const std::string& input) {
132 std::string s(input);
133 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
134 return s;
135}
136
137bool replace(std::string& str, const std::string& from, const std::string& to) {
138 size_t start_pos = str.find(from);
139 if (start_pos == std::string::npos) return false;
140 str.replace(start_pos, from.length(), to);
141 return true;
142}
143
144void copy_file(const std::string& src_path, const std::string& dest_path) {
145 std::ifstream source(src_path, std::ios::binary);
146 std::ofstream dest(dest_path, std::ios::binary);
147
148 dest << source.rdbuf();
149
150 source.close();
151 dest.close();
152}
153
154bool N0183CheckSumOk(const std::string& sentence) {
155 size_t check_start = sentence.find('*');
156 if (check_start == std::string::npos || check_start > sentence.size() - 3)
157 return false; // * not found, or it didn't have 2 characters following it.
158
159 std::string check_str = sentence.substr(check_start + 1, 2);
160 unsigned long checksum = strtol(check_str.c_str(), 0, 16);
161 if (checksum == 0L && check_str != "00") return false;
162
163 unsigned char calculated_checksum = 0;
164 for (std::string::const_iterator i = sentence.begin() + 1;
165 i != sentence.end() && *i != '*'; ++i)
166 calculated_checksum ^= static_cast<unsigned char>(*i);
167
168 return calculated_checksum == checksum;
169}
170
171std::string printable(const std::string& str) {
172 std::stringstream ss;
173 for (auto it = str.begin(); it != str.end(); it++) {
174 if (std::isprint(*it) && *it != '\r' && *it != '\n') {
175 ss << *it;
176 } else {
177 std::stringstream ss2;
178 ss2 << std::setw(2) << std::setfill('0') << std::uppercase << std::hex
179 << static_cast<int>(*it);
180 ss << "<" << ss2.str() << ">";
181 }
182 }
183 return ss.str();
184}
185
186} // namespace ocpn
Standard, mostly strings utilities.
Definition datetime.cpp:39
bool replace(std::string &str, const std::string &from, const std::string &to)
Perform in place substitution in str, replacing "from" with "to".
bool startswith(const std::string &str, const std::string &prefix)
Return true if s starts with given prefix.
std::string tolower(const std::string &input)
Return copy of s with all characters converted to lower case.
std::string printable(const std::string &str)
Return copy of str with non-printable chars replaced by hex like "<0D>".
std::vector< std::string > split(const char *token_string, const std::string &delimiter)
Return vector of items in s separated by delimiter.
std::string trim(std::string s)
Strip possibly trailing and/or leading space characters in s.
std::string rtrim(const std::string &arg)
Strip possibly trailing space characters in s.
bool endswith(const std::string &str, const std::string &suffix)
Return true if s ends with given suffix.
std::string ltrim(const std::string &arg)
Strip possibly leading space characters in s.
void copy_file(const std::string &src_path, const std::string &dest_path)
Copy file contents in path src_path to dest_path.
bool exists(const std::string &name)
bool N0183CheckSumOk(const std::string &sentence)
Check if checksum in a NMEA0183 sentence is correct.
std::string join(std::vector< std::string > v, char c)
Return a single string being the concatenation of all elements in v with character c in between.
void mkdir(const std::string path)
Miscellaneous utilities, many of which string related.