pmt: improve libhelper, etc.

This commit is contained in:
2025-09-13 11:36:29 +03:00
parent b99f20c6a1
commit 398b119cb4
9 changed files with 96 additions and 72 deletions

View File

@@ -44,6 +44,13 @@ enum LogLevels {
ABORT = static_cast<int>('A')
};
enum sizeCastTypes {
B = static_cast<int>('B'),
KB = static_cast<int>('K'),
MB = static_cast<int>('M'),
GB = static_cast<int>('G')
};
constexpr mode_t DEFAULT_FILE_PERMS = 0644;
constexpr mode_t DEFAULT_EXTENDED_FILE_PERMS = 0755;
constexpr mode_t DEFAULT_DIR_PERMS = 0755;
@@ -342,18 +349,29 @@ std::string pathDirname(std::string_view entry);
uint64_t getRandomOffset(uint64_t size, uint64_t bufferSize);
/**
* Convert input size to input multiple
* Convert input size to input multiple.
*/
std::string convertTo(uint64_t size, const std::string &multiple);
int convertTo(uint64_t size, sizeCastTypes type);
/**
* Convert input multiple variable to string.
*/
std::string multipleToString(sizeCastTypes type);
/**
* Format it input and return as std::string.
*/
__attribute__((format(printf, 1, 2))) std::string format(const char *format,
...);
/**
* Convert input size to input multiple
*/
template <uint64_t size> std::string convertTo(const std::string &multiple) {
if (multiple == "KB") return std::to_string(TO_KB(size));
if (multiple == "MB") return std::to_string(TO_MB(size));
if (multiple == "GB") return std::to_string(TO_GB(size));
return std::to_string(size);
template <uint64_t size> int convertTo(const sizeCastTypes type) {
if (type == KB) return TO_KB(size);
if (type == MB) return TO_MB(size);
if (type == GB) return TO_GB(size);
return static_cast<int>(size);
}
// -------------------------------
@@ -361,12 +379,12 @@ template <uint64_t size> std::string convertTo(const std::string &multiple) {
// -------------------------------
#ifdef __ANDROID__
/**
* Get input property as string.
* Get input property as string (for Android).
*/
std::string getProperty(std::string_view prop);
/**
* Reboot device to input mode.
* Reboot device to input mode (for Android).
*/
bool reboot(std::string_view arg);
#endif

View File

@@ -35,6 +35,7 @@
#include <sys/_system_properties.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cstdarg>
#ifdef __ANDROID__
// From system/core/libcutils/android_reboot.cpp android16-s2-release
@@ -228,14 +229,31 @@ bool reboot(const std::string_view arg) {
uint64_t getRandomOffset(const uint64_t size, const uint64_t bufferSize) {
if (size <= bufferSize) return 0;
const uint64_t maxOffset = size - bufferSize;
srand(time(nullptr));
return rand() % maxOffset;
}
std::string convertTo(const uint64_t size, const std::string &multiple) {
if (multiple == "KB") return std::to_string(TO_KB(size));
if (multiple == "MB") return std::to_string(TO_MB(size));
if (multiple == "GB") return std::to_string(TO_GB(size));
return std::to_string(size);
int convertTo(const uint64_t size, const sizeCastTypes type) {
if (type == KB) return TO_KB(size);
if (type == MB) return TO_MB(size);
if (type == GB) return TO_GB(size);
return static_cast<int>(size);
}
std::string multipleToString(const sizeCastTypes type) {
if (type == KB) return "KB";
if (type == MB) return "MB";
if (type == GB) return "GB";
return "B";
}
std::string format(const char *format, ...) {
va_list args;
va_start(args, format);
char str[1024];
vsnprintf(str, sizeof(str), format, args);
va_end(args);
return str;
}
std::string getLibVersion() { MKVERSION("libhelper"); }