mirror of
https://github.com/theoludwig/libcproject.git
synced 2025-05-21 23:21:15 +02:00
Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
269b1f7451
|
|||
c6df05e634
|
|||
07e2f4db45
|
|||
b9ba3fbff4
|
|||
7ef38fa993
|
|||
f99e4941e4
|
|||
6505e3ba49
|
|||
f0716c2e12
|
|||
78fe9ff404
|
|||
e5190818c4
|
|||
b922fd9cd3
|
|||
72645da4b2
|
4
.github/workflows/ci.yml
vendored
4
.github/workflows/ci.yml
vendored
@ -15,10 +15,10 @@ jobs:
|
|||||||
- run: 'sudo apt update'
|
- run: 'sudo apt update'
|
||||||
|
|
||||||
- name: 'Install Build Tools'
|
- name: 'Install Build Tools'
|
||||||
run: 'sudo apt-get install --yes build-essential gcc make clang-format'
|
run: 'sudo apt install --yes build-essential gcc make clang-format'
|
||||||
|
|
||||||
- name: 'Install Documentation Tools'
|
- name: 'Install Documentation Tools'
|
||||||
run: 'sudo apt-get install --yes doxygen doxygen-gui doxygen-doc graphviz'
|
run: 'sudo apt install --yes doxygen doxygen-gui doxygen-doc graphviz'
|
||||||
|
|
||||||
- run: 'gcc --version'
|
- run: 'gcc --version'
|
||||||
|
|
||||||
|
4
.github/workflows/release.yml
vendored
4
.github/workflows/release.yml
vendored
@ -24,10 +24,10 @@ jobs:
|
|||||||
- run: 'sudo apt update'
|
- run: 'sudo apt update'
|
||||||
|
|
||||||
- name: 'Install Build Tools'
|
- name: 'Install Build Tools'
|
||||||
run: 'sudo apt-get install --yes build-essential gcc make clang-format'
|
run: 'sudo apt install --yes build-essential gcc make clang-format'
|
||||||
|
|
||||||
- name: 'Install Documentation Tools'
|
- name: 'Install Documentation Tools'
|
||||||
run: 'sudo apt-get install --yes doxygen doxygen-gui doxygen-doc graphviz'
|
run: 'sudo apt install --yes doxygen doxygen-gui doxygen-doc graphviz'
|
||||||
|
|
||||||
- run: 'make set_version'
|
- run: 'make set_version'
|
||||||
|
|
||||||
|
4
Makefile
4
Makefile
@ -32,13 +32,13 @@ build/test/%.o: test/%.c ${HEADER_FILES} | build/test
|
|||||||
.PHONY: run
|
.PHONY: run
|
||||||
run: ${LIB} ./main.c
|
run: ${LIB} ./main.c
|
||||||
mkdir --parents ./bin
|
mkdir --parents ./bin
|
||||||
${CC} ${CC_FLAGS} -o ${MAIN_EXECUTABLE} ./main.c ${LIB_CC_FLAGS}
|
${CC} ${CC_FLAGS} ${CC_SANITIZER_FLAGS} -o ${MAIN_EXECUTABLE} ./main.c ${LIB_CC_FLAGS}
|
||||||
./${MAIN_EXECUTABLE} ${ARGS}
|
./${MAIN_EXECUTABLE} ${ARGS}
|
||||||
|
|
||||||
.PHONY: set_version
|
.PHONY: set_version
|
||||||
set_version: ${LIB} ./set_version.c
|
set_version: ${LIB} ./set_version.c
|
||||||
mkdir --parents ./bin
|
mkdir --parents ./bin
|
||||||
${CC} ${CC_FLAGS} -o ${SET_VERSION_EXECUTABLE} ./set_version.c ${LIB_CC_FLAGS}
|
${CC} ${CC_FLAGS} ${CC_SANITIZER_FLAGS} -o ${SET_VERSION_EXECUTABLE} ./set_version.c ${LIB_CC_FLAGS}
|
||||||
|
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
test: ${LIB} $(addprefix build/, ${TEST_OBJECTS})
|
test: ${LIB} $(addprefix build/, ${TEST_OBJECTS})
|
||||||
|
10
README.md
10
README.md
@ -37,10 +37,10 @@ For example on GNU/Linux Ubuntu:
|
|||||||
|
|
||||||
```sh
|
```sh
|
||||||
# Install Build Tools
|
# Install Build Tools
|
||||||
sudo apt-get install build-essential gcc make clang-format
|
sudo apt install build-essential gcc make clang-format
|
||||||
|
|
||||||
# Install Documentation Tools
|
# Install Documentation Tools
|
||||||
sudo apt-get install doxygen doxygen-gui doxygen-doc graphviz
|
sudo apt install doxygen doxygen-gui doxygen-doc graphviz
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
@ -92,9 +92,9 @@ touch main.c
|
|||||||
#include "libcproject/libcproject.h"
|
#include "libcproject/libcproject.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
string_t string_value = "Hello, world!"; // `string_t` is a typedef from `libcproject`
|
string_t string = "Hello, world!"; // `string_t` is a typedef from `libcproject`
|
||||||
printf("%s\n", string_value);
|
printf("%s\n", string);
|
||||||
printf("string_length = %ld\n", string_get_length(string_value)); // `string_get_length` is a function from `libcproject`
|
printf("string_length = %ld\n", string_get_length(string)); // `string_get_length` is a function from `libcproject`
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#ifndef __LIBCPROJECT_ARRAY_LIST__
|
#ifndef __LIBCPROJECT_ARRAY_LIST__
|
||||||
#define __LIBCPROJECT_ARRAY_LIST__
|
#define __LIBCPROJECT_ARRAY_LIST__
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
@ -26,24 +27,36 @@ struct array_list* array_list_initialization();
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Adds an element to the end of the array list.
|
* @brief Adds an element to the end of the array list.
|
||||||
|
*
|
||||||
|
* @param list
|
||||||
|
* @param element
|
||||||
* @since v1.2.0
|
* @since v1.2.0
|
||||||
*/
|
*/
|
||||||
void array_list_add(struct array_list* list, void* element);
|
void array_list_add(struct array_list* list, void* element);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Removes an element from the array list.
|
* @brief Removes an element from the array list.
|
||||||
|
*
|
||||||
|
* @param list
|
||||||
|
* @param index
|
||||||
* @since v1.2.0
|
* @since v1.2.0
|
||||||
*/
|
*/
|
||||||
void array_list_remove(struct array_list* list, size_t index);
|
void array_list_remove(struct array_list* list, size_t index);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Gets an element from the array list.
|
* @brief Gets an element from the array list.
|
||||||
|
*
|
||||||
|
* @param list
|
||||||
|
* @param index
|
||||||
|
* @return void*
|
||||||
* @since v1.2.0
|
* @since v1.2.0
|
||||||
*/
|
*/
|
||||||
void* array_list_get(struct array_list* list, size_t index);
|
void* array_list_get(struct array_list* list, size_t index);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Frees the array list.
|
* @brief Frees the array list.
|
||||||
|
*
|
||||||
|
* @param list
|
||||||
* @since v3.0.0
|
* @since v3.0.0
|
||||||
*/
|
*/
|
||||||
void array_list_free(struct array_list* list);
|
void array_list_free(struct array_list* list);
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
#include "character.h"
|
#include "character.h"
|
||||||
|
|
||||||
void character_append(string_t string_value, const char character) {
|
void character_append(string_t string, const char character) {
|
||||||
size_t length = string_get_length(string_value);
|
size_t length = string_get_length(string);
|
||||||
character_append_at(string_value, character, length);
|
character_append_at(string, character, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
void character_append_at(string_t string_value, const char character, const size_t index) {
|
void character_append_at(string_t string, const char character, const size_t index) {
|
||||||
size_t length = string_get_length(string_value);
|
size_t length = string_get_length(string);
|
||||||
for (size_t index_string = length; index_string > index; index_string--) {
|
for (size_t index_string = length; index_string > index; index_string--) {
|
||||||
string_value[index_string] = string_value[index_string - 1];
|
string[index_string] = string[index_string - 1];
|
||||||
}
|
}
|
||||||
string_value[index] = character;
|
string[index] = character;
|
||||||
string_value[length + 1] = '\0';
|
string[length + 1] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
char character_to_upper(const char character) {
|
char character_to_upper(const char character) {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#ifndef __LIBCPROJECT_CHARACTER__
|
#ifndef __LIBCPROJECT_CHARACTER__
|
||||||
#define __LIBCPROJECT_CHARACTER__
|
#define __LIBCPROJECT_CHARACTER__
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
@ -11,21 +12,21 @@
|
|||||||
* @brief Append a character to a string, assuming string points to an array
|
* @brief Append a character to a string, assuming string points to an array
|
||||||
* with enough space.
|
* with enough space.
|
||||||
*
|
*
|
||||||
* @param string_value
|
* @param string
|
||||||
* @param character
|
* @param character
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
void character_append(string_t string_value, char character);
|
void character_append(string_t string, char character);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Append a character to a string at a specific index, assuming string points to an array with enough space.
|
* @brief Append a character to a string at a specific index, assuming string points to an array with enough space.
|
||||||
*
|
*
|
||||||
* @param string_value
|
* @param string
|
||||||
* @param character
|
* @param character
|
||||||
* @param index
|
* @param index
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
void character_append_at(string_t string_value, const char character, const size_t index);
|
void character_append_at(string_t string, const char character, const size_t index);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Converts the character to uppercase.
|
* @brief Converts the character to uppercase.
|
||||||
@ -46,7 +47,9 @@ char character_to_lower(const char character);
|
|||||||
/**
|
/**
|
||||||
* @brief Check if the character is a digit ('0', '1', '2', '3', '4', '5', '6', '7, '8' or '9').
|
* @brief Check if the character is a digit ('0', '1', '2', '3', '4', '5', '6', '7, '8' or '9').
|
||||||
*
|
*
|
||||||
* @return true if the character is a digit, false otherwise
|
* @param character
|
||||||
|
* @return true
|
||||||
|
* @return false
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
bool character_get_is_digit(const char character);
|
bool character_get_is_digit(const char character);
|
||||||
@ -56,6 +59,7 @@ bool character_get_is_digit(const char character);
|
|||||||
* Return 0 if the character is not a letter.
|
* Return 0 if the character is not a letter.
|
||||||
*
|
*
|
||||||
* @param character
|
* @param character
|
||||||
|
* @return unsigned char
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
unsigned char character_get_alphabet_position(const char character);
|
unsigned char character_get_alphabet_position(const char character);
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
string_t convert_character_to_string(const char character) {
|
string_t convert_character_to_string(const char character) {
|
||||||
string_t string = malloc(sizeof(char) * 2);
|
string_t string = malloc(sizeof(char) * 2);
|
||||||
if (string == NULL) {
|
if (string == NULL) {
|
||||||
|
perror("Error (convert_character_to_string)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
string[0] = character;
|
string[0] = character;
|
||||||
@ -18,12 +19,12 @@ char convert_digit_to_character(const char digit) {
|
|||||||
return digit + '0';
|
return digit + '0';
|
||||||
}
|
}
|
||||||
|
|
||||||
long long convert_string_to_number(const string_t string_value) {
|
long long convert_string_to_number(const string_t string) {
|
||||||
bool is_negative = string_value[0] == '-';
|
bool is_negative = string[0] == '-';
|
||||||
long long integer = 0;
|
long long integer = 0;
|
||||||
size_t length = string_get_length(string_value);
|
size_t length = string_get_length(string);
|
||||||
for (size_t index = is_negative ? 1 : 0; index < length; index++) {
|
for (size_t index = is_negative ? 1 : 0; index < length; index++) {
|
||||||
integer = integer * 10 + convert_character_to_digit(string_value[index]);
|
integer = integer * 10 + convert_character_to_digit(string[index]);
|
||||||
}
|
}
|
||||||
return is_negative ? integer * -1 : integer;
|
return is_negative ? integer * -1 : integer;
|
||||||
}
|
}
|
||||||
@ -42,26 +43,26 @@ string_t convert_number_to_string(const long long integer) {
|
|||||||
if (is_negative) {
|
if (is_negative) {
|
||||||
length++;
|
length++;
|
||||||
}
|
}
|
||||||
string_t string_value = malloc(sizeof(char) * length);
|
string_t string = malloc(sizeof(char) * length);
|
||||||
if (string_value == NULL) {
|
if (string == NULL) {
|
||||||
|
perror("Error (convert_number_to_string)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
current = mathematics_absolute_value(integer);
|
current = mathematics_absolute_value(integer);
|
||||||
size_t index = 0;
|
size_t index = 0;
|
||||||
while (current != 0) {
|
while (current != 0) {
|
||||||
string_value[index++] = convert_digit_to_character(current % 10);
|
string[index++] = convert_digit_to_character(current % 10);
|
||||||
current = current / 10;
|
current = current / 10;
|
||||||
}
|
}
|
||||||
if (is_negative) {
|
if (is_negative) {
|
||||||
string_value[index++] = '-';
|
string[index++] = '-';
|
||||||
}
|
}
|
||||||
string_value[index] = '\0';
|
string[index] = '\0';
|
||||||
char* result = string_reverse(string_value);
|
string_reverse(string);
|
||||||
free(string_value);
|
return string;
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
string_t convert_number_from_base_10_to_base(unsigned long long number, unsigned int base) {
|
string_t convert_number_from_base_10_to_base(unsigned long long number, unsigned long base) {
|
||||||
if (number == 0) {
|
if (number == 0) {
|
||||||
return "0";
|
return "0";
|
||||||
}
|
}
|
||||||
@ -87,10 +88,10 @@ string_t convert_number_from_base_10_to_base(unsigned long long number, unsigned
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int convert_number_from_base_to_base_10(string_t number, unsigned int base) {
|
unsigned long convert_number_from_base_to_base_10(string_t number, unsigned long base) {
|
||||||
int length = string_get_length(number);
|
size_t length = string_get_length(number);
|
||||||
int exponent = length - 1;
|
int exponent = length - 1;
|
||||||
int result = 0;
|
unsigned long result = 0;
|
||||||
int index = 0;
|
int index = 0;
|
||||||
while (exponent >= 0) {
|
while (exponent >= 0) {
|
||||||
int current_number = (int)(number[index] - '0');
|
int current_number = (int)(number[index] - '0');
|
||||||
@ -106,6 +107,6 @@ int convert_number_from_base_to_base_10(string_t number, unsigned int base) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
string_t convert_number_from_base_to_another(string_t number, int base_from, int base_target) {
|
string_t convert_number_from_base_to_another(string_t number, unsigned long base_from, unsigned long base_target) {
|
||||||
return convert_number_from_base_10_to_base(convert_number_from_base_to_base_10(number, base_from), base_target);
|
return convert_number_from_base_10_to_base(convert_number_from_base_to_base_10(number, base_from), base_target);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#ifndef __LIBCPROJECT_CONVERT__
|
#ifndef __LIBCPROJECT_CONVERT__
|
||||||
#define __LIBCPROJECT_CONVERT__
|
#define __LIBCPROJECT_CONVERT__
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
@ -14,6 +15,7 @@
|
|||||||
* @brief Convert a character to a string.
|
* @brief Convert a character to a string.
|
||||||
*
|
*
|
||||||
* @param character
|
* @param character
|
||||||
|
* @return string_t
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string_t convert_character_to_string(const char character);
|
string_t convert_character_to_string(const char character);
|
||||||
@ -22,6 +24,7 @@ string_t convert_character_to_string(const char character);
|
|||||||
* @brief Convert a character to a digit.
|
* @brief Convert a character to a digit.
|
||||||
*
|
*
|
||||||
* @param character
|
* @param character
|
||||||
|
* @return char
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
char convert_character_to_digit(const char character);
|
char convert_character_to_digit(const char character);
|
||||||
@ -30,6 +33,7 @@ char convert_character_to_digit(const char character);
|
|||||||
* @brief Convert a digit to a character.
|
* @brief Convert a digit to a character.
|
||||||
*
|
*
|
||||||
* @param digit
|
* @param digit
|
||||||
|
* @return char
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
char convert_digit_to_character(const char digit);
|
char convert_digit_to_character(const char digit);
|
||||||
@ -37,15 +41,17 @@ char convert_digit_to_character(const char digit);
|
|||||||
/**
|
/**
|
||||||
* @brief Convert a string to a number.
|
* @brief Convert a string to a number.
|
||||||
*
|
*
|
||||||
* @param string_value
|
* @param string
|
||||||
|
* @return long long
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
long long convert_string_to_number(const string_t string_value);
|
long long convert_string_to_number(const string_t string);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Convert a number to a string.
|
* @brief Convert a number to a string.
|
||||||
*
|
*
|
||||||
* @param integer
|
* @param integer
|
||||||
|
* @return string_t
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string_t convert_number_to_string(const long long integer);
|
string_t convert_number_to_string(const long long integer);
|
||||||
@ -55,18 +61,20 @@ string_t convert_number_to_string(const long long integer);
|
|||||||
*
|
*
|
||||||
* @param number
|
* @param number
|
||||||
* @param base
|
* @param base
|
||||||
|
* @return string_t
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string_t convert_number_from_base_10_to_base(unsigned long long number, unsigned int base);
|
string_t convert_number_from_base_10_to_base(unsigned long long number, unsigned long base);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Convert a number with a specific base to a number base 10.
|
* @brief Convert a number with a specific base to a number base 10.
|
||||||
*
|
*
|
||||||
* @param number
|
* @param number
|
||||||
* @param base
|
* @param base
|
||||||
|
* @return int
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
int convert_number_from_base_to_base_10(string_t number, unsigned int base);
|
unsigned long convert_number_from_base_to_base_10(string_t number, unsigned long base);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Convert a number with a specific base to a number of specific base.
|
* @brief Convert a number with a specific base to a number of specific base.
|
||||||
@ -74,8 +82,9 @@ int convert_number_from_base_to_base_10(string_t number, unsigned int base);
|
|||||||
* @param number
|
* @param number
|
||||||
* @param base_from
|
* @param base_from
|
||||||
* @param base_target
|
* @param base_target
|
||||||
|
* @return string_t
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string_t convert_number_from_base_to_another(string_t number, int base_from, int base_target);
|
string_t convert_number_from_base_to_another(string_t number, unsigned long base_from, unsigned long base_target);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -18,9 +18,10 @@
|
|||||||
*
|
*
|
||||||
* @param path
|
* @param path
|
||||||
* @param file_content
|
* @param file_content
|
||||||
* @param file_size
|
* @param file_size The size of the file that was read (mutated by the function).
|
||||||
* @retval -1 if the file does not exist or if there is an error.
|
* @retval -1 if the file does not exist or if there is an error.
|
||||||
* @retval 0 for success.
|
* @retval 0 for success.
|
||||||
|
* @return int
|
||||||
* @since v1.0.0
|
* @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, off_t *file_size);
|
||||||
@ -33,6 +34,7 @@ int filesystem_read(string_t path, byte_t **file_content, off_t *file_size);
|
|||||||
* @param file_size
|
* @param file_size
|
||||||
* @retval -1 if there is an error.
|
* @retval -1 if there is an error.
|
||||||
* @retval 0 for success.
|
* @retval 0 for success.
|
||||||
|
* @return int
|
||||||
* @since v1.0.0
|
* @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, off_t file_size);
|
||||||
@ -41,8 +43,8 @@ int filesystem_write(string_t path, byte_t *file_content, off_t file_size);
|
|||||||
* @brief Check if a path exists.
|
* @brief Check if a path exists.
|
||||||
*
|
*
|
||||||
* @param path
|
* @param path
|
||||||
* @retval true if the path exists.
|
* @return true
|
||||||
* @retval false if the path does not exist.
|
* @return false
|
||||||
* @since v3.1.0
|
* @since v3.1.0
|
||||||
*/
|
*/
|
||||||
bool filesystem_exists(string_t path);
|
bool filesystem_exists(string_t path);
|
||||||
@ -54,6 +56,7 @@ bool filesystem_exists(string_t path);
|
|||||||
* @return int
|
* @return int
|
||||||
* @retval -1 if there is an error.
|
* @retval -1 if there is an error.
|
||||||
* @retval 0 for success.
|
* @retval 0 for success.
|
||||||
|
* @return int
|
||||||
* @since v3.1.0
|
* @since v3.1.0
|
||||||
*/
|
*/
|
||||||
int filesystem_remove(string_t path);
|
int filesystem_remove(string_t path);
|
||||||
@ -64,6 +67,7 @@ int filesystem_remove(string_t path);
|
|||||||
* @param path
|
* @param path
|
||||||
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
|
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
|
||||||
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
|
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
|
||||||
|
* @return string_t
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string_t filesystem_get_mimetype(string_t path);
|
string_t filesystem_get_mimetype(string_t path);
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#ifndef __LIBCPROJECT_HASH_MAP__
|
#ifndef __LIBCPROJECT_HASH_MAP__
|
||||||
#define __LIBCPROJECT_HASH_MAP__
|
#define __LIBCPROJECT_HASH_MAP__
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -34,22 +35,27 @@ struct hash_map_item {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Hash function (using SipHash 1-3 algorithm).
|
* @brief Hash function (using SipHash 1-3 algorithm).
|
||||||
* @param key
|
|
||||||
* @param capacity
|
|
||||||
* @see https://en.wikipedia.org/wiki/SipHash
|
* @see https://en.wikipedia.org/wiki/SipHash
|
||||||
* @see https://github.com/veorq/SipHash
|
* @see https://github.com/veorq/SipHash
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
* @param capacity
|
||||||
|
* @return uint64_t
|
||||||
* @since v2.0.0
|
* @since v2.0.0
|
||||||
*/
|
*/
|
||||||
uint64_t hash(string_t key, size_t capacity);
|
uint64_t hash(string_t key, size_t capacity);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Hash map initialization.
|
* @brief Hash map initialization.
|
||||||
|
*
|
||||||
|
* @return struct hash_map*
|
||||||
* @since v2.0.0
|
* @since v2.0.0
|
||||||
*/
|
*/
|
||||||
struct hash_map *hash_map_initialization();
|
struct hash_map *hash_map_initialization();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Add an item to the hash map.
|
* @brief Add an item to the hash map.
|
||||||
|
*
|
||||||
* @param hash_map
|
* @param hash_map
|
||||||
* @param key
|
* @param key
|
||||||
* @param data
|
* @param data
|
||||||
@ -69,14 +75,18 @@ void hash_map_remove(struct hash_map *hash_map, string_t key);
|
|||||||
* @brief Get an item from the hash map.
|
* @brief Get an item from the hash map.
|
||||||
* @param hash_map
|
* @param hash_map
|
||||||
* @param key
|
* @param key
|
||||||
|
* @return void*
|
||||||
* @since v2.0.0
|
* @since v2.0.0
|
||||||
*/
|
*/
|
||||||
void *hash_map_get(struct hash_map *hash_map, string_t key);
|
void *hash_map_get(struct hash_map *hash_map, string_t key);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Check if the hash map contains a key.
|
* @brief Check if the hash map contains a key.
|
||||||
|
*
|
||||||
* @param hash_map
|
* @param hash_map
|
||||||
* @param key
|
* @param key
|
||||||
|
* @return true
|
||||||
|
* @return false
|
||||||
* @since v2.0.0
|
* @since v2.0.0
|
||||||
*/
|
*/
|
||||||
bool hash_map_contains_key(struct hash_map *hash_map, string_t key);
|
bool hash_map_contains_key(struct hash_map *hash_map, string_t key);
|
||||||
@ -85,12 +95,15 @@ bool hash_map_contains_key(struct hash_map *hash_map, string_t key);
|
|||||||
* @brief Get the hash map keys.
|
* @brief Get the hash map keys.
|
||||||
*
|
*
|
||||||
* @param hash_map
|
* @param hash_map
|
||||||
|
* @return string_t*
|
||||||
* @since v2.0.0
|
* @since v2.0.0
|
||||||
*/
|
*/
|
||||||
string_t *hash_map_get_keys(struct hash_map *hash_map);
|
string_t *hash_map_get_keys(struct hash_map *hash_map);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Frees the hash map.
|
* @brief Frees the hash map.
|
||||||
|
*
|
||||||
|
* @param hash_map
|
||||||
* @since v3.0.0
|
* @since v3.0.0
|
||||||
*/
|
*/
|
||||||
void hash_map_free(struct hash_map *hash_map);
|
void hash_map_free(struct hash_map *hash_map);
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
#include "linked_list.h"
|
#include "linked_list.h"
|
||||||
|
|
||||||
struct linked_list *linked_list_initialization() {
|
struct linked_list *linked_list_initialization() {
|
||||||
struct linked_list *list = malloc(sizeof(*list));
|
struct linked_list *list = malloc(sizeof(struct linked_list));
|
||||||
if (list == NULL) {
|
if (list == NULL) {
|
||||||
|
perror("Error (linked_list_initialization)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
list->head = NULL;
|
list->head = NULL;
|
||||||
@ -11,8 +12,14 @@ struct linked_list *linked_list_initialization() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct linked_list_node *linked_list_add_in_head(struct linked_list *list, void *new_data) {
|
struct linked_list_node *linked_list_add_in_head(struct linked_list *list, void *new_data) {
|
||||||
struct linked_list_node *node_new = malloc(sizeof(*node_new));
|
if (list == NULL) {
|
||||||
if (list == NULL || node_new == NULL) {
|
errno = EINVAL;
|
||||||
|
perror("Error (linked_list_add_in_head)");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
struct linked_list_node *node_new = malloc(sizeof(struct linked_list_node));
|
||||||
|
if (node_new == NULL) {
|
||||||
|
perror("Error (linked_list_add_in_head)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
node_new->data = new_data;
|
node_new->data = new_data;
|
||||||
@ -24,6 +31,8 @@ struct linked_list_node *linked_list_add_in_head(struct linked_list *list, void
|
|||||||
|
|
||||||
void linked_list_delete_in_head(struct linked_list *list) {
|
void linked_list_delete_in_head(struct linked_list *list) {
|
||||||
if (list == NULL) {
|
if (list == NULL) {
|
||||||
|
errno = EINVAL;
|
||||||
|
perror("Error (linked_list_delete_in_head)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
if (list->head != NULL) {
|
if (list->head != NULL) {
|
||||||
@ -35,11 +44,17 @@ void linked_list_delete_in_head(struct linked_list *list) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct linked_list_node *linked_list_add_after_last(struct linked_list *list, void *new_data) {
|
struct linked_list_node *linked_list_add_after_last(struct linked_list *list, void *new_data) {
|
||||||
|
if (list == NULL) {
|
||||||
|
errno = EINVAL;
|
||||||
|
perror("Error (linked_list_add_after_last)");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
if (list->head == NULL) {
|
if (list->head == NULL) {
|
||||||
return linked_list_add_in_head(list, new_data);
|
return linked_list_add_in_head(list, new_data);
|
||||||
}
|
}
|
||||||
struct linked_list_node *node_new = malloc(sizeof(*node_new));
|
struct linked_list_node *node_new = malloc(sizeof(struct linked_list_node));
|
||||||
if (list == NULL || node_new == NULL) {
|
if (node_new == NULL) {
|
||||||
|
perror("Error (linked_list_add_after_last)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
node_new->data = new_data;
|
node_new->data = new_data;
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#ifndef __LIBCPROJECT_LINKED_LIST__
|
#ifndef __LIBCPROJECT_LINKED_LIST__
|
||||||
#define __LIBCPROJECT_LINKED_LIST__
|
#define __LIBCPROJECT_LINKED_LIST__
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
@ -13,7 +14,6 @@
|
|||||||
*/
|
*/
|
||||||
struct linked_list {
|
struct linked_list {
|
||||||
struct linked_list_node *head;
|
struct linked_list_node *head;
|
||||||
|
|
||||||
size_t length;
|
size_t length;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -28,42 +28,61 @@ struct linked_list_node {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Linked list initialization.
|
* @brief Linked list initialization.
|
||||||
|
*
|
||||||
|
* @return struct linked_list*
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
struct linked_list *linked_list_initialization();
|
struct linked_list *linked_list_initialization();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Add a new node in the head of the linked list.
|
* @brief Add a new node in the head of the linked list.
|
||||||
|
*
|
||||||
|
* @param list
|
||||||
|
* @param new_value
|
||||||
|
* @return struct linked_list_node*
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
struct linked_list_node *linked_list_add_in_head(struct linked_list *list, void *new_value);
|
struct linked_list_node *linked_list_add_in_head(struct linked_list *list, void *new_value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Delete node in the head of the linked list.
|
* @brief Delete node in the head of the linked list.
|
||||||
|
*
|
||||||
|
* @param list
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
void linked_list_delete_in_head(struct linked_list *list);
|
void linked_list_delete_in_head(struct linked_list *list);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Add a new node in the tail of the linked list.
|
* @brief Add a new node in the tail of the linked list.
|
||||||
|
*
|
||||||
|
* @param list
|
||||||
|
* @param new_data
|
||||||
|
* @return struct linked_list_node*
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
struct linked_list_node *linked_list_add_after_last(struct linked_list *list, void *new_data);
|
struct linked_list_node *linked_list_add_after_last(struct linked_list *list, void *new_data);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Reverse the linked list by creating a new one.
|
* @brief Reverse the linked list by creating a new one.
|
||||||
|
*
|
||||||
|
* @param list
|
||||||
|
* @return struct linked_list*
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
struct linked_list *linked_list_reverse(struct linked_list *list);
|
struct linked_list *linked_list_reverse(struct linked_list *list);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Reverse the linked list by mutating it.
|
* @brief Reverse the linked list by mutating it.
|
||||||
|
*
|
||||||
|
* @param list
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
void linked_list_reverse_mutate(struct linked_list *list);
|
void linked_list_reverse_mutate(struct linked_list *list);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Frees the linked list.
|
* @brief Frees the linked list.
|
||||||
|
*
|
||||||
|
* @param list
|
||||||
* @since v3.0.0
|
* @since v3.0.0
|
||||||
*/
|
*/
|
||||||
void linked_list_free(struct linked_list *list);
|
void linked_list_free(struct linked_list *list);
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#define MATHEMATICS_FLOAT_PRECISION 0.00000001
|
#define MATHEMATICS_FLOAT_PRECISION 0.00000001
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
@ -12,6 +13,8 @@
|
|||||||
*
|
*
|
||||||
* @param number1
|
* @param number1
|
||||||
* @param number2
|
* @param number2
|
||||||
|
* @return true
|
||||||
|
* @return false
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
bool mathematics_equals(const float number1, const float number2);
|
bool mathematics_equals(const float number1, const float number2);
|
||||||
@ -20,6 +23,7 @@ bool mathematics_equals(const float number1, const float number2);
|
|||||||
* @brief Get the absolute value of a number.
|
* @brief Get the absolute value of a number.
|
||||||
*
|
*
|
||||||
* @param number
|
* @param number
|
||||||
|
* @return unsigned long long
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
unsigned long long mathematics_absolute_value(const long long number);
|
unsigned long long mathematics_absolute_value(const long long number);
|
||||||
@ -29,15 +33,17 @@ unsigned long long mathematics_absolute_value(const long long number);
|
|||||||
*
|
*
|
||||||
* @param base
|
* @param base
|
||||||
* @param exponent
|
* @param exponent
|
||||||
|
* @return unsigned long long
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
unsigned long long mathematics_pow(unsigned long long base, unsigned long long exponent);
|
unsigned long long mathematics_pow(unsigned long long base, unsigned long long exponent);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Calculates the nth root of a number using Heron's method.
|
* @brief Calculates the nth root of a number.
|
||||||
*
|
*
|
||||||
* @param number
|
* @param number
|
||||||
* @param nth_root
|
* @param nth_root
|
||||||
|
* @return float
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
float mathematics_root(float number, unsigned int nth_root);
|
float mathematics_root(float number, unsigned int nth_root);
|
||||||
@ -46,6 +52,7 @@ float mathematics_root(float number, unsigned int nth_root);
|
|||||||
* @brief Calculates the square root of a number using Heron's method.
|
* @brief Calculates the square root of a number using Heron's method.
|
||||||
*
|
*
|
||||||
* @param number
|
* @param number
|
||||||
|
* @return float
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
float mathematics_square_root(float number);
|
float mathematics_square_root(float number);
|
||||||
@ -54,6 +61,7 @@ float mathematics_square_root(float number);
|
|||||||
* @brief Calculates the factorial of a number.
|
* @brief Calculates the factorial of a number.
|
||||||
*
|
*
|
||||||
* @param number
|
* @param number
|
||||||
|
* @return unsigned long long
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
unsigned long long mathematics_factorial(unsigned long long number);
|
unsigned long long mathematics_factorial(unsigned long long number);
|
||||||
|
17
lib/queue.c
17
lib/queue.c
@ -1,8 +1,9 @@
|
|||||||
#include "queue.h"
|
#include "queue.h"
|
||||||
|
|
||||||
struct queue *queue_initialization() {
|
struct queue *queue_initialization() {
|
||||||
struct queue *queue = malloc(sizeof(*queue));
|
struct queue *queue = malloc(sizeof(struct queue));
|
||||||
if (queue == NULL) {
|
if (queue == NULL) {
|
||||||
|
perror("Error (queue_initialization)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
queue->first = NULL;
|
queue->first = NULL;
|
||||||
@ -11,8 +12,14 @@ struct queue *queue_initialization() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void queue_push(struct queue *queue, void *data) {
|
void queue_push(struct queue *queue, void *data) {
|
||||||
struct queue_node *node_new = malloc(sizeof(*node_new));
|
if (queue == NULL) {
|
||||||
if (queue == NULL || node_new == NULL) {
|
errno = EINVAL;
|
||||||
|
perror("Error (queue_push)");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
struct queue_node *node_new = malloc(sizeof(struct queue_node));
|
||||||
|
if (node_new == NULL) {
|
||||||
|
perror("Error (queue_push)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
node_new->data = data;
|
node_new->data = data;
|
||||||
@ -31,6 +38,8 @@ void queue_push(struct queue *queue, void *data) {
|
|||||||
|
|
||||||
void *queue_pop(struct queue *queue) {
|
void *queue_pop(struct queue *queue) {
|
||||||
if (queue == NULL) {
|
if (queue == NULL) {
|
||||||
|
errno = EINVAL;
|
||||||
|
perror("Error (queue_pop)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
struct queue_node *node = queue->first;
|
struct queue_node *node = queue->first;
|
||||||
@ -46,6 +55,8 @@ void *queue_pop(struct queue *queue) {
|
|||||||
|
|
||||||
void queue_free(struct queue *queue) {
|
void queue_free(struct queue *queue) {
|
||||||
if (queue == NULL) {
|
if (queue == NULL) {
|
||||||
|
errno = EINVAL;
|
||||||
|
perror("Error (queue_free)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
struct queue_node *node = queue->first;
|
struct queue_node *node = queue->first;
|
||||||
|
11
lib/queue.h
11
lib/queue.h
@ -1,6 +1,7 @@
|
|||||||
#ifndef __LIBCPROJECT_QUEUE__
|
#ifndef __LIBCPROJECT_QUEUE__
|
||||||
#define __LIBCPROJECT_QUEUE__
|
#define __LIBCPROJECT_QUEUE__
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
@ -26,24 +27,34 @@ struct queue_node {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Queue initialization.
|
* @brief Queue initialization.
|
||||||
|
*
|
||||||
|
* @return struct queue*
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
struct queue *queue_initialization();
|
struct queue *queue_initialization();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Push data to queue.
|
* @brief Push data to queue.
|
||||||
|
*
|
||||||
|
* @param queue
|
||||||
|
* @param data
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
void queue_push(struct queue *queue, void *data);
|
void queue_push(struct queue *queue, void *data);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Pop data from queue.
|
* @brief Pop data from queue.
|
||||||
|
*
|
||||||
|
* @param queue
|
||||||
|
* @return void*
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
void *queue_pop(struct queue *queue);
|
void *queue_pop(struct queue *queue);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Frees the queue.
|
* @brief Frees the queue.
|
||||||
|
*
|
||||||
|
* @param queue
|
||||||
* @since v3.0.0
|
* @since v3.0.0
|
||||||
*/
|
*/
|
||||||
void queue_free(struct queue *queue);
|
void queue_free(struct queue *queue);
|
||||||
|
17
lib/stack.c
17
lib/stack.c
@ -1,8 +1,9 @@
|
|||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
|
|
||||||
struct stack *stack_initialization() {
|
struct stack *stack_initialization() {
|
||||||
struct stack *stack = malloc(sizeof(*stack));
|
struct stack *stack = malloc(sizeof(struct stack));
|
||||||
if (stack == NULL) {
|
if (stack == NULL) {
|
||||||
|
perror("Error (stack_initialization)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
stack->first = NULL;
|
stack->first = NULL;
|
||||||
@ -11,8 +12,14 @@ struct stack *stack_initialization() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void stack_push(struct stack *stack, void *data) {
|
void stack_push(struct stack *stack, void *data) {
|
||||||
struct stack_node *node_new = malloc(sizeof(*node_new));
|
if (stack == NULL) {
|
||||||
if (stack == NULL || data == NULL) {
|
errno = EINVAL;
|
||||||
|
perror("Error (stack_push)");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
struct stack_node *node_new = malloc(sizeof(struct stack_node));
|
||||||
|
if (data == NULL) {
|
||||||
|
perror("Error (stack_push)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
node_new->data = data;
|
node_new->data = data;
|
||||||
@ -23,6 +30,8 @@ void stack_push(struct stack *stack, void *data) {
|
|||||||
|
|
||||||
void *stack_pop(struct stack *stack) {
|
void *stack_pop(struct stack *stack) {
|
||||||
if (stack == NULL) {
|
if (stack == NULL) {
|
||||||
|
errno = EINVAL;
|
||||||
|
perror("Error (stack_pop)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
struct stack_node *node = stack->first;
|
struct stack_node *node = stack->first;
|
||||||
@ -38,6 +47,8 @@ void *stack_pop(struct stack *stack) {
|
|||||||
|
|
||||||
void stack_free(struct stack *stack) {
|
void stack_free(struct stack *stack) {
|
||||||
if (stack == NULL) {
|
if (stack == NULL) {
|
||||||
|
errno = EINVAL;
|
||||||
|
perror("Error (stack_free)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
struct stack_node *node = stack->first;
|
struct stack_node *node = stack->first;
|
||||||
|
11
lib/stack.h
11
lib/stack.h
@ -1,6 +1,7 @@
|
|||||||
#ifndef __LIBCPROJECT_STACK__
|
#ifndef __LIBCPROJECT_STACK__
|
||||||
#define __LIBCPROJECT_STACK__
|
#define __LIBCPROJECT_STACK__
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
@ -26,24 +27,34 @@ struct stack_node {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Stack initialization.
|
* @brief Stack initialization.
|
||||||
|
*
|
||||||
|
* @return struct stack*
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
struct stack *stack_initialization();
|
struct stack *stack_initialization();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Push data to stack.
|
* @brief Push data to stack.
|
||||||
|
*
|
||||||
|
* @param stack
|
||||||
|
* @param data
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
void stack_push(struct stack *stack, void *data);
|
void stack_push(struct stack *stack, void *data);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Pop data from stack.
|
* @brief Pop data from stack.
|
||||||
|
*
|
||||||
|
* @param stack
|
||||||
|
* @return void*
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
void *stack_pop(struct stack *stack);
|
void *stack_pop(struct stack *stack);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Frees the stack.
|
* @brief Frees the stack.
|
||||||
|
*
|
||||||
|
* @param stack
|
||||||
* @since v3.0.0
|
* @since v3.0.0
|
||||||
*/
|
*/
|
||||||
void stack_free(struct stack *stack);
|
void stack_free(struct stack *stack);
|
||||||
|
263
lib/string.c
263
lib/string.c
@ -1,135 +1,110 @@
|
|||||||
#include "string.h"
|
#include "string.h"
|
||||||
|
|
||||||
size_t string_get_length(const string_t string_value) {
|
size_t string_get_length(const string_t string) {
|
||||||
size_t length = 0;
|
size_t length = 0;
|
||||||
while (string_value[length] != '\0') {
|
while (string[length] != '\0') {
|
||||||
length++;
|
length++;
|
||||||
}
|
}
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
string_t string_to_uppercase(string_t string_value) {
|
void string_to_uppercase(string_t string) {
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string);
|
||||||
string_t result = malloc(sizeof(char) * (string_length + 1));
|
|
||||||
if (result == NULL) {
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
for (size_t index = 0; index < string_length; index++) {
|
for (size_t index = 0; index < string_length; index++) {
|
||||||
result[index] = character_to_upper(string_value[index]);
|
string[index] = character_to_upper(string[index]);
|
||||||
}
|
}
|
||||||
result[string_length] = '\0';
|
string[string_length] = '\0';
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
string_t string_to_lowercase(string_t string_value) {
|
void string_to_lowercase(string_t string) {
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string);
|
||||||
string_t result = malloc(sizeof(char) * (string_length + 1));
|
|
||||||
if (result == NULL) {
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
for (size_t index = 0; index < string_length; index++) {
|
for (size_t index = 0; index < string_length; index++) {
|
||||||
result[index] = character_to_lower(string_value[index]);
|
string[index] = character_to_lower(string[index]);
|
||||||
}
|
}
|
||||||
result[string_length] = '\0';
|
string[string_length] = '\0';
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
string_t string_replace(string_t string_value, char search, char replace) {
|
void string_replace(string_t string, char search, char replace) {
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string);
|
||||||
string_t result = malloc(sizeof(char) * (string_length + 1));
|
|
||||||
if (result == NULL) {
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
for (size_t index = 0; index < string_length; index++) {
|
for (size_t index = 0; index < string_length; index++) {
|
||||||
bool is_search_value = search == string_value[index];
|
bool is_search_value = search == string[index];
|
||||||
if (is_search_value) {
|
if (is_search_value) {
|
||||||
result[index] = replace;
|
string[index] = replace;
|
||||||
} else {
|
} else {
|
||||||
result[index] = string_value[index];
|
string[index] = string[index];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result[string_length] = '\0';
|
string[string_length] = '\0';
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
string_t string_trim_start(string_t string_value, char character) {
|
void string_remove_character(string_t string, char search) {
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string);
|
||||||
string_t result = malloc(sizeof(char) * (string_length + 1));
|
for (size_t index = 0; index < string_length; index++) {
|
||||||
if (result == NULL) {
|
if (string[index] == search) {
|
||||||
exit(EXIT_FAILURE);
|
for (size_t index_string = index; index_string < string_length; index_string++) {
|
||||||
|
string[index_string] = string[index_string + 1];
|
||||||
}
|
}
|
||||||
|
string_length--;
|
||||||
|
index--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
string[string_length] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
void string_trim_start(string_t string, char character) {
|
||||||
|
size_t string_length = string_get_length(string);
|
||||||
size_t index_space = 0;
|
size_t index_space = 0;
|
||||||
while (string_value[index_space] == character) {
|
while (string[index_space] == character) {
|
||||||
index_space++;
|
index_space++;
|
||||||
}
|
}
|
||||||
for (size_t index = index_space; index < string_length; index++) {
|
for (size_t index = 0; index < string_length - index_space; index++) {
|
||||||
result[index - index_space] = string_value[index];
|
string[index] = string[index + index_space];
|
||||||
}
|
}
|
||||||
result[string_length - index_space] = '\0';
|
string[string_length - index_space] = '\0';
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
string_t string_trim_end(string_t string_value, char character) {
|
void string_trim_end(string_t string, char character) {
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string);
|
||||||
string_t result = malloc(sizeof(char) * (string_length + 1));
|
|
||||||
if (result == NULL) {
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
size_t index_space = string_length - 1;
|
size_t index_space = string_length - 1;
|
||||||
while (string_value[index_space] == character) {
|
while (string[index_space] == character) {
|
||||||
index_space--;
|
index_space--;
|
||||||
}
|
}
|
||||||
for (size_t index = 0; index < index_space + 1; index++) {
|
string[index_space + 1] = '\0';
|
||||||
result[index] = string_value[index];
|
|
||||||
}
|
|
||||||
result[index_space + 1] = '\0';
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
string_t string_trim(string_t string_value, char character) {
|
void string_trim(string_t string, char character) {
|
||||||
string_t result_start = string_trim_start(string_value, character);
|
string_trim_start(string, character);
|
||||||
string_t result = string_trim_end(result_start, character);
|
string_trim_end(string, character);
|
||||||
free(result_start);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
string_t string_copy(const string_t source) {
|
string_t string_copy(const string_t string) {
|
||||||
size_t source_length = string_get_length(source);
|
size_t source_length = string_get_length(string);
|
||||||
string_t copy = malloc(sizeof(char) * (source_length + 1));
|
string_t copy = malloc(sizeof(char) * (source_length + 1));
|
||||||
if (copy == NULL) {
|
if (copy == NULL) {
|
||||||
|
perror("Error (string_copy)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
size_t index;
|
size_t index;
|
||||||
for (index = 0; index < source_length; index++) {
|
for (index = 0; index < source_length; index++) {
|
||||||
copy[index] = source[index];
|
copy[index] = string[index];
|
||||||
}
|
}
|
||||||
copy[index] = '\0';
|
copy[index] = '\0';
|
||||||
return copy;
|
return copy;
|
||||||
}
|
}
|
||||||
|
|
||||||
string_t string_capitalize(string_t string_value) {
|
void string_capitalize(string_t string) {
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string);
|
||||||
string_t result = malloc(sizeof(char) * (string_length + 1));
|
if (string_length == 0) {
|
||||||
if (result == NULL) {
|
return;
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
for (size_t index = 0; index < string_length; index++) {
|
string[0] = character_to_upper(string[0]);
|
||||||
bool is_first_character = index == 0;
|
|
||||||
if (is_first_character) {
|
|
||||||
result[index] = character_to_upper(string_value[index]);
|
|
||||||
} else {
|
|
||||||
result[index] = string_value[index];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result[string_length] = '\0';
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t string_total_occurrences_of_character(string_t string_value, char character) {
|
size_t string_total_occurrences_of_character(string_t string, char character) {
|
||||||
size_t result = 0;
|
size_t result = 0;
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string);
|
||||||
for (size_t index = 0; index < string_length; index++) {
|
for (size_t index = 0; index < string_length; index++) {
|
||||||
char current_character = string_value[index];
|
char current_character = string[index];
|
||||||
if (current_character == character) {
|
if (current_character == character) {
|
||||||
result += 1;
|
result += 1;
|
||||||
}
|
}
|
||||||
@ -137,21 +112,17 @@ size_t string_total_occurrences_of_character(string_t string_value, char charact
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
string_t string_reverse(const string_t string_value) {
|
void string_reverse(const string_t string) {
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string);
|
||||||
size_t index = 0;
|
size_t index_start = 0;
|
||||||
string_t result = malloc(sizeof(char) * (string_length + 1));
|
size_t index_end = string_length - 1;
|
||||||
if (result == NULL) {
|
while (index_start < index_end) {
|
||||||
exit(EXIT_FAILURE);
|
char temporary = string[index_start];
|
||||||
|
string[index_start] = string[index_end];
|
||||||
|
string[index_end] = temporary;
|
||||||
|
index_start++;
|
||||||
|
index_end--;
|
||||||
}
|
}
|
||||||
size_t result_index = 0;
|
|
||||||
for (index = string_length - 1; index > 0; index--) {
|
|
||||||
result[result_index] = string_value[index];
|
|
||||||
result_index++;
|
|
||||||
}
|
|
||||||
result[result_index] = string_value[index];
|
|
||||||
result[string_length] = '\0';
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool string_equals(const string_t string1, const string_t string2) {
|
bool string_equals(const string_t string1, const string_t string2) {
|
||||||
@ -166,11 +137,11 @@ bool string_equals(const string_t string1, const string_t string2) {
|
|||||||
return is_equal;
|
return is_equal;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool string_get_is_integer(const string_t string_value) {
|
bool string_get_is_integer(const string_t string) {
|
||||||
size_t index = 0;
|
size_t index = 0;
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string);
|
||||||
bool is_integer = string_length >= 1;
|
bool is_integer = string_length >= 1;
|
||||||
if (is_integer && string_value[0] == '-') {
|
if (is_integer && string[0] == '-') {
|
||||||
if (string_length == 1) {
|
if (string_length == 1) {
|
||||||
is_integer = false;
|
is_integer = false;
|
||||||
} else {
|
} else {
|
||||||
@ -178,7 +149,7 @@ bool string_get_is_integer(const string_t string_value) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
while (index < string_length && is_integer) {
|
while (index < string_length && is_integer) {
|
||||||
if (!character_get_is_digit(string_value[index])) {
|
if (!character_get_is_digit(string[index])) {
|
||||||
is_integer = false;
|
is_integer = false;
|
||||||
}
|
}
|
||||||
index++;
|
index++;
|
||||||
@ -186,36 +157,38 @@ bool string_get_is_integer(const string_t string_value) {
|
|||||||
return is_integer;
|
return is_integer;
|
||||||
}
|
}
|
||||||
|
|
||||||
string_t* string_split(const string_t string_value, char separator, size_t* result_size) {
|
string_t* string_split(const string_t string, char separator, size_t* result_size) {
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string);
|
||||||
size_t index_string = 0;
|
size_t index_string = 0;
|
||||||
size_t index_current = 0;
|
size_t index_current = 0;
|
||||||
size_t index_result = 0;
|
size_t index_result = 0;
|
||||||
string_t current = malloc(sizeof(char) * (string_length + 1));
|
string_t current = malloc(sizeof(char) * (string_length + 1));
|
||||||
string_t* result = NULL;
|
string_t* result = NULL;
|
||||||
if (current == NULL) {
|
if (current == NULL) {
|
||||||
|
perror("Error (string_split)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
while (index_string < string_length) {
|
while (index_string < string_length) {
|
||||||
if (string_value[index_string] == separator) {
|
if (string[index_string] == separator) {
|
||||||
current[index_current] = '\0';
|
current[index_current] = '\0';
|
||||||
result = realloc(result, sizeof(string_t) * (index_result + 1));
|
result = realloc(result, sizeof(string_t) * (index_result + 1));
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
|
perror("Error (string_split)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
result[index_result] = string_copy(current);
|
result[index_result] = string_copy(current);
|
||||||
index_result++;
|
index_result++;
|
||||||
index_current = 0;
|
index_current = 0;
|
||||||
} else {
|
} else {
|
||||||
current[index_current] = string_value[index_string];
|
current[index_current] = string[index_string];
|
||||||
index_current++;
|
index_current++;
|
||||||
}
|
}
|
||||||
index_string++;
|
index_string++;
|
||||||
}
|
}
|
||||||
|
|
||||||
current[index_current] = '\0';
|
current[index_current] = '\0';
|
||||||
result = realloc(result, sizeof(string_t) * (index_result + 1));
|
result = realloc(result, sizeof(string_t) * (index_result + 1));
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
|
perror("Error (string_split)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
result[index_result] = string_copy(current);
|
result[index_result] = string_copy(current);
|
||||||
@ -230,8 +203,9 @@ string_t string_join(string_t* array, const char separator, size_t array_length)
|
|||||||
total_length += string_get_length(array[index_array]);
|
total_length += string_get_length(array[index_array]);
|
||||||
}
|
}
|
||||||
size_t string_length = total_length + (array_length - 1);
|
size_t string_length = total_length + (array_length - 1);
|
||||||
string_t string_value = malloc(sizeof(char) * (string_length + 1));
|
string_t string = malloc(sizeof(char) * (string_length + 1));
|
||||||
if (string_value == NULL) {
|
if (string == NULL) {
|
||||||
|
perror("Error (string_join)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
size_t current_index = 0;
|
size_t current_index = 0;
|
||||||
@ -239,48 +213,45 @@ string_t string_join(string_t* array, const char separator, size_t array_length)
|
|||||||
string_t substring = array[index_array];
|
string_t substring = array[index_array];
|
||||||
size_t substring_length = string_get_length(substring);
|
size_t substring_length = string_get_length(substring);
|
||||||
for (size_t index_substring = 0; index_substring < substring_length; index_substring++) {
|
for (size_t index_substring = 0; index_substring < substring_length; index_substring++) {
|
||||||
string_value[current_index] = substring[index_substring];
|
string[current_index] = substring[index_substring];
|
||||||
current_index++;
|
current_index++;
|
||||||
}
|
}
|
||||||
bool is_last_character = index_array == (array_length - 1);
|
bool is_last_character = index_array == (array_length - 1);
|
||||||
if (!is_last_character) {
|
if (!is_last_character) {
|
||||||
string_value[current_index] = separator;
|
string[current_index] = separator;
|
||||||
current_index++;
|
current_index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
string_value[string_length] = '\0';
|
string[string_length] = '\0';
|
||||||
return string_value;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
string_t string_concatenate(string_t string1, string_t string2) {
|
void string_concatenate(string_t* destination, string_t source) {
|
||||||
size_t string1_length = string_get_length(string1);
|
size_t destination_length = string_get_length(*destination);
|
||||||
size_t string2_length = string_get_length(string2);
|
size_t source_length = string_get_length(source);
|
||||||
size_t result_length = string1_length + string2_length;
|
size_t new_length = destination_length + source_length;
|
||||||
string_t result = malloc(sizeof(char) * (result_length + 1));
|
*destination = realloc(*destination, sizeof(char) * (new_length + 1));
|
||||||
if (result == NULL) {
|
if (*destination == NULL) {
|
||||||
|
perror("Error (string_concatenate)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
size_t index_string1 = 0;
|
size_t index_destination = destination_length;
|
||||||
for (; index_string1 < string1_length; index_string1++) {
|
for (size_t index_source = 0; index_source < source_length; index_source++) {
|
||||||
result[index_string1] = string1[index_string1];
|
(*destination)[index_destination++] = source[index_source];
|
||||||
}
|
}
|
||||||
for (size_t index_string2 = 0; index_string2 < string2_length; index_string2++) {
|
(*destination)[index_destination] = '\0';
|
||||||
result[index_string1 + index_string2] = string2[index_string2];
|
|
||||||
}
|
|
||||||
result[result_length] = '\0';
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool string_get_has_unique_characters(const string_t string_value) {
|
bool string_get_has_unique_characters(const string_t string) {
|
||||||
bool has_unique = true;
|
bool has_unique = true;
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string);
|
||||||
struct hash_map* characters_already_seen = hash_map_initialization();
|
struct hash_map* characters_already_seen = hash_map_initialization();
|
||||||
string_t* keys = malloc(sizeof(string_t) * string_length);
|
string_t* keys = malloc(sizeof(string_t) * string_length);
|
||||||
for (size_t index = 0; index < string_length; index++) {
|
for (size_t index = 0; index < string_length; index++) {
|
||||||
keys[index] = NULL;
|
keys[index] = NULL;
|
||||||
}
|
}
|
||||||
for (size_t index = 0; index < string_length && has_unique; index++) {
|
for (size_t index = 0; index < string_length && has_unique; index++) {
|
||||||
char character = string_value[index];
|
char character = string[index];
|
||||||
keys[index] = convert_character_to_string(character);
|
keys[index] = convert_character_to_string(character);
|
||||||
string_t key = keys[index];
|
string_t key = keys[index];
|
||||||
if (hash_map_contains_key(characters_already_seen, key)) {
|
if (hash_map_contains_key(characters_already_seen, key)) {
|
||||||
@ -299,24 +270,24 @@ bool string_get_has_unique_characters(const string_t string_value) {
|
|||||||
return has_unique;
|
return has_unique;
|
||||||
}
|
}
|
||||||
|
|
||||||
string_t string_substring(const string_t string_value, size_t index_start, size_t index_end) {
|
string_t string_substring(const string_t string, size_t index_start, size_t index_end) {
|
||||||
size_t substring_length = index_end - index_start + 1;
|
size_t substring_length = index_end - index_start + 1;
|
||||||
string_t result = malloc(sizeof(char) * (substring_length + 1));
|
string_t result = malloc(sizeof(char) * (substring_length + 1));
|
||||||
for (size_t index = 0; index < substring_length; index++) {
|
for (size_t index = 0; index < substring_length; index++) {
|
||||||
result[index] = string_value[index_start + index];
|
result[index] = string[index_start + index];
|
||||||
}
|
}
|
||||||
result[substring_length] = '\0';
|
result[substring_length] = '\0';
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool string_get_is_substring(const string_t string_value, const string_t substring) {
|
bool string_get_is_substring(const string_t string, const string_t substring) {
|
||||||
bool is_substring = false;
|
bool is_substring = false;
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string);
|
||||||
size_t substring_length = string_get_length(substring);
|
size_t substring_length = string_get_length(substring);
|
||||||
for (size_t index_string = 0; index_string < string_length && !is_substring; index_string++) {
|
for (size_t index_string = 0; index_string < string_length && !is_substring; index_string++) {
|
||||||
size_t index_substring = 0;
|
size_t index_substring = 0;
|
||||||
size_t index_considered = index_string;
|
size_t index_considered = index_string;
|
||||||
while (index_substring < substring_length && string_value[index_considered] == substring[index_substring]) {
|
while (index_substring < substring_length && string[index_considered] == substring[index_substring]) {
|
||||||
index_substring++;
|
index_substring++;
|
||||||
index_considered++;
|
index_considered++;
|
||||||
}
|
}
|
||||||
@ -337,6 +308,7 @@ string_t string_get_formatted_number(const long long number, string_t separator)
|
|||||||
size_t formatted_length = number_string_length + (number_string_length - 1) / 3;
|
size_t formatted_length = number_string_length + (number_string_length - 1) / 3;
|
||||||
string_t result = malloc(sizeof(char) * (formatted_length + 1));
|
string_t result = malloc(sizeof(char) * (formatted_length + 1));
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
|
perror("Error (string_get_formatted_number)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
size_t count = 0;
|
size_t count = 0;
|
||||||
@ -356,23 +328,21 @@ string_t string_get_formatted_number(const long long number, string_t separator)
|
|||||||
}
|
}
|
||||||
free(number_string);
|
free(number_string);
|
||||||
result[formatted_length] = '\0';
|
result[formatted_length] = '\0';
|
||||||
string_t new_result = string_reverse(result);
|
string_reverse(result);
|
||||||
free(result);
|
|
||||||
if (is_negative) {
|
if (is_negative) {
|
||||||
string_t dash = convert_character_to_string('-');
|
string_t negative_result = convert_character_to_string('-');
|
||||||
string_t negative_result = string_concatenate(dash, new_result);
|
string_concatenate(&negative_result, result);
|
||||||
free(new_result);
|
free(result);
|
||||||
free(dash);
|
|
||||||
return negative_result;
|
return negative_result;
|
||||||
}
|
}
|
||||||
return new_result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
string_t string_get_last_occurence_of_character(const string_t string_value, char character) {
|
string_t string_get_last_occurence_of_character(const string_t string, char character) {
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string);
|
||||||
size_t index_last_occurrence = SIZE_MAX;
|
size_t index_last_occurrence = SIZE_MAX;
|
||||||
for (size_t index = 0; index < string_length; index++) {
|
for (size_t index = 0; index < string_length; index++) {
|
||||||
if (string_value[index] == character) {
|
if (string[index] == character) {
|
||||||
index_last_occurrence = index;
|
index_last_occurrence = index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -381,33 +351,34 @@ string_t string_get_last_occurence_of_character(const string_t string_value, cha
|
|||||||
}
|
}
|
||||||
string_t result = malloc(sizeof(char) * (string_length - index_last_occurrence + 1));
|
string_t result = malloc(sizeof(char) * (string_length - index_last_occurrence + 1));
|
||||||
if (result == NULL) {
|
if (result == NULL) {
|
||||||
|
perror("Error (string_get_last_occurence_of_character)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
size_t index_result = 0;
|
size_t index_result = 0;
|
||||||
for (size_t index = index_last_occurrence; index < string_length; index++) {
|
for (size_t index = index_last_occurrence; index < string_length; index++) {
|
||||||
result[index_result++] = string_value[index];
|
result[index_result++] = string[index];
|
||||||
}
|
}
|
||||||
result[index_result] = '\0';
|
result[index_result] = '\0';
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool string_starts_with(const string_t string_value, const string_t prefix) {
|
bool string_starts_with(const string_t string, const string_t prefix) {
|
||||||
bool starts_with = true;
|
bool starts_with = true;
|
||||||
size_t prefix_length = string_get_length(prefix);
|
size_t prefix_length = string_get_length(prefix);
|
||||||
for (size_t index = 0; index < prefix_length && starts_with; index++) {
|
for (size_t index = 0; index < prefix_length && starts_with; index++) {
|
||||||
starts_with = string_value[index] == prefix[index];
|
starts_with = string[index] == prefix[index];
|
||||||
}
|
}
|
||||||
return starts_with;
|
return starts_with;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool string_ends_with(const string_t string_value, const string_t prefix) {
|
bool string_ends_with(const string_t string, const string_t prefix) {
|
||||||
bool ends_with = true;
|
bool ends_with = true;
|
||||||
size_t string_length = string_get_length(string_value);
|
size_t string_length = string_get_length(string);
|
||||||
size_t prefix_length = string_get_length(prefix);
|
size_t prefix_length = string_get_length(prefix);
|
||||||
size_t index_string = string_length - 1;
|
size_t index_string = string_length - 1;
|
||||||
size_t index_prefix = prefix_length - 1;
|
size_t index_prefix = prefix_length - 1;
|
||||||
while (index_prefix > 0 && ends_with) {
|
while (index_prefix > 0 && ends_with) {
|
||||||
ends_with = string_value[index_string] == prefix[index_prefix];
|
ends_with = string[index_string] == prefix[index_prefix];
|
||||||
index_string--;
|
index_string--;
|
||||||
index_prefix--;
|
index_prefix--;
|
||||||
}
|
}
|
||||||
|
140
lib/string.h
140
lib/string.h
@ -1,6 +1,7 @@
|
|||||||
#ifndef __LIBCPROJECT_STRING__
|
#ifndef __LIBCPROJECT_STRING__
|
||||||
#define __LIBCPROJECT_STRING__
|
#define __LIBCPROJECT_STRING__
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -13,122 +14,155 @@
|
|||||||
/**
|
/**
|
||||||
* @brief Return the length of a string (excluding '\0').
|
* @brief Return the length of a string (excluding '\0').
|
||||||
*
|
*
|
||||||
* @param string_value
|
* @param string
|
||||||
|
* @return size_t
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
size_t string_get_length(const string_t string_value);
|
size_t string_get_length(const string_t string);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Converts all the alphabetic characters in a string to uppercase.
|
* @brief Converts all the alphabetic characters in a string to uppercase.
|
||||||
*
|
*
|
||||||
* @param string_value
|
* NOTE: Mutates the string.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string_t string_to_uppercase(string_t string_value);
|
void string_to_uppercase(string_t string);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Converts all the alphabetic characters in a string to lowercase.
|
* @brief Converts all the alphabetic characters in a string to lowercase.
|
||||||
*
|
*
|
||||||
* @param string_value
|
* NOTE: Mutates the string.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string_t string_to_lowercase(string_t string_value);
|
void string_to_lowercase(string_t string);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Replace all the occurrences of search value into replace value in the string.
|
* @brief Replace all the occurrences of search value into replace value in the string.
|
||||||
*
|
*
|
||||||
* @param string_value
|
* NOTE: Mutates the string.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
* @param search A character search value.
|
* @param search A character search value.
|
||||||
* @param replace A character containing the text to replace for match.
|
* @param replace A character containing the text to replace for match.
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string_t string_replace(string_t string_value, char search, char replace);
|
void string_replace(string_t string, char search, char replace);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Removes all the occurrences of a character in a string.
|
||||||
|
*
|
||||||
|
* NOTE: Mutates the string.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
|
* @param search A character search value.
|
||||||
|
* @since v4.1.0
|
||||||
|
*/
|
||||||
|
void string_remove_character(string_t string, char search);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Removes all `character` from the start of a string.
|
* @brief Removes all `character` from the start of a string.
|
||||||
*
|
*
|
||||||
* @param string_value
|
* NOTE: Mutates the string.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string_t string_trim_start(string_t string_value, char character);
|
void string_trim_start(string_t string, char character);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Removes all `character` from the end of a string.
|
* @brief Removes all `character` from the end of a string.
|
||||||
*
|
*
|
||||||
* @param string_value
|
* NOTE: Mutates the string.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string_t string_trim_end(string_t string_value, char character);
|
void string_trim_end(string_t string, char character);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Removes all `character` from the start and end of a string.
|
* @brief Removes all `character` from the start and end of a string.
|
||||||
*
|
*
|
||||||
* @param string_value
|
* NOTE: Mutates the string.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string_t string_trim(string_t string_value, char character);
|
void string_trim(string_t string, char character);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Return the copy of a string.
|
* @brief Return the copy of a string.
|
||||||
*
|
*
|
||||||
* @param string_value
|
* @param string
|
||||||
|
* @return string_t
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string_t string_copy(const string_t string_value);
|
string_t string_copy(const string_t string);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Capitalizes the string.
|
* @brief Capitalizes the string.
|
||||||
*
|
*
|
||||||
* @param string_value
|
* NOTE: Mutates the string.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string_t string_capitalize(string_t string_value);
|
void string_capitalize(string_t string);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Returns the total number of occurrences of the given character in the string.
|
* @brief Returns the total number of occurrences of the given character in the string.
|
||||||
*
|
*
|
||||||
* @param string_value
|
* @param string
|
||||||
* @param character
|
* @param character
|
||||||
|
* @return size_t
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
size_t string_total_occurrences_of_character(string_t string_value, char character);
|
size_t string_total_occurrences_of_character(string_t string, char character);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Reverse the characters in an array.
|
* @brief Reverse the characters in a string.
|
||||||
*
|
*
|
||||||
* @param string_value
|
* NOTE: Mutates the string.
|
||||||
|
*
|
||||||
|
* @param string
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string_t string_reverse(const string_t string_value);
|
void string_reverse(const string_t string);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Check if two strings are equals.
|
* @brief Check if two strings are equals.
|
||||||
*
|
*
|
||||||
* @param string1
|
* @param string1
|
||||||
* @param string2
|
* @param string2
|
||||||
* @return true if the strings are equals, false otherwise.
|
* @return true if the strings are equals.
|
||||||
|
* @return false if the strings are not equals.
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
bool string_equals(const string_t string1, const string_t string2);
|
bool string_equals(const string_t string1, const string_t string2);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Check if the string is a integer.
|
* @brief Check if the string is an integer.
|
||||||
*
|
*
|
||||||
* @param string_value
|
* @param string
|
||||||
* @return true if the string is a integer, false otherwise.
|
* @return true if the string is an integer.
|
||||||
|
* @return false if the string is not an integer.
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
bool string_get_is_integer(const string_t string_value);
|
bool string_get_is_integer(const string_t string);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Split a string into substrings using the specified separator and return them as an array and update the pointer `result_size` to the resulting size of the created array.
|
* @brief Split a string into substrings using the specified separator and return them as an array and update the pointer `result_size` to the resulting size of the created array.
|
||||||
*
|
*
|
||||||
* @param string_value
|
* @param string
|
||||||
* @param separator
|
* @param separator
|
||||||
* @param result_size
|
* @param result_size
|
||||||
|
* @return string_t*
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string_t* string_split(const string_t string_value, char separator, size_t* result_size);
|
string_t* string_split(const string_t string, char separator, size_t* result_size);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Adds all the elements of an array into a string, separated by the specified separator string.
|
* @brief Adds all the elements of an array into a string, separated by the specified separator string.
|
||||||
@ -136,6 +170,7 @@ string_t* string_split(const string_t string_value, char separator, size_t* resu
|
|||||||
* @param array
|
* @param array
|
||||||
* @param separator
|
* @param separator
|
||||||
* @param array_length
|
* @param array_length
|
||||||
|
* @return string_t
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string_t string_join(string_t* array, const char separator, size_t array_length);
|
string_t string_join(string_t* array, const char separator, size_t array_length);
|
||||||
@ -143,17 +178,20 @@ string_t string_join(string_t* array, const char separator, size_t array_length)
|
|||||||
/**
|
/**
|
||||||
* @brief Concatenate two strings.
|
* @brief Concatenate two strings.
|
||||||
*
|
*
|
||||||
* @param string1
|
* NOTE: Mutates the string `destination`.
|
||||||
* @param string2
|
*
|
||||||
|
* @param destination
|
||||||
|
* @param source
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string_t string_concatenate(string_t string1, string_t string2);
|
void string_concatenate(string_t* destination, string_t source);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Check if a string contains only unique characters.
|
* @brief Check if a string contains only unique characters.
|
||||||
*
|
*
|
||||||
* @param string
|
* @param string
|
||||||
* @return true if string contains only unique characters, false otherwise.
|
* @return true if string contains only unique characters.
|
||||||
|
* @return false if string contains duplicate characters.
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
bool string_get_has_unique_characters(const string_t string);
|
bool string_get_has_unique_characters(const string_t string);
|
||||||
@ -161,63 +199,69 @@ bool string_get_has_unique_characters(const string_t string);
|
|||||||
/**
|
/**
|
||||||
* @brief Returns the part of the string between the start and end indexes (both included).
|
* @brief Returns the part of the string between the start and end indexes (both included).
|
||||||
*
|
*
|
||||||
* @param string_value
|
* @param string
|
||||||
* @param index_start
|
* @param index_start
|
||||||
* @param index_end
|
* @param index_end
|
||||||
|
* @return string_t
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string_t string_substring(const string_t string_value, size_t index_start, size_t index_end);
|
string_t string_substring(const string_t string, size_t index_start, size_t index_end);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Check if a string contains a substring.
|
* @brief Check if a string contains a substring.
|
||||||
*
|
*
|
||||||
* @param string_value
|
* @param string
|
||||||
* @param substring
|
* @param substring
|
||||||
* @return true if the string contains the substring, false otherwise.
|
* @return true if the string contains the substring.
|
||||||
|
* @return false if the string does not contain the substring.
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
bool string_get_is_substring(const string_t string_value, const string_t substring);
|
bool string_get_is_substring(const string_t string, const string_t substring);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Format a number to a string with specified separator.
|
* @brief Format a number to a string with specified separator.
|
||||||
*
|
*
|
||||||
* @param number
|
* @param number
|
||||||
* @param separator
|
* @param separator
|
||||||
* @since v1.0.0
|
* @return string_t
|
||||||
* @code
|
* @code
|
||||||
* string_get_formatted_number(1000, " ") // "1 000"
|
* string_get_formatted_number(1000, " ") // "1 000"
|
||||||
* string_get_formatted_number(1000, ",") // "1,000"
|
* string_get_formatted_number(1000, ",") // "1,000"
|
||||||
* @endcode
|
* @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 long long number, string_t separator);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Returns a pointer to the last occurrence of character in the string.
|
* @brief Returns a pointer to the last occurrence of character in the string.
|
||||||
*
|
*
|
||||||
* @param string_value
|
* @param string
|
||||||
* @param character
|
* @param character
|
||||||
|
* @return string_t
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string_t string_get_last_occurence_of_character(const string_t string_value, char character);
|
string_t string_get_last_occurence_of_character(const string_t string, char character);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Check if a string starts with a substring.
|
* @brief Check if a string starts with a substring.
|
||||||
*
|
*
|
||||||
* @param string_value
|
* @param string
|
||||||
* @param prefix
|
* @param prefix
|
||||||
* @return true if the string starts with the substring, false otherwise.
|
* @return true if the string starts with the substring.
|
||||||
|
* @return false if the string does not start with the substring.
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
bool string_starts_with(const string_t string_value, const string_t prefix);
|
bool string_starts_with(const string_t string, const string_t prefix);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Check if a string ends with a substring.
|
* @brief Check if a string ends with a substring.
|
||||||
*
|
*
|
||||||
* @param string_value
|
* @param string
|
||||||
* @param prefix
|
* @param prefix
|
||||||
* @return true if the string ends with the substring, false otherwise.
|
* @return true if the string ends with the substring.
|
||||||
|
* @return false if the string does not end with the substring.
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
bool string_ends_with(const string_t string_value, const string_t prefix);
|
bool string_ends_with(const string_t string, const string_t prefix);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -3,26 +3,28 @@
|
|||||||
string_t terminal_input() {
|
string_t terminal_input() {
|
||||||
char character;
|
char character;
|
||||||
size_t length = 1;
|
size_t length = 1;
|
||||||
string_t string_value = malloc(length * sizeof(char));
|
string_t string = malloc(length * sizeof(char));
|
||||||
if (string_value == NULL) {
|
if (string == NULL) {
|
||||||
|
perror("Error (terminal_input)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
*string_value = '\0';
|
*string = '\0';
|
||||||
while ((character = getchar()) != '\n' && character != EOF) {
|
while ((character = getchar()) != '\n' && character != EOF) {
|
||||||
length++;
|
length++;
|
||||||
string_value = realloc(string_value, length * sizeof(char));
|
string = realloc(string, length * sizeof(char));
|
||||||
if (string_value == NULL) {
|
if (string == NULL) {
|
||||||
|
perror("Error (terminal_input)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
character_append(string_value, character);
|
character_append(string, character);
|
||||||
}
|
}
|
||||||
return string_value;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
void terminal_print_array(void* array, size_t array_size, size_t element_size, void (*print_element)(void*)) {
|
void terminal_print_array(void* array, size_t array_size, size_t element_size, void (*print_element)(void*)) {
|
||||||
printf("[");
|
printf("[");
|
||||||
for (size_t index = 0; index < array_size; index++) {
|
for (size_t index = 0; index < array_size; index++) {
|
||||||
void* element = (char*)array + index * element_size;
|
void* element = (string_t)array + index * element_size;
|
||||||
print_element(element);
|
print_element(element);
|
||||||
bool is_last = index == array_size - 1;
|
bool is_last = index == array_size - 1;
|
||||||
if (!is_last) {
|
if (!is_last) {
|
||||||
@ -45,21 +47,23 @@ void terminal_print_unsigned_long(void* value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void terminal_print_char(void* value) {
|
void terminal_print_char(void* value) {
|
||||||
printf("%c", *(char*)value);
|
printf("%c", *(string_t)value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void terminal_print_string(void* value) {
|
void terminal_print_string(void* value) {
|
||||||
printf("%s", (char*)value);
|
printf("%s", (string_t)value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void terminal_print_stack(struct stack* stack, void (*print_element)(void*)) {
|
void terminal_print_stack(struct stack* stack, void (*print_element)(void*)) {
|
||||||
if (stack == NULL) {
|
if (stack == NULL) {
|
||||||
|
errno = EINVAL;
|
||||||
|
perror("Error (terminal_print_stack)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
struct stack_node* node_current = stack->first;
|
struct stack_node* node_current = stack->first;
|
||||||
while (node_current != NULL) {
|
while (node_current != NULL) {
|
||||||
printf("|\t");
|
printf("|\t");
|
||||||
void* element = (char*)node_current->data;
|
void* element = node_current->data;
|
||||||
print_element(&element);
|
print_element(&element);
|
||||||
node_current = node_current->next;
|
node_current = node_current->next;
|
||||||
printf("\t|\n");
|
printf("\t|\n");
|
||||||
@ -68,12 +72,14 @@ void terminal_print_stack(struct stack* stack, void (*print_element)(void*)) {
|
|||||||
|
|
||||||
void terminal_print_queue(struct queue* queue, void (*print_element)(void*)) {
|
void terminal_print_queue(struct queue* queue, void (*print_element)(void*)) {
|
||||||
if (queue == NULL) {
|
if (queue == NULL) {
|
||||||
|
errno = EINVAL;
|
||||||
|
perror("Error (terminal_print_queue)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
struct queue_node* node_current = queue->first;
|
struct queue_node* node_current = queue->first;
|
||||||
while (node_current != NULL) {
|
while (node_current != NULL) {
|
||||||
printf("|\t");
|
printf("|\t");
|
||||||
void* element = (char*)node_current->data;
|
void* element = node_current->data;
|
||||||
print_element(&element);
|
print_element(&element);
|
||||||
node_current = node_current->next;
|
node_current = node_current->next;
|
||||||
printf("\t|\n");
|
printf("\t|\n");
|
||||||
@ -82,11 +88,13 @@ void terminal_print_queue(struct queue* queue, void (*print_element)(void*)) {
|
|||||||
|
|
||||||
void terminal_print_linked_list(struct linked_list* linked_list, void (*print_element)(void*)) {
|
void terminal_print_linked_list(struct linked_list* linked_list, void (*print_element)(void*)) {
|
||||||
if (linked_list == NULL) {
|
if (linked_list == NULL) {
|
||||||
|
errno = EINVAL;
|
||||||
|
perror("Error (terminal_print_linked_list)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
struct linked_list_node* node_current = linked_list->head;
|
struct linked_list_node* node_current = linked_list->head;
|
||||||
while (node_current != NULL) {
|
while (node_current != NULL) {
|
||||||
void* element = (char*)node_current->data;
|
void* element = (string_t)node_current->data;
|
||||||
node_current = node_current->next;
|
node_current = node_current->next;
|
||||||
print_element(&element);
|
print_element(&element);
|
||||||
printf(" -> ");
|
printf(" -> ");
|
||||||
@ -96,6 +104,8 @@ void terminal_print_linked_list(struct linked_list* linked_list, void (*print_el
|
|||||||
|
|
||||||
void terminal_print_hash_map(struct hash_map* hash_map, void (*print_element)(void*)) {
|
void terminal_print_hash_map(struct hash_map* hash_map, void (*print_element)(void*)) {
|
||||||
if (hash_map == NULL) {
|
if (hash_map == NULL) {
|
||||||
|
errno = EINVAL;
|
||||||
|
perror("Error (terminal_print_hash_map)");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
printf("{\n");
|
printf("{\n");
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#ifndef __LIBCPROJECT_TERMINAL__
|
#ifndef __LIBCPROJECT_TERMINAL__
|
||||||
#define __LIBCPROJECT_TERMINAL__
|
#define __LIBCPROJECT_TERMINAL__
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -16,6 +17,8 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Read a line from stdin.
|
* @brief Read a line from stdin.
|
||||||
|
*
|
||||||
|
* @return string_t
|
||||||
* @since v1.0.0
|
* @since v1.0.0
|
||||||
*/
|
*/
|
||||||
string_t terminal_input();
|
string_t terminal_input();
|
||||||
@ -110,7 +113,7 @@ void terminal_print_hash_map(struct hash_map* hash_map, void (*print_element)(vo
|
|||||||
/**
|
/**
|
||||||
* @brief Print an array list.
|
* @brief Print an array list.
|
||||||
*
|
*
|
||||||
* @param array_list
|
* @param list
|
||||||
* @param print_element
|
* @param print_element
|
||||||
* @since v3.0.0
|
* @since v3.0.0
|
||||||
*/
|
*/
|
||||||
|
6
main.c
6
main.c
@ -4,8 +4,8 @@
|
|||||||
#include "libcproject.h"
|
#include "libcproject.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
string_t string_value = "Hello, world!";
|
string_t string = "Hello, world!";
|
||||||
printf("%s\n", string_value);
|
printf("%s\n", string);
|
||||||
printf("string_length = %ld\n", string_get_length(string_value));
|
printf("string_length = %ld\n", string_get_length(string));
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
@ -8,14 +8,15 @@ int main(int argc, string_t* argv) {
|
|||||||
fprintf(stderr, "Usage: %s <version>\n", argv[0]);
|
fprintf(stderr, "Usage: %s <version>\n", argv[0]);
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
string_t content = "#ifndef __LIBCPROJECT_VERSION__\n";
|
string_t content = string_copy("#ifndef __LIBCPROJECT_VERSION__\n");
|
||||||
content = string_concatenate(content, "#define __LIBCPROJECT_VERSION__ \"");
|
string_concatenate(&content, "#define __LIBCPROJECT_VERSION__ \"");
|
||||||
content = string_concatenate(content, argv[1]);
|
string_concatenate(&content, argv[1]);
|
||||||
content = string_concatenate(content, "\"\n\n");
|
string_concatenate(&content, "\"\n\n");
|
||||||
content = string_concatenate(content, "#endif\n");
|
string_concatenate(&content, "#endif\n");
|
||||||
int result = filesystem_write("./version.h", (byte_t*)content, string_get_length(content));
|
int result = filesystem_write("./version.h", (byte_t*)content, string_get_length(content));
|
||||||
if (result == -1) {
|
if (result == -1) {
|
||||||
fprintf(stderr, "Error: Could not write to file.\n");
|
fprintf(stderr, "Error: Could not write to file.\n");
|
||||||
|
perror("Error (set_version)");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
printf("Success: Version set to %s.\n", argv[1]);
|
printf("Success: Version set to %s.\n", argv[1]);
|
||||||
|
@ -10,7 +10,7 @@ void convert_test() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void convert_character_to_string_test() {
|
void convert_character_to_string_test() {
|
||||||
char* result = convert_character_to_string('a');
|
string_t result = convert_character_to_string('a');
|
||||||
assert(assert_string_equal(result, "a"));
|
assert(assert_string_equal(result, "a"));
|
||||||
free(result);
|
free(result);
|
||||||
|
|
||||||
@ -77,7 +77,7 @@ void convert_string_to_number_test() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void convert_number_to_string_test() {
|
void convert_number_to_string_test() {
|
||||||
char* result = convert_number_to_string(0);
|
string_t result = convert_number_to_string(0);
|
||||||
assert(assert_string_equal(result, "0"));
|
assert(assert_string_equal(result, "0"));
|
||||||
free(result);
|
free(result);
|
||||||
|
|
||||||
@ -143,7 +143,7 @@ void convert_number_to_string_test() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void convert_number_from_base_to_another_test() {
|
void convert_number_from_base_to_another_test() {
|
||||||
char* result = convert_number_from_base_to_another("15", 10, 16);
|
string_t result = convert_number_from_base_to_another("15", 10, 16);
|
||||||
assert(assert_string_equal(result, "F"));
|
assert(assert_string_equal(result, "F"));
|
||||||
free(result);
|
free(result);
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ void string_test() {
|
|||||||
string_to_uppercase_test();
|
string_to_uppercase_test();
|
||||||
string_to_lowercase_test();
|
string_to_lowercase_test();
|
||||||
string_replace_test();
|
string_replace_test();
|
||||||
|
string_remove_character_test();
|
||||||
string_trim_start_test();
|
string_trim_start_test();
|
||||||
string_trim_end_test();
|
string_trim_end_test();
|
||||||
string_trim_test();
|
string_trim_test();
|
||||||
@ -30,46 +31,54 @@ void string_get_length_test() {
|
|||||||
string_t string = "Hello World!";
|
string_t string = "Hello World!";
|
||||||
size_t string_length = string_get_length(string);
|
size_t string_length = string_get_length(string);
|
||||||
assert(string_length == 12);
|
assert(string_length == 12);
|
||||||
|
assert(strlen(string) == string_length);
|
||||||
}
|
}
|
||||||
|
|
||||||
void string_to_uppercase_test() {
|
void string_to_uppercase_test() {
|
||||||
string_t string = "heLlO world";
|
string_t string = string_copy("heLlO world");
|
||||||
string = string_to_uppercase(string);
|
string_to_uppercase(string);
|
||||||
assert(assert_string_equal(string, "HELLO WORLD"));
|
assert(assert_string_equal(string, "HELLO WORLD"));
|
||||||
free(string);
|
free(string);
|
||||||
}
|
}
|
||||||
|
|
||||||
void string_to_lowercase_test() {
|
void string_to_lowercase_test() {
|
||||||
string_t string = "HellO WoRLd";
|
string_t string = string_copy("HellO WoRLd");
|
||||||
string = string_to_lowercase(string);
|
string_to_lowercase(string);
|
||||||
assert(assert_string_equal(string, "hello world"));
|
assert(assert_string_equal(string, "hello world"));
|
||||||
free(string);
|
free(string);
|
||||||
}
|
}
|
||||||
|
|
||||||
void string_replace_test() {
|
void string_replace_test() {
|
||||||
string_t string = "hello world";
|
string_t string = string_copy("hello world");
|
||||||
string = string_replace(string, 'l', 'z');
|
string_replace(string, 'l', 'z');
|
||||||
assert(assert_string_equal(string, "hezzo worzd"));
|
assert(assert_string_equal(string, "hezzo worzd"));
|
||||||
free(string);
|
free(string);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void string_remove_character_test() {
|
||||||
|
string_t string = string_copy("hello world");
|
||||||
|
string_remove_character(string, 'l');
|
||||||
|
assert(assert_string_equal(string, "heo word"));
|
||||||
|
free(string);
|
||||||
|
}
|
||||||
|
|
||||||
void string_trim_start_test() {
|
void string_trim_start_test() {
|
||||||
string_t string = " hello world ";
|
string_t string = string_copy(" hello world ");
|
||||||
string = string_trim_start(string, ' ');
|
string_trim_start(string, ' ');
|
||||||
assert(assert_string_equal(string, "hello world "));
|
assert(assert_string_equal(string, "hello world "));
|
||||||
free(string);
|
free(string);
|
||||||
}
|
}
|
||||||
|
|
||||||
void string_trim_end_test() {
|
void string_trim_end_test() {
|
||||||
string_t string = " hello world ";
|
string_t string = string_copy(" hello world ");
|
||||||
string = string_trim_end(string, ' ');
|
string_trim_end(string, ' ');
|
||||||
assert(assert_string_equal(string, " hello world"));
|
assert(assert_string_equal(string, " hello world"));
|
||||||
free(string);
|
free(string);
|
||||||
}
|
}
|
||||||
|
|
||||||
void string_trim_test() {
|
void string_trim_test() {
|
||||||
string_t string = " hello world ";
|
string_t string = string_copy(" hello world ");
|
||||||
string = string_trim(string, ' ');
|
string_trim(string, ' ');
|
||||||
assert(assert_string_equal(string, "hello world"));
|
assert(assert_string_equal(string, "hello world"));
|
||||||
free(string);
|
free(string);
|
||||||
}
|
}
|
||||||
@ -86,8 +95,8 @@ void string_copy_test() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void string_capitalize_test() {
|
void string_capitalize_test() {
|
||||||
string_t string = "hello world";
|
string_t string = string_copy("hello world");
|
||||||
string = string_capitalize(string);
|
string_capitalize(string);
|
||||||
assert(assert_string_equal(string, "Hello world"));
|
assert(assert_string_equal(string, "Hello world"));
|
||||||
free(string);
|
free(string);
|
||||||
}
|
}
|
||||||
@ -98,8 +107,8 @@ void string_total_occurrences_of_character_test() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void string_reverse_test() {
|
void string_reverse_test() {
|
||||||
string_t string = "hello world";
|
string_t string = string_copy("hello world");
|
||||||
string = string_reverse(string);
|
string_reverse(string);
|
||||||
assert(assert_string_equal(string, "dlrow olleh"));
|
assert(assert_string_equal(string, "dlrow olleh"));
|
||||||
free(string);
|
free(string);
|
||||||
}
|
}
|
||||||
@ -157,12 +166,14 @@ void string_join_test() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void string_concatenate_test() {
|
void string_concatenate_test() {
|
||||||
char* result = string_concatenate("abc", "def");
|
string_t result = string_copy("abc");
|
||||||
|
string_concatenate(&result, "def");
|
||||||
assert(assert_string_equal(result, "abcdef"));
|
assert(assert_string_equal(result, "abcdef"));
|
||||||
free(result);
|
free(result);
|
||||||
|
|
||||||
result = string_concatenate("abc", " defghi");
|
result = string_copy("abcz");
|
||||||
assert(assert_string_equal(result, "abc defghi"));
|
string_concatenate(&result, " defghi");
|
||||||
|
assert(assert_string_equal(result, "abcz defghi"));
|
||||||
free(result);
|
free(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,7 +203,7 @@ void string_get_is_substring_test() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void string_get_formatted_number_test() {
|
void string_get_formatted_number_test() {
|
||||||
char* result = string_get_formatted_number(1000, " ");
|
string_t result = string_get_formatted_number(1000, " ");
|
||||||
assert(assert_string_equal(result, "1 000"));
|
assert(assert_string_equal(result, "1 000"));
|
||||||
free(result);
|
free(result);
|
||||||
|
|
||||||
@ -224,7 +235,7 @@ void string_get_formatted_number_test() {
|
|||||||
void string_get_last_occurence_of_character_test() {
|
void string_get_last_occurence_of_character_test() {
|
||||||
string_t string = "abcdef";
|
string_t string = "abcdef";
|
||||||
|
|
||||||
char* result = string_get_last_occurence_of_character(string, 'a');
|
string_t result = string_get_last_occurence_of_character(string, 'a');
|
||||||
assert(assert_string_equal(result, "abcdef"));
|
assert(assert_string_equal(result, "abcdef"));
|
||||||
free(result);
|
free(result);
|
||||||
|
|
||||||
|
@ -18,6 +18,8 @@ void string_to_lowercase_test();
|
|||||||
|
|
||||||
void string_replace_test();
|
void string_replace_test();
|
||||||
|
|
||||||
|
void string_remove_character_test();
|
||||||
|
|
||||||
void string_trim_start_test();
|
void string_trim_start_test();
|
||||||
|
|
||||||
void string_trim_end_test();
|
void string_trim_end_test();
|
||||||
|
Reference in New Issue
Block a user