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);
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;
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);
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());
71std::vector<std::string>
split(
const std::string&
string,
72 const std::string& delimiter) {
73 return split(
string.c_str(), delimiter);
76bool exists(
const std::string& name) {
78 return (_access(name.c_str(), 0) != -1);
80 return (access(name.c_str(), F_OK) != -1);
85void mkdir(
const std::string path) {
86#if defined(_WIN32) && !defined(__MINGW32__)
88#elif defined(__MINGW32__)
95std::string
ltrim(
const std::string& arg) {
100 find_if(s.begin(), s.end(), [](
int ch) { return !isspace(ch); }));
104std::string
rtrim(
const std::string& arg) {
109 find_if(s.rbegin(), s.rend(), [](
int ch) { return !isspace(ch); }).base(),
114std::string
trim(std::string s) {
120std::string
join(std::vector<std::string> v,
char c) {
122 for (
auto p = v.begin(); p != v.end(); p++) {
124 if (p != v.end() - 1) {
131std::string
tolower(
const std::string& input) {
132 std::string s(input);
133 std::transform(s.begin(), s.end(), s.begin(),
::tolower);
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);
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);
148 dest << source.rdbuf();
155 size_t check_start = sentence.find(
'*');
156 if (check_start == std::string::npos || check_start > sentence.size() - 3)
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;
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);
168 return calculated_checksum == checksum;
172 std::stringstream ss;
173 for (
auto it = str.begin(); it != str.end(); it++) {
174 if (std::isprint(*it) && *it !=
'\r' && *it !=
'\n') {
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() <<
">";
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.