From 610f91d87152cbe48d3054fcf437d8239da6ef35 Mon Sep 17 00:00:00 2001 From: Amlal Date: Sat, 21 Dec 2024 21:59:13 +0100 Subject: IMP: :boom: Breaking changes some checks are needed to be done. Signed-off-by: Amlal --- dev/Kernel/src/Utils.cc | 223 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 223 insertions(+) create mode 100644 dev/Kernel/src/Utils.cc (limited to 'dev/Kernel/src/Utils.cc') diff --git a/dev/Kernel/src/Utils.cc b/dev/Kernel/src/Utils.cc new file mode 100644 index 00000000..0c3ac5f8 --- /dev/null +++ b/dev/Kernel/src/Utils.cc @@ -0,0 +1,223 @@ +/* ------------------------------------------- + + Copyright (C) 2024, TQ B.V, all rights reserved. + +------------------------------------------- */ + +#include + +namespace Kernel +{ + Int32 rt_string_cmp(const Char* src, const Char* cmp, Size size) + { + if (!cmp || + !src) + return 1; + + Int32 counter = 0; + + for (Size index = 0; index < size; ++index) + { + if (src[index] != cmp[index]) + ++counter; + } + + return counter; + } + + Void rt_zero_memory(voidPtr pointer, Size len) + { + rt_set_memory(pointer, 0, len); + } + + SizeT rt_string_len(const Char* str, SizeT _len) + { + SizeT len{0}; + + do + { + if (len > _len) + { + return _len; + } + + ++len; + } while (str[len] != '\0'); + + return len; + } + + Size rt_string_len(const Char* ptr) + { + SizeT cnt{0}; + + do + { + ++cnt; + } while (ptr[cnt] != 0); + + return cnt; + } + + voidPtr rt_set_memory(voidPtr src, UInt32 value, Size len) + { + if (!src || len < 1) + return nullptr; + + UInt32* start = reinterpret_cast(src); + + while (len) + { + *start = value; + ++start; + --len; + } + + return (voidPtr)start; + } + + Int rt_move_memory(const voidPtr src, voidPtr dst, Size len) + { + if (len < 1) + return 2; + + if (!src || !dst) + return 1; + + Char* srcChr = reinterpret_cast(src); + Char* dstChar = reinterpret_cast(dst); + SizeT index = 0; + + while (index < len) + { + dstChar[index] = srcChr[index]; + srcChr[index] = 0; + + ++index; + } + + return 0; + } + + Int rt_copy_memory(const voidPtr src, voidPtr dst, Size len) + { + if (len < 1) + return 0; + + char* srcChr = reinterpret_cast(src); + char* dstChar = reinterpret_cast(dst); + Size index = 0; + + while (index < len) + { + dstChar[index] = srcChr[index]; + ++index; + } + + return index; + } + + const Char* alloc_string(const Char* text) + { + if (!text) + return nullptr; + + const Char* string = new Char[rt_string_len(text)]; + + if (!string) + return nullptr; + + voidPtr vText = reinterpret_cast(const_cast(text)); + voidPtr vStr = reinterpret_cast(const_cast(string)); + + rt_copy_memory(vText, vStr, rt_string_len(text)); + + return string; + } + + Int32 rt_to_uppercase(Int32 character) + { + if (character >= 'a' && character <= 'z') + return character - 0x20; + + return character; + } + + Int32 rt_to_lower(Int32 character) + { + if (character >= 'A' && character <= 'Z') + return character + 0x20; + + return character; + } + + Bool rt_to_string(Char* str, Int32 limit, Int32 base) + { + if (limit == 0) + return false; + + Int copy_limit = limit; + Int cnt = 0; + Int ret = base; + + while (limit != 1) + { + ret = ret % 10; + str[cnt] = ret; + + ++cnt; + --limit; + --ret; + } + + str[copy_limit] = '\0'; + return true; + } + + Boolean is_space(Char chr) + { + return chr == ' '; + } + + Boolean is_newln(Char chr) + { + return chr == '\n'; + } + + voidPtr rt_string_in_string(const Char* in, const Char* needle) + { + for (SizeT i = 0; i < rt_string_len(in); ++i) + { + if (rt_string_cmp(in + i, needle, rt_string_len(needle)) == 0) + return reinterpret_cast(const_cast(in + i)); + } + + return nullptr; + } + + // @brief Checks for a string start at the character. + + Char* rt_string_has_char(Char* str, const Char chr) + { + while (*str != chr) + { + ++str; + + if (*str == 0) + return nullptr; + } + + return str; + } +} // namespace Kernel + +EXTERN_C void* memset(void* dst, int c, long long unsigned int len) +{ + return Kernel::rt_set_memory(dst, c, len); +} + +EXTERN_C void* memcpy(void* dst, const void* src, long long unsigned int len) +{ + Kernel::rt_copy_memory(const_cast(src), dst, len); + return dst; +} -- cgit v1.2.3