2025-02-12 14:51:05 +01:00
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* */
|
|
|
|
|
/* ::: :::::::: */
|
|
|
|
|
/* libft.h :+: :+: :+: */
|
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
|
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
|
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
|
/* Created: 2024/10/17 16:06:09 by jguelen #+# #+# */
|
2025-02-26 14:07:55 +01:00
|
|
|
/* Updated: 2025/03/03 13:02:45 by khais ### ########.fr */
|
2025-02-12 14:51:05 +01:00
|
|
|
/* */
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
|
|
#ifndef LIBFT_H
|
|
|
|
|
# define LIBFT_H
|
|
|
|
|
# include <unistd.h>
|
|
|
|
|
# include <ctype.h>
|
2025-02-14 15:06:01 +01:00
|
|
|
# include <stdbool.h>
|
2025-02-12 14:51:05 +01:00
|
|
|
# include <string.h>
|
|
|
|
|
# include <strings.h>
|
|
|
|
|
# include <stdlib.h>
|
|
|
|
|
# include <stdarg.h>
|
|
|
|
|
# include <limits.h>
|
|
|
|
|
|
|
|
|
|
# define PRINTF_SPECLIST "cspdiuxX%"
|
|
|
|
|
# define PRINTF_FLAGS "#+ -0"
|
|
|
|
|
# define HEXA_UP "0123456789ABCDEF"
|
|
|
|
|
# define HEXA_LOW "0123456789abcdef"
|
|
|
|
|
# define HEXA_RADIX_LOW "0x"
|
|
|
|
|
# define HEXA_RADIX_UP "0X"
|
|
|
|
|
# define HEX_LEN 16
|
|
|
|
|
# define SIZE_T_MAX 0xFFFFFFFFFFFFFFFF
|
|
|
|
|
|
|
|
|
|
typedef struct s_list
|
|
|
|
|
{
|
|
|
|
|
void *content;
|
|
|
|
|
struct s_list *next;
|
|
|
|
|
} t_list;
|
|
|
|
|
|
|
|
|
|
typedef enum e_printf_spec
|
|
|
|
|
{
|
|
|
|
|
PRINTF_NOSPEC = 0,
|
|
|
|
|
PRINTF_CHARSPEC = 1,
|
|
|
|
|
PRINTF_STRINGSPEC = 2,
|
|
|
|
|
PRINTF_POINTERSPEC = 3,
|
|
|
|
|
PRINTF_INTSPEC = 4,
|
|
|
|
|
PRINTF_UINTSPEC = 5,
|
|
|
|
|
PRINTF_HEXA_LOWSPEC = 6,
|
|
|
|
|
PRINTF_HEXA_UPSPEC = 7,
|
|
|
|
|
PRINTF_PERCENTSPEC = 8
|
|
|
|
|
} t_printf_spec;
|
|
|
|
|
|
|
|
|
|
typedef enum e_printf_flags
|
|
|
|
|
{
|
|
|
|
|
PRINTF_NOFLAG = 0,
|
|
|
|
|
PRINTF_PLUSFLAG = 1,
|
|
|
|
|
PRINTF_MINUSFLAG = 2,
|
|
|
|
|
PRINTF_SPACEFLAG = 4,
|
|
|
|
|
PRINTF_ZEROFLAG = 8,
|
|
|
|
|
PRINTF_HASHTAGFLAG = 16,
|
|
|
|
|
PRINTF_DOTFLAG = 32,
|
|
|
|
|
} t_printf_flags;
|
|
|
|
|
|
|
|
|
|
typedef union u_printf_var
|
|
|
|
|
{
|
|
|
|
|
char c;
|
|
|
|
|
char *s;
|
|
|
|
|
void *p;
|
|
|
|
|
int i;
|
|
|
|
|
unsigned int u;
|
|
|
|
|
} t_printf_var;
|
|
|
|
|
|
|
|
|
|
/*precision and minwith are to store integers but use long
|
|
|
|
|
to deal with errors.*/
|
|
|
|
|
typedef struct s_printf_format
|
|
|
|
|
{
|
|
|
|
|
t_printf_flags flags;
|
|
|
|
|
t_printf_spec specifier;
|
|
|
|
|
t_printf_var var;
|
|
|
|
|
long minwidth;
|
|
|
|
|
long precision;
|
|
|
|
|
char prefix[3];
|
|
|
|
|
int prefix_len;
|
|
|
|
|
int total_len;
|
|
|
|
|
} t_printf_format;
|
|
|
|
|
|
|
|
|
|
int ft_dprintf(int fd, const char *s, ...);
|
|
|
|
|
int ft_printf(const char *s, ...);
|
|
|
|
|
int ft_print_printf_format(t_printf_format *format);
|
|
|
|
|
/*ft_printf_parsing*/
|
|
|
|
|
int ft_printf_parsing(char *s, t_printf_format *format,
|
|
|
|
|
va_list *arg_list);
|
|
|
|
|
/*ft_printf_atoi*/
|
|
|
|
|
long ft_printf_atoi(char *str, char **current);
|
|
|
|
|
/*ft_printf_utils*/
|
|
|
|
|
int ft_int_len(int n, int base_len);
|
|
|
|
|
int ft_uint_len(unsigned int n, int base_len);
|
|
|
|
|
int ft_ull_len(unsigned long long n, int base_len);
|
|
|
|
|
/*Takes precision into account when relevant.*/
|
|
|
|
|
int var_len(t_printf_format *format);
|
|
|
|
|
/*ft_printf_utils2*/
|
|
|
|
|
void get_prefix_format(t_printf_format *format);
|
|
|
|
|
/**/
|
|
|
|
|
int ft_printf_str(const char *str, int maxchar);
|
|
|
|
|
int ft_pad_char(t_printf_format *format, char *c);
|
|
|
|
|
int ft_printf_badspec(t_printf_format *format);
|
|
|
|
|
int ft_printf_char(t_printf_format *format);
|
|
|
|
|
int ft_printf_string(t_printf_format *format);
|
|
|
|
|
int ft_printf_ptr(t_printf_format *format);
|
|
|
|
|
int ft_printf_int(t_printf_format *format);
|
|
|
|
|
int ft_printf_uint(t_printf_format *format);
|
|
|
|
|
int ft_printf_hexa(t_printf_format *format);
|
|
|
|
|
int ft_printf_percent(t_printf_format *format);
|
|
|
|
|
int ft_put_ull_base_fd(unsigned long long n, char *base,
|
|
|
|
|
int base_size, int fd);
|
|
|
|
|
int ft_isnumeric_conv(t_printf_format *format);
|
|
|
|
|
/* GET_NEXT_LINE*/
|
|
|
|
|
char *get_next_line(int fd);
|
|
|
|
|
/*ORIGINAL LIBFT*/
|
|
|
|
|
int ft_isalpha(int c);
|
|
|
|
|
int ft_isdigit(int c);
|
|
|
|
|
int ft_isalnum(int c);
|
|
|
|
|
int ft_isascii(int c);
|
|
|
|
|
int ft_isprint(int c);
|
|
|
|
|
size_t ft_strlen(const char *s);
|
|
|
|
|
void *ft_memset(void *s, int c, size_t n);
|
|
|
|
|
void ft_bzero(void *s, size_t n);
|
|
|
|
|
void *ft_memcpy(void *dest, const void *src, size_t n);
|
|
|
|
|
void *ft_memmove(void *dest, const void *src, size_t n);
|
|
|
|
|
size_t ft_strlcpy(char *dst, const char *src, size_t size);
|
|
|
|
|
size_t ft_strlcat(char *dst, const char *src, size_t size);
|
|
|
|
|
int ft_toupper(int c);
|
|
|
|
|
int ft_tolower(int c);
|
|
|
|
|
char *ft_strchr(const char *s, int c);
|
2025-02-14 13:30:18 +01:00
|
|
|
size_t ft_strchridx(const char *s, char c);
|
|
|
|
|
size_t ft_strnchridx(const char *s, char c);
|
2025-02-14 15:06:01 +01:00
|
|
|
size_t ft_strfchridx(const char *s, bool f(char));
|
|
|
|
|
size_t ft_strnfchridx(const char *s, bool f(char));
|
2025-02-12 14:51:05 +01:00
|
|
|
char *ft_strrchr(const char *s, int c);
|
2025-02-13 15:28:38 +01:00
|
|
|
int ft_strcmp(const char *s1, const char *s2);
|
2025-02-12 14:51:05 +01:00
|
|
|
int ft_strncmp(const char *s1, const char *s2, size_t n);
|
|
|
|
|
void *ft_memchr(const void *s, int c, size_t n);
|
|
|
|
|
int ft_memcmp(const void *s1, const void *s2, size_t n);
|
|
|
|
|
char *ft_strnstr(const char *big, const char *little, size_t len);
|
|
|
|
|
int ft_atoi(const char *nptr);
|
|
|
|
|
void *ft_calloc(size_t nmemb, size_t size);
|
|
|
|
|
char *ft_strdup(const char *s);
|
|
|
|
|
char *ft_substr(char const *s, unsigned int start, size_t len);
|
|
|
|
|
char *ft_strjoin(char const *s1, char const *s2);
|
|
|
|
|
char *ft_strtrim(char const *s1, char const *set);
|
|
|
|
|
char **ft_split(char const *s, char c);
|
|
|
|
|
char *ft_itoa(int n);
|
|
|
|
|
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
|
|
|
|
|
void ft_striteri(char *s, void (*f)(unsigned int, char*));
|
|
|
|
|
void ft_putchar_fd(char c, int fd);
|
|
|
|
|
void ft_putstr_fd(char *s, int fd);
|
|
|
|
|
void ft_putendl_fd(char *s, int fd);
|
|
|
|
|
void ft_putnbr_fd(int n, int fd);
|
|
|
|
|
int max_int(int a, int b);
|
|
|
|
|
int min_int(int a, int b);
|
|
|
|
|
long max_long(long a, long b);
|
|
|
|
|
long min_long(long a, long b);
|
2025-02-14 18:24:46 +01:00
|
|
|
char *ft_strjoin_sepc(char *s1, char *s2, char sep);
|
2025-02-12 14:51:05 +01:00
|
|
|
/*BONUSLIBFT*/
|
|
|
|
|
t_list *ft_lstnew(void *content);
|
|
|
|
|
void ft_lstadd_front(t_list **lst, t_list *new);
|
|
|
|
|
int ft_lstsize(t_list *lst);
|
|
|
|
|
t_list *ft_lstlast(t_list *lst);
|
|
|
|
|
void ft_lstadd_back(t_list **lst, t_list *new);
|
|
|
|
|
void ft_lstdelone(t_list *lst, void (*del)(void *));
|
|
|
|
|
void ft_lstclear(t_list **lst, void (*del)(void *));
|
|
|
|
|
void ft_lstiter(t_list *lst, void (*f)(void *));
|
|
|
|
|
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
|
2025-02-17 16:52:17 +01:00
|
|
|
/*frees two pointers and returns NULL*/
|
|
|
|
|
void *free2(void *p1, void *p2);
|
2025-02-12 14:51:05 +01:00
|
|
|
|
2025-02-26 14:07:55 +01:00
|
|
|
# include <stdio.h>
|
|
|
|
|
|
|
|
|
|
# include "../src/print_backtrace.h"
|
|
|
|
|
|
|
|
|
|
# define malloc(size) \
|
|
|
|
|
({ \
|
|
|
|
|
void * ret = malloc(size); \
|
|
|
|
|
if (!ret) \
|
|
|
|
|
fprintf(stderr, "malloc fail at %s:%d failed\n", \
|
|
|
|
|
__FILE__, __LINE__); \
|
|
|
|
|
else \
|
|
|
|
|
fprintf(stderr, "malloc at(%p) at %s:%d\n", \
|
|
|
|
|
ret, __FILE__, __LINE__); \
|
|
|
|
|
print_backtrace(); \
|
|
|
|
|
ret; \
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
# define ft_calloc(elmt_nbr, elmt_size) \
|
|
|
|
|
({ \
|
|
|
|
|
void * ret = ft_calloc(elmt_nbr, elmt_size); \
|
|
|
|
|
if (!ret) \
|
|
|
|
|
fprintf(stderr, "malloc fail at %s:%d failed\n", \
|
|
|
|
|
__FILE__, __LINE__); \
|
|
|
|
|
else \
|
|
|
|
|
fprintf(stderr, "ft_calloc at(%p) at %s:%d\n", \
|
|
|
|
|
ret, __FILE__, __LINE__); \
|
|
|
|
|
print_backtrace(); \
|
|
|
|
|
ret; \
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
# define free(ptr) \
|
|
|
|
|
({ \
|
|
|
|
|
fprintf(stderr, "free(%p) at %s:%d\n", \
|
|
|
|
|
ptr, __FILE__, __LINE__); \
|
|
|
|
|
print_backtrace(); \
|
|
|
|
|
free(ptr); \
|
|
|
|
|
})
|
|
|
|
|
|
2025-02-12 14:51:05 +01:00
|
|
|
#endif
|