1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2024-11-13 23:43:13 +01:00

fix!: usage of stdint instead of int types for cross-platform compatibility

BREAKING CHANGE: Functions signatures changed.

Fixes #9
This commit is contained in:
Théo LUDWIG 2024-09-25 14:26:15 +02:00
parent 6ac47429e8
commit 51d7123c8d
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
17 changed files with 365 additions and 93 deletions

View File

@ -2,7 +2,15 @@
struct array_list* array_list_initialization() {
struct array_list* list = malloc(sizeof(struct array_list));
if (list == NULL) {
perror("Error (array_list_initialization)");
exit(EXIT_FAILURE);
}
list->data = malloc(sizeof(void*) * ARRAY_LIST_INITIAL_CAPACITY);
if (list->data == NULL) {
perror("Error (array_list_initialization)");
exit(EXIT_FAILURE);
}
list->size = 0;
list->capacity = ARRAY_LIST_INITIAL_CAPACITY;
return list;
@ -13,6 +21,10 @@ void array_list_add(struct array_list* list, void* element) {
size_t previous_capacity = list->capacity;
list->capacity += ARRAY_LIST_INITIAL_CAPACITY;
list->data = realloc(list->data, sizeof(void*) * list->capacity);
if (list->data == NULL) {
perror("Error (array_list_add)");
exit(EXIT_FAILURE);
}
for (size_t index = previous_capacity; index < list->capacity; index++) {
list->data[index] = NULL;
}

View File

@ -3,6 +3,7 @@
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "types.h"

View File

@ -32,9 +32,9 @@ bool character_get_is_digit(const char character) {
return character >= '0' && character <= '9';
}
unsigned char character_get_alphabet_position(const char character) {
uint8_t character_get_alphabet_position(const char character) {
const char letter = character_to_lower(character);
unsigned char position = 0;
uint8_t position = 0;
if (letter >= 'a' && letter <= 'z') {
position = (letter - 'a') + 1;
}

View File

@ -59,9 +59,9 @@ bool character_get_is_digit(const char character);
* Return 0 if the character is not a letter.
*
* @param character
* @return unsigned char
* @return uint8_t
* @since v1.0.0
*/
unsigned char character_get_alphabet_position(const char character);
uint8_t character_get_alphabet_position(const char character);
#endif

View File

@ -19,9 +19,9 @@ char convert_digit_to_character(const char digit) {
return digit + '0';
}
long long convert_string_to_number(const string_t string) {
int64_t convert_string_to_number(const string_t string) {
bool is_negative = string[0] == '-';
long long integer = 0;
int64_t integer = 0;
size_t length = string_get_length(string);
for (size_t index = is_negative ? 1 : 0; index < length; index++) {
integer = integer * 10 + convert_character_to_digit(string[index]);
@ -29,13 +29,13 @@ long long convert_string_to_number(const string_t string) {
return is_negative ? integer * -1 : integer;
}
string_t convert_number_to_string(const long long integer) {
string_t convert_number_to_string(const int64_t integer) {
if (integer == 0) {
return convert_character_to_string('0');
}
bool is_negative = integer < 0;
size_t length = 1;
long long current = mathematics_absolute_value(integer);
int64_t current = mathematics_absolute_value(integer);
while (current != 0) {
current = current / 10;
length++;
@ -62,21 +62,21 @@ string_t convert_number_to_string(const long long integer) {
return string;
}
string_t convert_number_from_base_10_to_base(unsigned long long number, unsigned long base) {
string_t convert_number_from_base_10_to_base(uint64_t number, uint64_t base) {
if (number == 0) {
return "0";
}
int remainders[64];
int index = 0;
int64_t remainders[64];
int64_t index = 0;
while (number > 0) {
remainders[index] = number % base;
number = number / base;
index++;
}
string_t result = malloc(sizeof(char) * (index + 1));
int index_result = 0;
for (int iteration = index - 1; iteration >= 0; iteration--) {
int remainder = remainders[iteration];
int64_t index_result = 0;
for (int64_t iteration = index - 1; iteration >= 0; iteration--) {
int64_t remainder = remainders[iteration];
if (remainder >= 10) {
result[index_result] = (char)((remainder - 10) + 'A');
} else {
@ -88,17 +88,17 @@ string_t convert_number_from_base_10_to_base(unsigned long long number, unsigned
return result;
}
unsigned long convert_number_from_base_to_base_10(string_t number, unsigned long base) {
uint64_t convert_number_from_base_to_base_10(string_t number, uint64_t base) {
size_t length = string_get_length(number);
int exponent = length - 1;
unsigned long result = 0;
int index = 0;
int64_t exponent = length - 1;
uint64_t result = 0;
int64_t index = 0;
while (exponent >= 0) {
int current_number = (int)(number[index] - '0');
int64_t current_number = (int64_t)(number[index] - '0');
if (current_number >= 10) {
current_number = (int)(number[index] - 'A') + 10;
current_number = (int64_t)(number[index] - 'A') + 10;
} else {
current_number = (int)(number[index] - '0');
current_number = (int64_t)(number[index] - '0');
}
result = result + current_number * mathematics_pow(base, exponent);
exponent--;
@ -107,6 +107,6 @@ unsigned long convert_number_from_base_to_base_10(string_t number, unsigned long
return result;
}
string_t convert_number_from_base_to_another(string_t number, unsigned long base_from, unsigned long base_target) {
string_t convert_number_from_base_to_another(string_t number, uint64_t base_from, uint64_t base_target) {
return convert_number_from_base_10_to_base(convert_number_from_base_to_base_10(number, base_from), base_target);
}

View File

@ -42,10 +42,10 @@ char convert_digit_to_character(const char digit);
* @brief Convert a string to a number.
*
* @param string
* @return long long
* @return int64_t
* @since v1.0.0
*/
long long convert_string_to_number(const string_t string);
int64_t convert_string_to_number(const string_t string);
/**
* @brief Convert a number to a string.
@ -54,7 +54,7 @@ long long convert_string_to_number(const string_t string);
* @return string_t
* @since v1.0.0
*/
string_t convert_number_to_string(const long long integer);
string_t convert_number_to_string(const int64_t integer);
/**
* @brief Convert a number (base 10) to a string with a specific base.
@ -64,17 +64,17 @@ string_t convert_number_to_string(const long long integer);
* @return string_t
* @since v1.0.0
*/
string_t convert_number_from_base_10_to_base(unsigned long long number, unsigned long base);
string_t convert_number_from_base_10_to_base(uint64_t number, uint64_t base);
/**
* @brief Convert a number with a specific base to a number base 10.
*
* @param number
* @param base
* @return int
* @return uint64_t
* @since v1.0.0
*/
unsigned long convert_number_from_base_to_base_10(string_t number, unsigned long base);
uint64_t convert_number_from_base_to_base_10(string_t number, uint64_t base);
/**
* @brief Convert a number with a specific base to a number of specific base.
@ -85,6 +85,6 @@ unsigned long convert_number_from_base_to_base_10(string_t number, unsigned long
* @return string_t
* @since v1.0.0
*/
string_t convert_number_from_base_to_another(string_t number, unsigned long base_from, unsigned long base_target);
string_t convert_number_from_base_to_another(string_t number, uint64_t base_from, uint64_t base_target);
#endif

View File

@ -1,6 +1,6 @@
#include "filesystem.h"
int filesystem_read(string_t path, byte_t **file_content, off_t *file_size) {
int filesystem_read(string_t path, byte_t **file_content, size_t *file_size) {
int file_descriptor = open(path, O_RDONLY);
if (file_descriptor == -1) {
return -1;
@ -15,7 +15,7 @@ int filesystem_read(string_t path, byte_t **file_content, off_t *file_size) {
return 0;
}
int filesystem_write(string_t path, byte_t *file_content, off_t file_size) {
int filesystem_write(string_t path, byte_t *file_content, size_t file_size) {
int file_descriptor = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (file_descriptor == -1) {
return -1;

View File

@ -24,7 +24,7 @@
* @return int
* @since v1.0.0
*/
int filesystem_read(string_t path, byte_t **file_content, off_t *file_size);
int filesystem_read(string_t path, byte_t **file_content, size_t *file_size);
/**
* @brief Write the content to a file.
@ -37,7 +37,7 @@ int filesystem_read(string_t path, byte_t **file_content, off_t *file_size);
* @return int
* @since v1.0.0
*/
int filesystem_write(string_t path, byte_t *file_content, off_t file_size);
int filesystem_write(string_t path, byte_t *file_content, size_t file_size);
/**
* @brief Check if a path exists.

View File

@ -40,7 +40,7 @@ uint64_t hash(string_t key, size_t capacity) {
memcpy(&m, message + offset, sizeof(uint64_t));
v3 ^= m;
for (int i = 0; i < 2; i++) {
for (uint8_t i = 0; i < 2; i++) {
v0 = sip_round(v0, v1, v2, v3);
v1 = ROTATE_LEFT(v1, 13);
v2 = ROTATE_LEFT(v2, 16);
@ -49,7 +49,7 @@ uint64_t hash(string_t key, size_t capacity) {
}
v2 ^= 0xff;
for (int i = 0; i < 4; i++) {
for (uint8_t i = 0; i < 4; i++) {
v0 = sip_round(v0, v1, v2, v3);
v1 = ROTATE_LEFT(v1, 13);
v2 = ROTATE_LEFT(v2, 16);
@ -90,7 +90,7 @@ uint64_t hash(string_t key, size_t capacity) {
v3 ^= m;
for (int i = 0; i < 2; i++) {
for (uint8_t i = 0; i < 2; i++) {
v0 = sip_round(v0, v1, v2, v3);
v1 = ROTATE_LEFT(v1, 13);
v2 = ROTATE_LEFT(v2, 16);
@ -100,7 +100,7 @@ uint64_t hash(string_t key, size_t capacity) {
v0 ^= m;
v2 ^= 0xff;
for (int i = 0; i < 4; i++) {
for (uint8_t i = 0; i < 4; i++) {
v0 = sip_round(v0, v1, v2, v3);
v1 = ROTATE_LEFT(v1, 13);
v2 = ROTATE_LEFT(v2, 16);

View File

@ -1,23 +1,23 @@
#include "mathematics.h"
bool mathematics_equals(const double number1, const double number2) {
bool mathematics_equals(const float64_t number1, const float64_t number2) {
return (number1 - number2) < MATHEMATICS_DOUBLE_PRECISION;
}
unsigned long long mathematics_absolute_value(const long long number) {
uint64_t mathematics_absolute_value(const int64_t number) {
if (number >= 0) {
return number;
}
return -number;
}
unsigned long long mathematics_pow(unsigned long long base, unsigned long long exponent) {
uint64_t mathematics_pow(uint64_t base, uint64_t exponent) {
return exponent == 0 ? 1 : base * mathematics_pow(base, exponent - 1);
}
double mathematics_root(double number, unsigned int nth_root) {
double result = number;
double previous_result = 0;
float64_t mathematics_root(float64_t number, uint64_t nth_root) {
float64_t result = number;
float64_t previous_result = 0;
while (!mathematics_equals(result, previous_result)) {
result = (((nth_root - 1) * previous_result) + (number / mathematics_pow(result, nth_root - 1))) / nth_root;
previous_result = result;
@ -25,11 +25,11 @@ double mathematics_root(double number, unsigned int nth_root) {
return result;
}
double mathematics_square_root(double number) {
float64_t mathematics_square_root(float64_t number) {
return mathematics_root(number, 2);
}
unsigned long long mathematics_factorial(unsigned long long number) {
uint64_t mathematics_factorial(uint64_t number) {
return number == 0 ? 1 : number * mathematics_factorial(number - 1);
}

View File

@ -18,54 +18,54 @@
* @return false
* @since v1.0.0
*/
bool mathematics_equals(const double number1, const double number2);
bool mathematics_equals(const float64_t number1, const float64_t number2);
/**
* @brief Get the absolute value of a number.
*
* @param number
* @return unsigned long long
* @return uint64_t
* @since v1.0.0
*/
unsigned long long mathematics_absolute_value(const long long number);
uint64_t mathematics_absolute_value(const int64_t number);
/**
* @brief Calculates the power of a number.
*
* @param base
* @param exponent
* @return unsigned long long
* @return uint64_t
* @since v1.0.0
*/
unsigned long long mathematics_pow(unsigned long long base, unsigned long long exponent);
uint64_t mathematics_pow(uint64_t base, uint64_t exponent);
/**
* @brief Calculates the nth root of a number.
*
* @param number
* @param nth_root
* @return double
* @return float64_t
* @since v1.0.0
*/
double mathematics_root(double number, unsigned int nth_root);
float64_t mathematics_root(float64_t number, uint64_t nth_root);
/**
* @brief Calculates the square root of a number using Heron's method.
*
* @param number
* @return double
* @return float64_t
* @since v1.0.0
*/
double mathematics_square_root(double number);
float64_t mathematics_square_root(float64_t number);
/**
* @brief Calculates the factorial of a number.
*
* @param number
* @return unsigned long long
* @return uint64_t
* @since v1.0.0
*/
unsigned long long mathematics_factorial(unsigned long long number);
uint64_t mathematics_factorial(uint64_t number);
/**
* @brief Calulcates the opposite number (additive inverse).

View File

@ -296,7 +296,7 @@ bool string_get_is_substring(const string_t string, const string_t substring) {
return is_substring;
}
string_t string_get_formatted_number(const long long number, string_t separator) {
string_t string_get_formatted_number(const int64_t number, string_t separator) {
string_t number_string_temp = convert_number_to_string(number);
string_t number_string = number_string_temp;
bool is_negative = number_string_temp[0] == '-';
@ -433,7 +433,7 @@ string_t string_pad_start(const string_t string, const string_t pad_string, size
}
string_t string_zero_pad(uint64_t number, size_t places) {
string_t number_string = convert_number_to_string((long long)number);
string_t number_string = convert_number_to_string((int64_t)number);
string_t pad_string = string_copy("0");
string_t result = string_pad_start(number_string, pad_string, places);
free(pad_string);

View File

@ -232,7 +232,7 @@ bool string_get_is_substring(const string_t string, const string_t substring);
* @endcode
* @since v1.0.0
*/
string_t string_get_formatted_number(const long long number, string_t separator);
string_t string_get_formatted_number(const int64_t number, string_t separator);
/**
* @brief Returns a pointer to the last occurrence of character in the string.

View File

@ -21,6 +21,66 @@ string_t terminal_input() {
return string;
}
void terminal_print_int(void* value) {
printf("%d", *(int*)value);
}
void terminal_print_bool(void* value) {
printf("%s", *(bool*)value ? "true" : "false");
}
void terminal_print_long(void* value) {
printf("%ld", *(long*)value);
}
void terminal_print_unsigned_long(void* value) {
printf("%lu", *(unsigned long*)value);
}
void terminal_print_float(void* value) {
printf("%f", *(float*)value);
}
void terminal_print_double(void* value) {
printf("%f", *(double*)value);
}
void terminal_print_int8_t(void* value) {
printf("%d", *(int8_t*)value);
}
void terminal_print_int16_t(void* value) {
printf("%d", *(int16_t*)value);
}
void terminal_print_int32_t(void* value) {
printf("%d", *(int32_t*)value);
}
void terminal_print_int64_t(void* value) {
printf("%ld", *(int64_t*)value);
}
void terminal_print_uint8_t(void* value) {
printf("%u", *(uint8_t*)value);
}
void terminal_print_uint16_t(void* value) {
printf("%u", *(uint16_t*)value);
}
void terminal_print_uint32_t(void* value) {
printf("%u", *(uint32_t*)value);
}
void terminal_print_uint64_t(void* value) {
printf("%lu", *(uint64_t*)value);
}
void terminal_print_char(void* value) {
printf("%c", *(string_t)value);
}
void terminal_print_array(void* array, size_t array_size, size_t element_size, void (*print_element)(void*)) {
printf("[");
for (size_t index = 0; index < array_size; index++) {
@ -34,22 +94,6 @@ void terminal_print_array(void* array, size_t array_size, size_t element_size, v
printf("]\n");
}
void terminal_print_int(void* value) {
printf("%d", *(int*)value);
}
void terminal_print_long(void* value) {
printf("%ld", *(long*)value);
}
void terminal_print_unsigned_long(void* value) {
printf("%lu", *(unsigned long*)value);
}
void terminal_print_char(void* value) {
printf("%c", *(string_t)value);
}
void terminal_print_string(void* value) {
printf("%s", (string_t)value);
}
@ -64,7 +108,7 @@ void terminal_print_stack(struct stack* stack, void (*print_element)(void*)) {
while (node_current != NULL) {
printf("|\t");
void* element = node_current->data;
print_element(&element);
print_element(element);
node_current = node_current->next;
printf("\t|\n");
}
@ -80,7 +124,7 @@ void terminal_print_queue(struct queue* queue, void (*print_element)(void*)) {
while (node_current != NULL) {
printf("|\t");
void* element = node_current->data;
print_element(&element);
print_element(element);
node_current = node_current->next;
printf("\t|\n");
}
@ -96,7 +140,7 @@ void terminal_print_linked_list(struct linked_list* linked_list, void (*print_el
while (node_current != NULL) {
void* element = (string_t)node_current->data;
node_current = node_current->next;
print_element(&element);
print_element(element);
printf(" -> ");
}
printf("NULL\n");
@ -116,12 +160,21 @@ void terminal_print_hash_map(struct hash_map* hash_map, void (*print_element)(vo
printf("\t\"");
terminal_print_string(key);
printf("\" -> ");
print_element(&value);
print_element(value);
printf("\n");
}
printf("}\n");
free(keys);
}
void terminal_print_array_list(struct array_list* list, void (*print_element)(void*)) {
terminal_print_array(list->data, list->size, sizeof(void*), print_element);
printf("[");
for (size_t index = 0; index < list->size; index++) {
void* element = list->data[index];
print_element(element);
if (index < list->size - 1) {
printf(", ");
}
}
printf("]\n");
}

View File

@ -23,17 +23,6 @@
*/
string_t terminal_input();
/**
* @brief Print an array.
*
* @param array
* @param array_size
* @param element_size
* @param print_element
* @since v1.0.0
*/
void terminal_print_array(void* array, size_t array_size, size_t element_size, void (*print_element)(void*));
/**
* @brief Print a int.
*
@ -42,6 +31,14 @@ void terminal_print_array(void* array, size_t array_size, size_t element_size, v
*/
void terminal_print_int(void* value);
/**
* @brief Print a boolean.
*
* @param value
* @since v5.0.0
*/
void terminal_print_bool(void* value);
/**
* @brief Print a long.
*
@ -58,6 +55,86 @@ void terminal_print_long(void* value);
*/
void terminal_print_unsigned_long(void* value);
/**
* @brief Print a float.
*
* @param value
* @since v5.0.0
*/
void terminal_print_float(void* value);
/**
* @brief Print a double.
*
* @param value
* @since v5.0.0
*/
void terminal_print_double(void* value);
/**
* @brief Print a int8_t.
*
* @param value
* @since v5.0.0
*/
void terminal_print_int8_t(void* value);
/**
* @brief Print a int16_t.
*
* @param value
* @since v5.0.0
*/
void terminal_print_int16_t(void* value);
/**
* @brief Print a int32_t.
*
* @param value
* @since v5.0.0
*/
void terminal_print_int32_t(void* value);
/**
* @brief Print a int64_t.
*
* @param value
* @since v5.0.0
*/
void terminal_print_int64_t(void* value);
/**
* @brief Print a uint8_t.
*
* @param value
* @since v5.0.0
*/
void terminal_print_uint8_t(void* value);
/**
* @brief Print a uint16_t.
*
* @param value
* @since v5.0.0
*/
void terminal_print_uint16_t(void* value);
/**
* @brief Print a uint32_t.
*
* @param value
* @since v5.0.0
*/
void terminal_print_uint32_t(void* value);
/**
* @brief Print a uint64_t.
*
* @param value
* @since v5.0.0
*/
void terminal_print_uint64_t(void* value);
/**
* @brief Print a char.
*
@ -66,6 +143,17 @@ void terminal_print_unsigned_long(void* value);
*/
void terminal_print_char(void* value);
/**
* @brief Print an array.
*
* @param array
* @param array_size
* @param element_size
* @param print_element
* @since v1.0.0
*/
void terminal_print_array(void* array, size_t array_size, size_t element_size, void (*print_element)(void*));
/**
* @brief Print a string.
*

View File

@ -7,4 +7,7 @@ typedef uint8_t byte_t;
typedef char* string_t;
typedef float float32_t;
typedef double float64_t;
#endif

119
main.c
View File

@ -4,8 +4,123 @@
#include "libcproject.h"
int main() {
int integer = 5;
terminal_print_int(&integer);
printf("\n");
bool boolean = true;
printf("terminal_print_bool: ");
terminal_print_bool(&boolean);
printf(", ");
boolean = false;
terminal_print_bool(&boolean);
printf("\n");
long integer_long = 5;
printf("terminal_print_long: ");
terminal_print_long(&integer_long);
printf("\n");
unsigned long integer_unsigned_long = 5;
printf("terminal_print_unsigned_long: ");
terminal_print_unsigned_long(&integer_unsigned_long);
printf("\n");
float floating_point = 5.5;
printf("terminal_print_float: ");
terminal_print_float(&floating_point);
printf("\n");
double floating_point_double = 5.5;
printf("terminal_print_double: ");
terminal_print_double(&floating_point_double);
printf("\n");
int8_t integer_8 = 5;
printf("terminal_print_int8_t: ");
terminal_print_int8_t(&integer_8);
printf("\n");
int16_t integer_16 = 5;
printf("terminal_print_int16_t: ");
terminal_print_int16_t(&integer_16);
printf("\n");
int32_t integer_32 = 5;
printf("terminal_print_int32_t: ");
terminal_print_int32_t(&integer_32);
printf("\n");
int64_t integer_64 = 5;
printf("terminal_print_int64_t: ");
terminal_print_int64_t(&integer_64);
printf("\n");
uint8_t integer_unsigned_8 = 5;
printf("terminal_print_uint8_t: ");
terminal_print_uint8_t(&integer_unsigned_8);
printf("\n");
uint16_t integer_unsigned_16 = 5;
printf("terminal_print_uint16_t: ");
terminal_print_uint16_t(&integer_unsigned_16);
printf("\n");
uint32_t integer_unsigned_32 = 5;
printf("terminal_print_uint32_t: ");
terminal_print_uint32_t(&integer_unsigned_32);
printf("\n");
uint64_t integer_unsigned_64 = 5;
printf("terminal_print_uint64_t: ");
terminal_print_uint64_t(&integer_unsigned_64);
printf("\n");
char character = 'c';
printf("terminal_print_char: ");
terminal_print_char(&character);
printf("\n");
int array[] = {1, 2, 3, 4, 5};
printf("terminal_print_array: ");
terminal_print_array(array, 5, sizeof(int), terminal_print_int);
string_t string = "Hello, world!";
printf("%s\n", string);
printf("string_length = %ld\n", string_get_length(string));
printf("terminal_print_string: ");
terminal_print_string(string);
printf("\n");
struct stack* stack = stack_initialization();
stack_push(stack, &integer);
printf("terminal_print_stack: ");
terminal_print_stack(stack, terminal_print_int);
stack_free(stack);
struct queue* queue = queue_initialization();
queue_push(queue, &integer);
printf("terminal_print_queue: ");
terminal_print_queue(queue, terminal_print_int);
queue_free(queue);
struct linked_list* linked_list = linked_list_initialization();
linked_list_add_after_last(linked_list, string);
linked_list_add_after_last(linked_list, string);
printf("terminal_print_linked_list: ");
terminal_print_linked_list(linked_list, terminal_print_string);
linked_list_free(linked_list);
struct hash_map* hash_map = hash_map_initialization();
hash_map_add(hash_map, "key", &integer);
printf("terminal_print_hash_map: ");
terminal_print_hash_map(hash_map, terminal_print_int);
hash_map_free(hash_map);
struct array_list* array_list = array_list_initialization();
array_list_add(array_list, &integer);
array_list_add(array_list, &integer);
printf("terminal_print_array_list: ");
terminal_print_array_list(array_list, terminal_print_int);
array_list_free(array_list);
return EXIT_SUCCESS;
}