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);
45bool endswith(
const std::string& str,
const std::string& suffix) {
…}
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;
50bool startswith(
const std::string& str,
const std::string& prefix) {
…}
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);
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());
55std::vector<std::string>
split(
const char* token_string, {
…}
70std::vector<std::string>
split(
const std::string&
string,
71 const std::string& delimiter) {
72 return split(
string.c_str(), delimiter);
70std::vector<std::string>
split(
const std::string&
string, {
…}
75bool exists(
const std::string& name) {
77 return (_access(name.c_str(), 0) != -1);
79 return (access(name.c_str(), F_OK) != -1);
84void mkdir(
const std::string path) {
85#if defined(_WIN32) && !defined(__MINGW32__)
87#elif defined(__MINGW32__)
84void mkdir(
const std::string path) {
…}
94std::string
ltrim(
const std::string& arg) {
99 find_if(s.begin(), s.end(), [](
int ch) { return !isspace(ch); }));
94std::string
ltrim(
const std::string& arg) {
…}
103std::string
rtrim(
const std::string& arg) {
108 find_if(s.rbegin(), s.rend(), [](
int ch) { return !isspace(ch); }).base(),
103std::string
rtrim(
const std::string& arg) {
…}
113std::string
trim(std::string s) {
119std::string
join(std::vector<std::string> v,
char c) {
121 for (
auto p = v.begin(); p != v.end(); p++) {
123 if (p != v.end() - 1) {
119std::string
join(std::vector<std::string> v,
char c) {
…}
130std::string
tolower(
const std::string& input) {
131 std::string s(input);
132 std::transform(s.begin(), s.end(), s.begin(),
::tolower);
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);
136bool replace(std::string& str,
const std::string& from,
const std::string& to) {
…}
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);
147 dest << source.rdbuf();
143void copy_file(
const std::string& src_path,
const std::string& dest_path) {
…}
154 size_t check_start = sentence.find(
'*');
155 if (check_start == std::string::npos || check_start > sentence.size() - 3)
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;
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);
167 return calculated_checksum == checksum;
171 std::stringstream ss;
172 for (
auto it = str.begin(); it != str.end(); it++) {
173 if (std::isprint(*it) && *it !=
'\r' && *it !=
'\n') {
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() <<
">";
Standard, mostly strings utilities.
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.