feature (kernel): implemented a set of data to string conversion functions

This commit is contained in:
antifallobst 2023-02-11 18:09:35 +01:00
parent 7a471bc77b
commit 5b434d7264
3 changed files with 101 additions and 11 deletions

View File

@ -20,7 +20,15 @@
typedef const char* string_t;
uint32_t string_length (string_t str);
uint32_t string_length (string_t string);
bool string_compare (string_t a, string_t b);
void string_unsigned_dec_to_alpha (string_t string, uint64_t value);
void string_dec_to_alpha (string_t string, int64_t value);
void string_hex_8bit_to_alpha (string_t string, uint8_t value);
void string_hex_16bit_to_alpha (string_t string, uint16_t value);
void string_hex_32bit_to_alpha (string_t string, uint32_t value);
void string_hex_64bit_to_alpha (string_t string, uint64_t value);
void string_bin_to_alpha (string_t string, uint8_t num_bits, uint64_t value);
void string_bool_to_alpha (string_t string, bool value);
#endif //NOX_STRING_H

View File

@ -44,13 +44,9 @@ void kmain(boot_info_T boot_info) {
// this should cause a kernel panic
// int x = 1312 / 0;
void* debug;
debug = page_map_get_physical_address(g_kernel_page_map, (void*)0x100000000000);
page_map_map_memory(g_kernel_page_map, (void*)0x100000000000, pframe_request(), PM_FLAG_READ_WRITE);
debug = page_map_get_physical_address(g_kernel_page_map, (void*)0x100000000000);
memory_copy("test string", (void*)0x100000000000, 13);
log(LOG_DEBUG, (string_t)0x100000000000);
char string[17];
string_hex_64bit_to_alpha(string, &kmain);
log(LOG_DEBUG, string);
CORE_HALT_FOREVER

View File

@ -15,10 +15,13 @@
#include "utils/string.h"
#include "utils/memory.h"
#include "utils/math.h"
uint32_t string_length (string_t str) {
const char g_hex_chars[16] = "0123456789ABCDEF";
uint32_t string_length(string_t string) {
uint32_t n = 0;
char* c = (char*)str;
char* c = (char*)string;
while(*c != '\0') {
c++;
@ -36,3 +39,86 @@ bool string_compare(string_t a, string_t b) {
}
return memory_compare(a, b, a_len);
}
void string_unsigned_dec_to_alpha(string_t string, uint64_t value) {
char* buffer = (char*)string;
uint8_t length = 0;
uint64_t divisor = 10;
while (value / divisor > 0) {
length += 1;
divisor *= 10;
}
divisor = 10;
for (int i = length; i > 0; i--) {
buffer[i-1] = '0' + (value / divisor % 10);
divisor *= 10;
}
buffer[length] = '0' + (value % 10);
buffer[length + 1] = '\0';
}
void string_dec_to_alpha(string_t string, int64_t value) {
if (value < 0) {
char* buffer = (char*)string;
buffer[0] = '-';
string_unsigned_dec_to_alpha(&string[1], abs(value));
} else {
string_unsigned_dec_to_alpha(&string[0], value);
}
}
void string_hex_8bit_to_alpha(string_t string, uint8_t value) {
char* buffer = (char*)string;
buffer[1] = g_hex_chars[value & 0x0F];
value >>= 4;
buffer[0] = g_hex_chars[value & 0x0F];
buffer[2] = '\0';
}
void string_hex_16bit_to_alpha(string_t string, uint16_t value) {
char* buffer = (char*)string;
for (int i = 4; i > 0; i--) {
buffer[i-1] = g_hex_chars[value & 0x000F];
value >>= 4;
}
buffer[4] = '\0';
}
void string_hex_32bit_to_alpha(string_t string, uint32_t value) {
char* buffer = (char*)string;
for (int i = 8; i > 0; i--) {
buffer[i-1] = g_hex_chars[value & 0x0000000F];
value >>= 4;
}
buffer[8] = '\0';
}
void string_hex_64bit_to_alpha(string_t string, uint64_t value) {
char* buffer = (char*)string;
for (int i = 16; i > 0; i--) {
buffer[i-1] = g_hex_chars[value & 0x000000000000000F];
value >>= 4;
}
buffer[16] = '\0';
}
void string_bin_to_alpha(string_t string, uint8_t num_bits, uint64_t value) {
char* buffer = (char*)string;
num_bits = MIN(num_bits, 64);
for (uint8_t i = 0; i < num_bits; i++) {
buffer[i] = ((value & (0x8000000000000000 >> i)) > 0) ? '1' : '0';
}
buffer[num_bits] = '\0';
}
void string_bool_to_alpha(string_t string, bool value) {
memory_copy(value ? "true" : "false", (void*)string, value ? 5 : 6);
}