2016-01-31 03:34:24 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "string_format.hpp"
|
|
|
|
|
2023-09-12 15:38:19 -04:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2023-06-11 16:35:45 -04:00
|
|
|
/* This takes a pointer to the end of a buffer
|
|
|
|
* and fills it backwards towards the front.
|
|
|
|
* The return value 'q' is a pointer to the start.
|
|
|
|
* TODO: use std::array for all this. */
|
2023-07-11 16:48:36 -04:00
|
|
|
template <typename Int>
|
2016-01-31 03:34:24 -05:00
|
|
|
static char* to_string_dec_uint_internal(
|
2023-05-18 16:16:05 -04:00
|
|
|
char* p,
|
2023-07-11 16:48:36 -04:00
|
|
|
Int n) {
|
2023-05-18 16:16:05 -04:00
|
|
|
*p = 0;
|
|
|
|
auto q = p;
|
|
|
|
|
|
|
|
do {
|
2023-07-11 16:48:36 -04:00
|
|
|
*(--q) = n % 10 + '0';
|
2023-05-18 16:16:05 -04:00
|
|
|
n /= 10;
|
|
|
|
} while (n != 0);
|
|
|
|
|
|
|
|
return q;
|
2016-01-31 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static char* to_string_dec_uint_pad_internal(
|
2023-05-18 16:16:05 -04:00
|
|
|
char* const term,
|
|
|
|
const uint32_t n,
|
|
|
|
const int32_t l,
|
|
|
|
const char fill) {
|
|
|
|
auto q = to_string_dec_uint_internal(term, n);
|
|
|
|
|
2023-06-11 16:35:45 -04:00
|
|
|
// Fill with padding if needed.
|
|
|
|
// TODO: use std::array instead. There's no
|
|
|
|
// bounds checks on any of this!
|
2023-05-18 16:16:05 -04:00
|
|
|
if (fill) {
|
|
|
|
while ((term - q) < l) {
|
|
|
|
*(--q) = fill;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return q;
|
2016-01-31 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
2023-08-18 15:35:41 -04:00
|
|
|
static char* to_string_dec_uint_internal(uint64_t n, StringFormatBuffer& buffer, size_t& length) {
|
2023-06-11 16:35:45 -04:00
|
|
|
auto end = &buffer.back();
|
|
|
|
auto start = to_string_dec_uint_internal(end, n);
|
|
|
|
length = end - start;
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
2023-08-18 15:35:41 -04:00
|
|
|
char* to_string_dec_uint(uint64_t n, StringFormatBuffer& buffer, size_t& length) {
|
2023-07-11 16:48:36 -04:00
|
|
|
return to_string_dec_uint_internal(n, buffer, length);
|
|
|
|
}
|
|
|
|
|
2023-08-18 15:35:41 -04:00
|
|
|
char* to_string_dec_int(int64_t n, StringFormatBuffer& buffer, size_t& length) {
|
|
|
|
bool negative = n < 0;
|
|
|
|
auto start = to_string_dec_uint(negative ? -n : n, buffer, length);
|
|
|
|
|
|
|
|
if (negative) {
|
|
|
|
*(--start) = '-';
|
|
|
|
++length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return start;
|
2023-07-11 16:48:36 -04:00
|
|
|
}
|
|
|
|
|
2023-08-18 15:35:41 -04:00
|
|
|
std::string to_string_dec_int(int64_t n) {
|
2023-07-11 16:48:36 -04:00
|
|
|
StringFormatBuffer b{};
|
|
|
|
size_t len{};
|
2023-08-18 15:35:41 -04:00
|
|
|
char* str = to_string_dec_int(n, b, len);
|
2023-07-11 16:48:36 -04:00
|
|
|
return std::string(str, len);
|
|
|
|
}
|
|
|
|
|
2023-08-18 15:35:41 -04:00
|
|
|
std::string to_string_dec_uint(uint64_t n) {
|
2023-07-11 16:48:36 -04:00
|
|
|
StringFormatBuffer b{};
|
|
|
|
size_t len{};
|
|
|
|
char* str = to_string_dec_uint(n, b, len);
|
|
|
|
return std::string(str, len);
|
|
|
|
}
|
|
|
|
|
2016-12-05 06:56:41 -05:00
|
|
|
std::string to_string_bin(
|
2023-05-18 16:16:05 -04:00
|
|
|
const uint32_t n,
|
|
|
|
const uint8_t l) {
|
|
|
|
char p[33];
|
|
|
|
for (uint8_t c = 0; c < l; c++) {
|
|
|
|
if (n & (1 << (l - 1 - c)))
|
|
|
|
p[c] = '1';
|
|
|
|
else
|
|
|
|
p[c] = '0';
|
|
|
|
}
|
|
|
|
p[l] = 0;
|
|
|
|
return p;
|
2016-12-05 06:56:41 -05:00
|
|
|
}
|
|
|
|
|
2016-01-31 03:34:24 -05:00
|
|
|
std::string to_string_dec_uint(
|
2023-05-18 16:16:05 -04:00
|
|
|
const uint32_t n,
|
|
|
|
const int32_t l,
|
|
|
|
const char fill) {
|
|
|
|
char p[16];
|
|
|
|
auto term = p + sizeof(p) - 1;
|
|
|
|
auto q = to_string_dec_uint_pad_internal(term, n, l, fill);
|
|
|
|
|
|
|
|
// Right justify.
|
2023-07-12 14:00:45 -04:00
|
|
|
// (This code is redundant and won't do anything if a fill character was specified)
|
2023-05-18 16:16:05 -04:00
|
|
|
while ((term - q) < l) {
|
|
|
|
*(--q) = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
return q;
|
2016-01-31 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string to_string_dec_int(
|
2023-05-18 16:16:05 -04:00
|
|
|
const int32_t n,
|
|
|
|
const int32_t l,
|
|
|
|
const char fill) {
|
|
|
|
const size_t negative = (n < 0) ? 1 : 0;
|
|
|
|
uint32_t n_abs = negative ? -n : n;
|
|
|
|
|
|
|
|
char p[16];
|
|
|
|
auto term = p + sizeof(p) - 1;
|
|
|
|
auto q = to_string_dec_uint_pad_internal(term, n_abs, l - negative, fill);
|
|
|
|
|
|
|
|
// Add sign.
|
|
|
|
if (negative) {
|
|
|
|
*(--q) = '-';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Right justify.
|
2023-07-12 14:00:45 -04:00
|
|
|
// (This code is redundant and won't do anything if a fill character was specified)
|
2023-05-18 16:16:05 -04:00
|
|
|
while ((term - q) < l) {
|
|
|
|
*(--q) = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
return q;
|
2016-01-31 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
2021-06-23 18:52:15 -04:00
|
|
|
std::string to_string_decimal(float decimal, int8_t precision) {
|
2023-05-18 16:16:05 -04:00
|
|
|
double integer_part;
|
|
|
|
double fractional_part;
|
2021-06-23 18:52:15 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
std::string result;
|
2021-06-23 18:52:15 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
fractional_part = modf(decimal, &integer_part) * pow(10, precision);
|
2021-06-23 18:52:15 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
if (fractional_part < 0) {
|
|
|
|
fractional_part = -fractional_part;
|
|
|
|
}
|
2021-06-23 18:52:15 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
result = to_string_dec_int(integer_part) + "." + to_string_dec_uint(fractional_part, precision, '0');
|
|
|
|
|
|
|
|
return result;
|
2021-06-23 18:52:15 -04:00
|
|
|
}
|
|
|
|
|
2023-07-14 14:38:02 -04:00
|
|
|
// right-justified frequency in Hz, always 10 characters
|
2022-03-28 23:53:09 -04:00
|
|
|
std::string to_string_freq(const uint64_t f) {
|
2023-07-14 14:38:02 -04:00
|
|
|
std::string final_str{""};
|
|
|
|
|
|
|
|
if (f < 1000000)
|
|
|
|
final_str = to_string_dec_int(f, 10, ' ');
|
|
|
|
else
|
|
|
|
final_str = to_string_dec_int(f / 1000000, 4) + to_string_dec_int(f % 1000000, 6, '0');
|
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
return final_str;
|
2022-03-28 23:53:09 -04:00
|
|
|
}
|
|
|
|
|
2023-07-14 14:38:02 -04:00
|
|
|
// right-justified frequency in MHz, rounded to 4 decimal places, always 9 characters
|
2017-05-18 16:56:55 -04:00
|
|
|
std::string to_string_short_freq(const uint64_t f) {
|
2023-07-14 14:38:02 -04:00
|
|
|
auto final_str = to_string_dec_int(f / 1000000, 4) + "." + to_string_dec_int(((f + 50) / 100) % 10000, 4, '0');
|
|
|
|
return final_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
// non-justified non-padded frequency in MHz, rounded to specified number of decimal places
|
|
|
|
std::string to_string_rounded_freq(const uint64_t f, int8_t precision) {
|
|
|
|
std::string final_str{""};
|
|
|
|
static constexpr uint32_t pow10[7] = {
|
|
|
|
1,
|
|
|
|
10,
|
|
|
|
100,
|
|
|
|
1000,
|
|
|
|
10000,
|
|
|
|
100000,
|
|
|
|
1000000,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (precision < 1) {
|
2023-08-18 15:35:41 -04:00
|
|
|
final_str = to_string_dec_uint(f / 1000000);
|
2023-07-14 14:38:02 -04:00
|
|
|
} else {
|
|
|
|
if (precision > 6)
|
|
|
|
precision = 6;
|
|
|
|
|
|
|
|
uint32_t divisor = pow10[6 - precision];
|
|
|
|
|
2023-08-18 15:35:41 -04:00
|
|
|
final_str = to_string_dec_uint(f / 1000000) + "." + to_string_dec_int(((f + (divisor / 2)) / divisor) % pow10[precision], precision, '0');
|
2023-07-14 14:38:02 -04:00
|
|
|
}
|
2023-05-18 16:16:05 -04:00
|
|
|
return final_str;
|
2017-04-24 13:15:57 -04:00
|
|
|
}
|
|
|
|
|
2017-12-10 23:14:54 -05:00
|
|
|
std::string to_string_time_ms(const uint32_t ms) {
|
2023-05-18 16:16:05 -04:00
|
|
|
std::string final_str{""};
|
|
|
|
|
|
|
|
if (ms < 1000) {
|
|
|
|
final_str = to_string_dec_uint(ms) + "ms";
|
|
|
|
} else {
|
|
|
|
auto seconds = ms / 1000;
|
|
|
|
|
|
|
|
if (seconds >= 60)
|
|
|
|
final_str = to_string_dec_uint(seconds / 60) + "m";
|
|
|
|
|
|
|
|
return final_str + to_string_dec_uint(seconds % 60) + "s";
|
|
|
|
}
|
|
|
|
|
|
|
|
return final_str;
|
2017-12-10 23:14:54 -05:00
|
|
|
}
|
|
|
|
|
2023-09-12 15:38:19 -04:00
|
|
|
static char* to_string_hex_internal(char* ptr, uint64_t value, uint8_t length) {
|
|
|
|
if (length == 0)
|
|
|
|
return ptr;
|
|
|
|
|
|
|
|
*(--ptr) = uint_to_char(value & 0xF, 16);
|
|
|
|
return to_string_hex_internal(ptr, value >> 4, length - 1);
|
2016-01-31 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
2023-09-12 15:38:19 -04:00
|
|
|
std::string to_string_hex(uint64_t value, int32_t length) {
|
|
|
|
constexpr uint8_t buffer_length = 33;
|
|
|
|
char buffer[buffer_length];
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2023-09-12 15:38:19 -04:00
|
|
|
char* ptr = &buffer[buffer_length - 1];
|
|
|
|
*ptr = '\0';
|
|
|
|
|
|
|
|
length = std::min<uint8_t>(buffer_length - 1, length);
|
|
|
|
return to_string_hex_internal(ptr, value, length);
|
2016-01-31 03:34:24 -05:00
|
|
|
}
|
|
|
|
|
2023-09-12 15:38:19 -04:00
|
|
|
std::string to_string_hex_array(uint8_t* array, int32_t length) {
|
|
|
|
std::string str_return;
|
|
|
|
str_return.reserve(length);
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2023-09-12 15:38:19 -04:00
|
|
|
for (uint8_t i = 0; i < length; i++)
|
|
|
|
str_return += to_string_hex(array[i], 2);
|
2023-05-18 16:16:05 -04:00
|
|
|
|
|
|
|
return str_return;
|
2017-05-01 05:42:09 -04:00
|
|
|
}
|
|
|
|
|
2017-08-17 07:56:47 -04:00
|
|
|
std::string to_string_datetime(const rtc::RTC& value, const TimeFormat format) {
|
2023-05-18 16:16:05 -04:00
|
|
|
std::string string{""};
|
|
|
|
|
|
|
|
if (format == YMDHMS) {
|
|
|
|
string += to_string_dec_uint(value.year(), 4) + "-" +
|
|
|
|
to_string_dec_uint(value.month(), 2, '0') + "-" +
|
|
|
|
to_string_dec_uint(value.day(), 2, '0') + " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
string += to_string_dec_uint(value.hour(), 2, '0') + ":" +
|
|
|
|
to_string_dec_uint(value.minute(), 2, '0');
|
|
|
|
|
|
|
|
if ((format == YMDHMS) || (format == HMS))
|
|
|
|
string += ":" + to_string_dec_uint(value.second(), 2, '0');
|
|
|
|
|
|
|
|
return string;
|
2016-08-23 08:35:14 -04:00
|
|
|
}
|
|
|
|
|
2016-01-31 03:34:24 -05:00
|
|
|
std::string to_string_timestamp(const rtc::RTC& value) {
|
2023-05-18 16:16:05 -04:00
|
|
|
return to_string_dec_uint(value.year(), 4, '0') +
|
|
|
|
to_string_dec_uint(value.month(), 2, '0') +
|
|
|
|
to_string_dec_uint(value.day(), 2, '0') +
|
|
|
|
to_string_dec_uint(value.hour(), 2, '0') +
|
|
|
|
to_string_dec_uint(value.minute(), 2, '0') +
|
|
|
|
to_string_dec_uint(value.second(), 2, '0');
|
2018-01-09 16:12:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string to_string_FAT_timestamp(const FATTimestamp& timestamp) {
|
2023-05-18 16:16:05 -04:00
|
|
|
return to_string_dec_uint((timestamp.FAT_date >> 9) + 1980) + "-" +
|
|
|
|
to_string_dec_uint((timestamp.FAT_date >> 5) & 0xF, 2, '0') + "-" +
|
|
|
|
to_string_dec_uint((timestamp.FAT_date & 0x1F), 2, '0') + " " +
|
|
|
|
to_string_dec_uint((timestamp.FAT_time >> 11), 2, '0') + ":" +
|
|
|
|
to_string_dec_uint((timestamp.FAT_time >> 5) & 0x3F, 2, '0');
|
2016-01-31 03:34:24 -05:00
|
|
|
}
|
2017-11-02 13:11:26 -04:00
|
|
|
|
2023-05-19 16:39:35 -04:00
|
|
|
std::string to_string_file_size(uint32_t file_size) {
|
|
|
|
static const std::string suffix[5] = {"B", "kB", "MB", "GB", "??"};
|
|
|
|
size_t suffix_index = 0;
|
|
|
|
|
|
|
|
while (file_size >= 1024) {
|
|
|
|
file_size /= 1024;
|
|
|
|
suffix_index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (suffix_index > 4)
|
|
|
|
suffix_index = 4;
|
|
|
|
|
|
|
|
return to_string_dec_uint(file_size) + suffix[suffix_index];
|
|
|
|
}
|
|
|
|
|
2023-09-18 17:22:46 -04:00
|
|
|
std::string unit_auto_scale(double n, const uint32_t base_unit, uint32_t precision) {
|
2023-05-18 16:16:05 -04:00
|
|
|
const uint32_t powers_of_ten[5] = {1, 10, 100, 1000, 10000};
|
|
|
|
std::string string{""};
|
2023-09-18 17:22:46 -04:00
|
|
|
uint32_t prefix_index = base_unit;
|
2023-05-18 16:16:05 -04:00
|
|
|
double integer_part;
|
|
|
|
double fractional_part;
|
|
|
|
|
|
|
|
precision = std::min((uint32_t)4, precision);
|
|
|
|
|
|
|
|
while (n > 1000) {
|
|
|
|
n /= 1000.0;
|
|
|
|
prefix_index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
fractional_part = modf(n, &integer_part) * powers_of_ten[precision];
|
|
|
|
if (fractional_part < 0)
|
|
|
|
fractional_part = -fractional_part;
|
|
|
|
|
|
|
|
string = to_string_dec_int(integer_part);
|
|
|
|
if (precision)
|
2023-07-12 14:00:45 -04:00
|
|
|
string += '.' + to_string_dec_uint(fractional_part, precision, '0');
|
2023-05-18 16:16:05 -04:00
|
|
|
|
|
|
|
if (prefix_index != 3)
|
|
|
|
string += unit_prefix[prefix_index];
|
|
|
|
|
|
|
|
return string;
|
2017-11-02 13:11:26 -04:00
|
|
|
}
|
2020-08-24 16:31:27 -04:00
|
|
|
|
|
|
|
double get_decimals(double num, int16_t mult, bool round) {
|
2023-05-18 16:16:05 -04:00
|
|
|
num -= int(num); // keep decimals only
|
|
|
|
num *= mult; // Shift decimals into integers
|
|
|
|
if (!round) return num;
|
|
|
|
int16_t intnum = int(num); // Round it up if necessary
|
|
|
|
num -= intnum; // Get decimal part
|
|
|
|
if (num > .5) intnum++; // Round up
|
|
|
|
return intnum;
|
2020-08-24 16:31:27 -04:00
|
|
|
}
|
2023-02-28 14:04:03 -05:00
|
|
|
|
2023-06-15 03:45:13 -04:00
|
|
|
static const char* whitespace_str = " \t\r\n";
|
|
|
|
|
2023-06-15 16:15:26 -04:00
|
|
|
std::string trim(std::string_view str) {
|
2023-06-15 03:45:13 -04:00
|
|
|
auto first = str.find_first_not_of(whitespace_str);
|
2023-07-11 16:48:36 -04:00
|
|
|
if (first == std::string::npos)
|
|
|
|
return {};
|
|
|
|
|
2023-06-15 03:45:13 -04:00
|
|
|
auto last = str.find_last_not_of(whitespace_str);
|
2023-06-23 18:08:11 -04:00
|
|
|
return std::string{str.substr(first, last - first + 1)};
|
2023-05-12 14:08:07 -04:00
|
|
|
}
|
|
|
|
|
2023-06-15 16:15:26 -04:00
|
|
|
std::string trimr(std::string_view str) {
|
2023-06-15 03:45:13 -04:00
|
|
|
size_t last = str.find_last_not_of(whitespace_str);
|
2023-06-15 16:15:26 -04:00
|
|
|
return std::string{last != std::string::npos ? str.substr(0, last + 1) : ""};
|
2023-05-12 14:08:07 -04:00
|
|
|
}
|
|
|
|
|
2023-06-15 16:15:26 -04:00
|
|
|
std::string truncate(std::string_view str, size_t length) {
|
|
|
|
return std::string{str.length() <= length ? str : str.substr(0, length)};
|
2023-06-23 18:08:11 -04:00
|
|
|
}
|
2023-09-12 15:38:19 -04:00
|
|
|
|
|
|
|
uint8_t char_to_uint(char c, uint8_t radix) {
|
|
|
|
uint8_t v = 0;
|
|
|
|
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
v = c - '0';
|
|
|
|
else if (c >= 'A' && c <= 'F')
|
|
|
|
v = c - 'A' + 10; // A is dec: 10
|
|
|
|
else if (c >= 'a' && c <= 'f')
|
|
|
|
v = c - 'a' + 10; // A is dec: 10
|
|
|
|
|
|
|
|
return v < radix ? v : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char uint_to_char(uint8_t val, uint8_t radix) {
|
|
|
|
if (val >= radix)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (val < 10)
|
|
|
|
return '0' + val;
|
|
|
|
else
|
|
|
|
return 'A' + val - 10; // A is dec: 10
|
|
|
|
}
|