Libft
Custom implementation of core libc functions with additional utility helpers.
Loading...
Searching...
No Matches
libft.h
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* libft.h :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: gastesan <gastesan@student.42.fr> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/12/01 19:34:40 by gastesan #+# #+# */
9/* Updated: 2026/01/23 03:07:16 by gastesan ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#ifndef LIBFT_H
14# define LIBFT_H
15
16# include <stdarg.h>
17# include <stddef.h>
18# include <stdbool.h>
19# include <sys/types.h>
20
21# define BUFFER_SIZE 128
22
31typedef struct s_buff
32{
34 char *data;
36 size_t cap;
38 size_t len;
40
49typedef struct s_node
50{
52 void *content;
54 struct s_node *prev;
56 struct s_node *next;
58
63typedef t_node *t_list;
64
70
76
82
88
94
100
106
112
118
124
130
131/* ************************************************************************* */
132/* BUFF */
133/* ************************************************************************* */
134
145bool buff_init(t_buff *b, size_t initial_cap);
146
156bool buff_adjust(t_buff *buff);
157
168void buff_free(t_buff *b);
169
180int buff_get_index(t_buff *buff, char c);
181
196bool buff_prepend(t_buff *b, const char *str, long n);
197
214bool buff_insert(t_buff *b, size_t index, const char *str, long n);
215
230bool buff_append(t_buff *b, const char *str, long n);
231
243t_buff *buff_dup_n(const t_buff *src, size_t n);
244
257void buff_rm_part(t_buff *buff, size_t i_start, ssize_t len);
258
274bool buff_append_format(t_buff *buff, const char *fstring, ...)
275 __attribute__((format(printf, 2, 3)));
276
288bool buff_append_vformat(t_buff *buff, const char *fstring, va_list args);
289
301int buff_read_until(t_buff *buff, int fd, char c);
302
315bool buff_read_all(t_buff *buff, int fd);
316
317/* ************************************************************************* */
318/* CHR */
319/* ************************************************************************* */
320
328int ft_isalnum(int c);
329
337int ft_isalpha(int c);
338
346int ft_isascii(int c);
347
355int ft_isdigit(int c);
356
365bool ft_isincharset(char c, const char *charset);
366
374int ft_isprint(int c);
375
383int ft_isspace(char c);
384
392int ft_tolower(int c);
393
401int ft_toupper(int c);
402
403/* ************************************************************************* */
404/* CONV */
405/* ************************************************************************* */
406
414int ft_atoi(const char *str);
415
423long ft_atol(const char *str);
424
434char *ft_itoa(int n);
435
445char *ft_utoa(unsigned int n);
446
456char *ft_ltoa(long n);
457
468char *ft_ultoa_base(unsigned long n, const char *base);
469
482bool parse_int(char *s, int *out);
483
484/* ************************************************************************* */
485/* GNL */
486/* ************************************************************************* */
487
502t_buff *get_next_chunk(int fd, char separator);
503
510void get_next_chunk_free(void);
511
512/* ************************************************************************* */
513/* LIST */
514/* ************************************************************************* */
515
528
536void node_free(t_node **node, void (*del_content)(void*));
537
549bool list_add_end(t_list *list, void *new_content);
550
562bool list_add_start(t_list *list, void *new_content);
563
571size_t list_get_size(t_list list);
572
584void *list_get_content(t_list list, bool (*select_function)(void*));
585
597void *list_get_content_n(t_list list, size_t index);
598
609void *list_get_content_last(t_list list);
610
622t_node *list_get_node_n(t_list list, size_t index);
623
635
643void list_iter(t_list lst, void (*f)(void *));
644
656t_list list_map(t_list list, void *(*f)(void *), void (*del)(void *));
657
666void list_rm(t_list *list, t_node *node, void (*del_content)(void*));
667
675void list_rm_all(t_list *list, void (*del_content)(void*));
676
677/* ************************************************************************* */
678/* MALLOC */
679/* ************************************************************************* */
680
691void *ft_calloc(size_t count, size_t size);
692
704bool ft_realloc(char **buff, size_t cap, size_t newcap);
705
706/* ************************************************************************* */
707/* MATH */
708/* ************************************************************************* */
709
718long min(long a, long b);
719
728long max(long a, long b);
729
737size_t absolute(long nbr);
738
752long power(int a, int b);
753
764size_t modulo(long a, size_t b);
765
773int square_root_exact(int nb);
774
782int square_root_rounded(int nb);
783
784/* ************************************************************************* */
785/* MEM */
786/* ************************************************************************* */
787
795void ft_bzero(void *s, size_t n);
796
808void *ft_memchr(const void *s, int c, size_t n);
809
819int ft_memcmp(const void *s1, const void *s2, size_t n);
820
832void *ft_memcpy(void *dst, const void *src, size_t n);
833
843void *ft_memmove(void *dst, const void *src, size_t len);
844
854void *ft_memset(void *b, int c, size_t len);
855
856/* ************************************************************************* */
857/* PRINT */
858/* ************************************************************************* */
859
869int ft_vdprintf(int fd, const char *fstring, va_list args);
870
884int ft_dprintf(int fd, const char *fstring, ...)
885 __attribute__((format(printf, 2, 3)));
886
895int ft_vprintf(const char *fstring, va_list args);
896
909int ft_printf(const char *fstring, ...)
910 __attribute__((format(printf, 1, 2)));
911
912/* ************************************************************************* */
913/* PUT */
914/* ************************************************************************* */
915
922void ft_putchar_fd(char c, int fd);
923
931void ft_putendl_fd(char *s, int fd);
932
940void ft_putnbr_fd(int n, int fd);
941
949void ft_putstr_fd(char *s, int fd);
950
951/* ************************************************************************* */
952/* STR */
953/* ************************************************************************* */
954
966char **str_split(char const *s, char c);
967
975size_t str_count_words(char const *s, char sep);
976
983void str_array_free(char ***tab_ptr);
984
996char *str_chr(const char *s, int c);
997
1007char *str_dup(const char *s1);
1008
1016void str_iteri(char *s, void (*f)(unsigned int, char*));
1017
1028char *str_join(char const *s1, char const *s2);
1029
1039size_t str_lcat(char *dst, const char *src, size_t dstsize);
1040
1050size_t str_lcpy(char *dst, const char *src, size_t dstsize);
1051
1059size_t str_len(const char *s);
1060
1071char *str_mapi(char const *s, char (*f)(unsigned int, char));
1072
1082int str_ncmp(const char *s1, const char *s2, size_t n);
1083
1095char *str_nstr(const char *haystack, const char *needle, size_t len);
1096
1108char *str_rchr(const char *s, int c);
1109
1120char *str_trim(char const *s1, char const *set);
1121
1133char *str_sub(char const *s, unsigned int start, size_t len);
1134
1135#endif
bool buff_adjust(t_buff *buff)
Shrinks buffer capacity to match its current length.
Definition buff_life_cycle.c:66
bool buff_read_all(t_buff *buff, int fd)
Reads all available data from a file descriptor into buffer.
Definition buff_read.c:43
bool buff_prepend(t_buff *b, const char *str, long n)
Prepends a string to the beginning of the buffer.
Definition buff_ops.c:17
void buff_free(t_buff *b)
Frees the buffer's internal data.
Definition buff_life_cycle.c:88
bool buff_append(t_buff *b, const char *str, long n)
Appends a string to the end of the buffer.
Definition buff_ops.c:74
bool buff_insert(t_buff *b, size_t index, const char *str, long n)
Inserts a string at a specific index in the buffer.
Definition buff_ops.c:45
void buff_rm_part(t_buff *buff, size_t i_start, ssize_t len)
Removes a portion of the buffer starting at i_start.
Definition buff_ops.c:108
bool buff_init(t_buff *b, size_t initial_cap)
Initializes a buffer with the specified initial capacity.
Definition buff_life_cycle.c:19
t_buff * buff_dup_n(const t_buff *src, size_t n)
Duplicates up to n bytes of a buffer into a new buffer.
Definition buff_ops.c:89
bool buff_append_format(t_buff *buff, const char *fstring,...)
Appends formatted string to buffer using variadic arguments.
Definition buff_format.c:16
bool buff_append_vformat(t_buff *buff, const char *fstring, va_list args)
Appends formatted string to buffer using va_list.
Definition buff_format.c:27
int buff_read_until(t_buff *buff, int fd, char c)
Reads from a file descriptor until a specific character is found.
Definition buff_read.c:19
int buff_get_index(t_buff *buff, char c)
Finds the index of a character in the buffer.
Definition buff_utils.c:15
int ft_isdigit(int c)
Checks if a character is a digit ('0'-'9').
Definition ft_isdigit.c:13
bool ft_isincharset(char c, const char *charset)
Checks if a character is present in a character set.
Definition ft_isincharset.c:15
int ft_isalnum(int c)
Checks if a character is alphanumeric.
Definition ft_isalnum.c:13
int ft_tolower(int c)
Converts an uppercase letter to lowercase.
Definition ft_tolower.c:13
int ft_isprint(int c)
Checks if a character is printable (32-126).
Definition ft_isprint.c:13
int ft_isascii(int c)
Checks if a character is a valid ASCII character (0-127).
Definition ft_isascii.c:13
int ft_isalpha(int c)
Checks if a character is alphabetic.
Definition ft_isalpha.c:13
int ft_isspace(char c)
Checks if a character is whitespace (9-13).
Definition ft_isspace.c:13
int ft_toupper(int c)
Converts a lowercase letter to uppercase.
Definition ft_toupper.c:13
long ft_atol(const char *str)
Converts a string to a long integer.
Definition ft_atol.c:15
char * ft_ultoa_base(unsigned long n, const char *base)
Converts an unsigned long to a string in a given base.
Definition ft_ultoa_base.c:18
bool parse_int(char *s, int *out)
Parses a string to an integer with overflow detection.
Definition parse_int.c:16
char * ft_itoa(int n)
Converts an integer to a string.
Definition variants.c:20
char * ft_ltoa(long n)
Converts a long integer to a string.
Definition ft_ltoa.c:18
char * ft_utoa(unsigned int n)
Converts an unsigned integer to a string.
Definition variants.c:25
int ft_atoi(const char *str)
Converts a string to an integer.
Definition variants.c:15
t_buff * get_next_chunk(int fd, char separator)
Reads the next chunk from a file descriptor until separator is found.
Definition gnc.c:19
void get_next_chunk_free(void)
Frees all internal stashes used by get_next_chunk.
Definition gnc.c:86
t_list list_map(t_list list, void *(*f)(void *), void(*del)(void *))
Creates a new list by applying a function to each element.
Definition list_apply.c:30
void * list_get_content(t_list list, bool(*select_function)(void *))
Finds content in list matching a selection function.
Definition list_get.c:16
void list_iter(t_list lst, void(*f)(void *))
Applies a function to each element of the list.
Definition list_apply.c:16
size_t list_get_size(t_list list)
Calculates the number of nodes in the list.
Definition list_size.c:15
void * list_get_content_n(t_list list, size_t index)
Gets content at a specific index in the list.
Definition list_get.c:32
bool list_add_end(t_list *list, void *new_content)
Adds a new element at the end of the list.
Definition list_life_cycle.c:31
void list_rm(t_list *list, t_node *node, void(*del_content)(void *))
Removes a specific node from the list.
Definition list_life_cycle.c:54
t_node * list_get_node_last(t_list list)
Gets the last node in the list.
Definition list_get.c:69
t_node * node_new(void *content, t_node *prev, t_node *next)
Creates a new list node.
Definition node.c:16
void node_free(t_node **node, void(*del_content)(void *))
Frees a node and optionally its content.
Definition node.c:29
bool list_add_start(t_list *list, void *new_content)
Adds a new element at the start of the list.
Definition list_life_cycle.c:16
void list_rm_all(t_list *list, void(*del_content)(void *))
Removes all nodes from the list.
Definition list_life_cycle.c:74
void * list_get_content_last(t_list list)
Gets the content of the last node in the list.
Definition list_get.c:42
t_node * list_get_node_n(t_list list, size_t index)
Gets the node at a specific index in the list.
Definition list_get.c:52
bool ft_realloc(char **buff, size_t cap, size_t newcap)
Reallocates a buffer to a new capacity.
Definition ft_realloc.c:16
void * ft_calloc(size_t count, size_t size)
Allocates and zeroes memory for an array.
Definition ft_calloc.c:17
long min(long a, long b)
Returns the minimum of two long integers.
Definition min_max_abs.c:15
int square_root_exact(int nb)
Calculates the exact integer square root.
Definition square_root.c:13
size_t modulo(long a, size_t b)
Computes the modulo of a signed integer with an unsigned modulus.
Definition modulo.c:15
long max(long a, long b)
Returns the maximum of two long integers.
Definition min_max_abs.c:23
int square_root_rounded(int nb)
Calculates the nearest integer square root.
Definition square_root.c:29
long power(int a, int b)
Computes a raised to the power of b.
Definition power.c:1
size_t absolute(long nbr)
Returns the absolute value of a long integer.
Definition min_max_abs.c:31
int ft_memcmp(const void *s1, const void *s2, size_t n)
Compares two memory areas byte by byte.
Definition ft_memcmp.c:15
void * ft_memset(void *b, int c, size_t len)
Fills memory with a constant byte.
Definition ft_memset.c:15
void ft_bzero(void *s, size_t n)
Sets n bytes of memory to zero.
Definition ft_bzero.c:15
void * ft_memcpy(void *dst, const void *src, size_t n)
Copies n bytes from src to dst.
Definition ft_memcpy.c:15
void * ft_memchr(const void *s, int c, size_t n)
Locates the first occurrence of a byte in memory.
Definition ft_memchr.c:15
void * ft_memmove(void *dst, const void *src, size_t len)
Copies n bytes from src to dst, handling overlapping memory.
Definition ft_memmove.c:15
int ft_dprintf(int fd, const char *fstring,...)
Writes formatted output to a file descriptor.
Definition printer.c:36
int ft_vprintf(const char *fstring, va_list args)
Writes formatted output to stdout using va_list.
Definition printer.c:49
int ft_printf(const char *fstring,...)
Writes formatted output to stdout.
Definition printer.c:54
int ft_vdprintf(int fd, const char *fstring, va_list args)
Writes formatted output to a file descriptor using va_list.
Definition printer.c:18
void ft_putnbr_fd(int n, int fd)
Writes an integer to a file descriptor.
Definition ft_putnbr_fd.c:17
void ft_putstr_fd(char *s, int fd)
Writes a string to a file descriptor.
Definition ft_putstr_fd.c:16
void ft_putendl_fd(char *s, int fd)
Writes a string followed by newline to a file descriptor.
Definition ft_putendl_fd.c:16
char ** str_split(char const *s, char c)
Splits a string into an array of strings using a delimiter.
Definition str_split.c:18
char * str_join(char const *s1, char const *s2)
Concatenates two strings into a new string.
Definition str_join.c:16
size_t str_lcat(char *dst, const char *src, size_t dstsize)
Appends src to dst with size limit.
Definition str_lcat.c:15
size_t str_len(const char *s)
Calculates the length of a string.
Definition str_len.c:15
size_t str_lcpy(char *dst, const char *src, size_t dstsize)
Copies src to dst with size limit.
Definition str_lcpy.c:15
char * str_mapi(char const *s, char(*f)(unsigned int, char))
Creates a new string by applying a function to each character.
Definition str_mapi.c:16
char * str_nstr(const char *haystack, const char *needle, size_t len)
Locates a substring within a string, limited by length.
Definition str_nstr.c:15
char * str_sub(char const *s, unsigned int start, size_t len)
Extracts a substring from a string.
Definition str_sub.c:16
char * str_rchr(const char *s, int c)
Locates the last occurrence of a character in a string.
Definition str_rchr.c:15
char * str_chr(const char *s, int c)
Locates the first occurrence of a character in a string.
Definition str_strchr.c:15
void str_iteri(char *s, void(*f)(unsigned int, char *))
Applies a function to each character of a string with its index.
Definition str_iteri.c:13
char * str_trim(char const *s1, char const *set)
Trims characters from the beginning and end of a string.
Definition str_trim.c:18
char * str_dup(const char *s1)
Duplicates a string.
Definition str_dup.c:18
int str_ncmp(const char *s1, const char *s2, size_t n)
Compares at most n characters of two strings.
Definition str_ncmp.c:15
struct s_buff t_buff
struct s_node t_node
size_t str_count_words(char const *s, char sep)
Counts words in a string separated by a delimiter.
Definition str_split.c:47
void str_array_free(char ***tab_ptr)
Frees all strings in a null-terminated array and the tab itself.
Definition str_split.c:87
void ft_putchar_fd(char c, int fd)
Writes a character to a file descriptor.
Definition ft_putchar_fd.c:15
t_node * t_list
Type alias for a pointer to a list node (list head).
Definition libft.h:63
Dynamic buffer structure for efficient string/data manipulation.
Definition libft.h:32
size_t cap
Current allocated capacity (in bytes).
Definition libft.h:36
size_t len
Current length of data in the buffer (in bytes).
Definition libft.h:38
char * data
Pointer to the allocated data (owned by the buffer).
Definition libft.h:34
Doubly linked list node structure.
Definition libft.h:50
void * content
Pointer to the node's content (owned by the node).
Definition libft.h:52
struct s_node * next
Pointer to the next node (borrowed, may be NULL).
Definition libft.h:56
struct s_node * prev
Pointer to the previous node (borrowed, may be NULL).
Definition libft.h:54