Libft
Custom implementation of core libc functions with additional utility helpers.
Loading...
Searching...
No Matches
libft.h File Reference
#include <stdarg.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/types.h>
Include dependency graph for libft.h:

Go to the source code of this file.

Data Structures

union  u_const_cast
 Temporary pointer view used to reinterpret the same address with or without const qualification. More...
 
struct  s_buff
 Dynamic buffer structure for efficient string/data manipulation. More...
 
struct  s_string
 Dynamic string structure with NUL-terminated storage. More...
 
struct  s_node
 Doubly linked list node structure. More...
 
struct  s_btree_node
 Binary tree node structure. More...
 
struct  s_vector
 Dynamic array storing contiguous fixed-size items. More...
 
struct  s_key_value
 Key/value pair stored inside a hashmap bucket. More...
 
struct  s_hashmap
 Separate-chaining hash map keyed by NUL-terminated strings. More...
 

Macros

#define BUFFER_SIZE   128
 
#define VECTOR_INIT_CAP   16
 
#define HASHMAP_INIT_CAP   50
 Default number of buckets allocated when an empty map first grows.
 

Typedefs

typedef union u_const_cast t_const_cast
 Temporary pointer view used to reinterpret the same address with or without const qualification.
 
typedef struct s_buff t_buff
 
typedef struct s_string t_string
 
typedef struct s_node t_node
 
typedef t_nodet_list
 Type alias for a pointer to a list node (list head).
 
typedef struct s_btree_node t_btree_node
 
typedef struct s_vector t_vector
 
typedef struct s_key_value t_key_value
 
typedef struct s_hashmap t_hashmap
 

Functions

t_btree_nodebtree_new (void *data)
 Creates a new binary tree node.
 
void btree_set_left (t_btree_node *parent, t_btree_node *child)
 Sets the left child of a parent node.
 
void btree_set_right (t_btree_node *parent, t_btree_node *child)
 Sets the right child of a parent node.
 
t_btree_nodebtree_detach_left (t_btree_node *parent)
 Detaches and returns the left child of a parent node.
 
t_btree_nodebtree_detach_right (t_btree_node *parent)
 Detaches and returns the right child of a parent node.
 
void btree_free (t_btree_node **node, void(*data_free)(void *data))
 Recursively frees a node and its descendants.
 
bool buff_adjust (t_buff *buff)
 Shrinks buffer capacity to match its current length.
 
bool buff_append (t_buff *dst, const t_buff *src)
 Appends the content of a source buffer to the end of a buffer.
 
bool buff_append_format (t_buff *buff, const char *fstring,...)
 Appends formatted string to buffer using variadic arguments.
 
bool buff_append_n (t_buff *b, const char *str, long n)
 Appends n first bytes of a string to the end of the buffer.
 
bool buff_append_vformat (t_buff *buff, const char *fstring, va_list args)
 Appends formatted string to buffer using va_list.
 
bool buff_cmp (const t_buff *a, const t_buff *b)
 Compares two buffers data byte by byte.
 
bool buff_dup (t_buff *dst, const t_buff *src)
 Copies the content of a source buffer into an existing buffer.
 
bool buff_dup_n (t_buff *dst, const t_buff *src, size_t n)
 Copies up to n bytes from a source buffer into an existing buffer.
 
void buff_free (t_buff *buff)
 Frees the buffer's internal data.
 
void buff_free_void (void *buff)
 Frees a t_buff item through a generic void* callback signature.
 
ssize_t buff_get_index_c (const t_buff *buff, char c)
 Finds the index of the first occurrence of a character in the buffer.
 
ssize_t buff_get_index_s (const t_buff *buff, const char *s, ssize_t slen)
 Finds the first occurrence of a substring in the buffer.
 
char * buff_get_string (const t_buff *buff)
 Returns a newly allocated copy of the buffer content.
 
bool buff_grow (t_buff *buff, size_t target_len)
 Grows the buffer to accommodate the target length if necessary.
 
bool buff_init (t_buff *buff, size_t initial_cap, const char *str, long n)
 Initializes a buffer with the specified initial capacity.
 
bool buff_insert (t_buff *dst, size_t index, const t_buff *src)
 Inserts a source buffer in a buffer at the specified index.
 
bool buff_insert_n (t_buff *b, size_t index, const char *str, long n)
 Inserts n first bytes of a string in the buffer at specified index.
 
bool buff_prepend (t_buff *dst, const t_buff *src)
 Prepends a source buffer to the beginning of a buffer.
 
bool buff_prepend_n (t_buff *b, const char *str, long n)
 Prepends n first bytes of a string to the beginning of the buffer.
 
bool buff_read_all (t_buff *buff, int fd)
 Reads all available data from a file descriptor into buffer.
 
bool buff_read_until_c (t_buff *buff, int fd, char c)
 Reads from a file descriptor until a specific character is found.
 
bool buff_read_until_n (t_buff *buff, int fd, size_t n)
 Reads up to n bytes from a file descriptor into buffer.
 
bool buff_read_until_s (t_buff *buff, int fd, const char *s, ssize_t slen)
 Reads from a file descriptor until a specific substring is found.
 
void buff_rm_part (t_buff *buff, size_t i_start, ssize_t len)
 Removes a portion of the buffer starting at i_start.
 
int ft_isalnum (int c)
 Checks if a character is alphanumeric.
 
int ft_isalpha (int c)
 Checks if a character is alphabetic.
 
int ft_isascii (int c)
 Checks if a character is a valid ASCII character (0-127).
 
int ft_isdigit (int c)
 Checks if a character is a digit ('0'-'9').
 
bool ft_isincharset (char c, const char *charset)
 Checks if a character is present in a character set.
 
int ft_isprint (int c)
 Checks if a character is printable (32-126).
 
int ft_isspace (char c)
 Checks if a character is whitespace (9-13).
 
int ft_tolower (int c)
 Converts an uppercase letter to lowercase.
 
int ft_toupper (int c)
 Converts a lowercase letter to uppercase.
 
int ft_atoi (const char *str)
 Converts a string to an integer.
 
long ft_atol (const char *str)
 Converts a string to a long integer.
 
pid_t ft_atopid (const char *str)
 Converts a string to a pid_t.
 
size_t ft_atozu (const char *str)
 Converts a string to a size_t.
 
char * ft_itoa (int n)
 Converts an integer to a string.
 
char * ft_utoa (unsigned int n)
 Converts an unsigned integer to a string.
 
char * ft_zutoa (size_t n)
 Converts a size_t to a string.
 
char * ft_ltoa (long n)
 Converts a long integer to a string.
 
char * ft_pidtoa (pid_t n)
 Converts a pid_t to a string.
 
char * ft_ultoa_base (unsigned long n, const char *base)
 Converts an unsigned long to a string in a given base.
 
bool parse_int (char *s, int *out)
 Parses a string to an integer with overflow detection.
 
bool parse_long (char *s, long *out)
 Parses a string to a long with overflow detection.
 
void print_err (bool print_errno, const char *message)
 Prints "Error\n" to stderr with optional message and errno.
 
void fprint_err (bool print_errno, const char *safe, const char *fmt,...)
 Prints "Error\n" to stderr with safe prefix + formatted suffix.
 
bool hashmap_init (t_hashmap *map, size_t initial_cap, void(*del)(void *))
 Initializes an empty hash map.
 
void hashmap_free (t_hashmap *map)
 Frees a hash map and all of its contents.
 
void hashmap_clear (t_hashmap *map)
 Removes all key/value pairs from a hash map.
 
bool hashmap_put (t_hashmap *map, const char *key, void *value)
 Inserts a key/value pair, replacing any existing value for the key.
 
void * hashmap_get (const t_hashmap *map, const char *key)
 
const void * hashmap_get_const (const t_hashmap *map, const char *key)
 Retrieves the value associated with a key.
 
const t_key_value ** hashmap_get_all (const t_hashmap *map)
 Collects every key/value pair stored in the map.
 
bool hashmap_remove (t_hashmap *map, const char *key)
 Removes the pair associated with a key.
 
bool hashmap_contains (const t_hashmap *map, const char *key)
 Tests whether a key is present in the map.
 
t_key_valuekey_value_new (const char *key, void *value)
 Allocates a key/value pair.
 
void key_value_free (t_key_value **pair, void(*del)(void *))
 Frees a key/value pair and its contents.
 
t_nodenode_new (void *content, t_node *prev, t_node *next)
 Creates a new list node.
 
void node_free (t_node **node, void(*del_content)(void *))
 Frees a node and optionally its content.
 
bool list_add_end (t_list *list, void *new_content)
 Adds a new element at the end of the list.
 
bool list_add_start (t_list *list, void *new_content)
 Adds a new element at the start of the list.
 
size_t list_get_size (t_list list)
 Calculates the number of nodes in the list.
 
void * list_get_content (t_list list, bool(*select_function)(void *))
 Finds content in list matching a selection function.
 
void * list_get_content_n (t_list list, size_t index)
 Gets content at a specific index in the list.
 
void * list_get_content_last (t_list list)
 Gets the content of the last node in the list.
 
t_nodelist_get_node_n (t_list list, size_t index)
 Gets the node at a specific index in the list.
 
t_nodelist_get_node_last (t_list list)
 Gets the last node in the list.
 
void list_iter (t_list lst, void(*f)(void *))
 Applies a function to each element of the list.
 
t_list list_map (t_list list, void *(*f)(void *), void(*del)(void *))
 Creates a new list by applying a function to each element.
 
void list_rm (t_list *list, t_node *node, void(*del_content)(void *))
 Removes a specific node from the list.
 
void list_rm_all (t_list *list, void(*del_content)(void *))
 Removes all nodes from the list.
 
void * ft_calloc (size_t count, size_t size)
 Allocates and zeroes memory for an array.
 
bool ft_realloc (char **buff, size_t cap, size_t newcap)
 Reallocates a buffer to a new capacity.
 
long min (long a, long b)
 Returns the minimum of two long integers.
 
long max (long a, long b)
 Returns the maximum of two long integers.
 
size_t absolute (long nbr)
 Returns the absolute value of a long integer.
 
long power (int a, int b)
 Computes a raised to the power of b.
 
size_t modulo (long a, size_t b)
 Computes the modulo of a signed integer with an unsigned modulus.
 
int square_root_exact (int nb)
 Calculates the exact integer square root.
 
int square_root_rounded (int nb)
 Calculates the nearest integer square root.
 
void ft_bzero (void *s, size_t n)
 Sets n bytes of memory to zero.
 
void * ft_memchr (const void *s, int c, size_t n)
 Locates the first occurrence of a byte in memory.
 
int ft_memcmp (const void *s1, const void *s2, size_t n)
 Compares two memory areas byte by byte.
 
void * ft_memcpy (void *dst, const void *src, size_t n)
 Copies n bytes from src to dst.
 
void * ft_memmove (void *dst, const void *src, size_t len)
 Copies n bytes from src to dst, handling overlapping memory.
 
void * ft_memset (void *b, int c, size_t len)
 Fills memory with a constant byte.
 
int ft_vdprintf (int fd, const char *fstring, va_list args)
 Writes formatted output to a file descriptor using va_list.
 
int ft_dprintf (int fd, const char *fstring,...)
 Writes formatted output to a file descriptor.
 
int ft_vprintf (const char *fstring, va_list args)
 Writes formatted output to stdout using va_list.
 
int ft_printf (const char *fstring,...)
 Writes formatted output to stdout.
 
void ft_putchar_fd (char c, int fd)
 Writes a character to a file descriptor.
 
void ft_putendl_fd (char *s, int fd)
 Writes a string followed by newline to a file descriptor.
 
void ft_putnbr_fd (int n, int fd)
 Writes an integer to a file descriptor.
 
void ft_putstr_fd (char *s, int fd)
 Writes a string to a file descriptor.
 
void str_array_free (char ***tab_ptr)
 Frees all strings in a null-terminated array and the tab itself.
 
char * str_chr (const char *s, int c)
 Locates the first occurrence of a character in a string.
 
int str_cmp (const char *s1, const char *s2)
 Compares two strings.
 
size_t str_count_words (char const *s, char sep)
 Counts words in a string separated by a delimiter.
 
char * str_dup (const char *s1)
 Duplicates a string.
 
void str_iteri (char *s, void(*f)(unsigned int, char *))
 Applies a function to each character of a string with its index.
 
char * str_join (char const *s1, char const *s2)
 Concatenates two strings into a new string.
 
size_t str_lcat (char *dst, const char *src, size_t dstsize)
 Appends src to dst with size limit.
 
size_t str_lcpy (char *dst, const char *src, size_t dstsize)
 Copies src to dst with size limit.
 
size_t str_len (const char *s)
 Calculates the length of a string.
 
char * str_mapi (char const *s, char(*f)(unsigned int, char))
 Creates a new string by applying a function to each character.
 
int str_ncmp (const char *s1, const char *s2, size_t n)
 Compares at most n characters of two strings.
 
char * str_ndup (const char *src, size_t len)
 Duplicates the first len bytes of a string.
 
char * str_nstr (const char *haystack, const char *needle, size_t len)
 Locates a substring within a string, limited by length.
 
char * str_rchr (const char *s, int c)
 Locates the last occurrence of a character in a string.
 
char ** str_split (char const *s, char c)
 Splits a string into an array of strings using a delimiter.
 
char * str_sub (char const *s, unsigned int start, size_t len)
 Extracts a substring from a string.
 
char * str_trim (char const *s1, char const *set)
 Trims characters from the beginning and end of a string.
 
char * str_trim_leading (char const *s1, char const *set)
 
bool string_adjust (t_string *string)
 Shrinks string capacity to match its current length plus NUL byte.
 
bool string_append (t_string *dst, const t_string *src)
 Appends the content of a source string to the end of a string.
 
bool string_append_format (t_string *string, const char *fstring,...)
 Appends formatted text to string using variadic arguments.
 
bool string_append_n (t_string *s, const char *str, long n)
 Appends n first bytes of a string to the end of a dynamic string.
 
bool string_append_vformat (t_string *string, const char *fstring, va_list args)
 Appends formatted text to string using va_list.
 
bool string_cmp (const t_string *a, const t_string *b)
 Compares two strings byte by byte.
 
bool string_dup (t_string *dst, const t_string *src)
 Copies the content of a source string into an existing string.
 
bool string_dup_n (t_string *dst, const t_string *src, size_t n)
 Copies up to n bytes from a source string into an existing string.
 
void string_free (t_string *string)
 Frees the string's internal data.
 
void string_free_void (void *string)
 Frees a t_string item through a generic void* callback signature.
 
ssize_t string_get_index_c (const t_string *string, char c)
 Finds the index of the first occurrence of a character in the string.
 
ssize_t string_get_index_s (const t_string *string, const char *s, ssize_t slen)
 Finds the first occurrence of a substring in the string.
 
bool string_grow (t_string *string, size_t target_cap)
 Grows the string to accommodate the target length if necessary.
 
bool string_init (t_string *s, size_t initial_cap, const char *str, long n)
 Initializes a string with the specified initial capacity.
 
bool string_insert (t_string *dst, size_t index, const t_string *src)
 Inserts a source string in a string at the specified index.
 
bool string_insert_n (t_string *s, size_t index, const char *str, long n)
 Inserts n first bytes of a string at the specified index.
 
bool string_prepend (t_string *dst, const t_string *src)
 Prepends a source string to the beginning of a string.
 
bool string_prepend_n (t_string *s, const char *str, long n)
 Prepends n first bytes of a string to the beginning of a string.
 
bool string_read_all (t_string *string, int fd)
 Reads all available data from a file descriptor into string.
 
bool string_read_until_c (t_string *string, int fd, char c)
 Reads from a file descriptor until a specific character is found.
 
bool string_read_until_n (t_string *string, int fd, size_t n)
 Reads up to n bytes from a file descriptor into string.
 
bool string_read_until_s (t_string *string, int fd, const char *s, ssize_t slen)
 Reads from a file descriptor until a specific substring is found.
 
void string_rm_part (t_string *string, size_t i_start, ssize_t len)
 Removes a portion of the string starting at i_start.
 
bool string_split_at (const t_string *src, size_t index, t_string *out_before, t_string *out_after)
 Splits a string into two initialized strings at a byte index.
 
bool string_split_on_char (const t_string *src, char c, bool keep_empty_entries, t_vector *out)
 Splits a string on a delimiter character into a t_vector.
 
bool string_split_on_string (const t_string *src, const char *sep, bool keep_empty_entries, t_vector *out)
 Splits a string on a delimiter C-string into a t_vector.
 
void string_take (t_string *dst, char *src, size_t cap, ssize_t len)
 Installs an existing buffer in a string without copying it.
 
void string_take_string (t_string *dst, t_string *src)
 Move the internal storage of one string into another string.
 
void string_trim_leading (t_string *string, char c)
 Removes leading copies of a character from a string.
 
bool vector_init (t_vector *vector, size_t item_size, size_t cap)
 Initializes a vector with a given item size and initial capacity.
 
bool vector_grow (t_vector *vector)
 Grows vector capacity, usually by doubling it.
 
bool vector_adjust (t_vector *vector)
 Shrinks vector capacity to match its current length.
 
bool vector_dup (t_vector *dst, const t_vector *src)
 Duplicates a vector into another one.
 
void vector_free (t_vector *vector, void(*item_free)(void *item))
 Frees the vector's internal storage.
 
void vector_clear (t_vector *vector, void(*del)(void *))
 Removes all items from a vector without freeing its storage.
 
bool vector_push (t_vector *vector, const void *item)
 Appends one item at the end of the vector.
 
bool vector_pop (t_vector *vector, void *dst)
 Removes the last item from the vector.
 
bool vector_insert (t_vector *vector, size_t index, const void *item)
 Inserts one item at a specific index.
 
bool vector_remove (t_vector *vector, size_t index, void *dst)
 Removes one item at a specific index.
 
void vector_take (t_vector *dst, t_vector *src)
 Transfers a vector state into another one without copying items.
 
bool vector_merge (t_vector *dst, const t_vector *src, size_t index)
 Inserts all items from src into dst at a specific index.
 

Macro Definition Documentation

◆ BUFFER_SIZE

#define BUFFER_SIZE   128

◆ HASHMAP_INIT_CAP

#define HASHMAP_INIT_CAP   50

Default number of buckets allocated when an empty map first grows.

◆ VECTOR_INIT_CAP

#define VECTOR_INIT_CAP   16

Typedef Documentation

◆ t_btree_node

typedef struct s_btree_node t_btree_node

◆ t_buff

typedef struct s_buff t_buff

◆ t_const_cast

typedef union u_const_cast t_const_cast

Temporary pointer view used to reinterpret the same address with or without const qualification.

This helper does not duplicate data; it only exposes the same pointer as either const char * or char * when a borrowed string address must be returned through a non-const API.

◆ t_hashmap

typedef struct s_hashmap t_hashmap

◆ t_key_value

typedef struct s_key_value t_key_value

◆ t_list

Type alias for a pointer to a list node (list head).

◆ t_node

typedef struct s_node t_node

◆ t_string

typedef struct s_string t_string

◆ t_vector

typedef struct s_vector t_vector

Function Documentation

◆ buff_grow()

bool buff_grow ( t_buff buff,
size_t  target_len 
)

Grows the buffer to accommodate the target length if necessary.

No-op if current capacity is already sufficient.

Warning
buff must be initialized before calling this function.
Parameters
buffPointer to an initialized buffer (borrowed).
target_lenThe minimum length the buffer should accommodate.
Returns
true on success, false on memory allocation failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ft_putchar_fd()

void ft_putchar_fd ( char  c,
int  fd 
)

Writes a character to a file descriptor.

Parameters
cCharacter to write.
fdFile descriptor to write to.

◆ hashmap_get()

void * hashmap_get ( const t_hashmap map,
const char *  key 
)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ str_array_free()

void str_array_free ( char ***  tab_ptr)

Frees all strings in a null-terminated array and the tab itself.

Note
The tab is set at NULL after beeing freed.
Parameters
tabArray of strings to free.
Here is the caller graph for this function:

◆ str_count_words()

size_t str_count_words ( char const *  s,
char  sep 
)

Counts words in a string separated by a delimiter.

Parameters
sString to analyze.
sepDelimiter character.
Returns
Number of words.
Here is the caller graph for this function:

◆ str_trim_leading()

char * str_trim_leading ( char const *  s1,
char const *  set 
)
Here is the call graph for this function:

◆ string_grow()

bool string_grow ( t_string string,
size_t  target_cap 
)

Grows the string to accommodate the target length if necessary.

No-op if current capacity is already sufficient.

Warning
string must be initialized before calling this function.
Parameters
stringPointer to an initialized string (borrowed).
target_lenThe minimum length the string should accommodate.
Returns
true on success, false on memory allocation failure.
Here is the call graph for this function:
Here is the caller graph for this function: