Date: September 2024.
Libmx is a library of useful functions in C developed during Track C, the second stage of the Innovation Campus educational program.
The library includes:
Some functions of Libmx are my implementations of the standard C library functions.
mx_printchar | outputs a single character to the stdout |
mx_print_unicode | outputs ASCII and multibyte characters to the stdout |
mx_printstr | outputs a string of characters to the stdout |
mx_print_strarr | outputs an array of strings (must be NULL-terminated) to the stdout with a delimiter between the elements of the array |
mx_printint | outputs integer values to the stdout |
mx_pow | computes a number raised to the power of zero or a positive integer |
mx_sqrt | computes a non-negative square root (if it is natural) of a number |
mx_nbr_to_hex | converts a number to a hexadecimal string |
mx_hex_to_nbr | converts a hexadecimal string to a number |
mx_itoa | converts an integer to a string |
mx_foreach | applies some function to each element of an array of integers |
mx_binary_search | searches for a string in an array using the binary search algorithm |
mx_bubble_sort | sorts an array of strings in lexicographical order using the bubble sort algorithm |
mx_quicksort | sorts an array of strings by their lengths in ascending order using the quick sort algorithm (the middle element is pivot) |
mx_strlen | calculates the length of a string |
mx_swap_char | swaps the characters of a string using pointers |
mx_str_reverse | reverses a string using pointers |
mx_strdel | frees string memory and sets the string to NULL |
mx_del_strarr | deletes the contents of an array of strings (must be NULL-terminated), frees array memory and sets a pointer to NULL |
mx_get_char_index | finds the index of the first occurrence of a character in a string |
mx_strdup | duplicates a string |
mx_strndup | similar to mx_strdup, but duplicates at most a specified number of bytes |
mx_strcpy | copies a string to a buffer |
mx_strncpy | similar to mx_strcpy, but copies at most a specified number of bytes |
mx_strcmp | compares two strings |
mx_strcat | appends one string to the end of another |
mx_strstr | finds the first occurrence of a substring in a string |
mx_get_substr_index | finds the index of the first character of a substring |
mx_count_substr | counts the substrings in a string |
mx_count_words | counts words in a string (a word is a sequence of characters separated by a delimiter) |
mx_strnew | allocates memory for a string and initializes each character with '\0' |
mx_strtrim | takes a string and creates a new one from it without whitespace characters at the beginning or/and the end |
mx_del_extra_spaces | does the same as mx_strtrim and also separates words in the new string with exactly one space character |
mx_strsplit | converts a string into a NULL-terminated array of strings splitting the string at a delimiter |
mx_strjoin | concatenates two strings into a new single string |
mx_file_to_str | reads data from a file into a string |
mx_replace_substr | replaces all occurrences of a substring in a string with another substring |
mx_readline | reads a line from a file into a string until it reaches a delimiter character or EOF |
mx_memset | fills a memory area with a constant byte |
mx_memcpy | copies a specified number of bytes from one memory area to another |
mx_memccpy | similar to mx_memcpy, but stops when a specified character is found |
mx_memcmp | compares bytes of two memory areas |
mx_memchr | scans bytes of a memory area for the first instance of a character |
mx_memrchr | similar to mx_memchr, but searches in the opposite direction (from the end) |
mx_memmem | finds the start of the first occurrence of a substring in a memory area |
mx_memmove | copies a specified number of bytes from one memory area to another (memory areas may overlap) |
mx_realloc | changes the size of a memory block to a specified number of bytes |
// linked list node defined in libmx.h
typedef struct s_list {
void *data;
struct s_list *next;
} t_list;
mx_create_node | creates a new node of a linked list |
mx_push_front | inserts a new node at the beginning of a linked list |
mx_push_back | inserts a new node at the end of a linked list |
mx_pop_front | removes the first node of a linked list and frees the memory allocated for the node |
mx_pop_back | removes the last node of a linked list and frees the memory allocated for the node |
mx_list_size | calculates the number of nodes in a linked list |
mx_sort_list | sorts a list's contents in ascending order |
mx_isdigit | checks if a character is a digit |
mx_islower | checks if a character is lowercase |
mx_isupper | checks if a character is uppercase |
mx_isalpha* | checks if a character is alphabetic |
mx_isspace | checks if a character is whitespace |
mx_printerr* | outputs an error string to the stderr |
mx_strncmp | similar to mx_strcmp, but compares at most a specified number of bytes |
mx_atoi* | converts a string to an integer |
mx_clear_list* | removes all nodes of a linked list and frees the memory allocated for the nodes |
* extra functions that were added to Libmx during working on Pathfinder.
To compile the library, clone the repository, navigate to the project directory and build the project using make
command.
git clone https://github.com/VeronikaSukhonos/libmx
cd libmx
make
This will create the static library libmx.a
in the project directory.
To use the library, follow these steps:
1. Copy the header file libmx.h
into your project directory and include it in your source files:
#include "libmx.h"
2. Compile your project linking the library as follows:
clang -o your_project your_project.c -L/path/to/libmx -lmx
// linked list program
#include "libmx.h"
// additional functions to print a list and its size
void mx_print_list(t_list *head);
void mx_print_list_size(t_list *head);
// function to be passed as an argument to mx_sort_list
bool mx_cmp(void *a, void *b) {
return mx_strcmp(a, b) > 0;
}
int main(void) {
t_list *head = NULL;
// inserting new nodes in a list
mx_push_front(&head, "One");
mx_push_back(&head, "Two");
mx_push_back(&head, "Three");
mx_print_list(head);
// sorting the list in lexicographical order
head = mx_sort_list(head, &mx_cmp);
mx_print_list(head);
mx_print_list_size(head);
// removing the first node from the list
mx_pop_front(&head);
mx_print_list(head);
mx_print_list_size(head);
// removing all nodes from the list
mx_clear_list(&head);
mx_print_list(head);
mx_print_list_size(head);
}
void mx_print_list(t_list *head) {
t_list *ptr = head;
while (ptr != NULL) {
mx_printstr(ptr->data);
if (ptr->next != NULL)
mx_printstr(" -> ");
ptr = ptr->next;
}
mx_printstr("\n");
}
void mx_print_list_size(t_list *head) {
mx_printstr("List size is ");
mx_printint(mx_list_size(head));
mx_printstr("\n");
}
One -> Two -> Three
One -> Three -> Two
List size is 3
Three -> Two
List size is 2
List size is 0
The project is licensed under the terms of the MIT license. See the LICENSE file for details.