From 736e07144143850be33bfb7e60abbaf8bed9d0be Mon Sep 17 00:00:00 2001 From: vvaucoul Date: Sun, 28 Jul 2024 01:49:27 +0200 Subject: [PATCH] [Update] - Update Hephaistos --- Hephaistos/include/stddef.h | 2 +- Hephaistos/include/stdint.h | 2 +- Hephaistos/include/stdio/printk.h | 8 +- Hephaistos/include/string.h | 32 +- Hephaistos/include/time.h | 7 +- Hephaistos/src/algorithms/compares.c | 152 +++- Hephaistos/src/assert.c | 30 +- Hephaistos/src/convert.c | 667 ++++++++------- Hephaistos/src/ctype.c | 34 +- Hephaistos/src/errno.c | 286 +++---- Hephaistos/src/hexdump.c | 25 +- Hephaistos/src/math.c | 84 +- Hephaistos/src/memory.c | 3 +- Hephaistos/src/stdio/printk/print.c | 4 +- Hephaistos/src/stdio/printk/srcs/printk.c | 419 +++++----- .../src/stdio/printk/srcs/printk_manager.c | 8 +- .../printk/srcs/printk_special_manager.c | 2 +- .../src/stdio/printk/srcs/printk_utils.c | 6 +- Hephaistos/src/string.c | 771 +++++++++++------- Hephaistos/src/time.c | 24 +- Makefile | 2 +- tests/workflow_strings.c | 268 ++++-- workflows/hephaistos_workflow.c | 4 +- workflows/workflows.h | 4 +- 24 files changed, 1655 insertions(+), 1189 deletions(-) diff --git a/Hephaistos/include/stddef.h b/Hephaistos/include/stddef.h index dadcb9e..4eb218c 100644 --- a/Hephaistos/include/stddef.h +++ b/Hephaistos/include/stddef.h @@ -6,7 +6,7 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/11/20 10:08:03 by vvaucoul #+# #+# */ -/* Updated: 2024/07/26 20:10:30 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/28 01:24:10 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/Hephaistos/include/stdint.h b/Hephaistos/include/stdint.h index 05effbe..7f186e0 100644 --- a/Hephaistos/include/stdint.h +++ b/Hephaistos/include/stdint.h @@ -6,7 +6,7 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/26 20:09:04 by vvaucoul #+# #+# */ -/* Updated: 2024/07/27 09:49:23 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/28 01:44:15 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/Hephaistos/include/stdio/printk.h b/Hephaistos/include/stdio/printk.h index 50aed04..b88e520 100644 --- a/Hephaistos/include/stdio/printk.h +++ b/Hephaistos/include/stdio/printk.h @@ -6,7 +6,7 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/11/19 23:55:51 by vvaucoul #+# #+# */ -/* Updated: 2024/07/26 22:46:29 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/28 01:34:29 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,7 @@ typedef struct s_printk { va_list args; - char *format; + char format[1024]; int32_t space; bool space_is_star; @@ -54,7 +54,7 @@ extern void vsprintk(const char *str, void (*write)(const char), const char *for extern void __kpf_manage_space_front(const int arg_len); extern void __kpf_manage_space_back(const int arg_len); -extern size_t __kpf_manage_mod(const char *format, size_t i); +extern uint32_t __kpf_manage_mod(const char *format, uint32_t i); extern void __kpf_manage_char(); extern void __kpf_manage_nbr(); extern void __kpf_manage_str(); @@ -63,7 +63,7 @@ extern void __kpf_manage_hexa(); extern void __kpf_manage_unsigned(); extern void __kpf_manage_float(); -extern size_t __kptrlen(const void *ptr); +extern uint32_t __kptrlen(const void *ptr); extern uint32_t __k_get_len(uint32_t arg_len); #endif /* !PRINTK_H */ \ No newline at end of file diff --git a/Hephaistos/include/string.h b/Hephaistos/include/string.h index 7de052c..a23fc7b 100644 --- a/Hephaistos/include/string.h +++ b/Hephaistos/include/string.h @@ -6,7 +6,7 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/11/20 11:31:06 by vvaucoul #+# #+# */ -/* Updated: 2024/07/26 22:12:23 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/27 22:14:09 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,8 @@ // ! || ASM FUNCTIONS || // ! ||--------------------------------------------------------------------------------|| +#define USE_ASM_STRING_FUNCTIONS 1 + extern uint32_t __strlen(const char *str); extern uint32_t __nbrlen(int32_t nbr); extern uint32_t __nbrlen_base(int32_t nbr, const uint8_t base); @@ -29,34 +31,48 @@ extern int32_t __strncmp(const char *s1, const char *s2, uint32_t n); // ! || C FUNCTIONS || // ! ||--------------------------------------------------------------------------------|| +#if USE_ASM_STRING_FUNCTIONS == 1 + inline uint32_t nbrlen(int32_t nbr) { - return (__nbrlen(nbr)); + return (__nbrlen(nbr)); } inline uint32_t unbrlen(uint32_t nbr) { - return (__nbrlen((uint32_t)nbr)); + return (__nbrlen((uint32_t)nbr)); } inline uint32_t strlen(const char *str) { - return (__strlen(str)); + return (__strlen(str)); } inline uint32_t nbrlen_base(const int32_t nbr, const uint8_t base) { - return (__nbrlen_base(nbr, base)); + return (__nbrlen_base(nbr, base)); } inline uint32_t unbrlen_base(const uint32_t nbr, const uint8_t base) { - return (__nbrlen_base((uint32_t)nbr, base)); + return (__nbrlen_base((uint32_t)nbr, base)); } inline int32_t strcmp(const char *s1, const char *s2) { - return (__strcmp(s1, s2)); + return (__strcmp(s1, s2)); } inline int32_t strncmp(const char *s1, const char *s2, uint32_t n) { - return (__strncmp(s1, s2, n)); + return (__strncmp(s1, s2, n)); } +#else + +uint32_t nbrlen(int32_t nbr); +uint32_t unbrlen(uint32_t nbr); +uint32_t strlen(const char *str); +uint32_t nbrlen_base(const int32_t nbr, const uint8_t base); +uint32_t unbrlen_base(const uint32_t nbr, const uint8_t base); +int32_t strcmp(const char *s1, const char *s2); +int32_t strncmp(const char *s1, const char *s2, uint32_t n); + +#endif + char *strtrtc(const char *str, const char c); char *strclr(char *new_str, char *str); char *strcpy(char *dest, const char *src); diff --git a/Hephaistos/include/time.h b/Hephaistos/include/time.h index 04c0870..235bec0 100644 --- a/Hephaistos/include/time.h +++ b/Hephaistos/include/time.h @@ -6,7 +6,7 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/11/21 14:10:07 by vvaucoul #+# #+# */ -/* Updated: 2024/07/26 21:59:54 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/27 16:12:48 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,9 +15,8 @@ #include -typedef struct s_counter -{ - uint32_t start; +typedef struct s_counter { + uint32_t start; uint32_t end; } counter_t; diff --git a/Hephaistos/src/algorithms/compares.c b/Hephaistos/src/algorithms/compares.c index 8d2a268..d93bc30 100644 --- a/Hephaistos/src/algorithms/compares.c +++ b/Hephaistos/src/algorithms/compares.c @@ -6,7 +6,7 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/14 00:08:11 by vvaucoul #+# #+# */ -/* Updated: 2024/07/26 22:00:28 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/28 01:28:40 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,54 +17,200 @@ // ! || COMPARE FUNCTIONS || // ! ||--------------------------------------------------------------------------------|| +/** + * @brief Compares two integers. + * + * This function is used as a comparison function for sorting algorithms. + * It compares two integers pointed to by the parameters `a` and `b`. + * + * @param a Pointer to the first integer. + * @param b Pointer to the second integer. + * @return An integer less than, equal to, or greater than zero if the first integer is considered to be respectively less than, equal to, or greater than the second integer. + */ int cmp_int(const void *a, const void *b) { return (*(int *)a - *(int *)b); } +/** + * @brief Compares two strings. + * + * This function is used as a comparison function for sorting strings. + * It compares two strings pointed to by the arguments `a` and `b`. + * + * @param a Pointer to the first string. + * @param b Pointer to the second string. + * @return An integer less than, equal to, or greater than zero if the first string is found to be less than, equal to, or greater than the second string, respectively. + */ int cmp_string(const void *a, const void *b) { - return (strcmp((char *)a, (char *)b)); + return (strcmp((const char *)a, (const char *)b)); } +/** + * @brief Compares two characters. + * + * This function is used as a comparison function for sorting characters. + * It compares the values of the characters pointed to by the pointers `a` and `b`. + * + * @param a Pointer to the first character. + * @param b Pointer to the second character. + * @return An integer less than, equal to, or greater than zero if the first character is found to be less than, equal to, or greater than the second character, respectively. + */ int cmp_char(const void *a, const void *b) { return (*(char *)a - *(char *)b); } +/** + * @brief Compares two int8_t values. + * + * This function is used as a comparison function for sorting or searching algorithms. + * It compares the values pointed to by the pointers 'a' and 'b'. + * + * @param a Pointer to the first int8_t value. + * @param b Pointer to the second int8_t value. + * @return An integer less than, equal to, or greater than zero if the first value is found to be less than, equal to, or greater than the second value, respectively. + */ int cmp_int8_t(const void *a, const void *b) { return (*(int8_t *)a - *(int8_t *)b); } +/** + * @brief Compares two int16_t values. + * + * This function is used as a comparison function for sorting int16_t values. + * It compares the values pointed to by the pointers 'a' and 'b' and returns + * an integer value indicating their relative order. + * + * @param a Pointer to the first int16_t value. + * @param b Pointer to the second int16_t value. + * @return An integer value less than, equal to, or greater than zero if the + * first value is considered to be respectively less than, equal to, or + * greater than the second value. + */ int cmp_int16_t(const void *a, const void *b) { return (*(int16_t *)a - *(int16_t *)b); } +/** + * @brief Compares two int32_t values. + * + * This function is used as a comparison function for sorting int32_t values. + * It compares the values pointed to by the pointers 'a' and 'b' and returns + * an integer value indicating their relative order. + * + * @param a Pointer to the first int32_t value. + * @param b Pointer to the second int32_t value. + * @return An integer value less than, equal to, or greater than zero if the + * first value is considered to be respectively less than, equal to, or + * greater than the second value. + */ int cmp_int32_t(const void *a, const void *b) { return (*(int32_t *)a - *(int32_t *)b); } +/** + * @brief Compares two int64_t values. + * + * This function is used as a comparison function for sorting int64_t values. + * It compares the values pointed to by the pointers 'a' and 'b' and returns + * an integer value indicating their relative order. + * + * @param a Pointer to the first int64_t value. + * @param b Pointer to the second int64_t value. + * @return An integer value less than, equal to, or greater than zero if the + * first value is considered to be respectively less than, equal to, or + * greater than the second value. + */ int cmp_int64_t(const void *a, const void *b) { return (*(int64_t *)a > *(int64_t *)b) - (*(int64_t *)a < *(int64_t *)b); } +/** + * @brief Compares two uint8_t values. + * + * This function is used as a comparison function for sorting uint8_t values. + * It compares the values pointed to by the pointers 'a' and 'b'. + * + * @param a Pointer to the first uint8_t value. + * @param b Pointer to the second uint8_t value. + * @return An integer less than, equal to, or greater than zero if the first value is found to be less than, equal to, or greater than the second value, respectively. + */ int cmp_uint8_t(const void *a, const void *b) { return (*(uint8_t *)a - *(uint8_t *)b); } +/** + * @brief Compares two uint16_t values. + * + * This function is used as a comparison function for sorting or searching algorithms. + * It compares the values pointed to by the pointers 'a' and 'b' and returns an integer + * indicating their relative order. + * + * @param a Pointer to the first uint16_t value. + * @param b Pointer to the second uint16_t value. + * @return An integer less than, equal to, or greater than zero if the first value is + * considered to be less than, equal to, or greater than the second value, + * respectively. + */ int cmp_uint16_t(const void *a, const void *b) { return (*(uint16_t *)a - *(uint16_t *)b); } +/** + * @brief Compares two uint32_t values. + * + * This function is used as a comparison function for sorting uint32_t values. + * It compares the values pointed to by the pointers 'a' and 'b'. + * + * @param a Pointer to the first uint32_t value. + * @param b Pointer to the second uint32_t value. + * @return An integer less than, equal to, or greater than zero if the first value is found to be less than, equal to, or greater than the second value, respectively. + */ int cmp_uint32_t(const void *a, const void *b) { return (*(uint32_t *)a - *(uint32_t *)b); } +/** + * @brief Compares two uint64_t values. + * + * This function is used as a comparison function for sorting uint64_t values. + * It compares the values pointed to by the pointers 'a' and 'b' and returns + * an integer value indicating their relative order. + * + * @param a Pointer to the first uint64_t value. + * @param b Pointer to the second uint64_t value. + * @return An integer value less than, equal to, or greater than zero if the + * first value is considered to be respectively less than, equal to, or + * greater than the second value. + */ int cmp_uint64_t(const void *a, const void *b) { return (*(uint64_t *)a > *(uint64_t *)b) - (*(uint64_t *)a < *(uint64_t *)b); } +/** + * @brief Compares two floating-point numbers. + * + * This function is used as a comparison function for sorting floating-point numbers. + * It compares the values pointed to by the arguments 'a' and 'b'. + * + * @param a Pointer to the first floating-point number. + * @param b Pointer to the second floating-point number. + * @return An integer less than, equal to, or greater than zero if 'a' is found to be + * less than, equal to, or greater than 'b', respectively. + */ int cmp_float(const void *a, const void *b) { return (*(float *)a > *(float *)b) - (*(float *)a < *(float *)b); } +/** + * @brief Compares two double values. + * + * This function is used as a comparison function for sorting double values. + * It compares the values pointed to by the pointers 'a' and 'b'. + * + * @param a Pointer to the first double value. + * @param b Pointer to the second double value. + * @return An integer value less than, equal to, or greater than zero if the first value is found to be less than, equal to, or greater than the second value, respectively. + */ int cmp_double(const void *a, const void *b) { return (*(double *)a > *(double *)b) - (*(double *)a < *(double *)b); -} +} \ No newline at end of file diff --git a/Hephaistos/src/assert.c b/Hephaistos/src/assert.c index c2e7dbc..7a7b23d 100644 --- a/Hephaistos/src/assert.c +++ b/Hephaistos/src/assert.c @@ -6,13 +6,13 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/11/20 13:56:46 by vvaucoul #+# #+# */ -/* Updated: 2024/07/26 21:58:53 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/27 17:31:39 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ #include -#include #include +#include /** * @brief Assertion function used to check a condition and provide debugging information if the condition is false. @@ -24,12 +24,12 @@ * @return The value of the condition. */ __assert_t __assert(__assert_t condition, const char *file, const char *function, int line) { - if (!condition) { - printk(_RED "Assertion failed: " _END "%s - [" _RED "%u" _END "]: " _RED "%s" _END "\n", file, line, function); - pause(); // Let's kernel continue to run - return (__ASSERT_FAILED); - } - return (__ASSERT_SUCCESS); + if (!condition) { + printk(_RED "Assertion failed: " _END "%s - [" _RED "%u" _END "]: " _RED "%s" _END "\n", file, line, function); + pause(); // Let's kernel continue to run + return (__ASSERT_FAILED); + } + return (__ASSERT_SUCCESS); } /** @@ -43,11 +43,11 @@ __assert_t __assert(__assert_t condition, const char *file, const char *function * @return The value of the condition. */ __assert_t __assert_msg(__assert_t condition, const char *file, const char *function, int line, const char *msg) { - if (!condition) { - printk(_RED "Assertion failed: " _END "%s - [" _RED "%u" _END "]: " _RED "%s" _END "\n", file, line, function); - printk(_RED "Message: " _END "%s\n", msg); - pause(); // Let's kernel continue to run - return (__ASSERT_FAILED); - } - return (__ASSERT_SUCCESS); + if (!condition) { + printk(_RED "Assertion failed: " _END "%s - [" _RED "%u" _END "]: " _RED "%s" _END "\n", file, line, function); + printk(_RED "Message: " _END "%s\n", msg); + pause(); // Let's kernel continue to run + return (__ASSERT_FAILED); + } + return (__ASSERT_SUCCESS); } diff --git a/Hephaistos/src/convert.c b/Hephaistos/src/convert.c index 4696bf1..de617e0 100644 --- a/Hephaistos/src/convert.c +++ b/Hephaistos/src/convert.c @@ -6,7 +6,7 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/11/20 22:59:13 by vvaucoul #+# #+# */ -/* Updated: 2024/07/26 22:40:06 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/28 00:44:17 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,22 +18,24 @@ #include static int char_to_val(char c) { - const char *base = __ASCII_BASE__; - for (int i = 0; base[i]; i++) { - if (base[i] == c || base[i] == (c - 32)) { // Support for lowercase characters - return i; - } - } - return (-1); + const char *base = __ASCII_BASE__; + + for (int i = 0; base[i]; i++) { + if (base[i] == c || base[i] == (c - 32)) { // Support for lowercase characters + return i; + } + } + return (-1); } static uint32_t __get_nbr_base_length(uint32_t nbr, uint32_t base) { - uint32_t length = 0; - while (nbr > 0) { - nbr /= base; - length++; - } - return (length); + uint32_t length = 0; + + while (nbr > 0) { + nbr /= base; + length++; + } + return (length); } /** @@ -48,26 +50,26 @@ static uint32_t __get_nbr_base_length(uint32_t nbr, uint32_t base) { * @return The pointer to the resulting string. */ uint32_t uitoa_base(uint32_t nbr, int base, char str[__ITOA_BUFFER_LENGTH__]) { - int i; - int size; - - size = __get_nbr_base_length(nbr, base); - if (str == NULL) { - return (1); - } else if (nbr == 0) { - str[0] = '0'; - str[1] = '\0'; - return (0); - } else { - i = size - 1; - while (nbr) { - str[i] = __ASCII_BASE__[nbr % base]; - nbr /= base; - --i; - } - str[size] = '\0'; - } - return (0); + int i; + int size; + + size = __get_nbr_base_length(nbr, base); + if (str == NULL) { + return (1); + } else if (nbr == 0) { + str[0] = '0'; + str[1] = '\0'; + return (0); + } else { + i = size - 1; + while (nbr) { + str[i] = __ASCII_BASE__[nbr % base]; + nbr /= base; + --i; + } + str[size] = '\0'; + } + return (0); } /** @@ -83,36 +85,36 @@ uint32_t uitoa_base(uint32_t nbr, int base, char str[__ITOA_BUFFER_LENGTH__]) { * @return The length of the resulting string. */ int itoa_base(int nbr, int base, char str[__ITOA_BUFFER_LENGTH__]) { - int i; - int isneg; - - if (!str) { - return (1); - } else if (nbr == 0) { - str[0] = '0'; - str[1] = '\0'; - return (0); - } - - if ((isneg = ((nbr >= -2147483648 && nbr < 0) ? 1 : 0)) == 1) { - nbr *= -1; - } - i = (isneg ? 1 : 0); - while (nbr != 0) { - str[i] = __ASCII_BASE__[nbr % base]; - nbr /= base; - ++i; - } - str[i] = '\0'; - if (isneg == 1) { - str = strrevp(str, 1, i - 1); - } else { - str = strrev(str); - } - if (isneg) { - str[0] = '-'; - } - return (0); + int i; + int isneg; + + if (!str) { + return (1); + } else if (nbr == 0) { + str[0] = '0'; + str[1] = '\0'; + return (0); + } + + if ((isneg = ((nbr >= -2147483648 && nbr < 0) ? 1 : 0)) == 1) { + nbr *= -1; + } + i = (isneg ? 1 : 0); + while (nbr != 0) { + str[i] = __ASCII_BASE__[nbr % base]; + nbr /= base; + ++i; + } + str[i] = '\0'; + if (isneg == 1) { + str = strrevp(str, 1, i - 1); + } else { + str = strrev(str); + } + if (isneg) { + str[0] = '-'; + } + return (0); } /** @@ -124,25 +126,25 @@ int itoa_base(int nbr, int base, char str[__ITOA_BUFFER_LENGTH__]) { * @return The converted integer value. */ int atoi(const char *str) { - int res = 0; - int sign = 1; - - if (*str == '-') { - sign = -1; - str++; - } else if (*str == '+') { - str++; - } - - while (*str) { - if (isdigit(*str) == 0) { - str++; - continue; - } - res = res * 10 + (*str - '0'); - str++; - } - return (res * sign); + int res = 0; + int sign = 1; + + if (*str == '-') { + sign = -1; + str++; + } else if (*str == '+') { + str++; + } + + while (*str) { + if (isdigit(*str) == 0) { + str++; + continue; + } + res = res * 10 + (*str - '0'); + str++; + } + return (res * sign); } /** @@ -156,26 +158,26 @@ int atoi(const char *str) { * @return The converted integer value. */ int atoi_base_s(const char *str, int base) { - int res = 0; - int sign = 1; - - if (*str == '-') { - sign = -1; - str++; - } - - while (*str) { - int digit = char_to_val(*str); - if (digit == -1 || digit >= base) { - // Invalid character or digit for the specified base - break; - } - - res = res * base + digit; - str++; - } - - return res * sign; + int res = 0; + int sign = 1; + + if (*str == '-') { + sign = -1; + str++; + } + + while (*str) { + int digit = char_to_val(*str); + if (digit == -1 || digit >= base) { + // Invalid character or digit for the specified base + break; + } + + res = res * base + digit; + str++; + } + + return res * sign; } /** @@ -190,43 +192,43 @@ int atoi_base_s(const char *str, int base) { * @return void */ int itoa(int nbr, char str[__ITOA_BUFFER_LENGTH__]) { - int i; - bool is_neg; - - if (!str) { - return (1); - } else if (nbr == 0) { - str[0] = '0'; - str[1] = '\0'; - return (0); - } else if (nbr == INT_MIN) { - memcpy(str, "-2147483648", 12); - str[12] = '\0'; - return (0); - } else if (nbr == INT_MAX) { - memcpy(str, "2147483647", 11); - str[11] = '\0'; - return (0); - } - - is_neg = ((nbr >= -2147483647 && nbr < 0) ? 1 : 0); - if (is_neg) { - nbr *= -1; - } - i = (is_neg ? 1 : 0); - while (nbr) { - str[i] = nbr % 10 + 48; - nbr /= 10; - ++i; - } - if (is_neg) { - str[0] = '-'; - str = strrevp(str, 1, i - 1); - } else { - str = strrev(str); - } - str[i] = '\0'; - return (0); + int i; + bool is_neg; + + if (!str) { + return (1); + } else if (nbr == 0) { + str[0] = '0'; + str[1] = '\0'; + return (0); + } else if (nbr == INT_MIN) { + memcpy_s(str, __ITOA_BUFFER_LENGTH__, "-2147483648", 12); + str[12] = '\0'; + return (0); + } else if (nbr == INT_MAX) { + memcpy_s(str, __ITOA_BUFFER_LENGTH__, "2147483647", 11); + str[11] = '\0'; + return (0); + } + + is_neg = ((nbr >= -2147483647 && nbr < 0) ? 1 : 0); + if (is_neg) { + nbr *= -1; + } + i = (is_neg ? 1 : 0); + while (nbr) { + str[i] = nbr % 10 + 48; + nbr /= 10; + ++i; + } + if (is_neg) { + str[0] = '-'; + str = strrevp(str, 1, i - 1); + } else { + str = strrev(str); + } + str[i] = '\0'; + return (0); } /** @@ -240,28 +242,28 @@ int itoa(int nbr, char str[__ITOA_BUFFER_LENGTH__]) { * @return None. */ int uitoa(uint32_t nbr, char str[__ITOA_BUFFER_LENGTH__]) { - int i = 0; - - if (str == NULL) { - return (1); - } else if (nbr == 0) { - str[0] = '0'; - str[1] = '\0'; - return (0); - } else if (nbr == UINT_MAX) { - memcpy(str, "4294967295", 11); - str[11] = '\0'; - return (0); - } else { - while (nbr) { - str[i] = nbr % 10 + 48; - nbr /= 10; - ++i; - } - str[i] = '\0'; - str = strrev(str); - } - return (0); + int i = 0; + + if (str == NULL) { + return (1); + } else if (nbr == 0) { + str[0] = '0'; + str[1] = '\0'; + return (0); + } else if (nbr == UINT_MAX) { + memcpy_s(str, __ITOA_BUFFER_LENGTH__, "4294967295", 11); + str[11] = '\0'; + return (0); + } else { + while (nbr) { + str[i] = nbr % 10 + 48; + nbr /= 10; + ++i; + } + str[i] = '\0'; + str = strrev(str); + } + return (0); } /** @@ -273,16 +275,16 @@ int uitoa(uint32_t nbr, char str[__ITOA_BUFFER_LENGTH__]) { * @return A pointer to the string representation of the integer. */ char *itoa_s(int nbr) { - uint32_t length = __get_nbr_base_length(nbr, 10); - char *str = kmalloc(sizeof(char) * (length + 2)); - - if (str == NULL) { - return (NULL); - } else { - memset(str, 0, length + 2); - } - itoa(nbr, str); - return (str); + uint32_t length = __get_nbr_base_length(nbr, 10); + char *str = kmalloc(sizeof(char) * (length + 2)); + + if (str == NULL) { + return (NULL); + } else { + memset_s(str, length + 2, 0, length + 2); + } + itoa(nbr, str); + return (str); } /** @@ -294,16 +296,16 @@ char *itoa_s(int nbr) { * @return A pointer to the string representation of the integer. */ char *uitoa_s(uint32_t nbr) { - uint32_t length = __get_nbr_base_length(nbr, 10); - char *str = kmalloc(sizeof(char) * (length + 2)); - - if (str == NULL) { - return (NULL); - } else { - memset(str, 0, length + 2); - } - uitoa(nbr, str); - return (str); + uint32_t length = __get_nbr_base_length(nbr, 10); + char *str = kmalloc(sizeof(char) * (length + 2)); + + if (str == NULL) { + return (NULL); + } else { + memset_s(str, length + 2, 0, length + 2); + } + uitoa(nbr, str); + return (str); } /** @@ -317,16 +319,16 @@ char *uitoa_s(uint32_t nbr) { * @return A pointer to the string representation of the integer. */ char *itoa_base_s(int nbr, int base) { - uint32_t length = __get_nbr_base_length(nbr, base); - char *str = kmalloc(sizeof(char) * (length + 2)); - - if (str == NULL) { - return (NULL); - } else { - memset(str, 0, length + 2); - } - itoa_base(nbr, base, str); - return (str); + uint32_t length = __get_nbr_base_length(nbr, base); + char *str = kmalloc(sizeof(char) * (length + 2)); + + if (str == NULL) { + return (NULL); + } else { + memset_s(str, length + 2, 0, length + 2); + } + itoa_base(nbr, base, str); + return (str); } /** @@ -340,16 +342,16 @@ char *itoa_base_s(int nbr, int base) { * @return A char pointer to the string representation of the unsigned integer. */ char *uitoa_base_s(uint32_t nbr, int base) { - uint32_t length = __get_nbr_base_length(nbr, base); - char *str = kmalloc(sizeof(char) * (length + 2)); - - if (str == NULL) { - return (NULL); - } else { - memset(str, 0, length + 2); - } - uitoa_base(nbr, base, str); - return (str); + uint32_t length = __get_nbr_base_length(nbr, base); + char *str = kmalloc(sizeof(char) * (length + 2)); + + if (str == NULL) { + return (NULL); + } else { + memset_s(str, length + 2, 0, length + 2); + } + uitoa_base(nbr, base, str); + return (str); } /** @@ -363,55 +365,52 @@ char *uitoa_base_s(uint32_t nbr, int base) { * @return A pointer to the string representation of the floating-point number. */ char *ftoa(double num, int precision) { - - int buffer_size = 50; // Taille suffisante pour la plupart des cas - char *str = kmalloc(buffer_size); - if (str == NULL) { - return NULL; - } - - int int_part = (int)num; - double dec_part = num - (double)int_part; - if (num < 0) { - dec_part = -dec_part; // Assurez-vous que la partie décimale est positive - } - - int index = 0; - if (num < 0) { - str[index++] = '-'; - int_part = -int_part; // Assurez-vous que la partie entière est positive pour la conversion - } - - // Convertir la partie entière en chaîne - int start = index; - do { - str[index++] = (int_part % 10) + '0'; - int_part /= 10; - } while (int_part != 0); - - // Inverser les chiffres de la partie entière - int end = index - 1; - while (start < end) { - char temp = str[start]; - str[start] = str[end]; - str[end] = temp; - start++; - end--; - } - - // Ajouter la partie décimale - if (precision > 0) { - str[index++] = '.'; - for (int i = 0; i < precision; i++) { - dec_part *= 10; - int digit = (int)dec_part; - str[index++] = digit + '0'; - dec_part -= digit; - } - } - - str[index] = '\0'; // Ajouter le caractère de fin de chaîne - return str; + int buffer_size = 50; + + char *str = kmalloc(buffer_size); + if (str == NULL) { + return NULL; + } + + int int_part = (int)num; + double dec_part = num - (double)int_part; + if (num < 0) { + dec_part = -dec_part; + } + + int index = 0; + if (num < 0) { + str[index++] = '-'; + int_part = -int_part; + } + + int start = index; + do { + str[index++] = (int_part % 10) + '0'; + int_part /= 10; + } while (int_part != 0); + + int end = index - 1; + while (start < end) { + char temp = str[start]; + str[start] = str[end]; + str[end] = temp; + start++; + end--; + } + + if (precision > 0) { + str[index++] = '.'; + for (int i = 0; i < precision; i++) { + dec_part *= 10; + int digit = (int)dec_part; + str[index++] = digit + '0'; + dec_part -= digit; + } + } + + str[index] = '\0'; + return str; } /** @@ -421,27 +420,27 @@ char *ftoa(double num, int precision) { * @return The ASCII representation of the hexadecimal number. */ char *htoa(uint32_t num) { - char *str = kmalloc(sizeof(char) * 9); - uint32_t size = __get_nbr_base_length(num, 16); - int i = 0; - - if (str == NULL) { - return (NULL); - } else if (num == 0) { - str[0] = '0'; - str[1] = '\0'; - return (str); - } else { - memset(str, 0, 9); - } - while (i < 8) { - int temp = i++; - str[temp] = __ASCII_BASE__[(num >> (temp * 4)) & 0xF]; - } - str[i] = '\0'; - str[size] = '\0'; - str = strrev(str); - return (str); + char *str = kmalloc(sizeof(char) * 9); + uint32_t size = __get_nbr_base_length(num, 16); + int i = 0; + + if (str == NULL) { + return (NULL); + } else if (num == 0) { + str[0] = '0'; + str[1] = '\0'; + return (str); + } else { + memset_s(str, __ITOA_BUFFER_LENGTH__, 0, 9); + } + while (i < 8) { + int temp = i++; + str[temp] = __ASCII_BASE__[(num >> (temp * 4)) & 0xF]; + } + str[i] = '\0'; + str[size] = '\0'; + str = strrev(str); + return (str); } /** @@ -451,27 +450,27 @@ char *htoa(uint32_t num) { * @return The ASCII representation of the binary number. */ char *btoa(uint32_t num) { - char *str = kmalloc(sizeof(char) * 33); - uint32_t size = __get_nbr_base_length(num, 2); - int i = 0; - - if (str == NULL) { - return (NULL); - } else if (num == 0) { - str[0] = '0'; - str[1] = '\0'; - return (str); - } else { - memset(str, 0, 33); - } - while (i < 32) { - int temp = i++; - str[temp] = __ASCII_BASE__[(num >> (temp * 1)) & 0x1]; - } - str[i] = '\0'; - str[size] = '\0'; - str = strrev(str); - return (str); + char *str = kmalloc(sizeof(char) * 33); + uint32_t size = __get_nbr_base_length(num, 2); + int i = 0; + + if (str == NULL) { + return (NULL); + } else if (num == 0) { + str[0] = '0'; + str[1] = '\0'; + return (str); + } else { + memset_s(str, 33, 0, 33); + } + while (i < 32) { + int temp = i++; + str[temp] = __ASCII_BASE__[(num >> (temp * 1)) & 0x1]; + } + str[i] = '\0'; + str[size] = '\0'; + str = strrev(str); + return (str); } /** @@ -481,28 +480,28 @@ char *btoa(uint32_t num) { * @return The ASCII representation of the octal number. */ char *otoa(uint32_t num) { - char *str = kmalloc(sizeof(char) * 12); - uint32_t size = __get_nbr_base_length(num, 8); - - int i = 0; - - if (str == NULL) { - return (NULL); - } else if (num == 0) { - str[0] = '0'; - str[1] = '\0'; - return (str); - } else { - memset(str, 0, 12); - } - while (i < 11) { - int temp = i++; - str[temp] = __ASCII_BASE__[(num >> (temp * 3)) & 0x7]; - } - str[i] = '\0'; - str[size] = '\0'; - str = strrev(str); - return (str); + char *str = kmalloc(sizeof(char) * 12); + uint32_t size = __get_nbr_base_length(num, 8); + + int i = 0; + + if (str == NULL) { + return (NULL); + } else if (num == 0) { + str[0] = '0'; + str[1] = '\0'; + return (str); + } else { + memset_s(str, 12, 0, 12); + } + while (i < 11) { + int temp = i++; + str[temp] = __ASCII_BASE__[(num >> (temp * 3)) & 0x7]; + } + str[i] = '\0'; + str[size] = '\0'; + str = strrev(str); + return (str); } /** @@ -512,23 +511,23 @@ char *otoa(uint32_t num) { * @return The floating-point number representation of the string. */ double atof(const char *str) { - double res = 0; - double sign = 1; - double factor = 1; - - if (*str == '-') { - sign = -1; - str++; - } - for (; *str; str++) { - if (*str == '.') { - factor = 10.0; - continue; - } - res = res * 10.0 + (*str - '0'); - if (factor > 1) { - factor *= 10.0; - } - } - return sign * res / (factor > 1 ? factor / 10.0 : 1); + double res = 0; + double sign = 1; + double factor = 1; + + if (*str == '-') { + sign = -1; + str++; + } + for (; *str; str++) { + if (*str == '.') { + factor = 10.0; + continue; + } + res = res * 10.0 + (*str - '0'); + if (factor > 1) { + factor *= 10.0; + } + } + return sign * res / (factor > 1 ? factor / 10.0 : 1); } \ No newline at end of file diff --git a/Hephaistos/src/ctype.c b/Hephaistos/src/ctype.c index 63b0751..7c7da1b 100644 --- a/Hephaistos/src/ctype.c +++ b/Hephaistos/src/ctype.c @@ -6,7 +6,7 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/11/20 13:24:25 by vvaucoul #+# #+# */ -/* Updated: 2024/07/26 22:11:53 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/27 17:29:03 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ * @return true if the character is alphanumeric, false otherwise. */ bool isalnum(int c) { - return (isalpha(c) || isdigit(c)); + return (isalpha(c) || isdigit(c)); } /** @@ -29,7 +29,7 @@ bool isalnum(int c) { * @return true if the character is alphabetic, false otherwise. */ bool isalpha(int c) { - return (islower(c) || isupper(c)); + return (islower(c) || isupper(c)); } /** @@ -39,7 +39,7 @@ bool isalpha(int c) { * @return true if the character is a blank space or tab, false otherwise. */ bool isblank(int c) { - return (c == ' ' || c == '\t'); + return (c == ' ' || c == '\t'); } /** @@ -49,7 +49,7 @@ bool isblank(int c) { * @return true if the character is a control character, false otherwise. */ bool iscntrl(int c) { - return (c >= 0 && c <= 31) || c == 127; + return (c >= 0 && c <= 31) || c == 127; } /** @@ -59,7 +59,7 @@ bool iscntrl(int c) { * @return true if the character is a digit, false otherwise. */ bool isdigit(int c) { - return (c >= '0' && c <= '9'); + return (c >= '0' && c <= '9'); } /** @@ -69,7 +69,7 @@ bool isdigit(int c) { * @return true if the character is a printable character except space, false otherwise. */ bool isgraph(int c) { - return (c >= 33 && c <= 126); + return (c >= 33 && c <= 126); } /** @@ -79,7 +79,7 @@ bool isgraph(int c) { * @return true if the character is a lowercase letter, false otherwise. */ bool islower(int c) { - return (c >= 'a' && c <= 'z'); + return (c >= 'a' && c <= 'z'); } /** @@ -89,7 +89,7 @@ bool islower(int c) { * @return true if the character is a printable character, false otherwise. */ bool isprint(int c) { - return (c >= 32 && c <= 126); + return (c >= 32 && c <= 126); } /** @@ -99,7 +99,7 @@ bool isprint(int c) { * @return true if the character is a punctuation character, false otherwise. */ bool ispunct(int c) { - return (isgraph(c) && !isalnum(c)); + return (isgraph(c) && !isalnum(c)); } /** @@ -109,7 +109,7 @@ bool ispunct(int c) { * @return true if the character is an uppercase letter, false otherwise. */ bool isupper(int c) { - return (c >= 'A' && c <= 'Z'); + return (c >= 'A' && c <= 'Z'); } /** @@ -119,7 +119,7 @@ bool isupper(int c) { * @return true if the character is a hexadecimal digit, false otherwise. */ bool isxdigit(int c) { - return (isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')); + return (isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')); } /** @@ -129,7 +129,7 @@ bool isxdigit(int c) { * @return true if the character is a valid ASCII character, false otherwise. */ bool isascii(int c) { - return (c >= 0 && c <= 127); + return (c >= 0 && c <= 127); } /** @@ -139,7 +139,7 @@ bool isascii(int c) { * @return Non-zero value if the character is a white-space character, 0 otherwise. */ bool isspace(int c) { - return c == ' ' || c == '\t' || c == '\n' || c == '\r'; + return c == ' ' || c == '\t' || c == '\n' || c == '\r'; } /** @@ -149,7 +149,7 @@ bool isspace(int c) { * @return The ASCII equivalent of the character. */ int toascii(int c) { - return (c & 0x7F); + return (c & 0x7F); } /** @@ -159,7 +159,7 @@ int toascii(int c) { * @return The lowercase equivalent of the character. */ int tolower(int c) { - return (isupper(c) ? c + 32 : c); + return (isupper(c) ? c + 32 : c); } /** @@ -169,5 +169,5 @@ int tolower(int c) { * @return The uppercase equivalent of the character. */ int toupper(int c) { - return (islower(c) ? c - 32 : c); + return (islower(c) ? c - 32 : c); } \ No newline at end of file diff --git a/Hephaistos/src/errno.c b/Hephaistos/src/errno.c index 7b4b3c7..7742303 100644 --- a/Hephaistos/src/errno.c +++ b/Hephaistos/src/errno.c @@ -6,7 +6,7 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/22 13:00:29 by vvaucoul #+# #+# */ -/* Updated: 2024/07/26 19:51:49 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/27 17:27:20 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,139 +18,139 @@ errno_t errno = 0; // Array of error messages corresponding to error codes static const char *error_messages[] = { - [0] = "No error", - [EPERM] = "Operation not permitted", - [ENOENT] = "No such file or directory", - [ESRCH] = "No such process", - [EINTR] = "Interrupted system call", - [EIO] = "I/O error", - [ENXIO] = "No such device or address", - [E2BIG] = "Argument list too long", - [ENOEXEC] = "Exec format error", - [EBADF] = "Bad file number", - [ECHILD] = "No child processes", - [EAGAIN] = "Try again", - [ENOMEM] = "Out of memory", - [EACCES] = "Permission denied", - [EFAULT] = "Bad address", - [ENOTBLK] = "Block device required", - [EBUSY] = "Device or resource busy", - [EEXIST] = "File exists", - [EXDEV] = "Cross-device link", - [ENODEV] = "No such device", - [ENOTDIR] = "Not a directory", - [EISDIR] = "Is a directory", - [EINVAL] = "Invalid argument", - [ENFILE] = "File table overflow", - [EMFILE] = "Too many open files", - [ENOTTY] = "Not a typewriter", - [ETXTBSY] = "Text file busy", - [EFBIG] = "File too large", - [ENOSPC] = "No space left on device", - [ESPIPE] = "Illegal seek", - [EROFS] = "Read-only file system", - [EMLINK] = "Too many links", - [EPIPE] = "Broken pipe", - [EDOM] = "Math argument out of domain of func", - [ERANGE] = "Math result not representable", - [EDEADLK] = "Resource deadlock would occur", - [ENAMETOOLONG] = "File name too long", - [ENOLCK] = "No record locks available", - [ENOSYS] = "Invalid system call number", - [ENOTEMPTY] = "Directory not empty", - [ELOOP] = "Too many symbolic links encountered", - // [EWOULDBLOCK] = "Operation would block", // initializer overrides prior initialization of this subobject [EAGAIN] - [ENOMSG] = "No message of desired type", - [EIDRM] = "Identifier removed", - [ECHRNG] = "Channel number out of range", - [EL2NSYNC] = "Level 2 not synchronized", - [EL3HLT] = "Level 3 halted", - [EL3RST] = "Level 3 reset", - [ELNRNG] = "Link number out of range", - [EUNATCH] = "Protocol driver not attached", - [ENOCSI] = "No CSI structure available", - [EL2HLT] = "Level 2 halted", - [EBADE] = "Invalid exchange", - [EBADR] = "Invalid request descriptor", - [EXFULL] = "Exchange full", - [ENOANO] = "No anode", - [EBADRQC] = "Invalid request code", - [EBADSLT] = "Invalid slot", - [EBFONT] = "Bad font file format", - [ENOSTR] = "Device not a stream", - [ENODATA] = "No data available", - [ETIME] = "Timer expired", - [ENOSR] = "Out of streams resources", - [ENONET] = "Machine is not on the network", - [ENOPKG] = "Package not installed", - [EREMOTE] = "Object is remote", - [ENOLINK] = "Link has been severed", - [EADV] = "Advertise error", - [ESRMNT] = "Srmount error", - [ECOMM] = "Communication error on send", - [EPROTO] = "Protocol error", - [EMULTIHOP] = "Multihop attempted", - [EDOTDOT] = "RFS specific error", - [EBADMSG] = "Not a data message", - [EOVERFLOW] = "Value too large for defined data type", - [ENOTUNIQ] = "Name not unique on network", - [EBADFD] = "File descriptor in bad state", - [EREMCHG] = "Remote address changed", - [ELIBACC] = "Can not access a needed shared library", - [ELIBBAD] = "Accessing a corrupted shared library", - [ELIBSCN] = ".lib section in a.out corrupted", - [ELIBMAX] = "Attempting to link in too many shared libraries", - [ELIBEXEC] = "Cannot exec a shared library directly", - [EILSEQ] = "Illegal byte sequence", - [ERESTART] = "Interrupted system call should be restarted", - [ESTRPIPE] = "Streams pipe error", - [EUSERS] = "Too many users", - [ENOTSOCK] = "Socket operation on non-socket", - [EDESTADDRREQ] = "Destination address required", - [EMSGSIZE] = "Message too long", - [EPROTOTYPE] = "Protocol wrong type for socket", - [ENOPROTOOPT] = "Protocol not available", - [EPROTONOSUPPORT] = "Protocol not supported", - [ESOCKTNOSUPPORT] = "Socket type not supported", - [EOPNOTSUPP] = "Operation not supported on transport endpoint", - [EPFNOSUPPORT] = "Protocol family not supported", - [EAFNOSUPPORT] = "Address family not supported by protocol", - [EADDRINUSE] = "Address already in use", - [EADDRNOTAVAIL] = "Cannot assign requested address", - [ENETDOWN] = "Network is down", - [ENETUNREACH] = "Network is unreachable", - [ENETRESET] = "Network dropped connection because of reset", - [ECONNABORTED] = "Software caused connection abort", - [ECONNRESET] = "Connection reset by peer", - [ENOBUFS] = "No buffer space available", - [EISCONN] = "Transport endpoint is already connected", - [ENOTCONN] = "Transport endpoint is not connected", - [ESHUTDOWN] = "Cannot send after transport endpoint shutdown", - [ETOOMANYREFS] = "Too many references: cannot splice", - [ETIMEDOUT] = "Connection timed out", - [ECONNREFUSED] = "Connection refused", - [EHOSTDOWN] = "Host is down", - [EHOSTUNREACH] = "No route to host", - [EALREADY] = "Operation already in progress", - [EINPROGRESS] = "Operation now in progress", - [ESTALE] = "Stale NFS file handle", - [EUCLEAN] = "Structure needs cleaning", - [ENOTNAM] = "Not a XENIX named type file", - [ENAVAIL] = "No XENIX semaphores available", - [EISNAM] = "Is a named type file", - [EREMOTEIO] = "Remote I/O error", - [EDQUOT] = "Quota exceeded", - [ENOMEDIUM] = "No medium found", - [EMEDIUMTYPE] = "Wrong medium type", - [ECANCELED] = "Operation Canceled", - [ENOKEY] = "Required key not available", - [EKEYEXPIRED] = "Key has expired", - [EKEYREVOKED] = "Key has been revoked", - [EKEYREJECTED] = "Key was rejected by service", - [EOWNERDEAD] = "Owner died", - [ENOTRECOVERABLE] = "State not recoverable", - [ERFKILL] = "Operation not possible due to RF-kill", - [EHWPOISON] = "Memory page has hardware error"}; + [0] = "No error", + [EPERM] = "Operation not permitted", + [ENOENT] = "No such file or directory", + [ESRCH] = "No such process", + [EINTR] = "Interrupted system call", + [EIO] = "I/O error", + [ENXIO] = "No such device or address", + [E2BIG] = "Argument list too long", + [ENOEXEC] = "Exec format error", + [EBADF] = "Bad file number", + [ECHILD] = "No child processes", + [EAGAIN] = "Try again", + [ENOMEM] = "Out of memory", + [EACCES] = "Permission denied", + [EFAULT] = "Bad address", + [ENOTBLK] = "Block device required", + [EBUSY] = "Device or resource busy", + [EEXIST] = "File exists", + [EXDEV] = "Cross-device link", + [ENODEV] = "No such device", + [ENOTDIR] = "Not a directory", + [EISDIR] = "Is a directory", + [EINVAL] = "Invalid argument", + [ENFILE] = "File table overflow", + [EMFILE] = "Too many open files", + [ENOTTY] = "Not a typewriter", + [ETXTBSY] = "Text file busy", + [EFBIG] = "File too large", + [ENOSPC] = "No space left on device", + [ESPIPE] = "Illegal seek", + [EROFS] = "Read-only file system", + [EMLINK] = "Too many links", + [EPIPE] = "Broken pipe", + [EDOM] = "Math argument out of domain of func", + [ERANGE] = "Math result not representable", + [EDEADLK] = "Resource deadlock would occur", + [ENAMETOOLONG] = "File name too long", + [ENOLCK] = "No record locks available", + [ENOSYS] = "Invalid system call number", + [ENOTEMPTY] = "Directory not empty", + [ELOOP] = "Too many symbolic links encountered", + // [EWOULDBLOCK] = "Operation would block", // initializer overrides prior initialization of this subobject [EAGAIN] + [ENOMSG] = "No message of desired type", + [EIDRM] = "Identifier removed", + [ECHRNG] = "Channel number out of range", + [EL2NSYNC] = "Level 2 not synchronized", + [EL3HLT] = "Level 3 halted", + [EL3RST] = "Level 3 reset", + [ELNRNG] = "Link number out of range", + [EUNATCH] = "Protocol driver not attached", + [ENOCSI] = "No CSI structure available", + [EL2HLT] = "Level 2 halted", + [EBADE] = "Invalid exchange", + [EBADR] = "Invalid request descriptor", + [EXFULL] = "Exchange full", + [ENOANO] = "No anode", + [EBADRQC] = "Invalid request code", + [EBADSLT] = "Invalid slot", + [EBFONT] = "Bad font file format", + [ENOSTR] = "Device not a stream", + [ENODATA] = "No data available", + [ETIME] = "Timer expired", + [ENOSR] = "Out of streams resources", + [ENONET] = "Machine is not on the network", + [ENOPKG] = "Package not installed", + [EREMOTE] = "Object is remote", + [ENOLINK] = "Link has been severed", + [EADV] = "Advertise error", + [ESRMNT] = "Srmount error", + [ECOMM] = "Communication error on send", + [EPROTO] = "Protocol error", + [EMULTIHOP] = "Multihop attempted", + [EDOTDOT] = "RFS specific error", + [EBADMSG] = "Not a data message", + [EOVERFLOW] = "Value too large for defined data type", + [ENOTUNIQ] = "Name not unique on network", + [EBADFD] = "File descriptor in bad state", + [EREMCHG] = "Remote address changed", + [ELIBACC] = "Can not access a needed shared library", + [ELIBBAD] = "Accessing a corrupted shared library", + [ELIBSCN] = ".lib section in a.out corrupted", + [ELIBMAX] = "Attempting to link in too many shared libraries", + [ELIBEXEC] = "Cannot exec a shared library directly", + [EILSEQ] = "Illegal byte sequence", + [ERESTART] = "Interrupted system call should be restarted", + [ESTRPIPE] = "Streams pipe error", + [EUSERS] = "Too many users", + [ENOTSOCK] = "Socket operation on non-socket", + [EDESTADDRREQ] = "Destination address required", + [EMSGSIZE] = "Message too long", + [EPROTOTYPE] = "Protocol wrong type for socket", + [ENOPROTOOPT] = "Protocol not available", + [EPROTONOSUPPORT] = "Protocol not supported", + [ESOCKTNOSUPPORT] = "Socket type not supported", + [EOPNOTSUPP] = "Operation not supported on transport endpoint", + [EPFNOSUPPORT] = "Protocol family not supported", + [EAFNOSUPPORT] = "Address family not supported by protocol", + [EADDRINUSE] = "Address already in use", + [EADDRNOTAVAIL] = "Cannot assign requested address", + [ENETDOWN] = "Network is down", + [ENETUNREACH] = "Network is unreachable", + [ENETRESET] = "Network dropped connection because of reset", + [ECONNABORTED] = "Software caused connection abort", + [ECONNRESET] = "Connection reset by peer", + [ENOBUFS] = "No buffer space available", + [EISCONN] = "Transport endpoint is already connected", + [ENOTCONN] = "Transport endpoint is not connected", + [ESHUTDOWN] = "Cannot send after transport endpoint shutdown", + [ETOOMANYREFS] = "Too many references: cannot splice", + [ETIMEDOUT] = "Connection timed out", + [ECONNREFUSED] = "Connection refused", + [EHOSTDOWN] = "Host is down", + [EHOSTUNREACH] = "No route to host", + [EALREADY] = "Operation already in progress", + [EINPROGRESS] = "Operation now in progress", + [ESTALE] = "Stale NFS file handle", + [EUCLEAN] = "Structure needs cleaning", + [ENOTNAM] = "Not a XENIX named type file", + [ENAVAIL] = "No XENIX semaphores available", + [EISNAM] = "Is a named type file", + [EREMOTEIO] = "Remote I/O error", + [EDQUOT] = "Quota exceeded", + [ENOMEDIUM] = "No medium found", + [EMEDIUMTYPE] = "Wrong medium type", + [ECANCELED] = "Operation Canceled", + [ENOKEY] = "Required key not available", + [EKEYEXPIRED] = "Key has expired", + [EKEYREVOKED] = "Key has been revoked", + [EKEYREJECTED] = "Key was rejected by service", + [EOWNERDEAD] = "Owner died", + [ENOTRECOVERABLE] = "State not recoverable", + [ERFKILL] = "Operation not possible due to RF-kill", + [EHWPOISON] = "Memory page has hardware error"}; /** * @brief Retrieves the error message string associated with the given error number. @@ -162,10 +162,10 @@ static const char *error_messages[] = { * @return A pointer to a string containing the error message. */ const char *strerror(errno_t errnum) { - if (errnum >= 0 && errnum < sizeof(error_messages) / sizeof(error_messages[0]) && error_messages[errnum] != NULL) { - return error_messages[errnum]; - } - return "Unknown error"; + if (errnum >= 0 && errnum < sizeof(error_messages) / sizeof(error_messages[0]) && error_messages[errnum] != NULL) { + return error_messages[errnum]; + } + return "Unknown error"; } /** @@ -178,9 +178,9 @@ const char *strerror(errno_t errnum) { * @param s The prefix string to be printed before the error message. Can be `NULL`. */ void perror(const char *s) { - if (s && *s) { - printk("%s: %s\n", s, strerror(errno)); - } else { - printk("%s\n", strerror(errno)); - } + if (s && *s) { + printk("%s: %s\n", s, strerror(errno)); + } else { + printk("%s\n", strerror(errno)); + } } diff --git a/Hephaistos/src/hexdump.c b/Hephaistos/src/hexdump.c index 5fc06ee..14240c1 100644 --- a/Hephaistos/src/hexdump.c +++ b/Hephaistos/src/hexdump.c @@ -6,47 +6,40 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/11/20 14:15:55 by vvaucoul #+# #+# */ -/* Updated: 2024/07/26 20:12:43 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/27 17:27:48 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ #include #include -void khexdump(uint32_t ebp, int limit) -{ +void khexdump(uint32_t ebp, int limit) { int i = 0; if (limit <= 0) return; - do - { + do { printk(ebp == 0x00000800 ? _GREEN : _CYAN); - printk("0x%u: " _END, ebp); + printk("0x%08X: ", ebp); uint32_t next = ebp + 16; uint32_t tmp = ebp; - while (tmp < next && i < limit) - { - if (*(char *)tmp >= 32) - printk(_GREEN "%u " _END, *(char *)tmp); + for (; tmp < next && i < limit; ++tmp, ++i) { + if (*((char *)&tmp) >= 32) + printk(_GREEN "%02X " _END, *(char *)tmp); else printk(_END "00 " _END); - ++tmp; } next = ebp + 16; tmp = ebp; - while (tmp < next && i < limit) - { + for (; tmp < next && i < limit; ++tmp, ++i) { if (*(char *)tmp > 32) printk("%c", *(char *)tmp); else printk("."); - ++tmp; } printk("\n"); - i += 16; ebp += 16; } while (i < limit); -} \ No newline at end of file +} diff --git a/Hephaistos/src/math.c b/Hephaistos/src/math.c index b9d4688..abd31e9 100644 --- a/Hephaistos/src/math.c +++ b/Hephaistos/src/math.c @@ -6,7 +6,7 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/20 12:30:23 by vvaucoul #+# #+# */ -/* Updated: 2024/07/26 21:59:24 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/27 17:26:47 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,18 +18,18 @@ * @return The natural logarithm of x */ float ln(float x) { - float result = 0.0; - float term = (x - 1) / (x + 1); - float term_squared = term * term; - float current_term = term; - int n; + float result = 0.0; + float term = (x - 1) / (x + 1); + float term_squared = term * term; + float current_term = term; + int n; - for (n = 1; n <= 100; n += 2) { - result += current_term / n; - current_term *= term_squared; - } + for (n = 1; n <= 100; n += 2) { + result += current_term / n; + current_term *= term_squared; + } - return 2 * result; + return 2 * result; } /** @@ -38,13 +38,14 @@ float ln(float x) { * @return The sine of the angle */ float sin(float x) { - float result = x; - float term = x; - for (int n = 1; n <= 10; ++n) { - term *= -x * x / (2 * n * (2 * n + 1)); - result += term; - } - return result; + float result = x; + float term = x; + + for (int n = 1; n <= 10; ++n) { + term *= -x * x / (2 * n * (2 * n + 1)); + result += term; + } + return result; } /** @@ -53,15 +54,15 @@ float sin(float x) { * @return The cosine of the angle */ float cos(float x) { - float result = 1.0; - float term = 1.0; - for (int n = 1; n <= 10; ++n) { - term *= -x * x / (2 * n * (2 * n - 1)); - result += term; - } - return result; -} + float result = 1.0; + float term = 1.0; + for (int n = 1; n <= 10; ++n) { + term *= -x * x / (2 * n * (2 * n - 1)); + result += term; + } + return result; +} /** * @brief Calculate the exponential function of a number @@ -69,13 +70,14 @@ float cos(float x) { * @return The exponential of x */ float exp(float x) { - float result = 1.0; - float term = 1.0; - for (int n = 1; n <= 10; ++n) { - term *= x / n; - result += term; - } - return result; + float result = 1.0; + float term = 1.0; + + for (int n = 1; n <= 10; ++n) { + term *= x / n; + result += term; + } + return result; } /** @@ -84,7 +86,7 @@ float exp(float x) { * @return The tangent of the angle */ float tan(float x) { - return sin(x) / cos(x); + return sin(x) / cos(x); } /** @@ -95,9 +97,13 @@ float tan(float x) { * @return The clamped value */ int clamp(int value, int min, int max) { - if (value < min) return min; - if (value > max) return max; - return value; + if (value < min) { + return min; + } + if (value > max) { + return max; + } + return value; } /** @@ -107,7 +113,7 @@ int clamp(int value, int min, int max) { * @return The length of the hypotenuse */ float hypot(float x, float y) { - return sqrt(x * x + y * y); + return sqrt(x * x + y * y); } /** @@ -116,5 +122,5 @@ float hypot(float x, float y) { * @return 1 if x is positive, -1 if x is negative, 0 if x is zero */ int signum(float x) { - return (x > 0) - (x < 0); + return (x > 0) - (x < 0); } \ No newline at end of file diff --git a/Hephaistos/src/memory.c b/Hephaistos/src/memory.c index d63e7e8..eb51d96 100644 --- a/Hephaistos/src/memory.c +++ b/Hephaistos/src/memory.c @@ -6,12 +6,13 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/11/20 13:25:42 by vvaucoul #+# #+# */ -/* Updated: 2024/07/26 22:18:07 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/28 01:25:23 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ #include #include +#include /** * @brief Fills the first length bytes of the memory area pointed to by ptr with the constant byte value. diff --git a/Hephaistos/src/stdio/printk/print.c b/Hephaistos/src/stdio/printk/print.c index 24bab78..d9c3ad7 100644 --- a/Hephaistos/src/stdio/printk/print.c +++ b/Hephaistos/src/stdio/printk/print.c @@ -6,7 +6,7 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/23 12:19:39 by vvaucoul #+# #+# */ -/* Updated: 2024/07/26 22:16:45 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/28 01:37:55 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ @@ -337,7 +337,7 @@ void putunbr_hex_len(uint32_t n, int len) { void putptr_len(void *ptr, int len) { // 0x added without counting it in len putstr("0x"); - putunbr_hex_len((uint32_t)ptr, len); + putunbr_hex_len((uintptr_t)ptr, len); } void putf_len(const float value, int len) { diff --git a/Hephaistos/src/stdio/printk/srcs/printk.c b/Hephaistos/src/stdio/printk/srcs/printk.c index 6191c18..96a0b80 100644 --- a/Hephaistos/src/stdio/printk/srcs/printk.c +++ b/Hephaistos/src/stdio/printk/srcs/printk.c @@ -6,227 +6,242 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/22 15:06:11 by vvaucoul #+# #+# */ -/* Updated: 2024/07/26 22:15:23 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/28 01:41:00 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ #include +#include #include #include #include -#include -printk_t _g_printk; +printk_t _g_printk = { + .format = {0}, + .space = 0, + .space_is_star = false, + .is_neg_space = false, + .use_zero = false, + .precision = -1, +}; static int check_special_strings(const char *str) { - if (strncmp(str, _CURSOR_MOVE_UP, strlen(_CURSOR_MOVE_UP)) == 0) { - terminal_move_cursor_up(); - return (strlen(_CURSOR_MOVE_UP)); - } - return (0); + if (strncmp(str, _CURSOR_MOVE_UP, strlen(_CURSOR_MOVE_UP)) == 0) { + terminal_move_cursor_up(); + return (strlen(_CURSOR_MOVE_UP)); + } + return (0); } static int check_colors(const char *str) { - /* Check Foreground colors */ - if (strncmp(str, _END, strlen(_END)) == 0) { - terminal_set_color(VGA_COLOR_LIGHT_GREY); - terminal_set_background_color(VGA_COLOR_BLACK); - return (strlen(_END)); - } else if (strncmp(str, _RED, strlen(_RED)) == 0) { - terminal_set_color(VGA_COLOR_RED); - return (strlen(_RED)); - } else if (strncmp(str, _GREEN, strlen(_GREEN)) == 0) { - terminal_set_color(VGA_COLOR_GREEN); - return (strlen(_GREEN)); - } else if (strncmp(str, _BLUE, strlen(_BLUE)) == 0) { - terminal_set_color(VGA_COLOR_BLUE); - return (strlen(_BLUE)); - } else if (strncmp(str, _YELLOW, strlen(_YELLOW)) == 0) { - terminal_set_color(VGA_COLOR_BROWN); - return (strlen(_YELLOW)); - } else if (strncmp(str, _MAGENTA, strlen(_MAGENTA)) == 0) { - terminal_set_color(VGA_COLOR_MAGENTA); - return (strlen(_MAGENTA)); - } else if (strncmp(str, _CYAN, strlen(_CYAN)) == 0) { - terminal_set_color(VGA_COLOR_CYAN); - return (strlen(_CYAN)); - } else if (strncmp(str, _LGREY, strlen(_LGREY)) == 0) { - terminal_set_color(VGA_COLOR_LIGHT_GREY); - return (strlen(_LGREY)); - } else if (strncmp(str, _LBLUE, strlen(_LBLUE)) == 0) { - terminal_set_color(VGA_COLOR_LIGHT_BLUE); - return (strlen(_LBLUE)); - } else if (strncmp(str, _LGREEN, strlen(_LGREEN)) == 0) { - terminal_set_color(VGA_COLOR_LIGHT_GREEN); - return (strlen(_LGREEN)); - } else if (strncmp(str, _LCYAN, strlen(_LCYAN)) == 0) { - terminal_set_color(VGA_COLOR_LIGHT_CYAN); - return (strlen(_LCYAN)); - } else if (strncmp(str, _LRED, strlen(_LRED)) == 0) { - terminal_set_color(VGA_COLOR_LIGHT_RED); - return (strlen(_LRED)); - } else if (strncmp(str, _LMAGENTA, strlen(_LMAGENTA)) == 0) { - terminal_set_color(VGA_COLOR_LIGHT_MAGENTA); - return (strlen(_LMAGENTA)); - } else if (strncmp(str, _LYELLOW, strlen(_LYELLOW)) == 0) { - terminal_set_color(VGA_COLOR_LIGHT_BROWN); - return (strlen(_LYELLOW)); - } else if (strncmp(str, _WHITE, strlen(_WHITE)) == 0) { - terminal_set_color(VGA_COLOR_WHITE); - return (strlen(_WHITE)); - } - - /* Check Background colors */ - if (strncmp(str, _BG_BLACK, strlen(_BG_BLACK)) == 0) { - terminal_set_background_color(VGA_COLOR_BLACK); - return (strlen(_BG_BLACK)); - } else if (strncmp(str, _BG_BLUE, strlen(_BG_BLUE)) == 0) { - terminal_set_background_color(VGA_COLOR_BLUE); - return (strlen(_BG_BLUE)); - } else if (strncmp(str, _BG_GREEN, strlen(_BG_GREEN)) == 0) { - terminal_set_background_color(VGA_COLOR_GREEN); - return (strlen(_BG_GREEN)); - } else if (strncmp(str, _BG_CYAN, strlen(_BG_CYAN)) == 0) { - terminal_set_background_color(VGA_COLOR_CYAN); - return (strlen(_BG_CYAN)); - } else if (strncmp(str, _BG_RED, strlen(_BG_RED)) == 0) { - terminal_set_background_color(VGA_COLOR_RED); - return (strlen(_BG_RED)); - } else if (strncmp(str, _BG_MAGENTA, strlen(_BG_MAGENTA)) == 0) { - terminal_set_background_color(VGA_COLOR_MAGENTA); - return (strlen(_BG_MAGENTA)); - } else if (strncmp(str, _BG_YELLOW, strlen(_BG_YELLOW)) == 0) { - terminal_set_background_color(VGA_COLOR_BROWN); - return (strlen(_BG_YELLOW)); - } else if (strncmp(str, _BG_LGREY, strlen(_BG_LGREY)) == 0) { - terminal_set_background_color(VGA_COLOR_LIGHT_GREY); - return (strlen(_BG_LGREY)); - } else if (strncmp(str, _BG_LBLUE, strlen(_BG_LBLUE)) == 0) { - terminal_set_background_color(VGA_COLOR_LIGHT_BLUE); - return (strlen(_BG_LBLUE)); - } else if (strncmp(str, _BG_LGREEN, strlen(_BG_LGREEN)) == 0) { - terminal_set_background_color(VGA_COLOR_LIGHT_GREEN); - return (strlen(_BG_LGREEN)); - } else if (strncmp(str, _BG_LCYAN, strlen(_BG_LCYAN)) == 0) { - terminal_set_background_color(VGA_COLOR_LIGHT_CYAN); - return (strlen(_BG_LCYAN)); - } else if (strncmp(str, _BG_LRED, strlen(_BG_LRED)) == 0) { - terminal_set_background_color(VGA_COLOR_LIGHT_RED); - return (strlen(_BG_LRED)); - } else if (strncmp(str, _BG_LMAGENTA, strlen(_BG_LMAGENTA)) == 0) { - terminal_set_background_color(VGA_COLOR_LIGHT_MAGENTA); - return (strlen(_BG_LMAGENTA)); - } else if (strncmp(str, _BG_LYELLOW, strlen(_BG_LYELLOW)) == 0) { - terminal_set_background_color(VGA_COLOR_LIGHT_BROWN); - return (strlen(_BG_LYELLOW)); - } else if (strncmp(str, _BG_WHITE, strlen(_BG_WHITE)) == 0) { - terminal_set_background_color(VGA_COLOR_WHITE); - return (strlen(_BG_WHITE)); - } - - return (0); + /* Check Foreground colors */ + if (strncmp(str, _END, strlen(_END)) == 0) { + terminal_set_color(VGA_COLOR_LIGHT_GREY); + terminal_set_background_color(VGA_COLOR_BLACK); + return (strlen(_END)); + } else if (strncmp(str, _RED, strlen(_RED)) == 0) { + terminal_set_color(VGA_COLOR_RED); + return (strlen(_RED)); + } else if (strncmp(str, _GREEN, strlen(_GREEN)) == 0) { + terminal_set_color(VGA_COLOR_GREEN); + return (strlen(_GREEN)); + } else if (strncmp(str, _BLUE, strlen(_BLUE)) == 0) { + terminal_set_color(VGA_COLOR_BLUE); + return (strlen(_BLUE)); + } else if (strncmp(str, _YELLOW, strlen(_YELLOW)) == 0) { + terminal_set_color(VGA_COLOR_BROWN); + return (strlen(_YELLOW)); + } else if (strncmp(str, _MAGENTA, strlen(_MAGENTA)) == 0) { + terminal_set_color(VGA_COLOR_MAGENTA); + return (strlen(_MAGENTA)); + } else if (strncmp(str, _CYAN, strlen(_CYAN)) == 0) { + terminal_set_color(VGA_COLOR_CYAN); + return (strlen(_CYAN)); + } else if (strncmp(str, _LGREY, strlen(_LGREY)) == 0) { + terminal_set_color(VGA_COLOR_LIGHT_GREY); + return (strlen(_LGREY)); + } else if (strncmp(str, _LBLUE, strlen(_LBLUE)) == 0) { + terminal_set_color(VGA_COLOR_LIGHT_BLUE); + return (strlen(_LBLUE)); + } else if (strncmp(str, _LGREEN, strlen(_LGREEN)) == 0) { + terminal_set_color(VGA_COLOR_LIGHT_GREEN); + return (strlen(_LGREEN)); + } else if (strncmp(str, _LCYAN, strlen(_LCYAN)) == 0) { + terminal_set_color(VGA_COLOR_LIGHT_CYAN); + return (strlen(_LCYAN)); + } else if (strncmp(str, _LRED, strlen(_LRED)) == 0) { + terminal_set_color(VGA_COLOR_LIGHT_RED); + return (strlen(_LRED)); + } else if (strncmp(str, _LMAGENTA, strlen(_LMAGENTA)) == 0) { + terminal_set_color(VGA_COLOR_LIGHT_MAGENTA); + return (strlen(_LMAGENTA)); + } else if (strncmp(str, _LYELLOW, strlen(_LYELLOW)) == 0) { + terminal_set_color(VGA_COLOR_LIGHT_BROWN); + return (strlen(_LYELLOW)); + } else if (strncmp(str, _WHITE, strlen(_WHITE)) == 0) { + terminal_set_color(VGA_COLOR_WHITE); + return (strlen(_WHITE)); + } + + /* Check Background colors */ + if (strncmp(str, _BG_BLACK, strlen(_BG_BLACK)) == 0) { + terminal_set_background_color(VGA_COLOR_BLACK); + return (strlen(_BG_BLACK)); + } else if (strncmp(str, _BG_BLUE, strlen(_BG_BLUE)) == 0) { + terminal_set_background_color(VGA_COLOR_BLUE); + return (strlen(_BG_BLUE)); + } else if (strncmp(str, _BG_GREEN, strlen(_BG_GREEN)) == 0) { + terminal_set_background_color(VGA_COLOR_GREEN); + return (strlen(_BG_GREEN)); + } else if (strncmp(str, _BG_CYAN, strlen(_BG_CYAN)) == 0) { + terminal_set_background_color(VGA_COLOR_CYAN); + return (strlen(_BG_CYAN)); + } else if (strncmp(str, _BG_RED, strlen(_BG_RED)) == 0) { + terminal_set_background_color(VGA_COLOR_RED); + return (strlen(_BG_RED)); + } else if (strncmp(str, _BG_MAGENTA, strlen(_BG_MAGENTA)) == 0) { + terminal_set_background_color(VGA_COLOR_MAGENTA); + return (strlen(_BG_MAGENTA)); + } else if (strncmp(str, _BG_YELLOW, strlen(_BG_YELLOW)) == 0) { + terminal_set_background_color(VGA_COLOR_BROWN); + return (strlen(_BG_YELLOW)); + } else if (strncmp(str, _BG_LGREY, strlen(_BG_LGREY)) == 0) { + terminal_set_background_color(VGA_COLOR_LIGHT_GREY); + return (strlen(_BG_LGREY)); + } else if (strncmp(str, _BG_LBLUE, strlen(_BG_LBLUE)) == 0) { + terminal_set_background_color(VGA_COLOR_LIGHT_BLUE); + return (strlen(_BG_LBLUE)); + } else if (strncmp(str, _BG_LGREEN, strlen(_BG_LGREEN)) == 0) { + terminal_set_background_color(VGA_COLOR_LIGHT_GREEN); + return (strlen(_BG_LGREEN)); + } else if (strncmp(str, _BG_LCYAN, strlen(_BG_LCYAN)) == 0) { + terminal_set_background_color(VGA_COLOR_LIGHT_CYAN); + return (strlen(_BG_LCYAN)); + } else if (strncmp(str, _BG_LRED, strlen(_BG_LRED)) == 0) { + terminal_set_background_color(VGA_COLOR_LIGHT_RED); + return (strlen(_BG_LRED)); + } else if (strncmp(str, _BG_LMAGENTA, strlen(_BG_LMAGENTA)) == 0) { + terminal_set_background_color(VGA_COLOR_LIGHT_MAGENTA); + return (strlen(_BG_LMAGENTA)); + } else if (strncmp(str, _BG_LYELLOW, strlen(_BG_LYELLOW)) == 0) { + terminal_set_background_color(VGA_COLOR_LIGHT_BROWN); + return (strlen(_BG_LYELLOW)); + } else if (strncmp(str, _BG_WHITE, strlen(_BG_WHITE)) == 0) { + terminal_set_background_color(VGA_COLOR_WHITE); + return (strlen(_BG_WHITE)); + } + + return (0); } -static int kprint_mod(const char *format, size_t i) { - - /* CHECK SPECIAL DELIMITERS */ - - // SPACES - if (format[i] == __SPE_NEG) { - _g_printk.is_neg_space = true; - ++i; - } else - _g_printk.is_neg_space = false; - - if (format[i] == __SPE_ZERO) { - _g_printk.use_zero = true; - ++i; - } else - _g_printk.use_zero = false; - - int nbr = 0; - while (isdigit(format[i])) { - nbr = nbr * 10 + (format[i] - 0x30); - i++; - } - _g_printk.space = nbr; - - if (format[i] == __SPE_DOT) { - // Skip dot - i++; - int precision = 0; - - if (format[i] == __SPE_STAR) { - _g_printk.precision = va_arg(_g_printk.args, int); - i++; - } else { - while (isdigit(format[i])) { - precision = precision * 10 + (format[i] - 0x30); - i++; - } - _g_printk.precision = precision; - } - } else { - _g_printk.precision = -1; - } - - /* CHECK BASIC DELIMITERS */ - - if (format[i] == __DEL_MOD) - i = __kpf_manage_mod(format, i); - else if (format[i] == __DEL_C) - __kpf_manage_char(); - else if (format[i] == __DEL_D || format[i] == __DEL_I) - __kpf_manage_nbr(); - else if (format[i] == __DEL_S) - __kpf_manage_str(); - else if (format[i] == __DEL_P) - __kpf_manage_ptr(); - else if (format[i] == __DEL_U) - __kpf_manage_unsigned(); - else if (format[i] == __DEL_X) - __kpf_manage_hexa(); - else if (format[i] == __DEL_F) - __kpf_manage_float(); - return (i); +static int kprint_mod(const char *format, uint32_t i) { + + /* CHECK SPECIAL DELIMITERS */ + + // SPACES + if (format[i] == __SPE_NEG) { + _g_printk.is_neg_space = true; + ++i; + } else + _g_printk.is_neg_space = false; + + if (format[i] == __SPE_ZERO) { + _g_printk.use_zero = true; + ++i; + } else + _g_printk.use_zero = false; + + int nbr = 0; + while (isdigit(format[i])) { + nbr = nbr * 10 + (format[i] - 0x30); + i++; + } + _g_printk.space = nbr; + + if (format[i] == __SPE_DOT) { + // Skip dot + i++; + int precision = 0; + + if (format[i] == __SPE_STAR) { + _g_printk.precision = va_arg(_g_printk.args, int); + i++; + } else { + while (isdigit(format[i])) { + precision = precision * 10 + (format[i] - 0x30); + i++; + } + _g_printk.precision = precision; + } + } else { + _g_printk.precision = -1; + } + + /* CHECK BASIC DELIMITERS */ + + if (format[i] == __DEL_MOD) + i = __kpf_manage_mod(format, i); + else if (format[i] == __DEL_C) + __kpf_manage_char(); + else if (format[i] == __DEL_D || format[i] == __DEL_I) + __kpf_manage_nbr(); + else if (format[i] == __DEL_S) + __kpf_manage_str(); + else if (format[i] == __DEL_P) + __kpf_manage_ptr(); + else if (format[i] == __DEL_U) + __kpf_manage_unsigned(); + else if (format[i] == __DEL_X) + __kpf_manage_hexa(); + else if (format[i] == __DEL_F) + __kpf_manage_float(); + return (i); } static int kprintf_loop(const char *format) { - size_t i = 0; - int ret = 0; - - while (format[i]) { - if ((ret = (check_colors(format + i))) != 0) { - i += ret; - continue; - } else if ((ret = (check_special_strings(format + i))) != 0) { - i += ret; - continue; - } else - ret = 0; - if (format[i] == __DEL_MOD) { - i++; - i = kprint_mod(format, i); - } else { - terminal_putchar(format[i]); - } - ++i; - } - return (0); + uint32_t i = 0; + int ret = 0; + + while (format[i]) { + if ((ret = (check_colors(format + i))) != 0) { + i += ret; + continue; + } else if ((ret = (check_special_strings(format + i))) != 0) { + i += ret; + continue; + } else { + ret = 0; + } + if (format[i] == __DEL_MOD) { + i++; + i = kprint_mod(format, i); + } else { + terminal_putchar(format[i]); + } + ++i; + } + return (ret); } uint32_t printk(const char *format, ...) { - int ret; - - _g_printk.is_neg_space = false; - _g_printk.space = 0; - _g_printk.use_zero = false; - _g_printk.space_is_star = false; - _g_printk.precision = -1; - - va_start(_g_printk.args, format); - ret = kprintf_loop(format); - va_end(_g_printk.args); - refresh_cursor(); - return (ret); + int ret; + + // Clear previous format string + memset(_g_printk.format, 0, sizeof(_g_printk.format)); + memcpy(_g_printk.format, format, strlen(format)); + + _g_printk.space = 0; + _g_printk.space_is_star = false; + + _g_printk.is_neg_space = false; + _g_printk.use_zero = false; + + _g_printk.precision = -1; + + va_start(_g_printk.args, format); + ret = kprintf_loop(format); + va_end(_g_printk.args); + + refresh_cursor(); + return (ret); } \ No newline at end of file diff --git a/Hephaistos/src/stdio/printk/srcs/printk_manager.c b/Hephaistos/src/stdio/printk/srcs/printk_manager.c index f03e872..bc506a9 100644 --- a/Hephaistos/src/stdio/printk/srcs/printk_manager.c +++ b/Hephaistos/src/stdio/printk/srcs/printk_manager.c @@ -6,7 +6,7 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/30 15:54:20 by vvaucoul #+# #+# */ -/* Updated: 2024/07/26 22:16:22 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/28 01:44:56 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ #include #include -size_t __kpf_manage_mod(const char *format, size_t i) { +uint32_t __kpf_manage_mod(const char *format, uint32_t i) { __kpf_manage_space_front(1); --i; if (format[i + 1] != __DEL_MOD) @@ -80,8 +80,8 @@ void __kpf_manage_hexa() { } void __kpf_manage_unsigned() { - uint32_t nbr = va_arg(_g_printk.args, uint32_t); - uint32_t len = _g_printk.precision < 0 ? nbrlen(nbr) : _g_printk.precision; + uint64_t nbr = va_arg(_g_printk.args, uint64_t); + uint64_t len = _g_printk.precision < 0 ? nbrlen(nbr) : _g_printk.precision; __kpf_manage_space_front(len); putunbr_len(nbr, len); diff --git a/Hephaistos/src/stdio/printk/srcs/printk_special_manager.c b/Hephaistos/src/stdio/printk/srcs/printk_special_manager.c index fd8f6e7..bd04d26 100644 --- a/Hephaistos/src/stdio/printk/srcs/printk_special_manager.c +++ b/Hephaistos/src/stdio/printk/srcs/printk_special_manager.c @@ -6,7 +6,7 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/30 15:59:37 by vvaucoul #+# #+# */ -/* Updated: 2024/07/26 22:14:15 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/28 01:12:20 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/Hephaistos/src/stdio/printk/srcs/printk_utils.c b/Hephaistos/src/stdio/printk/srcs/printk_utils.c index 53441af..373cf60 100644 --- a/Hephaistos/src/stdio/printk/srcs/printk_utils.c +++ b/Hephaistos/src/stdio/printk/srcs/printk_utils.c @@ -6,14 +6,14 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/30 16:11:35 by vvaucoul #+# #+# */ -/* Updated: 2024/07/26 22:12:40 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/28 01:15:42 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ #include -size_t __kptrlen(const void *ptr) { - size_t len = 0; +uint32_t __kptrlen(const void *ptr) { + uint32_t len = 0; unsigned char *p = (unsigned char *)ptr; if (ptr == NULL) diff --git a/Hephaistos/src/string.c b/Hephaistos/src/string.c index 50e554a..753c2f5 100644 --- a/Hephaistos/src/string.c +++ b/Hephaistos/src/string.c @@ -6,13 +6,174 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/11/20 11:55:48 by vvaucoul #+# #+# */ -/* Updated: 2024/07/26 22:11:32 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/27 17:40:13 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ -#include -#include #include +#include +#include + +/** + * @file string.c + * @brief Implementation of string functions. + * + * This file contains the implementation of string functions used in the Hephaistos project. + * The USE_ASM_STRING_FUNCTIONS macro controls whether assembly language or C language string functions are used. + */ +#if USE_ASM_STRING_FUNCTIONS == 0 +/** + * @brief Calculate the number of digits in a signed integer. + * + * This function calculates the number of digits in a signed integer. + * + * @param nbr The signed integer for which to calculate the number of digits. + * @return The number of digits in the signed integer. + */ +uint32_t nbrlen(int32_t nbr) { + uint32_t len = 0; + if (nbr == 0) { + return (1); + } + if (nbr < 0) { + len++; + nbr = -nbr; + } + while (nbr > 0) { + nbr /= 10; + len++; + } + return (len); +} + +/** + * @brief Calculate the length of an unsigned 32-bit integer when converted to a string. + * + * This function calculates the number of characters required to represent an unsigned 32-bit integer + * when converted to a string. It does not include the null-terminating character. + * + * @param nbr The unsigned 32-bit integer to calculate the length for. + * @return The length of the unsigned 32-bit integer when converted to a string. + */ +uint32_t unbrlen(uint32_t nbr) { + uint32_t len = 0; + if (nbr == 0) { + return (1); + } + while (nbr > 0) { + nbr /= 10; + len++; + } + return (len); +} + +/** + * Calculates the length of a null-terminated string. + * + * @param str The input string. + * @return The length of the string. + */ +uint32_t strlen(const char *str) { + uint32_t len = 0; + while (str[len]) { + len++; + } + return (len); +} + +/** + * @brief Calculates the number of digits in an integer when represented in a given base. + * + * This function takes an integer and a base as input and returns the number of digits required to represent the integer in the given base. + * + * @param nbr The integer for which the number of digits needs to be calculated. + * @param base The base in which the integer is represented. + * + * @return The number of digits required to represent the integer in the given base. + */ +uint32_t nbrlen_base(const int32_t nbr, const uint8_t base) { + uint32_t len = 0; + int32_t n = nbr; + + if (n == 0) { + return (1); + } + if (n < 0) { + len++; + n = -n; + } + while (n > 0) { + n /= base; + len++; + } + return (len); +} + +/** + * Calculates the length of an unsigned integer when represented in a given base. + * + * @param nbr The unsigned integer for which to calculate the length. + * @param base The base in which the integer is represented. + * @return The length of the integer when represented in the given base. + */ +uint32_t unbrlen_base(const uint32_t nbr, const uint8_t base) { + uint32_t len = 0; + int32_t n = nbr; + + if (nbr == 0) { + return (1); + } + while (n > 0) { + n /= base; + len++; + } + return (len); +} + +/** + * @brief Compares two strings. + * + * This function compares the string pointed to by `s1` to the string pointed to by `s2`. + * The comparison is done lexicographically, character by character. + * + * @param s1 Pointer to the first string to be compared. + * @param s2 Pointer to the second string to be compared. + * + * @return An integer less than, equal to, or greater than zero if `s1` is found, respectively, to be less than, to match, or be greater than `s2`. + */ +int32_t strcmp(const char *s1, const char *s2) { + uint32_t i = 0; + while (s1[i] && s2[i]) { + if (s1[i] != s2[i]) { + return (s1[i] - s2[i]); + } + i++; + } + return (s1[i] - s2[i]); +} + +/** + * @brief Compares at most n characters of two strings. + * + * This function compares the first n characters of the strings s1 and s2. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2. + * + * @param s1 Pointer to the first string to be compared. + * @param s2 Pointer to the second string to be compared. + * @param n Number of characters to compare. + * @return An integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2. + */ +int32_t strncmp(const char *s1, const char *s2, uint32_t n) { + uint32_t i = 0; + + while (s1[i] && s2[i] && i < n) { + if (s1[i] != s2[i]) { + return (s1[i] - s2[i]); + } + i++; + } + return (s1[i] - s2[i]); +} +#endif /** * @brief Copies the C string pointed by source into the array pointed by destination, including the null character. @@ -23,17 +184,17 @@ * @return Pointer to the destination array. */ char *strcpy(char *dest, const char *src) { - if (dest == NULL || src == NULL) { - return (NULL); - } - - uint32_t i = 0; - while (src[i]) { - dest[i] = src[i]; - i++; - } - dest[i] = '\0'; - return (dest); + if (dest == NULL || src == NULL) { + return (NULL); + } + + uint32_t i = 0; + while (src[i]) { + dest[i] = src[i]; + i++; + } + dest[i] = '\0'; + return (dest); } /** @@ -49,17 +210,17 @@ char *strcpy(char *dest, const char *src) { * @return A pointer to the destination string. */ char *strncpy(char *dest, const char *src, uint32_t n) { - if (dest == NULL || src == NULL) { - return (NULL); - } - - uint32_t i = 0; - while (src[i] && i < n) { - dest[i] = src[i]; - i++; - } - dest[i] = '\0'; - return (dest); + if (dest == NULL || src == NULL) { + return (NULL); + } + + uint32_t i = 0; + while (src[i] && i < n) { + dest[i] = src[i]; + i++; + } + dest[i] = '\0'; + return (dest); } /** @@ -70,24 +231,24 @@ char *strncpy(char *dest, const char *src, uint32_t n) { * @return The truncated string. */ char *strtrtc(const char *str, const char c) { - if (str == NULL) { - return (NULL); - } - - char *new_str = kmalloc(sizeof(char) * strlen(str) + 1); - if (new_str == NULL) { - return (NULL); - } - - bzero(new_str, strlen(str) + 1); - - uint32_t i = 0; - while (str[i] && str[i] != c) { - new_str[i] = str[i]; - i++; - } - new_str[i] = '\0'; - return (new_str); + if (str == NULL) { + return (NULL); + } + + char *new_str = kmalloc(sizeof(char) * strlen(str) + 1); + if (new_str == NULL) { + return (NULL); + } + + bzero(new_str, strlen(str) + 1); + + uint32_t i = 0; + while (str[i] && str[i] != c) { + new_str[i] = str[i]; + i++; + } + new_str[i] = '\0'; + return (new_str); } /** @@ -98,35 +259,35 @@ char *strtrtc(const char *str, const char c) { * @return The cleared string. */ char *strclr(char *new_str, char *str) { - if (str == NULL) { - return (NULL); - } - bzero(new_str, strlen(new_str)); - - uint32_t i = 0; - /* Skip all firsts spaces */ - while (str[i] && str[i] == ' ') { - i++; - } - - uint32_t j = 0; - - /* Skip all spaces in the string */ - while (str[i]) { - if (str[i] == ' ') { - while (str[i] && str[i] == ' ') - i++; - if (str[i] == '\0') - break; - new_str[j] = ' '; - j++; - } - new_str[j] = str[i]; - i++; - j++; - } - new_str[j] = '\0'; - return (new_str); + if (str == NULL) { + return (NULL); + } + bzero(new_str, strlen(new_str)); + + uint32_t i = 0; + /* Skip all firsts spaces */ + while (str[i] && str[i] == ' ') { + i++; + } + + uint32_t j = 0; + + /* Skip all spaces in the string */ + while (str[i]) { + if (str[i] == ' ') { + while (str[i] && str[i] == ' ') + i++; + if (str[i] == '\0') + break; + new_str[j] = ' '; + j++; + } + new_str[j] = str[i]; + i++; + j++; + } + new_str[j] = '\0'; + return (new_str); } /** @@ -140,22 +301,22 @@ char *strclr(char *new_str, char *str) { * @return A pointer to the resulting string. */ char *strcat(char *dest, const char *src) { - if (dest == NULL || src == NULL) { - return (NULL); - } - - uint32_t i = 0; - uint32_t j = 0; - while (dest[i]) { - i++; - } - while (src[j]) { - dest[i] = src[j]; - i++; - j++; - } - dest[i] = '\0'; - return (dest); + if (dest == NULL || src == NULL) { + return (NULL); + } + + uint32_t i = 0; + uint32_t j = 0; + while (dest[i]) { + i++; + } + while (src[j]) { + dest[i] = src[j]; + i++; + j++; + } + dest[i] = '\0'; + return (dest); } /** @@ -166,20 +327,20 @@ char *strcat(char *dest, const char *src) { * @return A pointer to the joined string. */ char *strjoin(char *s1, char *s2) { - if (s1 == NULL || s2 == NULL) { - return (NULL); - } + if (s1 == NULL || s2 == NULL) { + return (NULL); + } - char *new_str = kmalloc(sizeof(char) * (strlen(s1) + strlen(s2) + 1)); - if (new_str == NULL) { - return (NULL); - } + char *new_str = kmalloc(sizeof(char) * (strlen(s1) + strlen(s2) + 1)); + if (new_str == NULL) { + return (NULL); + } - bzero(new_str, strlen(s1) + strlen(s2) + 1); + bzero(new_str, strlen(s1) + strlen(s2) + 1); - strcpy(new_str, s1); - strcat(new_str, s2); - return (new_str); + strcpy(new_str, s1); + strcat(new_str, s2); + return (new_str); } /** @@ -192,18 +353,18 @@ char *strjoin(char *s1, char *s2) { * or NULL if the character is not found. */ char *strchr(const char *s, int c) { - if (s == NULL) { - return (NULL); - } - - uint32_t i = 0; - while (s[i]) { - if (s[i] == c) { - return ((char *)&s[i]); - } - i++; - } - return (NULL); + if (s == NULL) { + return (NULL); + } + + uint32_t i = 0; + while (s[i]) { + if (s[i] == c) { + return ((char *)&s[i]); + } + i++; + } + return (NULL); } /** @@ -214,18 +375,18 @@ char *strchr(const char *s, int c) { * @return A pointer to the last occurrence of the character in the string, or NULL if the character is not found. */ char *strrchr(const char *s, int c) { - if (s == NULL) { - return (NULL); - } - - uint32_t i = strlen(s); - while (i > 0) { - if (s[i] == c) { - return ((char *)&s[i]); - } - i--; - } - return (NULL); + if (s == NULL) { + return (NULL); + } + + uint32_t i = strlen(s); + while (i > 0) { + if (s[i] == c) { + return ((char *)&s[i]); + } + i--; + } + return (NULL); } /** @@ -236,19 +397,19 @@ char *strrchr(const char *s, int c) { * @return A pointer to the duplicated string, or NULL if insufficient memory was available. */ char *strdup(const char *s) { - if (s == NULL) { - return (NULL); - } + if (s == NULL) { + return (NULL); + } - char *new_str = kmalloc(sizeof(char) * strlen(s) + 1); - if (new_str == NULL) { - return (NULL); - } + char *new_str = kmalloc(sizeof(char) * strlen(s) + 1); + if (new_str == NULL) { + return (NULL); + } - bzero(new_str, strlen(s) + 1); + bzero(new_str, strlen(s) + 1); - strcpy(new_str, s); - return (new_str); + strcpy(new_str, s); + return (new_str); } /** @@ -262,19 +423,19 @@ char *strdup(const char *s) { * @return A pointer to the newly allocated string, or NULL if an error occurred. */ char *strndup(const char *s, uint32_t n) { - if (s == NULL) { - return (NULL); - } + if (s == NULL) { + return (NULL); + } - char *new_str = kmalloc(sizeof(char) * n + 1); - if (new_str == NULL) { - return (NULL); - } + char *new_str = kmalloc(sizeof(char) * n + 1); + if (new_str == NULL) { + return (NULL); + } - bzero(new_str, n + 1); + bzero(new_str, n + 1); - strncpy(new_str, s, n); - return (new_str); + strncpy(new_str, s, n); + return (new_str); } /** @@ -286,19 +447,19 @@ char *strndup(const char *s, uint32_t n) { * @return The trimmed string. */ char *strtrim(char const *s) { - if (s == NULL) { - return (NULL); - } + if (s == NULL) { + return (NULL); + } - char *new_str = kmalloc(sizeof(char) * strlen(s) + 1); - if (new_str == NULL) { - return (NULL); - } + char *new_str = kmalloc(sizeof(char) * strlen(s) + 1); + if (new_str == NULL) { + return (NULL); + } - bzero(new_str, strlen(s) + 1); + bzero(new_str, strlen(s) + 1); - strclr(new_str, (char *)s); - return (new_str); + strclr(new_str, (char *)s); + return (new_str); } /** @@ -314,19 +475,19 @@ char *strtrim(char const *s) { * @return The extracted substring as a dynamically allocated string. */ char *strsub(char const *s, unsigned int start, uint32_t len) { - if (s == NULL) { - return (NULL); - } + if (s == NULL) { + return (NULL); + } - char *new_str = kmalloc(sizeof(char) * len + 1); - if (new_str == NULL) { - return (NULL); - } + char *new_str = kmalloc(sizeof(char) * len + 1); + if (new_str == NULL) { + return (NULL); + } - bzero(new_str, len + 1); + bzero(new_str, len + 1); - strncpy(new_str, &s[start], len); - return (new_str); + strncpy(new_str, &s[start], len); + return (new_str); } /** @@ -340,66 +501,66 @@ char *strsub(char const *s, unsigned int start, uint32_t len) { * @return A pointer to an array of strings. */ char **strsplit(char const *s, char c) { - if (s == NULL) { - return (NULL); - } - - char *new_str = strtrim(s); - if (new_str == NULL) { - return (NULL); - } - - uint32_t i = 0; - uint32_t j = 0; - uint32_t k = 0; - uint32_t nb_words = 0; - - while (new_str[i]) { - if (new_str[i] == c) { - nb_words++; - } - i++; - } - nb_words++; - - char **tab = kmalloc(sizeof(char *) * (nb_words + 1)); - if (tab == NULL) { - return (NULL); - } - - i = 0; - while (new_str[i]) { - if (new_str[i] == c) { - tab[j] = strsub(new_str, k, i - k); - j++; - k = i + 1; - } - i++; - } - tab[j] = strsub(new_str, k, i - k); - tab[j + 1] = NULL; - kfree(new_str); - return (tab); + if (s == NULL) { + return (NULL); + } + + char *new_str = strtrim(s); + if (new_str == NULL) { + return (NULL); + } + + uint32_t i = 0; + uint32_t j = 0; + uint32_t k = 0; + uint32_t nb_words = 0; + + while (new_str[i]) { + if (new_str[i] == c) { + nb_words++; + } + i++; + } + nb_words++; + + char **tab = kmalloc(sizeof(char *) * (nb_words + 1)); + if (tab == NULL) { + return (NULL); + } + + i = 0; + while (new_str[i]) { + if (new_str[i] == c) { + tab[j] = strsub(new_str, k, i - k); + j++; + k = i + 1; + } + i++; + } + tab[j] = strsub(new_str, k, i - k); + tab[j + 1] = NULL; + kfree(new_str); + return (tab); } static int strspn(const char *s, const char *accept) { - if (s == NULL || accept == NULL) - return (0); - - uint32_t i = 0; - uint32_t j = 0; - while (s[i]) { - j = 0; - while (accept[j]) { - if (s[i] == accept[j]) - break; - j++; - } - if (accept[j] == '\0') - return (i); - i++; - } - return (i); + if (s == NULL || accept == NULL) + return (0); + + uint32_t i = 0; + uint32_t j = 0; + while (s[i]) { + j = 0; + while (accept[j]) { + if (s[i] == accept[j]) + break; + j++; + } + if (accept[j] == '\0') + return (i); + i++; + } + return (i); } /** @@ -413,23 +574,23 @@ static int strspn(const char *s, const char *accept) { * @return A pointer to the first occurrence of any character from 'charset' in 'str', or a null pointer if no match is found. */ char *strpbrk(const char *s, const char *accept) { - if (s == NULL || accept == NULL) { - return (NULL); - } - - uint32_t i = 0; - uint32_t j = 0; - while (s[i]) { - j = 0; - while (accept[j]) { - if (s[i] == accept[j]) { - return ((char *)&s[i]); - } - j++; - } - i++; - } - return (NULL); + if (s == NULL || accept == NULL) { + return (NULL); + } + + uint32_t i = 0; + uint32_t j = 0; + while (s[i]) { + j = 0; + while (accept[j]) { + if (s[i] == accept[j]) { + return ((char *)&s[i]); + } + j++; + } + i++; + } + return (NULL); } /** @@ -453,27 +614,27 @@ char *strpbrk(const char *s, const char *accept) { * This pointer is stored internally in the function and is not accessible to the calling program. */ char *strtok(char *str, const char *delim) { - static char *last; - char *token; - - if (str == NULL) { - str = last; - } - - str += strspn(str, delim); - if (*str == '\0') { - return (NULL); - } - - token = str; - str = strpbrk(token, delim); - if (str == NULL) { - last = NULL; - } else { - *str = '\0'; - last = str + 1; - } - return (token); + static char *last; + char *token; + + if (str == NULL) { + str = last; + } + + str += strspn(str, delim); + if (*str == '\0') { + return (NULL); + } + + token = str; + str = strpbrk(token, delim); + if (str == NULL) { + last = NULL; + } else { + *str = '\0'; + last = str + 1; + } + return (token); } /** @@ -486,21 +647,21 @@ char *strtok(char *str, const char *delim) { * string haystack, or NULL if the substring is not found. */ char *strstr(const char *haystack, const char *needle) { - if (haystack == NULL || needle == NULL) { - return (NULL); - } - - uint32_t i = 0; - uint32_t j = 0; - while (haystack[i]) { - j = 0; - while (needle[j] && haystack[i + j] == needle[j]) - j++; - if (needle[j] == '\0') - return ((char *)&haystack[i]); - i++; - } - return (NULL); + if (haystack == NULL || needle == NULL) { + return (NULL); + } + + uint32_t i = 0; + uint32_t j = 0; + while (haystack[i]) { + j = 0; + while (needle[j] && haystack[i + j] == needle[j]) + j++; + if (needle[j] == '\0') + return ((char *)&haystack[i]); + i++; + } + return (NULL); } /** @@ -512,22 +673,22 @@ char *strstr(const char *haystack, const char *needle) { * @return The reversed string. */ char *strrev(char *str) { - if (str == NULL) { - return (NULL); - } - - uint32_t i = 0; - uint32_t j = strlen(str) - 1; - char tmp; - - while (i < j) { - tmp = str[i]; - str[i] = str[j]; - str[j] = tmp; - i++; - j--; - } - return (str); + if (str == NULL) { + return (NULL); + } + + uint32_t i = 0; + uint32_t j = strlen(str) - 1; + char tmp; + + while (i < j) { + tmp = str[i]; + str[i] = str[j]; + str[j] = tmp; + i++; + j--; + } + return (str); } /** @@ -541,20 +702,20 @@ char *strrev(char *str) { * @return The reversed string. */ char *strrevp(char *str, uint32_t start, uint32_t end) { - if (str == NULL) { - return (NULL); - } - - uint32_t i = start; - uint32_t j = end; - char tmp; - - while (i < j) { - tmp = str[i]; - str[i] = str[j]; - str[j] = tmp; - i++; - j--; - } - return (str); + if (str == NULL) { + return (NULL); + } + + uint32_t i = start; + uint32_t j = end; + char tmp; + + while (i < j) { + tmp = str[i]; + str[i] = str[j]; + str[j] = tmp; + i++; + j--; + } + return (str); } \ No newline at end of file diff --git a/Hephaistos/src/time.c b/Hephaistos/src/time.c index f6c1299..8292a26 100644 --- a/Hephaistos/src/time.c +++ b/Hephaistos/src/time.c @@ -6,13 +6,13 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/11/21 14:11:09 by vvaucoul #+# #+# */ -/* Updated: 2024/07/26 22:00:08 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/27 17:23:51 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ +#include #include #include -#include /** * @brief Starts the time counter by recording the current timer ticks as the start time. @@ -20,8 +20,8 @@ * @param counter Pointer to the counter structure to be started. */ void counter_start(counter_t *counter) { - assert(counter != NULL); - counter->start = pit_get_ticks(); + assert(counter != NULL); + counter->start = pit_get_ticks(); } /** @@ -30,8 +30,8 @@ void counter_start(counter_t *counter) { * @param counter Pointer to the counter structure to be stopped. */ void counter_stop(counter_t *counter) { - assert(counter != NULL); - counter->end = pit_get_ticks(); + assert(counter != NULL); + counter->end = pit_get_ticks(); } /** @@ -41,8 +41,8 @@ void counter_stop(counter_t *counter) { * @return The start time of the counter. */ uint32_t counter_get_start(counter_t *counter) { - assert(counter != NULL); - return counter->start; + assert(counter != NULL); + return counter->start; } /** @@ -52,8 +52,8 @@ uint32_t counter_get_start(counter_t *counter) { * @return The end time of the counter. */ uint32_t counter_get_end(counter_t *counter) { - assert(counter != NULL); - return counter->end; + assert(counter != NULL); + return counter->end; } /** @@ -63,6 +63,6 @@ uint32_t counter_get_end(counter_t *counter) { * @return The elapsed time between the start and end times of the counter. */ uint32_t counter_get_time(counter_t *counter) { - assert(counter != NULL); - return counter->end - counter->start; + assert(counter != NULL); + return counter->end - counter->start; } \ No newline at end of file diff --git a/Makefile b/Makefile index 8da0666..820d740 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: vvaucoul +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/11/19 23:52:42 by vvaucoul #+# #+# # -# Updated: 2024/07/26 22:20:34 by vvaucoul ### ########.fr # +# Updated: 2024/07/28 01:30:17 by vvaucoul ### ########.fr # # # # **************************************************************************** # diff --git a/tests/workflow_strings.c b/tests/workflow_strings.c index 830ea95..0508cc9 100644 --- a/tests/workflow_strings.c +++ b/tests/workflow_strings.c @@ -6,129 +6,257 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/14 18:41:26 by vvaucoul #+# #+# */ -/* Updated: 2024/01/14 18:54:11 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/27 19:07:50 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ -#include #include +#include + +#include +#include void test_strtrtc() { - char *result = strtrtc("Hello, world!", ','); - assert(strcmp(result, "Hello") == 0); - kfree(result); + char *result; + + result = strtrtc("Hello, world!", ','); + assert(strcmp(result, "Hello") == 0); + kfree(result); + + result = strtrtc("Hello, world!", '!'); + assert(strcmp(result, "Hello, world") == 0); + kfree(result); + + result = strtrtc("Hello, world!", 'x'); // Character not in string + assert(strcmp(result, "Hello, world!") == 0); + kfree(result); } void test_strclr() { - char str[] = "Hello, world!"; - char *result = strclr(str, str); - assert(strcmp(result, "") == 0); + char str[] = "Hello, world!"; + char *result; + + result = strclr(str, str); + assert(strcmp(result, "") == 0); + + // Test with different strings + char str2[] = "Test string"; + result = strclr(str2, str2); + assert(strcmp(result, "") == 0); } void test_strcpy() { - char dest[20]; - strcpy(dest, "Hello, world!"); - assert(strcmp(dest, "Hello, world!") == 0); + char dest[20]; + + strcpy(dest, "Hello, world!"); + assert(strcmp(dest, "Hello, world!") == 0); + + strcpy(dest, ""); + assert(strcmp(dest, "") == 0); } void test_strncpy() { - char dest[20]; - strncpy(dest, "Hello, world!", 5); - assert(strncmp(dest, "Hello", 5) == 0); + char dest[20]; + + strncpy(dest, "Hello, world!", 5); + dest[5] = '\0'; // Ensure null termination + assert(strcmp(dest, "Hello") == 0); + + strncpy(dest, "Hi", 5); + dest[2] = '\0'; + assert(strcmp(dest, "Hi") == 0); } void test_strcat() { - char dest[20] = "Hello"; - strcat(dest, ", world!"); - assert(strcmp(dest, "Hello, world!") == 0); + char dest[20] = "Hello"; + + strcat(dest, ", world!"); + assert(strcmp(dest, "Hello, world!") == 0); + + strcpy(dest, "Hi"); + strcat(dest, ""); + assert(strcmp(dest, "Hi") == 0); } void test_strjoin() { - char *result = strjoin("Hello", ", world!"); - assert(strcmp(result, "Hello, world!") == 0); - kfree(result); + char *result; + + result = strjoin("Hello", ", world!"); + assert(strcmp(result, "Hello, world!") == 0); + kfree(result); + + result = strjoin("", "world!"); + assert(strcmp(result, "world!") == 0); + kfree(result); + + result = strjoin("Hello", ""); + assert(strcmp(result, "Hello") == 0); + kfree(result); } void test_strchr() { - char *result = strchr("Hello, world!", ','); - assert(strcmp(result, ", world!") == 0); + char *result; + + result = strchr("Hello, world!", ','); + assert(strcmp(result, ", world!") == 0); + + result = strchr("Hello, world!", '!'); + assert(strcmp(result, "!") == 0); + + result = strchr("Hello, world!", 'x'); // Character not in string + assert(result == NULL); } void test_strrchr() { - char *result = strrchr("Hello, world!", 'o'); - assert(strcmp(result, "orld!") == 0); + char *result; + + result = strrchr("Hello, world!", 'o'); + assert(strcmp(result, "orld!") == 0); + + result = strrchr("Hello, world!", 'H'); + assert(strcmp(result, "Hello, world!") == 0); + + result = strrchr("Hello, world!", 'x'); // Character not in string + assert(result == NULL); } void test_strdup() { - char *result = strdup("Hello, world!"); - assert(strcmp(result, "Hello, world!") == 0); - kfree(result); + char *result; + + result = strdup("Hello, world!"); + assert(strcmp(result, "Hello, world!") == 0); + kfree(result); + + result = strdup(""); + assert(strcmp(result, "") == 0); + kfree(result); } void test_strndup() { - char *result = strndup("Hello, world!", 5); - assert(strncmp(result, "Hello", 5) == 0); - kfree(result); + char *result; + + result = strndup("Hello, world!", 5); + assert(strncmp(result, "Hello", 5) == 0); + kfree(result); + + result = strndup("Hi", 5); // Source string is shorter than n + assert(strcmp(result, "Hi") == 0); + kfree(result); } void test_strtrim() { - char *result = strtrim(" Hello, world! "); - assert(strcmp(result, "Hello, world!") == 0); - kfree(result); + char *result; + + result = strtrim(" Hello, world! "); + assert(strcmp(result, "Hello, world!") == 0); + kfree(result); + + result = strtrim(" "); + assert(strcmp(result, "") == 0); + kfree(result); + + result = strtrim("NoSpaces"); + assert(strcmp(result, "NoSpaces") == 0); + kfree(result); } void test_strsub() { - char *result = strsub("Hello, world!", 7, 5); - assert(strcmp(result, "world") == 0); - kfree(result); + char *result; + + result = strsub("Hello, world!", 7, 5); + assert(strcmp(result, "world") == 0); + kfree(result); + + result = strsub("Test string", 0, 4); + assert(strcmp(result, "Test") == 0); + kfree(result); } void test_strsplit() { - char **result = strsplit("Hello, world!", ','); - assert(strcmp(result[0], "Hello") == 0); - assert(strcmp(result[1], " world!") == 0); + char **result; + + result = strsplit("Hello, world!", ','); + assert(strcmp(result[0], "Hello") == 0); + assert(strcmp(result[1], " world!") == 0); + kfree(result[0]); + kfree(result[1]); + kfree(result); - // Remember to kfree each string and the array itself - kfree(result[0]); - kfree(result[1]); - kfree(result); + result = strsplit("No,split,here", ';'); + assert(strcmp(result[0], "No,split,here") == 0); + kfree(result[0]); + kfree(result); } void test_strtok() { - char str[] = "Hello, world!"; - char *token = strtok(str, ","); - assert(strcmp(token, "Hello") == 0); - token = strtok(NULL, ","); - assert(strcmp(token, " world!") == 0); + char str[] = "Hello, world!"; + char *token; + + token = strtok(str, ","); + assert(strcmp(token, "Hello") == 0); + token = strtok(NULL, ","); + assert(strcmp(token, " world!") == 0); } void test_strstr() { - char *result = strstr("Hello, world!", "world"); - assert(strcmp(result, "world!") == 0); + char *result; + + result = strstr("Hello, world!", "world"); + assert(strcmp(result, "world!") == 0); + + result = strstr("No match here", "absent"); + assert(result == NULL); } void test_strpbrk() { - char *result = strpbrk("Hello, world!", ",!"); - assert(strcmp(result, ", world!") == 0); + char *result; + + result = strpbrk("Hello, world!", ",!"); + assert(strcmp(result, ", world!") == 0); + + result = strpbrk("Hello, world!", "xyz"); + assert(result == NULL); +} + +void test_strrev() { + char str1[] = "Hello"; + char str2[] = "A"; + + char *result = strrev(str1); + assert(strcmp(result, "olleH") == 0); + + result = strrev(str2); + assert(strcmp(result, "A") == 0); +} + +void test_strrevp() { + char str[] = "abcdef"; + + char *result = strrevp(str, 1, 4); + assert(strcmp(result, "aedcbf") == 0); } int workflow_hephaistos_strings(void) { - test_strtrtc(); - test_strclr(); - test_strcpy(); - test_strncpy(); - test_strcat(); - test_strjoin(); - test_strchr(); - test_strrchr(); - test_strdup(); - test_strndup(); - test_strtrim(); - test_strsub(); - test_strsplit(); - test_strtok(); - test_strstr(); - test_strpbrk(); - - return (0); + test_strtrtc(); + test_strclr(); + test_strcpy(); + test_strncpy(); + test_strcat(); + test_strjoin(); + test_strchr(); + test_strrchr(); + test_strdup(); + test_strndup(); + test_strtrim(); + test_strsub(); + test_strsplit(); + test_strtok(); + test_strstr(); + test_strpbrk(); + test_strrev(); + test_strrevp(); + + printk("All tests passed successfully.\n"); + + return (0); } \ No newline at end of file diff --git a/workflows/hephaistos_workflow.c b/workflows/hephaistos_workflow.c index 0efb0db..181a947 100644 --- a/workflows/hephaistos_workflow.c +++ b/workflows/hephaistos_workflow.c @@ -6,12 +6,12 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/11/20 11:36:33 by vvaucoul #+# #+# */ -/* Updated: 2024/07/22 12:16:18 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/27 19:02:03 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ #include -#include +#include <../workflows/workflows.h> /** * @brief This function is a test called in the kernel to check all the lib. diff --git a/workflows/workflows.h b/workflows/workflows.h index 53f489e..8773ec4 100644 --- a/workflows/workflows.h +++ b/workflows/workflows.h @@ -6,7 +6,7 @@ /* By: vvaucoul +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/09 14:55:33 by vvaucoul #+# #+# */ -/* Updated: 2024/07/22 21:06:45 by vvaucoul ### ########.fr */ +/* Updated: 2024/07/27 19:07:58 by vvaucoul ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,4 +38,6 @@ extern int workflow_hephaistos_a_avl(void); extern int hephaistos_workflow(void); +extern void hephaistos_off_tests(void); + #endif /* !__H_WORKFLOWS_H */ \ No newline at end of file