/
bortsov
/
ch32v203-BatteryDisplay
Обзор
Документация
Войти
/
bortsov
/
ch32v203-BatteryDisplay
Код
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
source/iobase.cpp
118 строк
2 KB
Vitaliy Bortsov
build: fix warnings
18 мар 2026, 21:20
18 мар 2026, 21:20
8f56e49
Код
Авторство
О чём код?
#include "iobase.h" #include <cmath> #include <cstring> static unsigned int uint32_tToA(char* to, const uint32_t v_) { uint32_t v = v_; constexpr int maxSize = 12; /* Only for 32-bit integers: 32/Log_2[10]=9.63:*/ char buf[maxSize]; char* const ptrEnd = buf + sizeof(buf) - 1; char* ptr = ptrEnd; *ptr = 0; do { --ptr; *ptr = '0' + static_cast<char>(v % 10); v /= 10; } while (v); const int lengthWithEndOfString = ptrEnd - ptr + 1; memcpy(to, ptr, lengthWithEndOfString); return lengthWithEndOfString; } class CommunicationBase& operator << (class CommunicationBase& s, const unsigned int v) { constexpr size_t maxLength = 11; /* максимальная длина строкового представления 32-битного беззнакового числа */ char buf[maxLength]; const auto lengthWithEndOfString = uint32_tToA(buf, v); const auto lengthWithoutEndOfString = lengthWithEndOfString - 1; s.putArray(buf, lengthWithoutEndOfString); return s; } class CommunicationBase& operator << (class CommunicationBase& s, const uint64_t& binary) { if (binary == 0) { s.putChar('0'); } else { constexpr int MAX_CHARS{20}; /* maximum is 18'446'744'073'709'551'615 -- 20 chars */ uint64_t v{binary}; uint64_t temp = 10'000'000'000'000'000'000ULL; bool flagZero{true}; for (int i = 0; i < MAX_CHARS; ++i) { const int v2{static_cast<int>(v / temp)}; v -= v2 * temp; temp /= 10; if (flagZero) { if (v2 != 0) { flagZero = false; } } if (flagZero == false) { s.putChar(static_cast<char>(v2) + '0'); } } } return s; } class CommunicationBase& operator<< (class CommunicationBase& s, const char* str) { s.putString(str); return s; } class CommunicationBase& operator<< (class CommunicationBase& s, const char ch) { s.putChar(ch); return s; } class CommunicationBase& operator<< (class CommunicationBase& s, const int v) { unsigned int temp; if (v < 0) { s << '-'; temp = -v; } else { temp = v; } s << temp; return s; } class CommunicationBase& operator << (class CommunicationBase& s, const std::bitset<32>& v32) { for (int i = 31; i >= 0; --i) { s << static_cast<char>(v32[i] + '0'); } return s; } void CommunicationBase::putString(const char* str) { putArray(str, strlen(str)); }