bless
This commit is contained in:
parent
81319a9d5d
commit
30ee6767ac
109
gnl/get_next_line.c
Normal file
109
gnl/get_next_line.c
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* get_next_line.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/18 20:20:17 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/05/27 17:44:59 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "get_next_line.h"
|
||||||
|
|
||||||
|
void *buffer_extend(void *ptr_old, size_t size_new, size_t size_old)
|
||||||
|
{
|
||||||
|
void *ptr_new;
|
||||||
|
void *ptr_back;
|
||||||
|
void *ptr_old_free;
|
||||||
|
|
||||||
|
ptr_new = g_calloc(size_new + 1, 1);
|
||||||
|
if (!ptr_new)
|
||||||
|
return (free(ptr_old), ptr_old = NULL, NULL);
|
||||||
|
ptr_back = ptr_new;
|
||||||
|
ptr_old_free = ptr_old;
|
||||||
|
while (size_old--)
|
||||||
|
*(char *)ptr_new++ = *(char *)ptr_old++;
|
||||||
|
return (free(ptr_old_free), ptr_old_free = NULL, ptr_back);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *handle_no_nl(char **buf, char **left)
|
||||||
|
{
|
||||||
|
if (left)
|
||||||
|
{
|
||||||
|
free(*left);
|
||||||
|
*left = NULL;
|
||||||
|
}
|
||||||
|
if (!buf)
|
||||||
|
return (NULL);
|
||||||
|
if (g_strlen(*buf))
|
||||||
|
return (*buf);
|
||||||
|
free(*buf);
|
||||||
|
*buf = NULL;
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *line_extract(char **buf_joined, char **left, size_t line_len)
|
||||||
|
{
|
||||||
|
char *line;
|
||||||
|
|
||||||
|
line = g_substr(*buf_joined, 0, line_len);
|
||||||
|
if (!line)
|
||||||
|
return (free(*buf_joined), *buf_joined = NULL, NULL);
|
||||||
|
*left = g_substr(*buf_joined, line_len, g_strlen(*buf_joined) - line_len);
|
||||||
|
free(*buf_joined);
|
||||||
|
*buf_joined = NULL;
|
||||||
|
if (!*left)
|
||||||
|
return (free(line), line = NULL, NULL);
|
||||||
|
return (line);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *line_handle(char **buf_fetch)
|
||||||
|
{
|
||||||
|
char *buf_joined;
|
||||||
|
size_t line_len;
|
||||||
|
static char *left;
|
||||||
|
|
||||||
|
if (!buf_fetch)
|
||||||
|
return (free(left), NULL);
|
||||||
|
buf_joined = g_strjoin(left, *buf_fetch);
|
||||||
|
free(*buf_fetch);
|
||||||
|
*buf_fetch = NULL;
|
||||||
|
free(left);
|
||||||
|
left = NULL;
|
||||||
|
if (!buf_joined)
|
||||||
|
return (NULL);
|
||||||
|
line_len = find_newline(buf_joined);
|
||||||
|
if (line_len)
|
||||||
|
return (line_extract(&buf_joined, &left, line_len));
|
||||||
|
return (handle_no_nl(&buf_joined, &left));
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_next_line(int fd)
|
||||||
|
{
|
||||||
|
char *buf;
|
||||||
|
ssize_t bytes_read;
|
||||||
|
size_t buf_size_cur;
|
||||||
|
size_t buf_size_prev;
|
||||||
|
|
||||||
|
if (fd < 0)
|
||||||
|
return (line_handle(NULL), NULL);
|
||||||
|
buf = g_calloc(sizeof(*buf), BUFFER_SIZE + 1);
|
||||||
|
buf_size_prev = 0;
|
||||||
|
buf_size_cur = BUFFER_SIZE;
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
if (!buf)
|
||||||
|
return (line_handle(NULL));
|
||||||
|
bytes_read = read(fd, buf + buf_size_prev, BUFFER_SIZE);
|
||||||
|
if (bytes_read < 0)
|
||||||
|
return (free(buf), buf = NULL, line_handle(NULL));
|
||||||
|
if (find_newline(buf) || bytes_read == 0)
|
||||||
|
break ;
|
||||||
|
buf_size_prev = buf_size_cur;
|
||||||
|
buf_size_cur += BUFFER_SIZE;
|
||||||
|
buf = buffer_extend(buf, buf_size_cur, buf_size_prev);
|
||||||
|
}
|
||||||
|
return (line_handle(&buf));
|
||||||
|
}
|
||||||
38
gnl/get_next_line.h
Normal file
38
gnl/get_next_line.h
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* get_next_line.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/15 14:07:36 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/05/27 17:43:21 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef GET_NEXT_LINE_H
|
||||||
|
|
||||||
|
# define GET_NEXT_LINE_H
|
||||||
|
|
||||||
|
# ifndef BUFFER_SIZE
|
||||||
|
|
||||||
|
# define BUFFER_SIZE 50
|
||||||
|
|
||||||
|
# endif
|
||||||
|
|
||||||
|
# define MAX_FD 1024
|
||||||
|
|
||||||
|
# include <stdlib.h>
|
||||||
|
# include <unistd.h>
|
||||||
|
# include <fcntl.h>
|
||||||
|
# include <stdint.h>
|
||||||
|
|
||||||
|
char *get_next_line(int fd);
|
||||||
|
char *g_strjoin(char const *s1, char const *s2);
|
||||||
|
size_t g_strlen(char const *str);
|
||||||
|
void *g_calloc(size_t n, size_t s);
|
||||||
|
char *g_substr(char const *s, unsigned int start, size_t len);
|
||||||
|
char *line_handle(char **buf_fetch);
|
||||||
|
size_t find_newline(char *buf);
|
||||||
|
|
||||||
|
#endif
|
||||||
118
gnl/get_next_line_utils.c
Normal file
118
gnl/get_next_line_utils.c
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* get_next_line_utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/15 14:14:13 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/05/20 11:38:03 by victor ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "get_next_line.h"
|
||||||
|
|
||||||
|
size_t g_strlen(char const *str)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
if (!str)
|
||||||
|
return (0);
|
||||||
|
i = 0;
|
||||||
|
while (*str)
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
str++;
|
||||||
|
}
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t find_newline(char *buf)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
if (!buf)
|
||||||
|
return (0);
|
||||||
|
i = 0;
|
||||||
|
while (*buf)
|
||||||
|
{
|
||||||
|
if (*buf == '\n')
|
||||||
|
return (i + 1);
|
||||||
|
i++;
|
||||||
|
buf++;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *g_strjoin(char const *s1, char const *s2)
|
||||||
|
{
|
||||||
|
char *tmp;
|
||||||
|
unsigned int i;
|
||||||
|
unsigned int j;
|
||||||
|
|
||||||
|
if (!s2)
|
||||||
|
return (NULL);
|
||||||
|
if (!s1)
|
||||||
|
return (g_substr(s2, 0, g_strlen(s2)));
|
||||||
|
tmp = g_calloc(g_strlen(s1) + g_strlen(s2) + 1, sizeof(*tmp));
|
||||||
|
if (!tmp)
|
||||||
|
return (NULL);
|
||||||
|
i = 0;
|
||||||
|
if (s1)
|
||||||
|
{
|
||||||
|
while (s1[i])
|
||||||
|
{
|
||||||
|
tmp[i] = s1[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
j = 0;
|
||||||
|
if (s2)
|
||||||
|
while (s2[j])
|
||||||
|
tmp[i++] = s2[j++];
|
||||||
|
return (tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *g_substr(char const *s, unsigned int start, size_t len)
|
||||||
|
{
|
||||||
|
char *tmp;
|
||||||
|
unsigned int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (!s || start >= g_strlen(s) || len <= 0)
|
||||||
|
{
|
||||||
|
tmp = malloc(1);
|
||||||
|
if (!tmp)
|
||||||
|
return (NULL);
|
||||||
|
tmp[i] = 0;
|
||||||
|
return (tmp);
|
||||||
|
}
|
||||||
|
if (len + start > g_strlen(s))
|
||||||
|
len = g_strlen(s) - start;
|
||||||
|
tmp = g_calloc(len + 1, sizeof(*tmp));
|
||||||
|
if (!tmp)
|
||||||
|
return (NULL);
|
||||||
|
while (i < len && s[i])
|
||||||
|
{
|
||||||
|
tmp[i] = s[i + start];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *g_calloc(size_t n, size_t s)
|
||||||
|
{
|
||||||
|
char *tmp;
|
||||||
|
unsigned long i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (n == 0)
|
||||||
|
return (malloc(0));
|
||||||
|
if (SIZE_MAX / n < s)
|
||||||
|
return (NULL);
|
||||||
|
tmp = malloc(n * s);
|
||||||
|
if (tmp)
|
||||||
|
while (i < n * s)
|
||||||
|
tmp[i++] = 0;
|
||||||
|
return ((void *)tmp);
|
||||||
|
}
|
||||||
74
libft/Makefile
Normal file
74
libft/Makefile
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# ::: :::::::: #
|
||||||
|
# Makefile :+: :+: :+: #
|
||||||
|
# +:+ +:+ +:+ #
|
||||||
|
# By: vvobis <marvin@42.fr> +#+ +:+ +#+ #
|
||||||
|
# +#+#+#+#+#+ +#+ #
|
||||||
|
# Created: 2024/09/17 14:23:19 by vvobis #+# #+# #
|
||||||
|
# Updated: 2024/12/10 21:16:51 by vvobis ### ########.fr #
|
||||||
|
# #
|
||||||
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
NAME := libft.a
|
||||||
|
|
||||||
|
CC := cc
|
||||||
|
|
||||||
|
CFLAGS:= -Wall -Wextra -Werror
|
||||||
|
|
||||||
|
SRC := ft_bzero.c ft_isalnum.c ft_isalpha.c ft_isdigit.c \
|
||||||
|
ft_isprint.c ft_memcpy.c ft_memmove.c ft_memset.c \
|
||||||
|
ft_strlcat.c ft_strlcpy.c ft_strlen.c \
|
||||||
|
ft_isascii.c ft_strchr.c ft_strrchr.c ft_strncmp.c \
|
||||||
|
ft_toupper.c ft_tolower.c ft_memchr.c ft_strnstr.c \
|
||||||
|
ft_atoi.c ft_atod.c ft_memcmp.c ft_calloc.c ft_strdup.c \
|
||||||
|
ft_substr.c ft_strjoin.c ft_strtrim.c ft_split.c \
|
||||||
|
ft_itoa.c ft_strmapi.c ft_striteri.c ft_putchar_fd.c \
|
||||||
|
ft_putstr_fd.c ft_putendl_fd.c ft_putnbr_fd.c \
|
||||||
|
ft_isspace.c ft_free.c ft_read.c
|
||||||
|
|
||||||
|
SRCGNL := ../gnl/get_next_line.c ../gnl/get_next_line_utils.c
|
||||||
|
|
||||||
|
SRCPRINT := printf/ft_fprintf.c printf/ft_printf.c printf/ft_putascii.c printf/ft_putptr.c printf/ft_puthex.c printf/ft_putfloat.c
|
||||||
|
|
||||||
|
SRCBON := ft_lstnew_bonus.c ft_lstadd_front_bonus.c ft_lstsize_bonus.c ft_lstlast_bonus.c ft_lstadd_back_bonus.c ft_lstdelone_bonus.c ft_lstclear_bonus.c ft_lstiter_bonus.c ft_lstmap_bonus.c
|
||||||
|
|
||||||
|
OBJBON := $(SRCBON:%.c=%.o)
|
||||||
|
|
||||||
|
OBJ := $(SRC:%.c=%.o)
|
||||||
|
|
||||||
|
OBJGNL := $(SRCGNL:%.c=%.o)
|
||||||
|
|
||||||
|
OBJPRINT := $(SRCPRINT:%.c=%.o)
|
||||||
|
|
||||||
|
LIBDIR := lib
|
||||||
|
|
||||||
|
LIBS := printf
|
||||||
|
|
||||||
|
all: $(NAME)
|
||||||
|
|
||||||
|
bonus: $(OBJBON) $(OBJ)
|
||||||
|
ar rcs libft.a $(OBJ) $(OBJBON)
|
||||||
|
|
||||||
|
$(NAME): $(LIBDIR) $(OBJ) $(OBJGNL) $(OBJPRINT) $(LIBS)
|
||||||
|
ar rcs $(LIBDIR)/$@ $(OBJ) $(OBJGNL) $(OBJPRINT)
|
||||||
|
|
||||||
|
$(LIBDIR):
|
||||||
|
mkdir -p $@
|
||||||
|
|
||||||
|
%.o: %.c
|
||||||
|
$(CC) -c $(CFLAGS) $^ -o $@
|
||||||
|
|
||||||
|
re: fclean all
|
||||||
|
|
||||||
|
so:
|
||||||
|
$(CC) -fPIC $(CFLAGS) $(SRC) test.c
|
||||||
|
gcc -shared -o libft.so $(OBJ) $(OBJBON)
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
rm -f $(NAME)
|
||||||
|
make fclean -C $(LIBS)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f $(OBJ) $(OBJBON) $(OBJGNL)
|
||||||
|
make clean -C $(LIBS)
|
||||||
57
libft/ft_atod.c
Normal file
57
libft/ft_atod.c
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_atod.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: victor </var/spool/mail/victor> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/09/08 22:25:35 by victor #+# #+# */
|
||||||
|
/* Updated: 2024/12/05 20:08:42 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
double atod_collect_fraction(char *n, double d)
|
||||||
|
{
|
||||||
|
int period;
|
||||||
|
int mult;
|
||||||
|
|
||||||
|
mult = 1;
|
||||||
|
period = 0;
|
||||||
|
while (*n && !ft_isspace(*n))
|
||||||
|
{
|
||||||
|
if (*n == '.' && period == 0)
|
||||||
|
{
|
||||||
|
period = 1;
|
||||||
|
n++;
|
||||||
|
continue ;
|
||||||
|
}
|
||||||
|
else if (((*n == '.' && period == 1) || (*n != '.' && !ft_isdigit(*n))))
|
||||||
|
return (ft_putendl_fd("Invalid double format: Too many periods", \
|
||||||
|
2), exit(1), 0);
|
||||||
|
if (period)
|
||||||
|
mult *= 10;
|
||||||
|
d *= 10;
|
||||||
|
d += (*n - '0');
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
return (d / mult);
|
||||||
|
}
|
||||||
|
|
||||||
|
double ft_atod(char *n)
|
||||||
|
{
|
||||||
|
double d;
|
||||||
|
int sign;
|
||||||
|
|
||||||
|
if (!n)
|
||||||
|
exit(1);
|
||||||
|
sign = 1;
|
||||||
|
if (*n == '-')
|
||||||
|
{
|
||||||
|
sign = -1;
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
d = 0;
|
||||||
|
return (atod_collect_fraction(n, d) * sign);
|
||||||
|
}
|
||||||
60
libft/ft_atoi.c
Normal file
60
libft/ft_atoi.c
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_atoi.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: bszilas <bszilas@student.42vienna.com> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/10 10:49:04 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/09/13 22:19:12 by bszilas ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
int is_space(char const c)
|
||||||
|
{
|
||||||
|
if ((c >= 9 && c <= 13) || c == ' ')
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_atoi(char const *s)
|
||||||
|
{
|
||||||
|
int nb;
|
||||||
|
char const *tmp;
|
||||||
|
|
||||||
|
nb = 0;
|
||||||
|
while (is_space(*s))
|
||||||
|
s++;
|
||||||
|
tmp = s;
|
||||||
|
if (*tmp == '+' || *tmp == '-')
|
||||||
|
tmp++;
|
||||||
|
while (*tmp >= '0' && *tmp <= '9')
|
||||||
|
{
|
||||||
|
nb *= 10;
|
||||||
|
nb += (*tmp - '0');
|
||||||
|
tmp++;
|
||||||
|
}
|
||||||
|
if (*s == '-')
|
||||||
|
nb = -nb;
|
||||||
|
return (nb);
|
||||||
|
}
|
||||||
|
|
||||||
|
long ft_atol(const char *nptr)
|
||||||
|
{
|
||||||
|
int64_t n;
|
||||||
|
int sign;
|
||||||
|
|
||||||
|
n = 0;
|
||||||
|
sign = 1;
|
||||||
|
while (ft_isspace(*nptr))
|
||||||
|
nptr++;
|
||||||
|
if (*nptr == '-')
|
||||||
|
sign = -1;
|
||||||
|
if (sign == -1 || *nptr == '+')
|
||||||
|
nptr++;
|
||||||
|
while (*nptr >= '0' && *nptr <= '9')
|
||||||
|
n = n * 10 + (*nptr++ - '0');
|
||||||
|
return (n * sign);
|
||||||
|
}
|
||||||
19
libft/ft_bzero.c
Normal file
19
libft/ft_bzero.c
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_bzero.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/02 16:04:55 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/09 20:54:53 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void ft_bzero(void *s, size_t n)
|
||||||
|
{
|
||||||
|
while (n--)
|
||||||
|
*(char *)s++ = 0;
|
||||||
|
}
|
||||||
30
libft/ft_calloc.c
Normal file
30
libft/ft_calloc.c
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_calloc.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/04 13:44:00 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/10 16:39:56 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void *ft_calloc(size_t n, size_t s)
|
||||||
|
{
|
||||||
|
char *tmp;
|
||||||
|
unsigned long i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (n == 0)
|
||||||
|
return (malloc(0));
|
||||||
|
if (SIZE_MAX / n < s)
|
||||||
|
return (NULL);
|
||||||
|
tmp = malloc(n * s);
|
||||||
|
if (tmp)
|
||||||
|
while (i < n * s)
|
||||||
|
tmp[i++] = 0;
|
||||||
|
return ((void *)tmp);
|
||||||
|
}
|
||||||
22
libft/ft_free.c
Normal file
22
libft/ft_free.c
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_free.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/09/10 12:16:39 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/09/10 12:17:34 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void ft_free(void *ptr_ptr)
|
||||||
|
{
|
||||||
|
void **ptr;
|
||||||
|
|
||||||
|
ptr = ptr_ptr;
|
||||||
|
free(*ptr);
|
||||||
|
*ptr = NULL;
|
||||||
|
}
|
||||||
20
libft/ft_isalnum.c
Normal file
20
libft/ft_isalnum.c
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_isalnum.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/02 15:59:18 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/02 15:59:23 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
int ft_isalnum(int c)
|
||||||
|
{
|
||||||
|
if (ft_isdigit(c) || ft_isalpha(c))
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
20
libft/ft_isalpha.c
Normal file
20
libft/ft_isalpha.c
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_isalpha.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/02 15:59:49 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/02 16:00:04 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
int ft_isalpha(int c)
|
||||||
|
{
|
||||||
|
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
20
libft/ft_isascii.c
Normal file
20
libft/ft_isascii.c
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_isascii.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/02 19:48:35 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/02 19:49:36 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
int ft_isascii(int c)
|
||||||
|
{
|
||||||
|
if (c >= 0 && c <= 127)
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
18
libft/ft_isdigit.c
Normal file
18
libft/ft_isdigit.c
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_isdigit.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/02 16:14:56 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/02 19:38:45 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
int ft_isdigit(int c)
|
||||||
|
{
|
||||||
|
if (c >= '0' && c <= '9')
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
18
libft/ft_isprint.c
Normal file
18
libft/ft_isprint.c
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_isprint.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/02 16:00:49 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/02 16:00:52 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
int ft_isprint(int c)
|
||||||
|
{
|
||||||
|
if (c >= 32 && c <= 126)
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
18
libft/ft_isspace.c
Normal file
18
libft/ft_isspace.c
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_isspace.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: victor </var/spool/mail/victor> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/09/08 18:27:49 by victor #+# #+# */
|
||||||
|
/* Updated: 2024/09/08 18:27:55 by victor ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
int ft_isspace(int c)
|
||||||
|
{
|
||||||
|
return (c == ' ' || (c > 9 && c < 14));
|
||||||
|
}
|
||||||
75
libft/ft_itoa.c
Normal file
75
libft/ft_itoa.c
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_itoa.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/05 20:16:48 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/11 14:48:59 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
static int get_len(int n)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (n < 0)
|
||||||
|
n = -n;
|
||||||
|
i = 0;
|
||||||
|
while (n)
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
n /= 10;
|
||||||
|
}
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *is_negative(int n)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char *num;
|
||||||
|
|
||||||
|
i = get_len(n);
|
||||||
|
num = malloc(sizeof(*num) * i + 2);
|
||||||
|
if (!num)
|
||||||
|
return (NULL);
|
||||||
|
num[i + 1] = 0;
|
||||||
|
while (n)
|
||||||
|
{
|
||||||
|
num[i--] = -(n % 10) + 48;
|
||||||
|
n /= 10;
|
||||||
|
}
|
||||||
|
num[i] = 0x2d;
|
||||||
|
return (num);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *is_positive(int n)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char *num;
|
||||||
|
|
||||||
|
i = get_len(n);
|
||||||
|
num = malloc(sizeof(*num) * i + 1);
|
||||||
|
if (!num)
|
||||||
|
return (NULL);
|
||||||
|
num[i--] = 0;
|
||||||
|
while (n)
|
||||||
|
{
|
||||||
|
num[i--] = (n % 10) + 48;
|
||||||
|
n /= 10;
|
||||||
|
}
|
||||||
|
return (num);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ft_itoa(int n)
|
||||||
|
{
|
||||||
|
if (n == 0)
|
||||||
|
return (ft_strdup("0"));
|
||||||
|
else if (n < 0)
|
||||||
|
return (is_negative(n));
|
||||||
|
else
|
||||||
|
return (is_positive(n));
|
||||||
|
}
|
||||||
30
libft/ft_lstadd_back_bonus.c
Normal file
30
libft/ft_lstadd_back_bonus.c
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_lstadd_back.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/09 13:51:32 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/09 18:17:12 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void ft_lstadd_back(t_list **lst, t_list *new)
|
||||||
|
{
|
||||||
|
t_list *tmp;
|
||||||
|
|
||||||
|
if (!new)
|
||||||
|
return ;
|
||||||
|
if (*lst)
|
||||||
|
{
|
||||||
|
tmp = *lst;
|
||||||
|
while (tmp->next)
|
||||||
|
tmp = tmp->next;
|
||||||
|
tmp->next = new;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
*lst = new;
|
||||||
|
}
|
||||||
26
libft/ft_lstadd_front_bonus.c
Normal file
26
libft/ft_lstadd_front_bonus.c
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_lstadd_front.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/09 12:53:08 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/09 18:17:50 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void ft_lstadd_front(t_list **lst, t_list *new)
|
||||||
|
{
|
||||||
|
if (!lst || !new)
|
||||||
|
return ;
|
||||||
|
if (!*lst)
|
||||||
|
*lst = new;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
new->next = *lst;
|
||||||
|
*lst = new;
|
||||||
|
}
|
||||||
|
}
|
||||||
28
libft/ft_lstclear_bonus.c
Normal file
28
libft/ft_lstclear_bonus.c
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_lstclear.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/09 15:44:23 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/09 22:35:41 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void ft_lstclear(t_list **lst, void (*del)(void *))
|
||||||
|
{
|
||||||
|
t_list *tmp;
|
||||||
|
|
||||||
|
if (!*lst || !del || !lst)
|
||||||
|
return ;
|
||||||
|
while (*lst)
|
||||||
|
{
|
||||||
|
tmp = (*lst)->next;
|
||||||
|
del((*lst)->content);
|
||||||
|
free(*lst);
|
||||||
|
*lst = tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
21
libft/ft_lstdelone_bonus.c
Normal file
21
libft/ft_lstdelone_bonus.c
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_lstdelone.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/09 14:24:03 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/09 15:42:04 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void ft_lstdelone(t_list *lst, void (*del)(void *))
|
||||||
|
{
|
||||||
|
if (!lst || !del)
|
||||||
|
return ;
|
||||||
|
del(lst->content);
|
||||||
|
free(lst);
|
||||||
|
}
|
||||||
24
libft/ft_lstiter_bonus.c
Normal file
24
libft/ft_lstiter_bonus.c
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_lstiter.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/09 15:57:51 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/09 18:20:05 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void ft_lstiter(t_list *lst, void (*f)(void *))
|
||||||
|
{
|
||||||
|
if (!lst || !f)
|
||||||
|
return ;
|
||||||
|
while (lst)
|
||||||
|
{
|
||||||
|
f(lst->content);
|
||||||
|
lst = lst->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
22
libft/ft_lstlast_bonus.c
Normal file
22
libft/ft_lstlast_bonus.c
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_lstlast.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/09 13:47:08 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/09 15:11:51 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
t_list *ft_lstlast(t_list *lst)
|
||||||
|
{
|
||||||
|
if (!lst)
|
||||||
|
return (NULL);
|
||||||
|
while (lst->next)
|
||||||
|
lst = lst->next;
|
||||||
|
return (lst);
|
||||||
|
}
|
||||||
44
libft/ft_lstmap_bonus.c
Normal file
44
libft/ft_lstmap_bonus.c
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_lstmap.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/09 16:18:03 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/09 23:40:57 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
static void *free_list(t_list **head, void (*del)(void *), void *fcontent)
|
||||||
|
{
|
||||||
|
if (fcontent)
|
||||||
|
del(fcontent);
|
||||||
|
ft_lstclear(head, del);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
|
||||||
|
{
|
||||||
|
t_list *tmp;
|
||||||
|
t_list *head;
|
||||||
|
void *fcontent;
|
||||||
|
|
||||||
|
if (!lst || !f || !del)
|
||||||
|
return (NULL);
|
||||||
|
head = NULL;
|
||||||
|
while (lst)
|
||||||
|
{
|
||||||
|
fcontent = f(lst->content);
|
||||||
|
if (!fcontent)
|
||||||
|
return (free_list(&head, del, fcontent));
|
||||||
|
tmp = ft_lstnew(fcontent);
|
||||||
|
if (!tmp)
|
||||||
|
return (free_list(&head, del, fcontent));
|
||||||
|
ft_lstadd_back(&head, tmp);
|
||||||
|
lst = lst->next;
|
||||||
|
}
|
||||||
|
return (head);
|
||||||
|
}
|
||||||
25
libft/ft_lstnew_bonus.c
Normal file
25
libft/ft_lstnew_bonus.c
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_lstnew.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/09 11:10:05 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/09 18:59:48 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
t_list *ft_lstnew(void *content)
|
||||||
|
{
|
||||||
|
t_list *tmp;
|
||||||
|
|
||||||
|
tmp = malloc(sizeof(*tmp));
|
||||||
|
if (!tmp)
|
||||||
|
return (NULL);
|
||||||
|
tmp->next = NULL;
|
||||||
|
tmp->content = content;
|
||||||
|
return (tmp);
|
||||||
|
}
|
||||||
28
libft/ft_lstsize_bonus.c
Normal file
28
libft/ft_lstsize_bonus.c
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_lstsize.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/09 13:43:08 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/09 18:21:11 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
int ft_lstsize(t_list *lst)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (!lst)
|
||||||
|
return (0);
|
||||||
|
i = 1;
|
||||||
|
while (lst->next)
|
||||||
|
{
|
||||||
|
lst = lst->next;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
31
libft/ft_memchr.c
Normal file
31
libft/ft_memchr.c
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_memchr.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/03 15:13:59 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/09 20:55:24 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void *ft_memchr(void *s, int c, size_t n)
|
||||||
|
{
|
||||||
|
unsigned char *p;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
p = (unsigned char *)s;
|
||||||
|
while (i < n)
|
||||||
|
{
|
||||||
|
if (p[i] == (unsigned char)c)
|
||||||
|
return ((void *)&p[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
if (p[i - 1] == (unsigned char)c)
|
||||||
|
return ((void *)&p[i]);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
31
libft/ft_memcmp.c
Normal file
31
libft/ft_memcmp.c
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_memcmp.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/03 15:27:15 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/09 19:21:02 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
int ft_memcmp(void *s1, void const *s2, size_t n)
|
||||||
|
{
|
||||||
|
unsigned char *p1;
|
||||||
|
unsigned char *p2;
|
||||||
|
unsigned int i;
|
||||||
|
|
||||||
|
p1 = (unsigned char *)s1;
|
||||||
|
p2 = (unsigned char *)s2;
|
||||||
|
i = 0;
|
||||||
|
while (i < n)
|
||||||
|
{
|
||||||
|
if (p1[i] != p2[i])
|
||||||
|
return (p1[i] - p2[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
32
libft/ft_memcpy.c
Normal file
32
libft/ft_memcpy.c
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_memcpy.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/02 17:13:18 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/09 19:21:29 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void *ft_memcpy(void *dest, void const *src, size_t n)
|
||||||
|
{
|
||||||
|
char *d;
|
||||||
|
char *s;
|
||||||
|
|
||||||
|
if (n == 0 || (dest == NULL && src == NULL))
|
||||||
|
return (dest);
|
||||||
|
if (dest == NULL || src == NULL)
|
||||||
|
{
|
||||||
|
*(char *)dest = 1;
|
||||||
|
*(char *)src = 1;
|
||||||
|
}
|
||||||
|
d = (char *) dest;
|
||||||
|
s = (char *) src;
|
||||||
|
while (n--)
|
||||||
|
*d++ = *s++;
|
||||||
|
return (dest);
|
||||||
|
}
|
||||||
30
libft/ft_memmove.c
Normal file
30
libft/ft_memmove.c
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_memmove.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/02 17:42:28 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/09 19:21:54 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void *ft_memmove(void *dest, void const *src, size_t n)
|
||||||
|
{
|
||||||
|
unsigned char *d;
|
||||||
|
unsigned char *s;
|
||||||
|
|
||||||
|
if (!dest && !src)
|
||||||
|
return (dest);
|
||||||
|
d = (unsigned char *)dest;
|
||||||
|
s = (unsigned char *)src;
|
||||||
|
if (s < d)
|
||||||
|
while (n--)
|
||||||
|
d[n] = s[n];
|
||||||
|
else
|
||||||
|
ft_memcpy(dest, (void *)src, n);
|
||||||
|
return (dest);
|
||||||
|
}
|
||||||
23
libft/ft_memset.c
Normal file
23
libft/ft_memset.c
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_memset.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/02 15:57:27 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/03 11:18:10 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void *ft_memset(void *s, int c, size_t n)
|
||||||
|
{
|
||||||
|
char *str;
|
||||||
|
|
||||||
|
str = (char *)s;
|
||||||
|
while (n--)
|
||||||
|
str[n] = c;
|
||||||
|
return (s);
|
||||||
|
}
|
||||||
18
libft/ft_putchar_fd.c
Normal file
18
libft/ft_putchar_fd.c
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_putchar_fd.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/07 17:34:30 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/07 17:53:33 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void ft_putchar_fd(char c, int fd)
|
||||||
|
{
|
||||||
|
write(fd, &c, 1);
|
||||||
|
}
|
||||||
21
libft/ft_putendl_fd.c
Normal file
21
libft/ft_putendl_fd.c
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_putendl_fd.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/07 17:57:33 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/07 18:00:49 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void ft_putendl_fd(char *str, int fd)
|
||||||
|
{
|
||||||
|
if (!str)
|
||||||
|
return ;
|
||||||
|
ft_putstr_fd(str, fd);
|
||||||
|
ft_putchar_fd(0x0a, fd);
|
||||||
|
}
|
||||||
27
libft/ft_putnbr_fd.c
Normal file
27
libft/ft_putnbr_fd.c
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_putnbr_fd.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/07 18:02:14 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/11 11:23:10 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void ft_putnbr_fd(int n, int fd)
|
||||||
|
{
|
||||||
|
if (n < 0)
|
||||||
|
ft_putchar_fd(0x2d, fd);
|
||||||
|
if (n <= -10)
|
||||||
|
ft_putnbr_fd(n / -10, fd);
|
||||||
|
if (n >= 10)
|
||||||
|
ft_putnbr_fd(n / 10, fd);
|
||||||
|
if (n >= 0)
|
||||||
|
ft_putchar_fd(n % 10 + 0x30, fd);
|
||||||
|
if (n < 0)
|
||||||
|
ft_putchar_fd(-(n % -10) + 0x30, fd);
|
||||||
|
}
|
||||||
21
libft/ft_putstr_fd.c
Normal file
21
libft/ft_putstr_fd.c
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_putstr_fd.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/07 17:48:41 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/07 17:52:16 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void ft_putstr_fd(char *str, int fd)
|
||||||
|
{
|
||||||
|
if (!str)
|
||||||
|
return ;
|
||||||
|
while (*str)
|
||||||
|
ft_putchar_fd(*str++, fd);
|
||||||
|
}
|
||||||
25
libft/ft_read.c
Normal file
25
libft/ft_read.c
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_read.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/11/18 12:49:30 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/12/10 21:18:38 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
int ft_read(int fd, char *character, unsigned int size_to_read)
|
||||||
|
{
|
||||||
|
int bytes_read;
|
||||||
|
|
||||||
|
bytes_read = read(fd, character, size_to_read);
|
||||||
|
if (bytes_read == -1)
|
||||||
|
{
|
||||||
|
ft_fprintf(STDERR_FILENO, "Read failed\n");
|
||||||
|
}
|
||||||
|
return (bytes_read);
|
||||||
|
}
|
||||||
84
libft/ft_split.c
Normal file
84
libft/ft_split.c
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_split.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/05 20:14:20 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/09 23:55:49 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
static int count(char const *s, char const c)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
|
||||||
|
n = 0;
|
||||||
|
if (*s != c && *s)
|
||||||
|
n = 1;
|
||||||
|
while (*s)
|
||||||
|
{
|
||||||
|
if (*s == c && *(s + 1) != c && *(s + 1))
|
||||||
|
n++;
|
||||||
|
s++;
|
||||||
|
}
|
||||||
|
return (n);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int count_sub(char const *s, char const c)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (*s != c && *s)
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
s++;
|
||||||
|
}
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char **free_all(char **back)
|
||||||
|
{
|
||||||
|
char **tmp;
|
||||||
|
|
||||||
|
tmp = back;
|
||||||
|
while (*back)
|
||||||
|
{
|
||||||
|
free(*back);
|
||||||
|
back++;
|
||||||
|
}
|
||||||
|
free(tmp);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
char **ft_split(char const *s, char const c)
|
||||||
|
{
|
||||||
|
char **tmp;
|
||||||
|
char **back;
|
||||||
|
|
||||||
|
if (!s)
|
||||||
|
return (NULL);
|
||||||
|
tmp = (char **)ft_calloc(sizeof(*tmp), count(s, c) + 1);
|
||||||
|
if (!tmp)
|
||||||
|
return (NULL);
|
||||||
|
back = tmp;
|
||||||
|
while (*s && tmp)
|
||||||
|
{
|
||||||
|
while (*s == c && *s)
|
||||||
|
s++;
|
||||||
|
if (*s)
|
||||||
|
{
|
||||||
|
*tmp = ft_substr(s, 0, count_sub(s, c));
|
||||||
|
if (!*tmp)
|
||||||
|
return (free_all(back));
|
||||||
|
}
|
||||||
|
tmp++;
|
||||||
|
while (*s != c && *s)
|
||||||
|
s++;
|
||||||
|
}
|
||||||
|
return (back);
|
||||||
|
}
|
||||||
29
libft/ft_strchr.c
Normal file
29
libft/ft_strchr.c
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strchr.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/03 12:20:50 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/09 20:54:14 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
char *ft_strchr(char const *s, int c)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (s[i])
|
||||||
|
{
|
||||||
|
if (s[i] == (char)c)
|
||||||
|
return ((char *)&s[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
if ((char)c == 0)
|
||||||
|
return ((char *)&s[i]);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
31
libft/ft_strdup.c
Normal file
31
libft/ft_strdup.c
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strdup.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/04 18:44:05 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/12 10:44:23 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
char *ft_strdup(char const *s)
|
||||||
|
{
|
||||||
|
char *tmp;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
tmp = ft_calloc(ft_strlen(s) + 1, sizeof(*tmp));
|
||||||
|
if (!tmp)
|
||||||
|
return (NULL);
|
||||||
|
while (s[i])
|
||||||
|
{
|
||||||
|
tmp[i] = s[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
tmp[i] = 0;
|
||||||
|
return (tmp);
|
||||||
|
}
|
||||||
27
libft/ft_striteri.c
Normal file
27
libft/ft_striteri.c
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_striteri.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/07 17:24:08 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/09 19:27:07 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
void ft_striteri(char const *s, void (*f)(unsigned int, char *))
|
||||||
|
{
|
||||||
|
unsigned int i;
|
||||||
|
|
||||||
|
if (!s || !f)
|
||||||
|
return ;
|
||||||
|
i = 0;
|
||||||
|
while (s[i])
|
||||||
|
{
|
||||||
|
f(i, (char *)&s[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
41
libft/ft_strjoin.c
Normal file
41
libft/ft_strjoin.c
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strjoin.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/04 19:08:03 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/12 10:07:26 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
char *ft_strjoin(char const *s1, char const *s2)
|
||||||
|
{
|
||||||
|
char *tmp;
|
||||||
|
unsigned int i;
|
||||||
|
unsigned int j;
|
||||||
|
|
||||||
|
if (!s1 || !s2)
|
||||||
|
return (NULL);
|
||||||
|
tmp = ft_calloc(ft_strlen(s1) + ft_strlen(s2) + 1, sizeof(*tmp));
|
||||||
|
if (!tmp)
|
||||||
|
return (NULL);
|
||||||
|
i = 0;
|
||||||
|
while (s1[i])
|
||||||
|
{
|
||||||
|
tmp[i] = s1[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
j = 0;
|
||||||
|
while (s2[j])
|
||||||
|
{
|
||||||
|
tmp[i] = s2[j];
|
||||||
|
i++;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
tmp[i] = 0;
|
||||||
|
return (tmp);
|
||||||
|
}
|
||||||
37
libft/ft_strlcat.c
Normal file
37
libft/ft_strlcat.c
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strlcat.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/04 16:10:59 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/10 16:41:14 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
size_t ft_strlcat(char *dest, char const *src, size_t size)
|
||||||
|
{
|
||||||
|
size_t dlen;
|
||||||
|
size_t slen;
|
||||||
|
|
||||||
|
if (size == 0)
|
||||||
|
return (ft_strlen(src));
|
||||||
|
dlen = ft_strlen(dest);
|
||||||
|
slen = ft_strlen(src);
|
||||||
|
if (size <= dlen)
|
||||||
|
return (slen + size);
|
||||||
|
if (dlen + slen < size)
|
||||||
|
{
|
||||||
|
ft_memcpy(&dest[dlen], src, slen);
|
||||||
|
dest[dlen + slen] = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ft_memcpy(&dest[dlen], src, size - dlen);
|
||||||
|
dest[size - 1] = 0;
|
||||||
|
}
|
||||||
|
return (dlen + slen);
|
||||||
|
}
|
||||||
32
libft/ft_strlcpy.c
Normal file
32
libft/ft_strlcpy.c
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strlcpy.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/02 19:03:18 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/09 19:28:44 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
size_t ft_strlcpy(char *dest, char const *src, size_t size)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (size != 0)
|
||||||
|
{
|
||||||
|
while (src[i] != '\0' && i < size - 1)
|
||||||
|
{
|
||||||
|
dest[i] = src[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
dest[i] = '\0';
|
||||||
|
}
|
||||||
|
while (src[i] != '\0')
|
||||||
|
i++;
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
27
libft/ft_strlen.c
Normal file
27
libft/ft_strlen.c
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strlen.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/02 16:01:03 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/12 10:40:59 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
size_t ft_strlen(char const *str)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (*str++)
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
if (i == SIZE_MAX)
|
||||||
|
break ;
|
||||||
|
}
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
33
libft/ft_strmapi.c
Normal file
33
libft/ft_strmapi.c
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strmapi.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/07 17:01:35 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/12 10:13:57 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
|
||||||
|
{
|
||||||
|
char *tmp;
|
||||||
|
unsigned int i;
|
||||||
|
|
||||||
|
if (!s || !f)
|
||||||
|
return (NULL);
|
||||||
|
tmp = ft_calloc(ft_strlen(s) + 1, sizeof(*tmp));
|
||||||
|
if (!tmp)
|
||||||
|
return (NULL);
|
||||||
|
i = 0;
|
||||||
|
while (s[i])
|
||||||
|
{
|
||||||
|
tmp[i] = f(i, s[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
tmp[i] = 0;
|
||||||
|
return (tmp);
|
||||||
|
}
|
||||||
31
libft/ft_strncmp.c
Normal file
31
libft/ft_strncmp.c
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strncmp.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/03 13:33:22 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/12 10:29:59 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
int ft_strncmp(char const *s1, char const *s2, size_t n)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
if (n == 0)
|
||||||
|
return (0);
|
||||||
|
i = 0;
|
||||||
|
while ((s1[i] || s2[i]) && i < n)
|
||||||
|
{
|
||||||
|
if (s1[i] < s2[i])
|
||||||
|
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
|
||||||
|
else if (s1[i] > s2[i])
|
||||||
|
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
36
libft/ft_strnstr.c
Normal file
36
libft/ft_strnstr.c
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strnstr.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/03 15:36:41 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/10 16:42:02 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
char *ft_strnstr(char const *s1, char const *s2, size_t n)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
size_t j;
|
||||||
|
|
||||||
|
if (!*s2)
|
||||||
|
return ((char *)s1);
|
||||||
|
i = 0;
|
||||||
|
while (i < n && s1[i])
|
||||||
|
{
|
||||||
|
j = 0;
|
||||||
|
if (s1[i + j] == s2[j])
|
||||||
|
{
|
||||||
|
while (s1[i + j] == s2[j] && s1[i + j] && i + j < n)
|
||||||
|
j++;
|
||||||
|
if (!s2[j])
|
||||||
|
return ((char *)&s1[i]);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
33
libft/ft_strrchr.c
Normal file
33
libft/ft_strrchr.c
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strrchr.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/03 12:20:50 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/09 20:54:22 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
char *ft_strrchr(char const *s, int c)
|
||||||
|
{
|
||||||
|
char *n;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
n = NULL;
|
||||||
|
while (s[i] != 0)
|
||||||
|
{
|
||||||
|
if (s[i] == (char)c)
|
||||||
|
n = (char *)&s[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
if ((char)c == 0)
|
||||||
|
return ((char *)&s[i]);
|
||||||
|
if (n)
|
||||||
|
return (n);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
47
libft/ft_strtrim.c
Normal file
47
libft/ft_strtrim.c
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strtrim.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/04 19:30:35 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/15 13:07:55 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
static int is_set(char c, char const *s)
|
||||||
|
{
|
||||||
|
while (*s)
|
||||||
|
if (c == *s++)
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int find_end(char const *s1, char const *set)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (*s1)
|
||||||
|
s1++;
|
||||||
|
while (is_set(*--s1, set))
|
||||||
|
i++;
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *ft_strtrim(char const *s1, char const *set)
|
||||||
|
{
|
||||||
|
if (!s1)
|
||||||
|
return (NULL);
|
||||||
|
if (!set)
|
||||||
|
return ((char *)s1);
|
||||||
|
while (is_set(*s1, set) && *s1)
|
||||||
|
s1++;
|
||||||
|
if ((int)ft_strlen(s1) > find_end(s1, set))
|
||||||
|
return (ft_substr(s1, 0, ft_strlen(s1) - find_end(s1, set)));
|
||||||
|
else
|
||||||
|
return (ft_calloc(1, 1));
|
||||||
|
}
|
||||||
41
libft/ft_substr.c
Normal file
41
libft/ft_substr.c
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_substr.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/04 18:58:25 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/09 21:50:44 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
char *ft_substr(char const *s, unsigned int start, size_t len)
|
||||||
|
{
|
||||||
|
char *tmp;
|
||||||
|
unsigned int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (!s || start >= ft_strlen(s) || len <= 0)
|
||||||
|
{
|
||||||
|
tmp = malloc(1);
|
||||||
|
if (!tmp)
|
||||||
|
return (NULL);
|
||||||
|
tmp[i] = 0;
|
||||||
|
return (tmp);
|
||||||
|
}
|
||||||
|
if (len + start > ft_strlen(s))
|
||||||
|
len = ft_strlen(s) - start;
|
||||||
|
tmp = malloc(sizeof(*tmp) * len + 1);
|
||||||
|
if (!tmp)
|
||||||
|
return (NULL);
|
||||||
|
while (i < len && s[i])
|
||||||
|
{
|
||||||
|
tmp[i] = s[i + start];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
tmp[i] = 0;
|
||||||
|
return (tmp);
|
||||||
|
}
|
||||||
20
libft/ft_tolower.c
Normal file
20
libft/ft_tolower.c
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_tolower.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/03 12:01:54 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/03 12:15:18 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
int ft_tolower(int c)
|
||||||
|
{
|
||||||
|
if (c >= 65 && c <= 90)
|
||||||
|
c += 32;
|
||||||
|
return (c);
|
||||||
|
}
|
||||||
20
libft/ft_toupper.c
Normal file
20
libft/ft_toupper.c
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_toupper.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/03 12:18:44 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/04/03 12:19:05 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft.h"
|
||||||
|
|
||||||
|
int ft_toupper(int c)
|
||||||
|
{
|
||||||
|
if (c >= 97 && c <= 122)
|
||||||
|
c -= 32;
|
||||||
|
return (c);
|
||||||
|
}
|
||||||
BIN
libft/lib/libft.a
Normal file
BIN
libft/lib/libft.a
Normal file
Binary file not shown.
92
libft/libft.h
Normal file
92
libft/libft.h
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* libft.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: bszilas <bszilas@student.42vienna.com> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/09 23:43:36 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/12/10 21:12:01 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef LIBFT_H
|
||||||
|
|
||||||
|
# define LIBFT_H
|
||||||
|
|
||||||
|
# include "../gnl/get_next_line.h"
|
||||||
|
# include "./printf/ft_printf.h"
|
||||||
|
# include <stdlib.h>
|
||||||
|
# include <unistd.h>
|
||||||
|
# include <stddef.h>
|
||||||
|
# include <stdint.h>
|
||||||
|
# include <limits.h>
|
||||||
|
|
||||||
|
typedef struct s_list
|
||||||
|
{
|
||||||
|
void *content;
|
||||||
|
struct s_list *next;
|
||||||
|
} t_list;
|
||||||
|
|
||||||
|
int ft_isalpha(int c);
|
||||||
|
int ft_isdigit(int c);
|
||||||
|
int ft_isalnum(int c);
|
||||||
|
int ft_isprint(int c);
|
||||||
|
int ft_isascii(int c);
|
||||||
|
int ft_isspace(int c);
|
||||||
|
|
||||||
|
/*Memory Management*/
|
||||||
|
int ft_memcmp(void *s1, void const *s2, size_t n);
|
||||||
|
void *ft_memset(void *s, int c, size_t n);
|
||||||
|
void ft_bzero(void *s, size_t n);
|
||||||
|
void *ft_memcpy(void *dest, void const *src, size_t n);
|
||||||
|
void *ft_memmove(void *dest, void const *src, size_t n);
|
||||||
|
void *ft_memchr(void *s, int c, size_t n);
|
||||||
|
void *ft_calloc(size_t n, size_t s);
|
||||||
|
|
||||||
|
/*Info Conversion*/
|
||||||
|
int ft_atoi(char const *s);
|
||||||
|
double ft_atod(char *s);
|
||||||
|
char *ft_itoa(int n);
|
||||||
|
char **ft_split(char const *s, char c);
|
||||||
|
void ft_free(void *ptr_ptr);
|
||||||
|
long ft_atol(const char *nptr);
|
||||||
|
|
||||||
|
/*String Manip*/
|
||||||
|
int ft_toupper(int c);
|
||||||
|
int ft_tolower(int c);
|
||||||
|
int ft_strncmp(char const *s1, char const *s2, size_t n);
|
||||||
|
char *ft_strnstr(char const *s1, char const *s2, size_t n);
|
||||||
|
char *ft_strchr(char const *s, int c);
|
||||||
|
char *ft_strrchr(char const *s, int c);
|
||||||
|
char *ft_strdup(char const *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_strmapi(char const *s, char (*f)(unsigned int, char));
|
||||||
|
void ft_striteri(char const *s, void (*f)(unsigned int, char *));
|
||||||
|
size_t ft_strlcpy(char *dest, char const *src, size_t size);
|
||||||
|
size_t ft_strlcat(char *dest, char const *src, size_t size);
|
||||||
|
size_t ft_strlen(char const *str);
|
||||||
|
|
||||||
|
/*List manip*/
|
||||||
|
int ft_lstsize(t_list *lst);
|
||||||
|
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 *));
|
||||||
|
void ft_lstadd_front(t_list **lst, t_list *node_new);
|
||||||
|
void ft_lstadd_back(t_list **lst, t_list *node_new);
|
||||||
|
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
|
||||||
|
t_list *ft_lstnew(void *content);
|
||||||
|
t_list *ft_lstlast(t_list *lst);
|
||||||
|
|
||||||
|
/*Output*/
|
||||||
|
void ft_putchar_fd(char c, int fd);
|
||||||
|
void ft_putstr_fd(char *str, int fd);
|
||||||
|
void ft_putendl_fd(char *str, int fd);
|
||||||
|
void ft_putnbr_fd(int n, int fd);
|
||||||
|
|
||||||
|
/* Get Next Line */
|
||||||
|
char *get_next_line(int fd);
|
||||||
|
|
||||||
|
#endif
|
||||||
50
libft/printf/Makefile
Normal file
50
libft/printf/Makefile
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# ::: :::::::: #
|
||||||
|
# Makefile :+: :+: :+: #
|
||||||
|
# +:+ +:+ +:+ #
|
||||||
|
# By: victor </var/spool/mail/victor> +#+ +:+ +#+ #
|
||||||
|
# +#+#+#+#+#+ +#+ #
|
||||||
|
# Created: 2024/09/14 09:21:46 by victor #+# #+# #
|
||||||
|
# Updated: 2024/11/11 11:52:05 by marvin ### ########.fr #
|
||||||
|
# #
|
||||||
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
LIBDIR := lib
|
||||||
|
NAME := $(LIBDIR)/libftprintf.a
|
||||||
|
CC := gcc
|
||||||
|
CFLAGS = -Wall -Werror -Wextra -g3
|
||||||
|
SRC := ft_fprintf.c ft_printf.c ft_putascii.c ft_puthex.c ft_putptr.c ft_strlen.c ft_putfloat.c
|
||||||
|
OBJ := $(SRC:.c=.o)
|
||||||
|
|
||||||
|
ifdef RELEASE
|
||||||
|
CFLAGS -= -g3
|
||||||
|
endif
|
||||||
|
|
||||||
|
MKDIR = mkdir
|
||||||
|
ifeq ($(OS), Windows_NT)
|
||||||
|
RM = del /Q
|
||||||
|
else
|
||||||
|
RM = rm -rf
|
||||||
|
MKDIR += -p
|
||||||
|
endif
|
||||||
|
|
||||||
|
all: $(NAME)
|
||||||
|
|
||||||
|
$(NAME): $(OBJ)
|
||||||
|
ar rsc $@ $(OBJ)
|
||||||
|
|
||||||
|
$(OBJ): $(SRC) $(LIBDIR)
|
||||||
|
$(CC) $(CFLAGS) -c $(SRC)
|
||||||
|
|
||||||
|
$(LIBDIR):
|
||||||
|
$(MKDIR) $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(RM) $(OBJ)
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
$(RM) $(LIBDIR)
|
||||||
|
|
||||||
|
|
||||||
|
re: fclean all
|
||||||
38
libft/printf/ft_fprintf.c
Normal file
38
libft/printf/ft_fprintf.c
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_fprintf.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/09/17 12:04:30 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/09/17 12:09:29 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "./ft_printf.h"
|
||||||
|
|
||||||
|
int ft_fprintf(int fd, const char *format, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
int count;
|
||||||
|
|
||||||
|
if (!format || fd < 0)
|
||||||
|
return (-1);
|
||||||
|
va_start(args, format);
|
||||||
|
count = 0;
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
while (*format != '%' && *format)
|
||||||
|
ft_putchar(*format++, &count, fd);
|
||||||
|
if (!*format)
|
||||||
|
break ;
|
||||||
|
else
|
||||||
|
format++;
|
||||||
|
if (!*format || !handle_arg(args, *format, &count, fd))
|
||||||
|
return (-1);
|
||||||
|
format++;
|
||||||
|
}
|
||||||
|
va_end(args);
|
||||||
|
return (count);
|
||||||
|
}
|
||||||
63
libft/printf/ft_printf.c
Normal file
63
libft/printf/ft_printf.c
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_printf.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/18 17:50:35 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/09/17 14:11:57 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_printf.h"
|
||||||
|
|
||||||
|
int handle_arg(va_list args, char format, int *count, int fd)
|
||||||
|
{
|
||||||
|
if (format == 'd' || format == 'i')
|
||||||
|
ft_putnbr(va_arg(args, int), count, fd);
|
||||||
|
else if (format == 'u')
|
||||||
|
ft_putnbr(va_arg(args, unsigned int), count, fd);
|
||||||
|
else if (format == 's')
|
||||||
|
ft_putstr(va_arg(args, char *), count, fd);
|
||||||
|
else if (format == 'X')
|
||||||
|
ft_puthex_upper(va_arg(args, unsigned int), count, fd);
|
||||||
|
else if (format == 'x')
|
||||||
|
ft_puthex_lower(va_arg(args, unsigned int), count, fd);
|
||||||
|
else if (format == 'p')
|
||||||
|
ft_putptr(va_arg(args, void *), count, fd);
|
||||||
|
else if (format == 'c')
|
||||||
|
ft_putchar(va_arg(args, int), count, fd);
|
||||||
|
else if (format == '%')
|
||||||
|
ft_putchar('%', count, fd);
|
||||||
|
else if (format == 'f')
|
||||||
|
ft_putnbrf(va_arg(args, double), count, 5, fd);
|
||||||
|
else
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_printf(const char *format, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
int count;
|
||||||
|
|
||||||
|
if (!format)
|
||||||
|
return (-1);
|
||||||
|
va_start(args, format);
|
||||||
|
count = 0;
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
while (*format != '%' && *format)
|
||||||
|
ft_putchar(*format++, &count, STDOUT_FILENO);
|
||||||
|
if (!*format)
|
||||||
|
break ;
|
||||||
|
else
|
||||||
|
format++;
|
||||||
|
if (!*format || !handle_arg(args, *format, &count, STDOUT_FILENO))
|
||||||
|
return (-1);
|
||||||
|
format++;
|
||||||
|
}
|
||||||
|
va_end(args);
|
||||||
|
return (count);
|
||||||
|
}
|
||||||
52
libft/printf/ft_printf.h
Normal file
52
libft/printf/ft_printf.h
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_printf.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/18 12:50:35 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/11/11 11:42:50 by marvin ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef FT_PRINTF_H
|
||||||
|
|
||||||
|
# define FT_PRINTF_H
|
||||||
|
|
||||||
|
# include <stdarg.h>
|
||||||
|
# include <stdlib.h>
|
||||||
|
# include <stdio.h>
|
||||||
|
|
||||||
|
# ifdef _WIN32
|
||||||
|
# include <io.h>
|
||||||
|
# undef STDIN_FILENO
|
||||||
|
|
||||||
|
# define STDIN_FILENO _fileno(stdin)
|
||||||
|
# undef STDOUT_FILENO
|
||||||
|
|
||||||
|
# define STDOUT_FILENO _fileno(stdout)
|
||||||
|
|
||||||
|
# define write _write
|
||||||
|
|
||||||
|
# else
|
||||||
|
|
||||||
|
# include <unistd.h>
|
||||||
|
|
||||||
|
# endif
|
||||||
|
|
||||||
|
int ft_fprintf(int fd, const char *format, ...);
|
||||||
|
int ft_printf(const char *format, ...);
|
||||||
|
|
||||||
|
int handle_arg(va_list args, char format, int *count, int fd);
|
||||||
|
|
||||||
|
void ft_puthex_lower(unsigned long nbr, int *count, int fd);
|
||||||
|
void ft_puthex_upper(unsigned long nbr, int *count, int fd);
|
||||||
|
void ft_putchar(int c, int *count, int fd);
|
||||||
|
void ft_putnbr(long n, int *count, int fd);
|
||||||
|
void ft_putnbrf(double f, int *count, int precision, int fd);
|
||||||
|
void ft_putstr(const char *str, int *count, int fd);
|
||||||
|
void ft_putptr(void *ptr, int *count, int fd);
|
||||||
|
size_t ft_strlen(const char *s);
|
||||||
|
|
||||||
|
#endif
|
||||||
43
libft/printf/ft_putascii.c
Normal file
43
libft/printf/ft_putascii.c
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_putascii.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/18 17:54:53 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/09/17 16:35:10 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_printf.h"
|
||||||
|
|
||||||
|
void ft_putchar(int c, int *count, int fd)
|
||||||
|
{
|
||||||
|
write(fd, &c, 1);
|
||||||
|
*count += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_putnbr(long n, int *count, int fd)
|
||||||
|
{
|
||||||
|
if (n < 0)
|
||||||
|
ft_putchar(0x2d, count, fd);
|
||||||
|
if (n <= -10)
|
||||||
|
ft_putnbr(n / -10, count, fd);
|
||||||
|
if (n >= 10)
|
||||||
|
ft_putnbr(n / 10, count, fd);
|
||||||
|
if (n >= 0)
|
||||||
|
ft_putchar(n % 10 + 0x30, count, fd);
|
||||||
|
if (n < 0)
|
||||||
|
ft_putchar(-(n % -10) + 0x30, count, fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_putstr(const char *str, int *count, int fd)
|
||||||
|
{
|
||||||
|
if (!str)
|
||||||
|
{
|
||||||
|
*count += ft_fprintf(fd, "(null)");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
*count += ft_fprintf(fd, str);
|
||||||
|
}
|
||||||
34
libft/printf/ft_putfloat.c
Normal file
34
libft/printf/ft_putfloat.c
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_putfloat.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/09/17 11:50:45 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/09/17 12:07:47 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_printf.h"
|
||||||
|
|
||||||
|
void ft_putnbrf(double f, int *count, int precision, int fd)
|
||||||
|
{
|
||||||
|
int number;
|
||||||
|
double fraction;
|
||||||
|
unsigned int mult;
|
||||||
|
|
||||||
|
mult = 1;
|
||||||
|
while (precision-- > 0)
|
||||||
|
{
|
||||||
|
mult *= 10;
|
||||||
|
}
|
||||||
|
number = (int)f;
|
||||||
|
fraction = f - (double)number;
|
||||||
|
if (fraction < 0)
|
||||||
|
fraction = -fraction;
|
||||||
|
ft_putnbr(number, count, fd);
|
||||||
|
ft_putchar('.', count, fd);
|
||||||
|
number = fraction * mult;
|
||||||
|
ft_putnbr(number, count, fd);
|
||||||
|
}
|
||||||
33
libft/printf/ft_puthex.c
Normal file
33
libft/printf/ft_puthex.c
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_puthex.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/18 17:52:38 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/09/17 12:09:29 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_printf.h"
|
||||||
|
|
||||||
|
void ft_puthex_upper(unsigned long nbr, int *count, int fd)
|
||||||
|
{
|
||||||
|
char *base_str;
|
||||||
|
|
||||||
|
base_str = "0123456789ABCDEF";
|
||||||
|
if (nbr >= 16)
|
||||||
|
ft_puthex_upper(nbr / 16, count, fd);
|
||||||
|
ft_putchar(base_str[(nbr % 16)], count, fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_puthex_lower(unsigned long nbr, int *count, int fd)
|
||||||
|
{
|
||||||
|
char *base_str;
|
||||||
|
|
||||||
|
base_str = "0123456789abcdef";
|
||||||
|
if (nbr >= 16)
|
||||||
|
ft_puthex_lower(nbr / 16, count, fd);
|
||||||
|
ft_putchar(base_str[(nbr % 16)], count, fd);
|
||||||
|
}
|
||||||
27
libft/printf/ft_putptr.c
Normal file
27
libft/printf/ft_putptr.c
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_putptr.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/18 17:53:53 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/11/10 20:42:58 by marvin ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_printf.h"
|
||||||
|
|
||||||
|
void ft_putptr(void *ptr, int *count, int fd)
|
||||||
|
{
|
||||||
|
void **to_print;
|
||||||
|
|
||||||
|
if (!ptr)
|
||||||
|
{
|
||||||
|
*count += ft_fprintf(fd, "(nil)");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
to_print = &ptr;
|
||||||
|
*count += ft_printf("0x");
|
||||||
|
ft_puthex_lower((unsigned long long)*to_print, count, fd);
|
||||||
|
}
|
||||||
28
libft/printf/ft_strlen.c
Normal file
28
libft/printf/ft_strlen.c
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strlen.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/02 16:01:03 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/09/14 09:24:02 by victor ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_printf.h"
|
||||||
|
#include "limits.h"
|
||||||
|
|
||||||
|
size_t ft_strlen(char const *str)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (*str++)
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
if (i == ULONG_MAX)
|
||||||
|
break ;
|
||||||
|
}
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
57
memory/Makefile
Normal file
57
memory/Makefile
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# ::: :::::::: #
|
||||||
|
# Makefile :+: :+: :+: #
|
||||||
|
# +:+ +:+ +:+ #
|
||||||
|
# By: vvobis <marvin@42.fr> +#+ +:+ +#+ #
|
||||||
|
# +#+#+#+#+#+ +#+ #
|
||||||
|
# Created: 2024/11/10 15:02:31 by vvobis #+# #+# #
|
||||||
|
# Updated: 2024/12/10 21:23:05 by vvobis ### ########.fr #
|
||||||
|
# #
|
||||||
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
CC := gcc
|
||||||
|
|
||||||
|
CFLAGS = -Werror -Wextra -Wall
|
||||||
|
|
||||||
|
SRC := memory.c list.c ft_free.c
|
||||||
|
|
||||||
|
OBJDIR := obj
|
||||||
|
OBJ := $(SRC:%.c=$(OBJDIR)/%.o)
|
||||||
|
|
||||||
|
LIBDIR := lib
|
||||||
|
TARGET := $(LIBDIR)/memory.a
|
||||||
|
|
||||||
|
ifdef RELEASE
|
||||||
|
CFLAGS -= -g3
|
||||||
|
endif
|
||||||
|
|
||||||
|
MKDIR = mkdir
|
||||||
|
ifeq ($(OS), Windows_NT)
|
||||||
|
RM = del /S /Q
|
||||||
|
else
|
||||||
|
RM = rm -rf
|
||||||
|
MKDIR += -p
|
||||||
|
endif
|
||||||
|
|
||||||
|
all: $(TARGET)
|
||||||
|
|
||||||
|
$(TARGET): $(OBJ) | $(LIBDIR)
|
||||||
|
ar rcs $@ $(OBJ)
|
||||||
|
|
||||||
|
$(OBJDIR)/%.o: %.c | $(OBJDIR)
|
||||||
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
$(LIBDIR): $(OBJDIR)
|
||||||
|
$(MKDIR) $(LIBDIR)
|
||||||
|
|
||||||
|
$(OBJDIR):
|
||||||
|
$(MKDIR) $(OBJDIR)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(RM) $(OBJDIR)
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
$(RM) $(LIBDIR)
|
||||||
|
|
||||||
|
re: fclean all
|
||||||
22
memory/ft_free.c
Normal file
22
memory/ft_free.c
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_free.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/09/10 12:16:39 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/12/10 21:23:56 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "memory.h"
|
||||||
|
|
||||||
|
void ft_free(void *ptr_ptr)
|
||||||
|
{
|
||||||
|
void **ptr;
|
||||||
|
|
||||||
|
ptr = ptr_ptr;
|
||||||
|
free(*ptr);
|
||||||
|
*ptr = NULL;
|
||||||
|
}
|
||||||
BIN
memory/lib/memory.a
Normal file
BIN
memory/lib/memory.a
Normal file
Binary file not shown.
96
memory/list.c
Normal file
96
memory/list.c
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* list.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: anarama <anarama@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/27 11:44:46 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/11/10 15:09:54 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "list.h"
|
||||||
|
#include "memory.h"
|
||||||
|
|
||||||
|
t_clean *lst_node_new(void *content, void (*del)(void *))
|
||||||
|
{
|
||||||
|
t_clean *new;
|
||||||
|
|
||||||
|
if (!content || !del)
|
||||||
|
return (NULL);
|
||||||
|
new = malloc(sizeof(*new));
|
||||||
|
if (!new)
|
||||||
|
return (NULL);
|
||||||
|
new->content = content;
|
||||||
|
new->clean = del;
|
||||||
|
new->next = NULL;
|
||||||
|
return (new);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lst_node_del(t_clean **lst)
|
||||||
|
{
|
||||||
|
(*lst)->clean((*lst)->content);
|
||||||
|
ft_free(&*lst);
|
||||||
|
}
|
||||||
|
|
||||||
|
void lst_node_del_clean(t_clean **lst, void *mem)
|
||||||
|
{
|
||||||
|
t_clean *tmp;
|
||||||
|
t_clean *head;
|
||||||
|
|
||||||
|
if (!lst || !*lst)
|
||||||
|
return ;
|
||||||
|
head = *lst;
|
||||||
|
tmp = *lst;
|
||||||
|
while ((*lst) && (*lst)->next->content != mem)
|
||||||
|
(*lst) = (*lst)->next;
|
||||||
|
if (!*lst)
|
||||||
|
return ;
|
||||||
|
tmp = *lst;
|
||||||
|
if ((*lst)->next && (*lst)->next->next)
|
||||||
|
{
|
||||||
|
tmp = (*lst)->next->next;
|
||||||
|
lst_node_del(&(*lst)->next);
|
||||||
|
(*lst)->next = tmp;
|
||||||
|
}
|
||||||
|
else if ((*lst)->next)
|
||||||
|
lst_node_del(&(*lst)->next);
|
||||||
|
else
|
||||||
|
lst_node_del(lst);
|
||||||
|
*lst = head;
|
||||||
|
}
|
||||||
|
|
||||||
|
void lst_list_clean(t_clean **head)
|
||||||
|
{
|
||||||
|
t_clean *tmp;
|
||||||
|
|
||||||
|
while (*head)
|
||||||
|
{
|
||||||
|
tmp = (*head)->next;
|
||||||
|
lst_node_del(head);
|
||||||
|
*head = tmp;
|
||||||
|
}
|
||||||
|
free(*head);
|
||||||
|
}
|
||||||
|
|
||||||
|
int lst_add_back(t_clean **node, t_clean *new)
|
||||||
|
{
|
||||||
|
t_clean *tmp;
|
||||||
|
|
||||||
|
if (!new)
|
||||||
|
{
|
||||||
|
perror("malloc");
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
if (*node)
|
||||||
|
{
|
||||||
|
tmp = *node;
|
||||||
|
while (tmp->next)
|
||||||
|
tmp = tmp->next;
|
||||||
|
tmp->next = new;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
*node = new;
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
33
memory/list.h
Normal file
33
memory/list.h
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* list.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/11/10 14:48:59 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/11/10 15:08:38 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef LIST_C
|
||||||
|
# define LIST_C
|
||||||
|
|
||||||
|
# include <stdlib.h>
|
||||||
|
# include <stdio.h>
|
||||||
|
|
||||||
|
typedef struct s_clean
|
||||||
|
{
|
||||||
|
void *content;
|
||||||
|
void (*clean)(void *del);
|
||||||
|
struct s_clean *next;
|
||||||
|
} t_clean;
|
||||||
|
|
||||||
|
/* List */
|
||||||
|
t_clean *lst_node_new(void *content, void (*del)(void *));
|
||||||
|
void lst_node_del(t_clean **lst);
|
||||||
|
void lst_node_del_clean(t_clean **lst, void *mem);
|
||||||
|
void lst_list_clean(t_clean **head);
|
||||||
|
int lst_add_back(t_clean **node, t_clean *node_new);
|
||||||
|
|
||||||
|
#endif
|
||||||
36
memory/memory.c
Normal file
36
memory/memory.c
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* memory.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/11/10 14:47:33 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/11/11 11:53:06 by marvin ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
# include "list.h"
|
||||||
|
# include "memory.h"
|
||||||
|
# include <stdlib.h>
|
||||||
|
# include <stdio.h>
|
||||||
|
|
||||||
|
void lst_memory(void *mem, void (*del)(void *c), int mode)
|
||||||
|
{
|
||||||
|
static t_clean *list;
|
||||||
|
t_clean *new;
|
||||||
|
|
||||||
|
if (mode == FAIL)
|
||||||
|
return (lst_list_clean(&list), exit(EXIT_FAILURE));
|
||||||
|
else if (mode == END)
|
||||||
|
return (lst_list_clean(&list));
|
||||||
|
else if (mode == FREE)
|
||||||
|
return (lst_node_del_clean(&list, mem));
|
||||||
|
else if (!mem && mode != AT_EXIT)
|
||||||
|
return (lst_list_clean(&list), perror("malloc"), exit(EXIT_FAILURE));
|
||||||
|
new = lst_node_new(mem, del);
|
||||||
|
if (!new)
|
||||||
|
return (del(mem), lst_list_clean(&list), \
|
||||||
|
perror(""), exit(EXIT_FAILURE));
|
||||||
|
lst_add_back(&list, new);
|
||||||
|
}
|
||||||
30
memory/memory.h
Normal file
30
memory/memory.h
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* memory.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/11/10 14:46:26 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/12/10 21:22:01 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef MEMORY_H
|
||||||
|
# define MEMORY_H
|
||||||
|
|
||||||
|
# include <stdlib.h>
|
||||||
|
# include "../libft/libft.h"
|
||||||
|
|
||||||
|
enum e_alloc
|
||||||
|
{
|
||||||
|
ADD,
|
||||||
|
FAIL,
|
||||||
|
END,
|
||||||
|
FREE,
|
||||||
|
AT_EXIT,
|
||||||
|
};
|
||||||
|
|
||||||
|
void lst_memory(void *mem, void (*del)(void *c), int mode);
|
||||||
|
|
||||||
|
#endif
|
||||||
BIN
memory/obj/ft_free.o
Normal file
BIN
memory/obj/ft_free.o
Normal file
Binary file not shown.
BIN
memory/obj/list.o
Normal file
BIN
memory/obj/list.o
Normal file
Binary file not shown.
BIN
memory/obj/memory.o
Normal file
BIN
memory/obj/memory.o
Normal file
Binary file not shown.
50
printf/Makefile
Normal file
50
printf/Makefile
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# ::: :::::::: #
|
||||||
|
# Makefile :+: :+: :+: #
|
||||||
|
# +:+ +:+ +:+ #
|
||||||
|
# By: victor </var/spool/mail/victor> +#+ +:+ +#+ #
|
||||||
|
# +#+#+#+#+#+ +#+ #
|
||||||
|
# Created: 2024/09/14 09:21:46 by victor #+# #+# #
|
||||||
|
# Updated: 2024/11/11 11:52:05 by marvin ### ########.fr #
|
||||||
|
# #
|
||||||
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
LIBDIR := lib
|
||||||
|
NAME := $(LIBDIR)/libftprintf.a
|
||||||
|
CC := gcc
|
||||||
|
CFLAGS = -Wall -Werror -Wextra -g3
|
||||||
|
SRC := ft_fprintf.c ft_printf.c ft_putascii.c ft_puthex.c ft_putptr.c ft_strlen.c ft_putfloat.c
|
||||||
|
OBJ := $(SRC:.c=.o)
|
||||||
|
|
||||||
|
ifdef RELEASE
|
||||||
|
CFLAGS -= -g3
|
||||||
|
endif
|
||||||
|
|
||||||
|
MKDIR = mkdir
|
||||||
|
ifeq ($(OS), Windows_NT)
|
||||||
|
RM = del /Q
|
||||||
|
else
|
||||||
|
RM = rm -rf
|
||||||
|
MKDIR += -p
|
||||||
|
endif
|
||||||
|
|
||||||
|
all: $(NAME)
|
||||||
|
|
||||||
|
$(NAME): $(OBJ)
|
||||||
|
ar rsc $@ $(OBJ)
|
||||||
|
|
||||||
|
$(OBJ): $(SRC) $(LIBDIR)
|
||||||
|
$(CC) $(CFLAGS) -c $(SRC)
|
||||||
|
|
||||||
|
$(LIBDIR):
|
||||||
|
$(MKDIR) $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(RM) $(OBJ)
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
$(RM) $(LIBDIR)
|
||||||
|
|
||||||
|
|
||||||
|
re: fclean all
|
||||||
38
printf/ft_fprintf.c
Normal file
38
printf/ft_fprintf.c
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_fprintf.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/09/17 12:04:30 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/09/17 12:09:29 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "./ft_printf.h"
|
||||||
|
|
||||||
|
int ft_fprintf(int fd, const char *format, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
int count;
|
||||||
|
|
||||||
|
if (!format || fd < 0)
|
||||||
|
return (-1);
|
||||||
|
va_start(args, format);
|
||||||
|
count = 0;
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
while (*format != '%' && *format)
|
||||||
|
ft_putchar(*format++, &count, fd);
|
||||||
|
if (!*format)
|
||||||
|
break ;
|
||||||
|
else
|
||||||
|
format++;
|
||||||
|
if (!*format || !handle_arg(args, *format, &count, fd))
|
||||||
|
return (-1);
|
||||||
|
format++;
|
||||||
|
}
|
||||||
|
va_end(args);
|
||||||
|
return (count);
|
||||||
|
}
|
||||||
63
printf/ft_printf.c
Normal file
63
printf/ft_printf.c
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_printf.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/18 17:50:35 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/09/17 14:11:57 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_printf.h"
|
||||||
|
|
||||||
|
int handle_arg(va_list args, char format, int *count, int fd)
|
||||||
|
{
|
||||||
|
if (format == 'd' || format == 'i')
|
||||||
|
ft_putnbr(va_arg(args, int), count, fd);
|
||||||
|
else if (format == 'u')
|
||||||
|
ft_putnbr(va_arg(args, unsigned int), count, fd);
|
||||||
|
else if (format == 's')
|
||||||
|
ft_putstr(va_arg(args, char *), count, fd);
|
||||||
|
else if (format == 'X')
|
||||||
|
ft_puthex_upper(va_arg(args, unsigned int), count, fd);
|
||||||
|
else if (format == 'x')
|
||||||
|
ft_puthex_lower(va_arg(args, unsigned int), count, fd);
|
||||||
|
else if (format == 'p')
|
||||||
|
ft_putptr(va_arg(args, void *), count, fd);
|
||||||
|
else if (format == 'c')
|
||||||
|
ft_putchar(va_arg(args, int), count, fd);
|
||||||
|
else if (format == '%')
|
||||||
|
ft_putchar('%', count, fd);
|
||||||
|
else if (format == 'f')
|
||||||
|
ft_putnbrf(va_arg(args, double), count, 5, fd);
|
||||||
|
else
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_printf(const char *format, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
int count;
|
||||||
|
|
||||||
|
if (!format)
|
||||||
|
return (-1);
|
||||||
|
va_start(args, format);
|
||||||
|
count = 0;
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
while (*format != '%' && *format)
|
||||||
|
ft_putchar(*format++, &count, STDOUT_FILENO);
|
||||||
|
if (!*format)
|
||||||
|
break ;
|
||||||
|
else
|
||||||
|
format++;
|
||||||
|
if (!*format || !handle_arg(args, *format, &count, STDOUT_FILENO))
|
||||||
|
return (-1);
|
||||||
|
format++;
|
||||||
|
}
|
||||||
|
va_end(args);
|
||||||
|
return (count);
|
||||||
|
}
|
||||||
52
printf/ft_printf.h
Normal file
52
printf/ft_printf.h
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_printf.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/18 12:50:35 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/11/11 11:42:50 by marvin ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef FT_PRINTF_H
|
||||||
|
|
||||||
|
# define FT_PRINTF_H
|
||||||
|
|
||||||
|
# include <stdarg.h>
|
||||||
|
# include <stdlib.h>
|
||||||
|
# include <stdio.h>
|
||||||
|
|
||||||
|
# ifdef _WIN32
|
||||||
|
# include <io.h>
|
||||||
|
# undef STDIN_FILENO
|
||||||
|
|
||||||
|
# define STDIN_FILENO _fileno(stdin)
|
||||||
|
# undef STDOUT_FILENO
|
||||||
|
|
||||||
|
# define STDOUT_FILENO _fileno(stdout)
|
||||||
|
|
||||||
|
# define write _write
|
||||||
|
|
||||||
|
# else
|
||||||
|
|
||||||
|
# include <unistd.h>
|
||||||
|
|
||||||
|
# endif
|
||||||
|
|
||||||
|
int ft_fprintf(int fd, const char *format, ...);
|
||||||
|
int ft_printf(const char *format, ...);
|
||||||
|
|
||||||
|
int handle_arg(va_list args, char format, int *count, int fd);
|
||||||
|
|
||||||
|
void ft_puthex_lower(unsigned long nbr, int *count, int fd);
|
||||||
|
void ft_puthex_upper(unsigned long nbr, int *count, int fd);
|
||||||
|
void ft_putchar(int c, int *count, int fd);
|
||||||
|
void ft_putnbr(long n, int *count, int fd);
|
||||||
|
void ft_putnbrf(double f, int *count, int precision, int fd);
|
||||||
|
void ft_putstr(const char *str, int *count, int fd);
|
||||||
|
void ft_putptr(void *ptr, int *count, int fd);
|
||||||
|
size_t ft_strlen(const char *s);
|
||||||
|
|
||||||
|
#endif
|
||||||
43
printf/ft_putascii.c
Normal file
43
printf/ft_putascii.c
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_putascii.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/18 17:54:53 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/09/17 16:35:10 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_printf.h"
|
||||||
|
|
||||||
|
void ft_putchar(int c, int *count, int fd)
|
||||||
|
{
|
||||||
|
write(fd, &c, 1);
|
||||||
|
*count += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_putnbr(long n, int *count, int fd)
|
||||||
|
{
|
||||||
|
if (n < 0)
|
||||||
|
ft_putchar(0x2d, count, fd);
|
||||||
|
if (n <= -10)
|
||||||
|
ft_putnbr(n / -10, count, fd);
|
||||||
|
if (n >= 10)
|
||||||
|
ft_putnbr(n / 10, count, fd);
|
||||||
|
if (n >= 0)
|
||||||
|
ft_putchar(n % 10 + 0x30, count, fd);
|
||||||
|
if (n < 0)
|
||||||
|
ft_putchar(-(n % -10) + 0x30, count, fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_putstr(const char *str, int *count, int fd)
|
||||||
|
{
|
||||||
|
if (!str)
|
||||||
|
{
|
||||||
|
*count += ft_fprintf(fd, "(null)");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
*count += ft_fprintf(fd, str);
|
||||||
|
}
|
||||||
34
printf/ft_putfloat.c
Normal file
34
printf/ft_putfloat.c
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_putfloat.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/09/17 11:50:45 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/09/17 12:07:47 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_printf.h"
|
||||||
|
|
||||||
|
void ft_putnbrf(double f, int *count, int precision, int fd)
|
||||||
|
{
|
||||||
|
int number;
|
||||||
|
double fraction;
|
||||||
|
unsigned int mult;
|
||||||
|
|
||||||
|
mult = 1;
|
||||||
|
while (precision-- > 0)
|
||||||
|
{
|
||||||
|
mult *= 10;
|
||||||
|
}
|
||||||
|
number = (int)f;
|
||||||
|
fraction = f - (double)number;
|
||||||
|
if (fraction < 0)
|
||||||
|
fraction = -fraction;
|
||||||
|
ft_putnbr(number, count, fd);
|
||||||
|
ft_putchar('.', count, fd);
|
||||||
|
number = fraction * mult;
|
||||||
|
ft_putnbr(number, count, fd);
|
||||||
|
}
|
||||||
33
printf/ft_puthex.c
Normal file
33
printf/ft_puthex.c
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_puthex.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/18 17:52:38 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/09/17 12:09:29 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_printf.h"
|
||||||
|
|
||||||
|
void ft_puthex_upper(unsigned long nbr, int *count, int fd)
|
||||||
|
{
|
||||||
|
char *base_str;
|
||||||
|
|
||||||
|
base_str = "0123456789ABCDEF";
|
||||||
|
if (nbr >= 16)
|
||||||
|
ft_puthex_upper(nbr / 16, count, fd);
|
||||||
|
ft_putchar(base_str[(nbr % 16)], count, fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_puthex_lower(unsigned long nbr, int *count, int fd)
|
||||||
|
{
|
||||||
|
char *base_str;
|
||||||
|
|
||||||
|
base_str = "0123456789abcdef";
|
||||||
|
if (nbr >= 16)
|
||||||
|
ft_puthex_lower(nbr / 16, count, fd);
|
||||||
|
ft_putchar(base_str[(nbr % 16)], count, fd);
|
||||||
|
}
|
||||||
27
printf/ft_putptr.c
Normal file
27
printf/ft_putptr.c
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_putptr.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/18 17:53:53 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/11/10 20:42:58 by marvin ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_printf.h"
|
||||||
|
|
||||||
|
void ft_putptr(void *ptr, int *count, int fd)
|
||||||
|
{
|
||||||
|
void **to_print;
|
||||||
|
|
||||||
|
if (!ptr)
|
||||||
|
{
|
||||||
|
*count += ft_fprintf(fd, "(nil)");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
to_print = &ptr;
|
||||||
|
*count += ft_printf("0x");
|
||||||
|
ft_puthex_lower((unsigned long long)*to_print, count, fd);
|
||||||
|
}
|
||||||
28
printf/ft_strlen.c
Normal file
28
printf/ft_strlen.c
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strlen.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/02 16:01:03 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/09/14 09:24:02 by victor ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_printf.h"
|
||||||
|
#include "limits.h"
|
||||||
|
|
||||||
|
size_t ft_strlen(char const *str)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (*str++)
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
if (i == ULONG_MAX)
|
||||||
|
break ;
|
||||||
|
}
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
86
prompt_lib/Makefile
Normal file
86
prompt_lib/Makefile
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# ::: :::::::: #
|
||||||
|
# Makefile :+: :+: :+: #
|
||||||
|
# +:+ +:+ +:+ #
|
||||||
|
# By: anarama <anarama@student.42.fr> +#+ +:+ +#+ #
|
||||||
|
# +#+#+#+#+#+ +#+ #
|
||||||
|
# Created: 2024/07/05 12:24:47 by victor #+# #+# #
|
||||||
|
# Updated: 2024/12/10 21:20:03 by vvobis ### ########.fr #
|
||||||
|
# #
|
||||||
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
# COMPILER AND FLAGS
|
||||||
|
CC := cc
|
||||||
|
CFLAGS = -Werror -Wall -Wextra -g3
|
||||||
|
|
||||||
|
RM = rm
|
||||||
|
|
||||||
|
ifdef release
|
||||||
|
CFLAGS -= -g3
|
||||||
|
endif
|
||||||
|
|
||||||
|
|
||||||
|
MKDIR = mkdir
|
||||||
|
|
||||||
|
ifeq ($(OS), Windows_NT)
|
||||||
|
RM = del /S /Q
|
||||||
|
else
|
||||||
|
RM = rm -rf
|
||||||
|
MKDIR += -p
|
||||||
|
endif
|
||||||
|
|
||||||
|
# DIRECTORIES
|
||||||
|
|
||||||
|
SRCDIR := src
|
||||||
|
OBJDIR := obj
|
||||||
|
|
||||||
|
# SOURCES AND OBJECTS
|
||||||
|
SRC := prompt_input.c prompt_string_management.c \
|
||||||
|
prompt_utils.c tab_completion.c \
|
||||||
|
escape_sequences.c arrowkeys.c \
|
||||||
|
prompt_print.c tab_get_word.c \
|
||||||
|
non_blocking_mode.c prompt_handle_chars.c \
|
||||||
|
utils.c terminal.c
|
||||||
|
|
||||||
|
OBJ := $(SRC:%.c=$(OBJDIR)/%.o)
|
||||||
|
SRCS := $(addprefix $(SRCDIR)/, $(SRC))
|
||||||
|
|
||||||
|
# TARGETS
|
||||||
|
MEMORY := ../memory/lib/memory.a
|
||||||
|
LIBFT := ../libft/lib/libft.a
|
||||||
|
|
||||||
|
LIBS := $(MEMORY) $(LIBFT)
|
||||||
|
LIBDIR := lib
|
||||||
|
NAME := $(LIBDIR)/prompt.a
|
||||||
|
|
||||||
|
all: $(NAME)
|
||||||
|
|
||||||
|
test: $(NAME)
|
||||||
|
$(CC) $(CFLAGS) main.c $(NAME) $(LIBS)
|
||||||
|
|
||||||
|
$(NAME): $(OBJ) prompt.h | $(LIBS)
|
||||||
|
ar rcs $@ $(OBJ) $(LIBS)
|
||||||
|
|
||||||
|
$(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR)
|
||||||
|
$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
$(OBJDIR): $(LIBDIR)
|
||||||
|
$(MKDIR) $@
|
||||||
|
|
||||||
|
$(LIBDIR):
|
||||||
|
$(MKDIR) $@
|
||||||
|
|
||||||
|
$(LIBS):
|
||||||
|
make -C ../memory
|
||||||
|
make -C ../libft
|
||||||
|
|
||||||
|
clean:
|
||||||
|
make fclean -C ../memory
|
||||||
|
make fclean -C ../libft
|
||||||
|
$(RM) $(OBJDIR)
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
$(RM) $(LIBDIR)
|
||||||
|
|
||||||
|
re: fclean all
|
||||||
BIN
prompt_lib/lib/prompt.a
Normal file
BIN
prompt_lib/lib/prompt.a
Normal file
Binary file not shown.
BIN
prompt_lib/obj/arrowkeys.o
Normal file
BIN
prompt_lib/obj/arrowkeys.o
Normal file
Binary file not shown.
BIN
prompt_lib/obj/escape_sequences.o
Normal file
BIN
prompt_lib/obj/escape_sequences.o
Normal file
Binary file not shown.
BIN
prompt_lib/obj/ft_putstr_fd.o
Normal file
BIN
prompt_lib/obj/ft_putstr_fd.o
Normal file
Binary file not shown.
BIN
prompt_lib/obj/ft_read.o
Normal file
BIN
prompt_lib/obj/ft_read.o
Normal file
Binary file not shown.
BIN
prompt_lib/obj/non_blocking_mode.o
Normal file
BIN
prompt_lib/obj/non_blocking_mode.o
Normal file
Binary file not shown.
BIN
prompt_lib/obj/prompt_handle_chars.o
Normal file
BIN
prompt_lib/obj/prompt_handle_chars.o
Normal file
Binary file not shown.
BIN
prompt_lib/obj/prompt_input.o
Normal file
BIN
prompt_lib/obj/prompt_input.o
Normal file
Binary file not shown.
BIN
prompt_lib/obj/prompt_print.o
Normal file
BIN
prompt_lib/obj/prompt_print.o
Normal file
Binary file not shown.
BIN
prompt_lib/obj/prompt_string_management.o
Normal file
BIN
prompt_lib/obj/prompt_string_management.o
Normal file
Binary file not shown.
BIN
prompt_lib/obj/prompt_utils.o
Normal file
BIN
prompt_lib/obj/prompt_utils.o
Normal file
Binary file not shown.
BIN
prompt_lib/obj/tab_completion.o
Normal file
BIN
prompt_lib/obj/tab_completion.o
Normal file
Binary file not shown.
BIN
prompt_lib/obj/tab_get_word.o
Normal file
BIN
prompt_lib/obj/tab_get_word.o
Normal file
Binary file not shown.
BIN
prompt_lib/obj/terminal.o
Normal file
BIN
prompt_lib/obj/terminal.o
Normal file
Binary file not shown.
BIN
prompt_lib/obj/utils.o
Normal file
BIN
prompt_lib/obj/utils.o
Normal file
Binary file not shown.
141
prompt_lib/prompt.h
Normal file
141
prompt_lib/prompt.h
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* prompt.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/11/10 14:40:41 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/12/10 21:19:14 by vvobis ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef PROMPT_H
|
||||||
|
# define PROMPT_H
|
||||||
|
|
||||||
|
# include <stdlib.h>
|
||||||
|
# include <sys/types.h>
|
||||||
|
# include <stdint.h>
|
||||||
|
# include <stdio.h>
|
||||||
|
# include "../memory/memory.h"
|
||||||
|
# include <errno.h>
|
||||||
|
# include "../printf/ft_printf.h"
|
||||||
|
# include "../libft/libft.h"
|
||||||
|
# include <fcntl.h>
|
||||||
|
|
||||||
|
# include <stdbool.h>
|
||||||
|
|
||||||
|
|
||||||
|
# ifdef _WIN32
|
||||||
|
|
||||||
|
# include <windows.h>
|
||||||
|
# include <io.h>
|
||||||
|
|
||||||
|
# define NL "\r\n"
|
||||||
|
|
||||||
|
# undef STDOUT_FILENO
|
||||||
|
# undef STDIN_FILENO
|
||||||
|
|
||||||
|
# define STDIN_FILENO _fileno(stdin)
|
||||||
|
# define STDOUT_FILENO _fileno(stdout)
|
||||||
|
|
||||||
|
# else
|
||||||
|
|
||||||
|
# include <unistd.h>
|
||||||
|
# include <termios.h>
|
||||||
|
# define NL "\n"
|
||||||
|
|
||||||
|
# endif
|
||||||
|
|
||||||
|
# define EOT 4
|
||||||
|
# define ESC 27
|
||||||
|
# define DEL 127
|
||||||
|
|
||||||
|
# define SCREEN_DISBLE_WRAPPING "\033[?7l"
|
||||||
|
# define SCREEN_ENABLE_WRAPPING "\033[?7h"
|
||||||
|
# define SCREEN_CLEAR_TO_EOL "\033[K"
|
||||||
|
# define SCREEN_CLEAR "\033[2J"
|
||||||
|
# define SCREEN_CLEAR_TO_EOF "\033[J"
|
||||||
|
|
||||||
|
# define PROMPT_COMMAND_STACK_SIZE 16
|
||||||
|
# define PROMPT_INPUT_BUFFER_SIZE 4096
|
||||||
|
# define CURSOR_MOVE_HOME "\033[H"
|
||||||
|
# define GREEN "\033[0;32m"
|
||||||
|
# define RESET "\033[0m"
|
||||||
|
# define RED "\033[0;31m"
|
||||||
|
# define CURSOR_MOVE_UP "\033[A"
|
||||||
|
# define CURSOR_MOVE_DOWN "\033[B"
|
||||||
|
# define CURSOR_MOVE_RIGHT "\033[C"
|
||||||
|
# define CURSOR_MOVE_LEFT "\033[D"
|
||||||
|
# define CURSOR_POSITION_GET "\033[6n"
|
||||||
|
# define CURSOR_POSITION_SET "\033[%d;%dH"
|
||||||
|
# define CURSOR_POSITION_SAVE "\033[s"
|
||||||
|
# define CURSOR_POSITION_RESTORE "\033[u"
|
||||||
|
|
||||||
|
# define BUFFER_CAPACITY 64
|
||||||
|
|
||||||
|
typedef struct s_history_buffer
|
||||||
|
{
|
||||||
|
uint32_t write;
|
||||||
|
uint32_t read;
|
||||||
|
uint32_t buffer_capacity;
|
||||||
|
char *buffer[BUFFER_CAPACITY];
|
||||||
|
} t_history_buffer;
|
||||||
|
|
||||||
|
typedef struct s_prompt
|
||||||
|
{
|
||||||
|
bool exists;
|
||||||
|
char *prompt;
|
||||||
|
t_history_buffer history;
|
||||||
|
uint32_t cursor_position[2];
|
||||||
|
void (*prompt_display_func)(char *);
|
||||||
|
uint32_t prompt_length;
|
||||||
|
char *command;
|
||||||
|
} t_prompt;
|
||||||
|
|
||||||
|
/* The Main function, all other ones shouldnt be used outside this module */
|
||||||
|
/* Returns string of input that has to be freed. Internally, this stores a static prompt struct */
|
||||||
|
/* that stores the current state and history of the input buffer */
|
||||||
|
char *prompt_get(const char *prompt);
|
||||||
|
void prompt_destroy(void *prompt);
|
||||||
|
|
||||||
|
int64_t ft_read(int fd, char *character, uint32_t size_to_read);
|
||||||
|
|
||||||
|
t_prompt prompt_create_internal(const char *prompt);
|
||||||
|
char *prompt_get_input_internal(t_prompt *prompt, uint32_t prompt_initial_size, const char *delimiter);
|
||||||
|
|
||||||
|
uint32_t prompt_display_string_set_internal(t_prompt *prompt, const char *prompt_string);
|
||||||
|
void prompt_print_custom_string_internal(char *string);
|
||||||
|
|
||||||
|
/* Cursor Manipulation */
|
||||||
|
void cursor_position_get(uint32_t cursor_position[2]);
|
||||||
|
void cursor_position_save(void);
|
||||||
|
void cursor_position_restore(void);
|
||||||
|
void cursor_position_set(uint32_t row, uint32_t column);
|
||||||
|
|
||||||
|
/* Prompt Buffer Management */
|
||||||
|
bool prompt_handle_escape_sequence_internal(t_prompt *prompt, char buffer[], char **input, uint32_t cursor_position[2]);
|
||||||
|
bool prompt_handle_new_character_to_input_internal(char **input, char character, uint32_t *cursor_position, uint32_t prompt_length);
|
||||||
|
uint8_t prompt_handle_single_char_input_internal(char **input, char buffer[], uint32_t cursor_position[2], bool *do_refresh);
|
||||||
|
bool prompt_handle_multiple_character_internal(char **input, char buffer[], uint32_t cursor_position[2], uint32_t prompt_length);
|
||||||
|
void prompt_handle_rapid_input_internal(char buffer[], uint32_t cursor_position[2], char **input, uint32_t cursor_position_base);
|
||||||
|
void prompt_handle_backspace_internal( char *input, uint32_t *cursor_position_current, uint32_t input_length_current);
|
||||||
|
void prompt_refresh_line_internal(char *input, uint32_t cursor_position_base, uint32_t cursor_position[2]);
|
||||||
|
char *buffer_size_manage(char **input, uint32_t old_size, uint32_t size_to_add, uint32_t scalar);
|
||||||
|
void prompt_src_string_insert_internal(char *string_to_insert, char **current_input, char *position_to_insert, uint32_t current_word_length);
|
||||||
|
|
||||||
|
/* Non Blocking mode */
|
||||||
|
void blocking_mode_toggle(int fd, int flag);
|
||||||
|
|
||||||
|
/* Tab Completion */
|
||||||
|
void prompt_handle_tab_internal(char **input, t_prompt *prompt);
|
||||||
|
void prompt_handle_tab_no_match_internal(const char *input_path, uint32_t cursor_position_current[2], t_prompt *prompt);
|
||||||
|
void prompt_handle_tab_yes_match_internal(t_prompt *prompt, const char *next_word_match, char **input, uint32_t current_word_length);
|
||||||
|
char *prompt_determine_word_internal(char *input, char **input_path, uint32_t cursor_position_current);
|
||||||
|
void prompt_get_next_word_match_internal(char **input, t_prompt *prompt, char *input_path, bool *is_directory);
|
||||||
|
|
||||||
|
/* Termios */
|
||||||
|
void terminal_raw_mode_enable_internal(void);
|
||||||
|
void terminal_raw_mode_disable_internal(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
75
prompt_lib/src/arrowkeys.c
Normal file
75
prompt_lib/src/arrowkeys.c
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* arrowkeys.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: anarama <anarama@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/07/08 11:02:39 by victor #+# #+# */
|
||||||
|
/* Updated: 2024/11/11 15:25:23 by victor ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../prompt.h"
|
||||||
|
|
||||||
|
static void prompt_handle_arrow_key_up_internal( t_history_buffer *history,
|
||||||
|
char **input,
|
||||||
|
uint32_t cursor_position_current[2])
|
||||||
|
{
|
||||||
|
if (cursor_position_current[0] > 0)
|
||||||
|
ft_putstr_fd(CURSOR_MOVE_UP, 1);
|
||||||
|
if (history->read == 0)
|
||||||
|
history->read = history->buffer_capacity - 1;
|
||||||
|
else
|
||||||
|
history->read -= 1;
|
||||||
|
if (history->buffer[history->read] == 0)
|
||||||
|
{
|
||||||
|
history->read = (history->read + 1) % history->buffer_capacity;
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
if (history->buffer[history->write] == 0)
|
||||||
|
history->buffer[history->write] = *input;
|
||||||
|
*input = history->buffer[history->read];
|
||||||
|
cursor_position_current[1] = ft_strlen(*input);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void prompt_handle_arrow_key_down_internal(t_history_buffer *history, uint32_t cursor_position[2], char **input)
|
||||||
|
{
|
||||||
|
if (history->read == history->write)
|
||||||
|
return ;
|
||||||
|
history->read = (history->read + 1) % history->buffer_capacity;
|
||||||
|
*input = history->buffer[history->read];
|
||||||
|
cursor_position[1] = ft_strlen(*input);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void prompt_handle_arrow_key_right_internal(uint32_t *cursor_position_current, uint32_t prompt_length_current, uint32_t prompt_string_length)
|
||||||
|
{
|
||||||
|
if (cursor_position_current[1] < prompt_length_current)
|
||||||
|
cursor_position_current[1]++;
|
||||||
|
cursor_position_set(cursor_position_current[0], cursor_position_current[1] + prompt_string_length);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void prompt_handle_arrow_key_left_internal( uint32_t *cursor_position_current, uint32_t prompt_string_length)
|
||||||
|
{
|
||||||
|
if (cursor_position_current[1] > 0)
|
||||||
|
cursor_position_current[1]--;
|
||||||
|
cursor_position_set(cursor_position_current[0], cursor_position_current[1] + prompt_string_length);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool prompt_handle_escape_sequence_internal(t_prompt *prompt, char buffer[], char **input, uint32_t cursor_position_current[2])
|
||||||
|
{
|
||||||
|
uint32_t prompt_length_current;
|
||||||
|
|
||||||
|
prompt_length_current = ft_strlen(*input);
|
||||||
|
if (buffer[0] == 91 && buffer[1] == 65)
|
||||||
|
return (prompt_handle_arrow_key_up_internal(&prompt->history, \
|
||||||
|
input, cursor_position_current), 1);
|
||||||
|
else if (buffer[0] == 91 && buffer[1] == 66)
|
||||||
|
return (prompt_handle_arrow_key_down_internal(&prompt->history, \
|
||||||
|
cursor_position_current, input), 1);
|
||||||
|
else if (buffer[0] == 91 && buffer[1] == 67)
|
||||||
|
prompt_handle_arrow_key_right_internal(cursor_position_current, prompt_length_current, prompt->prompt_length);
|
||||||
|
else if (buffer[0] == 91 && buffer[1] == 68)
|
||||||
|
prompt_handle_arrow_key_left_internal(cursor_position_current, prompt->prompt_length);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
63
prompt_lib/src/escape_sequences.c
Normal file
63
prompt_lib/src/escape_sequences.c
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* escape_sequences.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: anarama <anarama@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/07/07 19:29:29 by vvobis #+# #+# */
|
||||||
|
/* Updated: 2024/11/11 15:17:42 by victor ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../prompt.h"
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
|
||||||
|
void cursor_position_get(uint32_t cursor_position[2])
|
||||||
|
{
|
||||||
|
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
|
CONSOLE_SCREEN_BUFFER_INFO info;
|
||||||
|
|
||||||
|
if (!GetConsoleScreenBufferInfo(hConsole, &info))
|
||||||
|
{
|
||||||
|
ft_printf("Failed to get Cursor Positon\n");
|
||||||
|
lst_memory(NULL, NULL, FAIL);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cursor_position[0] = info.dwCursorPosition.Y;
|
||||||
|
cursor_position[1] = info.dwCursorPosition.X;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
void cursor_position_get(uint32_t cursor_position[2])
|
||||||
|
{
|
||||||
|
char cursor_position_str[32];
|
||||||
|
char *cursor_position_str_ptr;
|
||||||
|
char *cursor_position_str_ptr2;
|
||||||
|
int bytes_read;
|
||||||
|
|
||||||
|
ft_bzero(&cursor_position_str, 32);
|
||||||
|
write(1, CURSOR_POSITION_GET, ft_strlen(CURSOR_POSITION_GET));
|
||||||
|
bytes_read = read(1, cursor_position_str, 32);
|
||||||
|
if (bytes_read == -1)
|
||||||
|
return (perror("read"));
|
||||||
|
cursor_position_str_ptr = cursor_position_str;
|
||||||
|
cursor_position_str_ptr2 = ft_strchr(cursor_position_str, ';');
|
||||||
|
if (!cursor_position_str_ptr2)
|
||||||
|
return ;
|
||||||
|
*cursor_position_str_ptr2 = 0;
|
||||||
|
while (!(*cursor_position_str_ptr >= '0'
|
||||||
|
&& *cursor_position_str_ptr <= '9'))
|
||||||
|
cursor_position_str_ptr++;
|
||||||
|
cursor_position[0] = ft_atoi(cursor_position_str_ptr);
|
||||||
|
cursor_position_str_ptr = cursor_position_str_ptr2 + 1;
|
||||||
|
cursor_position_str_ptr2 = ft_strchr(cursor_position_str_ptr, 'R');
|
||||||
|
*cursor_position_str_ptr2 = 0;
|
||||||
|
cursor_position[1] = ft_atoi(cursor_position_str_ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user