|
libft
|
A modular C library built from scratch during 42/Hive School.
π View on GitHub π View full documentation
This C library was developed during my training at 42/Hive School and has grown over time into a practical, reusable toolkit. It includes a wide range of utilities for string, array, 2Darray manipulation, memory management, data structures, math, and custom I/O β all written from scratch in C.
By reimplementing core functions like printf and qsort, I deepened my understanding of low-level programming, pointer logic, and performance optimization. Each function was added to solve real challenges I encountered, helping me develop a strong foundation in writing robust, readable, reusable and maintainable code.
Special care was given to error handling, code clarity, project structure and building documentation. The library is organized, well-documented, and designed for portability across UNIX-like systems.
In addition to replicating standard behaviors, Libft introduces custom tools not found in the C standard library β giving greater flexibility and control in real-world use cases. It promotes code reusability and can be easily integrated into new or existing C projects.
Libft is a foundational C library that offers a comprehensive toolkit for low-level programming. It emphasizes performance, reliability, and clarity, making it a solid base for building larger software systems.
π Memory-Safe Operations All functions are implemented with a strong focus on memory safety. This includes careful handling of buffer boundaries, null-terminated strings, and dynamic memory allocation to prevent common pitfalls like buffer overflows or memory leaks.
β οΈ Consistent Error Handling Functions gracefully handle edge cases and failure scenarios, such as null inputs or allocation errors. Return values are designed to be predictable and easily checkable, reducing the risk of undefined behavior.
π‘οΈ Defensive Design Every function is built to fail safely. This defensive approach ensures that developers can integrate Libft into their codebases without introducing instability or hidden bugs.
π Solid Documentation Each module is thoroughly documented using Doxygen, with clear descriptions, usage guidelines, and grouping by functionality. This makes the library easy to navigate, understand, and maintainβeven as it grows in complexity.
π§ͺ Tested and Modular Each component of the library is modular, making it easy to maintain, extend, or integrate into larger projects. The code follows clear conventions to support readability and maintainability.
π Educational and Practical Originally designed as an educational exercise, Libft serves both as a learning tool and a practical foundation for writing robust C programs without relying on the standard library in constrained environments.
ft_strcpy, ft_strdup, and ft_strcat ensure safe copying, avoiding buffer overflows or unintentional memory corruption.ft_strdup), gracefully handle errors such as memory allocation failures, returning NULL when necessary.ft_strjoin allow concatenation of strings while managing memory efficiently.ft_strtrim helps remove unwanted characters from both ends of a string, useful for sanitizing input.ft_split splits a string by a delimiter (usually a character) and returns an array of substrings. It's ideal for parsing inputs like CSV or space-separated values.ft_split_charset splits a string based on any set of characters from a given charset, providing a more flexible solution for splitting strings with multiple delimiters or special characters.ft_strcmp and ft_strncmp provide reliable ways to compare strings, supporting various use cases such as lexicographical sorting or equality checks.ft_strncpy and ft_strdup allow for secure string copying and duplication, handling edge cases like null-terminated strings or fixed-size buffers.Libft provides a set of functions for string-to-number and number-to-string conversions, allowing seamless manipulation and transformation between different data types.
ft_itoa: Converts an integer to a string representation.ft_itoa_base: Converts an integer to a string representation in any given base (binary, hexadecimal).ft_atoi: Converts a string to an integer, handling edge cases like leading whitespaces and signs.ft_atoll: Similar to ft_atoi but returns a long long value for larger integers.ft_utoa: Converts an unsigned integer to a string.These functions allow reliable conversion between different data types efficiently and are often used for parsing, formatting in many applications.
Libft provides a set of functions for efficiently working with arrays, including handling their sizes, copying, freeing, and printing.
ft_arraysize: Returns the number of elements in an array, useful for dynamic arrays where size is not explicitly tracked.ft_copy_strarray: Creates a duplicate of an array of strings, making it easy to clone or manipulate arrays without modifying the original.ft_free_array: Frees the memory occupied by an array, preventing memory leaks when dynamically allocated arrays are no longer needed.ft_putintarray: Prints an array of integers, useful for debugging or displaying arrays in a readable format.These functions streamline common array operations while ensuring memory safety and efficiency.
ft_printfThis is a custom implementation of the standard printf function, created as part of my Hive School curriculum. The goal of this project was to replicate the behavior of the printf function while gaining a deeper understanding of how it works.
s, d, x, c, etc.)printf libraryThis project helped me strengthen my skills in variadic functions, formatting string output, and dynamic memory allocation in C.
Libft provides essential functions for managing 2D arrays (matrices), enabling efficient creation, manipulation, and printing of matrix-like structures.
ft_create_matrix: Allocates memory for a 2D array (matrix) of integers, allowing for easy manipulation of grid-like data structures.ft_copy_matrix: Creates a copy of an existing matrix, useful for preserving original data when performing operations on copies.ft_free_matrix: Frees the memory allocated for a matrix, ensuring that there are no memory leaks.ft_print_matrix: Prints the contents of a matrix in a human-readable format, making it easier to debug or visualize the data.ft_strarr_to_matrix: Converts an array of strings into a matrix of integers, useful for parsing string-based data into a structured matrix format.These functions simplify the management of matrix-based data, enhancing both the readability and performance of matrix operations.
Libft provides a suite of memory management functions that offer essential tools for safe and efficient manipulation of memory. These utilities are designed to handle tasks such as memory allocation, copying, setting, and comparing, ensuring that the program handles memory safely and without errors.
ft_calloc, ft_realloc).ft_memcpy, ft_memmove, and ft_memset.ft_memcmp and ft_memchr.These functions are crucial for handling raw memory operations and are often used in lower-level system programming, embedded systems, and scenarios where performance and memory safety are key.
Libft provides a collection of Linked List functions that allow for easy management of dynamic data structures. These functions enable efficient manipulation of linked lists for various use cases, such as quick prototyping or managing non-contiguous memory.
ft_lstnew: Creates a new node with the given content.ft_lstdelone: Deletes a node and frees its memory.ft_lstclear: Removes all nodes in a list, freeing the entire structure.ft_lstadd_front: Inserts a node at the beginning of the list.ft_lstadd_back: Inserts a node at the end of the list.ft_lstdelete: Deletes a specific node from the list.ft_lstiter: Iterates over each node and applies a function to its content.ft_lstmap: Creates a new list by applying a function to each nodeβs content.ft_lstsize: Returns the total number of elements in the list.ft_lstlast: Retrieves the last node of the list.These functions offer a flexible approach to working with linked lists and can be easily adapted for complex tasks requiring dynamic data handling.
Libft provides a set of I/O helper functions designed to simplify reading and writing operations with file descriptors, while ensuring safe and efficient data handling.
get_next_line: Reads a line from a file descriptor, returning it as a string. This is useful for reading files line by line without loading the entire file into memory.ft_putstr_fd: Safely writes a string to a specified file descriptor, ensuring proper handling of different output streams.ft_putnbr_fd: Writes an integer to a file descriptor, converting it to a string for output.These functions streamline file input and output operations while ensuring safe memory usage and error handling, making them ideal for tasks that involve reading from or writing to files or standard output.
ft_qsort)A fully generic, optimized quicksort implementation for sorting any array type.
ft_swap** handles generic byte-wise swapping. Libft provides a set of mathematical and vector-related functions that are essential for performing geometric and mathematical operations, particularly in 2D/3D vector calculations.
deg_to_rad: Converts degrees to radians.rad_to_deg: Converts radians to degrees.wrap_angle: Normalizes an angle to the range [0, 2Ο).ft_manhattan_dist_point: Computes the Manhattan distance between two t_point structs (integer points).ft_manhattan_dist_dpoint: Computes the Manhattan distance between two t_dpoint structs (floating-point points).ft_euclidean_dist_point: Computes the Euclidean distance between two t_point structs (integer points).ft_euclidean_dist_dpoint: Computes the Euclidean distance between two t_dpoint structs (floating-point points).clamp: Clamps a value between a specified minimum and maximum.get_fractional_part: Returns the fractional part of a floating-point number.wrap_in_range: Wraps a value within a specified range.get_vector_length: Calculates the magnitude (length) of a vector.divide_vector_by_scalar: Divides a vector by a scalar.get_normalized_vector: Returns the unit vector (normalized vector) from a given vector.get_vector_angle: Returns the angle of a vector relative to the x-axis.get_vector_angle_between: Calculates the angle between two vectors.get_direction_vector: Returns the direction vector from one point to another.get_unit_direction_vector: Returns the unit direction vector from one point to another.get_dot_product: Computes the dot product of two vectors.These functions provide the foundation for performing efficient vector mathematics, distance calculations, and angle conversions, crucial for projects involving graphics, physics simulations, or geometry-related tasks.
ft_rand)ft_rand is a custom implementation of a random number generator (RNG) designed to provide efficient and deterministic random numbers. It uses the Xorshift32 algorithm for generating pseudo-random integers, based on the current system time.
min and max) for generating random numbers within a given interval.This function is useful for simulations, games, or any applications where you need randomized behavior without relying on external libraries or functions.
π οΈ To build the static library, in the libft folder run:
π§ͺ Example
π₯οΈ Compile your program with the library, main.c being in the same directory as libft folder:
βΉοΈ This links your code against libft.a, using libft.h for declarations.
This project is licensed under the [MIT License](LICENSE).
You are free to use, modify, and distribute this code for academic, personal, or professional purposes. Attribution is appreciated but not required.
If you have any questions, suggestions, or feedback, feel free to reach out:
You're also welcome to open an issue or leave a comment on the repository.