OpenCPN Partial API docs
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
84void mkdir(const std::string path) {
85#if defined(_WIN32) && !defined(__MINGW32__)
86 _mkdir(path.c_str());
87#elif defined(__MINGW32__)
88 ::mkdir(path.c_str());
89#else
90 ::mkdir(path.c_str(), 0755);
91#endif
92}
93
94std::string ltrim(const std::string& arg) {
95 using namespace std;
96
97 string s(arg);
98 s.erase(s.begin(),
99 find_if(s.begin(), s.end(), [](int ch) { return !isspace(ch); }));
100 return s;
101}
102
103std::string rtrim(const std::string& arg) {
104 using namespace std;
105
106 string s(arg);
107 s.erase(
108 find_if(s.rbegin(), s.rend(), [](int ch) { return !isspace(ch); }).base(),
109 s.end());
110 return s;
111}
112
113std::string trim(std::string s) {
114 s = ltrim(s);
115 s = rtrim(s);
116 return s;
117}
118
119std::string join(std::vector<std::string> v, char c) {
120 std::string s;
121 for (auto p = v.begin(); p != v.end(); p++) {
122 s += *p;
123 if (p != v.end() - 1) {
124 s += c;
125 }
126 }
127 return s;
128}
129
130std::string tolower(const std::string& input) {
131 std::string s(input);
132 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
133 return s;
134}
135
136bool replace(std::string& str, const std::string& from, const std::string& to) {
137 size_t start_pos = str.find(from);
138 if (start_pos == std::string::npos) return false;
139 str.replace(start_pos, from.length(), to);
140 return true;
141}
142
143void copy_file(const std::string& src_path, const std::string& dest_path) {
144 std::ifstream source(src_path, std::ios::binary);
145 std::ofstream dest(dest_path, std::ios::binary);
146
147 dest << source.rdbuf();
148
149 source.close();
150 dest.close();
151}
152
153bool N0183CheckSumOk(const std::string& sentence) {
154 size_t check_start = sentence.find('*');
155 if (check_start == std::string::npos || check_start > sentence.size() - 3)
156 return false; // * not found, or it didn't have 2 characters following it.
157
158 std::string check_str = sentence.substr(check_start + 1, 2);
159 unsigned long checksum = strtol(check_str.c_str(), 0, 16);
160 if (checksum == 0L && check_str != "00") return false;
161
162 unsigned char calculated_checksum = 0;
163 for (std::string::const_iterator i = sentence.begin() + 1;
164 i != sentence.end() && *i != '*'; ++i)
165 calculated_checksum ^= static_cast<unsigned char>(*i);
166
167 return calculated_checksum == checksum;
168}
169
170std::string printable(const std::string& str) {
171 std::stringstream ss;
172 for (auto it = str.begin(); it != str.end(); it++) {
173 if (std::isprint(*it) && *it != '\r' && *it != '\n') {
174 ss << *it;
175 } else {
176 std::stringstream ss2;
177 ss2 << std::setw(2) << std::setfill('0') << std::uppercase << std::hex
178 << static_cast<int>(*it);
179 ss << "<" << ss2.str() << ">";
180 }
181 }
182 return ss.str();
183}
184
185} // namespace ocpn
Standard, mostly strings utilities.
Definition datetime.cpp:28
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.