add ft_printf libft (included in ft_printf) and get_nextline

This commit is contained in:
Matthos Denys 2021-01-10 17:42:17 +03:00
parent 19af3b21e6
commit e507504526
73 changed files with 3518 additions and 0 deletions

53
ft_printf/Makefile Normal file
View File

@ -0,0 +1,53 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/10/28 11:52:03 by mdenys #+# #+# #
# Updated: 2020/12/16 05:38:16 by mdenys ### ########.fr #
# #
# **************************************************************************** #
LIBFT = ./libft/libft.a
N_TEMP = temp.a
NAME = libftprintf.a
SRCS = ft_printf.c parser/ft_parser.c parser/ft_parser_utils.c \
putchar_maker/ft_put_c.c putchar_maker/put_d/ft_put_d.c putchar_maker/put_d/ft_put_d_utilc.c putchar_maker/ft_put_p.c putchar_maker/ft_put_s.c putchar_maker/ft_put_u.c putchar_maker/ft_put_x.c \
putchar_maker/put_u/ft_put_u.c putchar_maker/put_u/ft_put_u_utilc.c\
putchar_maker/put_x/ft_put_x.c putchar_maker/put_x/ft_put_x_utilc.c\
putchar_maker/put_p/ft_put_p.c putchar_maker/put_p/ft_put_p_utilc.c\
putchar_maker/putmaket_utils.c putchar_maker/put_d/ft_put_d_undersize.c
SURPL_O = *.o
CC = gcc
FLAGS = -c -Wall -Wextra -Werror
INCLUDES = -I./includes
OBJS = $(SRCS:.c=.o)
$(NAME): $(OBJS)
$(MAKE) bonus -C ./libft
cp libft/libft.a $(NAME)
$(CC) $(FLAGS) $(INCLUDES) $(SRCS)
ar -rcs $(NAME) $(OBJS)
all : $(NAME)
clean :
$(MAKE) clean -C ./libft
rm -rf $(SURPL_O)
rm -rf $(OBJS)
fclean : clean
$(MAKE) fclean -C ./libft
rm -rf $(NAME)
re : fclean all

46
ft_printf/ft_printf.c Normal file
View File

@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/19 16:46:57 by mdenys #+# #+# */
/* Updated: 2020/12/14 21:59:53 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_putchar_fd_count(t_printf *list, char c, int fd)
{
write(fd, &c, 1);
list->count += 1;
}
int ft_printf(const char *format, ...)
{
int i;
char *ptr;
va_list name;
i = 0;
va_start(name, format);
ptr = (char *)format;
while (*ptr)
{
if (*ptr == '%')
{
ptr++;
i += ft_parser(&ptr, &name);
}
else
{
ft_putchar_fd(*ptr, 1);
i++;
ptr++;
}
}
va_end(name);
return (i);
}

98
ft_printf/ft_printf.h Normal file
View File

@ -0,0 +1,98 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/19 16:50:07 by mdenys #+# #+# */
/* Updated: 2020/12/16 06:00:04 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
# include <stdarg.h>
# include "libft/libft.h"
# include <stdlib.h>
typedef struct s_printf
{
int wh;
int prec;
unsigned is_left:1;
unsigned is_zero:1;
unsigned is_prec:1;
unsigned rec_prec:1;
unsigned to_up:1;
unsigned v_min:1;
unsigned count;
} t_printf;
void ft_putnbr_fd_count(t_printf *list, long int n, int fd);
void ft_putnbr_u_fd_count(t_printf *list, unsigned int n, int fd);
void ft_putchar_fd_count(t_printf *list, char c, int fd);
int ft_printf(const char *format, ...);
int ft_parser(char **str, va_list *name);
void while_c(t_printf *list, char c, int i);
void d_isprec(t_printf *list, int value);
int lennbr(long int n);
int is_flag(char **str, t_printf *params);
int is_star(char **str, va_list *arg, t_printf *params);
int is_digit(char **str, t_printf *params);
int is_type(char **str, int *cout, va_list *arg, t_printf *params);
int len_value(t_printf *list, long int value);
void d_isleft(t_printf *list, int value);
void d_isleft_2(t_printf *list, int value);
void ft_write_d(t_printf *list, int value);
void ft_write_d_2(t_printf *list, int value);
void for_min_diszero(t_printf *list, int value, int tmp, int *c);
void for_diszero(t_printf *list, int value, int tmp, int c);
void printf_value(t_printf *list, int value);
int len_u_value(t_printf *list, unsigned int value);
void u_isleft(t_printf *list, unsigned int value);
void u_iszero(t_printf *list, unsigned int value);
void u_isprec(t_printf *list, unsigned int value);
void ft_write_u(t_printf *list, unsigned int value);
int u_lennbr(unsigned int n);
void printf_u_value(t_printf *list, unsigned int value);
void for_uiszero(t_printf *list, unsigned int value, int tmp, \
unsigned int c);
void for_min_uiszero(t_printf *list, unsigned int value, int tmp,\
unsigned int *c);
void u_isleft_ext(t_printf *list, unsigned int value);
void x_isleft(t_printf *list, unsigned int value);
void x_iszero(t_printf *list, unsigned int value);
void x_isprec(t_printf *list, unsigned int value);
int len_x_value(t_printf *list, unsigned int value);
void ft_write_x(t_printf *list, unsigned int value);
void printf_x_value(t_printf *list, unsigned int value);
void print_hex(unsigned int value, t_printf *list);
int len_hex(unsigned int value);
void for_xiszero(t_printf *list, unsigned int value, int \
tmp, unsigned int c);
void for_min_xiszero(t_printf *list, unsigned int value, int tmp,\
unsigned int *c);
void print_hep(t_printf *list, long int value);
int len_hep(long int value);
void for_piszero(t_printf *list, long int value, long int tmp, \
long int c);
void for_min_piszero(t_printf *list, long int value, long int tmp,\
long int *c);
void printf_p_value(t_printf *list, long int value);
void p_isleft(t_printf *list, long int value);
void p_iszero(t_printf *list, long int value);
void p_isprec(t_printf *list, long int value);
int len_hep(long int value);
void ft_write_p(t_printf *list, long int value);
void printf_p_value_2(t_printf *list, long int value, int b);
int ft_put_percent(t_printf *list);
int ft_put_c(t_printf *list, va_list *name);
int ft_put_d(t_printf *list, va_list *name);
int ft_put_p(t_printf *list, va_list *name);
int ft_put_s(t_printf *list, va_list *name);
int ft_put_u(t_printf *list, va_list *name);
int ft_put_x(t_printf *list, va_list *name);
#endif

49
ft_printf/libft/Makefile Normal file
View File

@ -0,0 +1,49 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/10/28 11:52:03 by mdenys #+# #+# #
# Updated: 2020/12/04 22:45:21 by mdenys ### ########.fr #
# #
# **************************************************************************** #
SRCS = ft_isalnum.c ft_isprint.c ft_memcmp.c ft_putchar_fd.c ft_split.c \
ft_strlcat.c ft_strncmp.c ft_substr.c ft_atoi.c ft_isalpha.c \
ft_itoa.c ft_memcpy.c ft_putendl_fd.c ft_strchr.c ft_strlcpy.c \
ft_strnstr.c ft_tolower.c ft_bzero.c ft_isascii.c ft_memccpy.c \
ft_memmove.c ft_putnbr_fd.c ft_strdup.c ft_strlen.c ft_strrchr.c \
ft_toupper.c ft_calloc.c ft_isdigit.c ft_memchr.c ft_memset.c \
ft_putstr_fd.c ft_strjoin.c ft_strmapi.c ft_strtrim.c ft_putchar.c
OBJS = $(SRCS:.c=.o)
BONUS = ft_lstadd_back.c ft_lstadd_front.c ft_lstclear.c \
ft_lstdelone.c ft_lstiter.c ft_lstlast.c \
ft_lstmap.c ft_lstnew.c ft_lstsize.c
BONUS_OBJS = $(BONUS:.c=.o)
CC = gcc
RM = rm -f
CFLAGS = -Wall -Wextra -Werror -I.
NAME = libft.a
all: $(NAME)
$(NAME): $(OBJS)
ar rcs $(NAME) $(OBJS)
clean:
$(RM) $(OBJS) $(BONUS_OBJS)
fclean: clean
$(RM) $(NAME)
re: fclean $(NAME)
bonus: $(OBJS) $(BONUS_OBJS)
ar rcs $(NAME) $(OBJS) $(BONUS_OBJS)
.PHONY: all clean fclean re bonus

51
ft_printf/libft/ft_atoi.c Normal file
View File

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/29 13:36:13 by mdenys #+# #+# */
/* Updated: 2020/11/05 17:22:30 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ch(char ch)
{
if (ch == '\t' || ch == '\n' || ch == ' ' || \
ch == '\v' || ch == '\f' || ch == '\r')
return (1);
else
return (0);
}
int ft_atoi(const char *str)
{
int i;
unsigned long int result;
int operator;
i = 0;
operator = 1;
result = 0;
while (ch(str[i]))
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i] == '-')
operator *= -1;
i++;
}
while (str[i] >= '0' && str[i] <= '9')
{
if (result > 922337203685477580 && operator == -1)
return (0);
else if (result > 922337203685477580 && operator == 1)
return (-1);
result = result * 10 + str[i] - '0';
i++;
}
return (result * operator);
}

View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/29 12:19:35 by mdenys #+# #+# */
/* Updated: 2020/10/29 18:24:02 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_bzero(void *s, size_t num)
{
char *tmp;
size_t i;
i = 0;
if (num)
{
tmp = s;
while (i < num)
{
tmp[i] = '\0';
i++;
}
}
}

View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/01 19:59:31 by mdenys #+# #+# */
/* Updated: 2020/11/01 20:41:24 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_calloc(size_t num, size_t size)
{
void *addr;
unsigned int nbytes;
nbytes = num * size;
addr = malloc(nbytes);
if (!size || !num || (size == 0 && num == 0))
return ((void *)addr);
if (!addr)
return (NULL);
else
{
ft_memset(addr, '\0', nbytes);
}
return ((void *)addr);
}

View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/29 16:43:05 by mdenys #+# #+# */
/* Updated: 2020/10/29 19:50:43 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalnum(int c)
{
return (ft_isalpha(c) || ft_isdigit(c));
}

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/29 14:03:52 by mdenys #+# #+# */
/* Updated: 2020/10/30 11:46:57 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalpha(int c)
{
if ((c > 64 && c < 91) || (c > 96 && c < 123))
return (1);
return (0);
}

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/29 18:53:32 by mdenys #+# #+# */
/* Updated: 2020/10/30 11:54:20 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isascii(int c)
{
if (c >= 0 && c <= 127)
return (1);
return (0);
}

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/29 14:29:08 by mdenys #+# #+# */
/* Updated: 2020/10/30 11:52:05 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isdigit(int ch)
{
if (ch >= '0' && ch <= '9')
return (1);
return (0);
}

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/29 18:53:32 by mdenys #+# #+# */
/* Updated: 2020/10/30 11:53:10 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isprint(int c)
{
if (c >= 32 && c <= 126)
return (1);
return (0);
}

55
ft_printf/libft/ft_itoa.c Normal file
View File

@ -0,0 +1,55 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/03 20:26:38 by mdenys #+# #+# */
/* Updated: 2020/11/04 13:20:27 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int nbleng(unsigned int n)
{
unsigned int i;
i = 0;
while (n >= 10)
{
n /= 10;
i++;
}
return (i + 1);
}
char *ft_itoa(int n)
{
char *dst;
unsigned int leng;
unsigned int nbr;
unsigned int i;
nbr = (n < 0 ? -n : n);
leng = nbleng(nbr);
i = 0;
if (!(dst = (char *)malloc(sizeof(char) * leng + 1 + (n < 0 ? 1 : 0))))
return (NULL);
if (n < 0)
{
dst[i] = '-';
leng++;
}
i = leng - 1;
while (nbr >= 10)
{
dst[i] = nbr % 10 + '0';
nbr /= 10;
i--;
}
dst[i] = nbr % 10 + '0';
dst[leng] = '\0';
return (dst);
}

View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/04 18:15:55 by mdenys #+# #+# */
/* Updated: 2020/11/04 18:36:49 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *temp;
temp = *lst;
if (*lst)
{
while (temp->next != NULL)
temp = temp->next;
temp->next = new;
}
else if (*lst == NULL)
{
*lst = new;
}
}

View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_front.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/04 17:29:57 by mdenys #+# #+# */
/* Updated: 2020/11/04 17:54:55 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_front(t_list **lst, t_list *new)
{
if (new)
{
new->next = *lst;
*lst = new;
}
}

View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/04 20:53:46 by mdenys #+# #+# */
/* Updated: 2020/11/04 21:34:51 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstclear(t_list **lst, void (*del)(void*))
{
t_list *new;
if (*lst)
{
while (*lst)
{
del((*lst)->content);
new = *lst;
*lst = new->next;
free(new);
}
}
*lst = NULL;
}

View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdelone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/04 18:37:58 by mdenys #+# #+# */
/* Updated: 2020/11/04 20:49:50 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstdelone(t_list *lst, void (*del)(void *))
{
if (lst)
{
del(lst->content);
free(lst);
}
}

View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstiter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/04 21:10:38 by mdenys #+# #+# */
/* Updated: 2020/11/04 21:24:22 by mdenys ### ########.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;
}
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/04 18:07:26 by mdenys #+# #+# */
/* Updated: 2020/11/04 18:14:43 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstlast(t_list *lst)
{
t_list *new;
new = lst;
if (lst)
{
while (new->next != NULL)
new = new->next;
return (new);
}
return (NULL);
}

View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/04 21:25:01 by mdenys #+# #+# */
/* Updated: 2020/11/05 16:46:12 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *nlst;
t_list *new;
if (!lst)
return (NULL);
nlst = NULL;
while (lst)
{
new = ft_lstnew(f(lst->content));
if (!new)
{
ft_lstclear(&nlst, del);
return (NULL);
}
ft_lstadd_back(&nlst, new);
lst = lst->next;
}
return (nlst);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/04 17:06:10 by mdenys #+# #+# */
/* Updated: 2020/11/04 17:29:10 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstnew(void *content)
{
t_list *str;
str = (t_list*)malloc(sizeof(t_list));
if (str)
{
str->content = content;
str->next = NULL;
return (str);
}
return (NULL);
}

View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstsize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/04 17:57:03 by mdenys #+# #+# */
/* Updated: 2020/11/04 18:06:42 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_lstsize(t_list *lst)
{
int i;
t_list *new;
i = 0;
new = lst;
while (new)
{
new = new->next;
i++;
}
return (i);
}

View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memccpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/30 15:35:27 by mdenys #+# #+# */
/* Updated: 2020/11/04 17:38:29 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memccpy(void *dst, const void *src, int ch, size_t n)
{
unsigned char *d;
unsigned char *s;
unsigned int i;
i = 0;
d = dst;
s = (unsigned char *)src;
if (!n)
return (0);
while (i < n)
{
d[i] = s[i];
if (s[i] == (unsigned char)ch)
return (dst + i + 1);
i++;
}
return (0);
}

View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/30 19:55:27 by mdenys #+# #+# */
/* Updated: 2020/11/04 17:40:00 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memchr(const void *arr, int c, size_t n)
{
unsigned char *d;
unsigned int i;
i = 0;
d = (unsigned char*)arr;
while (i < n)
{
if (d[i] == (unsigned char)c)
{
return (void*)(d + i);
}
i++;
}
return (NULL);
}

View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/31 12:46:41 by mdenys #+# #+# */
/* Updated: 2020/11/04 17:41:43 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_memcmp(const void *buf1, const void *buf2, size_t n)
{
unsigned int i;
unsigned char *d;
unsigned char *s;
if (!n)
return (0);
i = 0;
d = (unsigned char*)buf1;
s = (unsigned char*)buf2;
while (d[i] == s[i] && n - 1 > i)
i++;
if (d[i] != s[i])
return (d[i] - s[i]);
else
return (0);
}

View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/30 14:43:54 by mdenys #+# #+# */
/* Updated: 2020/10/31 13:33:48 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memcpy(void *destination, const void *source, size_t n)
{
unsigned char *d;
unsigned char *s;
int i;
i = 0;
d = (unsigned char *)destination;
s = (unsigned char *)source;
if (!n || destination == source)
return (destination);
while (n--)
{
d[i] = s[i];
i++;
}
return (destination);
}

View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/30 17:04:51 by mdenys #+# #+# */
/* Updated: 2020/11/04 18:41:12 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memmove(void *dst, const void *source, size_t n)
{
unsigned char *d;
unsigned char *s;
unsigned int i;
i = 0;
d = dst;
s = (unsigned char *)source;
if (!n)
return (dst);
else if (s < d)
{
while (++i <= n)
d[n - i] = s[n - i];
}
else if (s)
{
while (i < n)
{
d[i] = s[i];
i++;
}
}
return (dst);
}

View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/29 10:24:40 by mdenys #+# #+# */
/* Updated: 2020/10/29 17:01:59 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memset(void *dest, int val, size_t num)
{
char *tmp;
size_t i;
i = 0;
if (num)
{
tmp = dest;
while (i < num)
{
tmp[i] = val;
i++;
}
}
return (dest);
}

View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/04 22:44:21 by mdenys #+# #+# */
/* Updated: 2020/12/04 22:49:12 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putchar(char c)
{
write(1, &c, 1);
}

View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/04 13:02:20 by mdenys #+# #+# */
/* Updated: 2020/11/04 15:47:39 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putchar_fd(char c, int fd)
{
if (fd)
write(fd, &c, 1);
}

View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/04 14:53:54 by mdenys #+# #+# */
/* Updated: 2020/11/04 14:57:01 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putendl_fd(char *s, int fd)
{
int i;
i = 0;
if (s)
{
while (s[i])
{
ft_putchar_fd(s[i], fd);
i++;
}
ft_putchar_fd('\n', fd);
}
}

View File

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/04 15:04:13 by mdenys #+# #+# */
/* Updated: 2020/12/16 05:49:18 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putnbr_fd(long int n, int fd)
{
long int nbr;
if (n < 0)
{
ft_putchar_fd('-', fd);
nbr = (unsigned int)(n * -1);
}
else
nbr = (unsigned int)n;
if (nbr >= 10)
ft_putnbr_fd(nbr / 10, fd);
ft_putchar_fd((char)(nbr % 10 + '0'), fd);
}
void ft_putnbr_u_fd(unsigned int n, int fd)
{
unsigned int nbr;
if (n < 0)
{
ft_putchar_fd('-', fd);
nbr = (unsigned int)(n * -1);
}
else
nbr = (unsigned int)n;
if (nbr >= 10)
ft_putnbr_fd(nbr / 10, fd);
ft_putchar_fd((char)(nbr % 10 + '0'), fd);
}

View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/04 13:45:44 by mdenys #+# #+# */
/* Updated: 2020/11/04 13:48:29 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putstr_fd(char *s, int fd)
{
int i;
i = 0;
if (s)
{
while (s[i])
{
ft_putchar_fd(s[i], fd);
i++;
}
}
}

View File

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/03 13:48:14 by mdenys #+# #+# */
/* Updated: 2020/11/03 20:34:46 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
static int ft_isspace(char c, char b)
{
return (c == b);
}
static int count_words(char *str, char c)
{
int count;
count = 0;
while (*str)
{
while (*str && ft_isspace(*str, c))
str++;
if (*str && !ft_isspace(*str, c))
{
count++;
while (*str && !ft_isspace(*str, c))
str++;
}
}
return (count);
}
static char *malloc_word(char *str, char c)
{
char *word;
int i;
i = 0;
while (str[i] && !ft_isspace(str[i], c))
i++;
word = (char *)malloc(sizeof(char) * (i + 1));
i = 0;
while (str[i] && !ft_isspace(str[i], c))
{
word[i] = str[i];
i++;
}
word[i] = '\0';
return (word);
}
char **ft_split(const char *str, char c)
{
char **arr;
int i;
i = 0;
arr = NULL;
if (str && (arr = malloc(sizeof(char *) * \
(count_words((char*)str, c) + 1))))
{
while (*str)
{
while (*str && ft_isspace(*str, c))
str++;
if (*str && !ft_isspace(*str, c))
{
arr[i] = malloc_word((char*)str, c);
i++;
while (*str && !ft_isspace(*str, c))
str++;
}
}
arr[i] = NULL;
}
return (arr);
}

View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/01 13:44:25 by mdenys #+# #+# */
/* Updated: 2020/11/05 18:00:06 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strchr(const char *str, int ch)
{
unsigned char *d;
unsigned int i;
unsigned int len;
len = ft_strlen((char *)str);
i = 0;
d = (unsigned char *)str;
if (str)
{
while (d[i] != (unsigned char)ch && i < len)
{
i++;
}
if (d[i] == (unsigned char)ch)
{
return (void *)(d + i);
}
return (0);
}
return (0);
}

View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/01 20:43:44 by mdenys #+# #+# */
/* Updated: 2020/11/01 21:11:41 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strdup(const char *str)
{
char *test;
int len;
len = ft_strlen((char *)str) + 1;
test = malloc(len * sizeof(char));
if (test == NULL)
return (NULL);
ft_strlcpy(test, str, (unsigned int)len);
return (test);
}

View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/02 17:18:40 by mdenys #+# #+# */
/* Updated: 2020/11/02 17:56:57 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strjoin(char const *s1, char const *s2)
{
int len1;
int len2;
char *new;
if (s1 && s2)
{
len1 = ft_strlen((char*)s1);
len2 = ft_strlen((char*)s2);
new = malloc(sizeof(char *) * len1 + len2 + 1);
if (new)
{
ft_strlcpy(new, s1, len1 + 1);
ft_strlcat(new, s2, len1 + len2 + 1);
return (void *)(new);
}
}
return (0);
}

View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/31 17:10:24 by mdenys #+# #+# */
/* Updated: 2020/11/05 16:58:02 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcat(char *dst, const char *src, size_t size)
{
unsigned int i;
unsigned int len;
len = 0;
if (!dst && !src)
return (0);
len = ft_strlen(dst);
i = 0;
if (size < len)
return (ft_strlen((char *)src) + size);
if (size)
{
while (src[i] && len + 1 < size)
dst[len++] = src[i++];
dst[len] = '\0';
}
while (src[i])
{
i++;
len++;
}
return (len);
}

View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/28 19:26:31 by mdenys #+# #+# */
/* Updated: 2020/11/05 16:57:58 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcpy(char *dst, const char *src, size_t size)
{
unsigned int i;
unsigned int count;
count = 0;
if (!dst && !src)
return (0);
while (src[count] != '\0')
{
++count;
}
i = 0;
if (size > 0)
{
while (src[i] != '\0' && i < (size - 1) && src[i])
{
dst[i] = src[i];
++i;
}
dst[i] = '\0';
}
return (count);
}

View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/28 19:10:44 by mdenys #+# #+# */
/* Updated: 2020/11/23 18:40:34 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strlen(char *str)
{
int i;
i = 0;
if (*str)
{
while (str[i] != '\0')
{
i++;
}
}
return (i);
}

View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/04 12:41:12 by mdenys #+# #+# */
/* Updated: 2020/11/04 13:01:25 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
int i;
int len;
char *str;
i = 0;
str = NULL;
if (s)
{
len = ft_strlen((char*)s);
str = (char*)malloc(sizeof(char) * len + 1);
if (str)
{
while (s[i])
{
str[i] = f(i, s[i]);
++i;
}
str[i] = '\0';
return (str);
}
}
return (0);
}

View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/29 12:22:15 by mdenys #+# #+# */
/* Updated: 2020/11/05 17:52:29 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strncmp(const char *s1, const char *s2, size_t a)
{
unsigned int i;
unsigned char *d;
unsigned char *s;
d = (unsigned char *)s1;
s = (unsigned char *)s2;
i = 0;
while (d[i] && s[i] && i < a)
{
if (d[i] != s[i])
return (d[i] - s[i]);
i++;
}
if (i != a)
{
return (d[i] - s[i]);
}
return (0);
}

View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/01 18:43:20 by mdenys #+# #+# */
/* Updated: 2020/11/04 18:41:20 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *hst, const char *ndl, size_t size)
{
unsigned int i;
unsigned int j;
if (ndl[0] == '\0')
return ((char *)hst);
i = 0;
while (hst[i])
{
j = 0;
if (hst[i] == ndl[j])
{
while (hst[i + j] == ndl[j] && i + j < size)
{
j++;
if (!ndl[j])
return ((void *)(hst + i));
}
}
i++;
}
return (0);
}

View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/01 16:14:36 by mdenys #+# #+# */
/* Updated: 2020/11/05 17:56:32 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strrchr(const char *str, int ch)
{
unsigned char *d;
unsigned int i;
unsigned int len;
len = ft_strlen((char *)str);
i = 0;
d = (unsigned char *)str;
if (str)
{
while (d[len] != (unsigned char)ch && 0 < len)
{
--len;
}
if (d[len] == (unsigned char)ch)
{
return (void *)(d + len);
}
}
return (0);
}

View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/02 17:58:31 by mdenys #+# #+# */
/* Updated: 2020/11/03 11:18:41 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(char const *s1, char const *set)
{
char *new;
int len;
len = 0;
if (!s1 || !set)
return (NULL);
while (*s1 && ft_strchr(set, *s1))
s1++;
len = ft_strlen((char*)s1);
while (len && ft_strchr(set, s1[len]))
len--;
new = ft_substr((char *)(s1), 0, len + 1);
return (new);
}

View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/02 12:21:46 by mdenys #+# #+# */
/* Updated: 2020/11/02 16:58:52 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *test;
test = NULL;
if (s)
{
test = malloc(sizeof(*s) * len + 1);
if (test && start <= (unsigned int)ft_strlen((char *)s))
{
ft_strlcpy(test, s + start, len + 1);
return (void *)(test);
}
}
return (void *)(test);
}

View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tolower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/28 14:40:51 by mdenys #+# #+# */
/* Updated: 2020/10/29 20:01:38 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_tolower(int a)
{
if (a >= 65 && a <= 90)
{
return (a + 32);
}
else
{
return (a);
}
return (0);
}

View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_toupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/28 15:04:43 by mdenys #+# #+# */
/* Updated: 2020/10/29 20:02:26 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_toupper(int a)
{
if (a >= 97 && a <= 122)
{
return (a - 32);
}
else
{
return (a);
}
return (0);
}

72
ft_printf/libft/libft.h Normal file
View File

@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/29 14:47:19 by mdenys #+# #+# */
/* Updated: 2020/12/14 19:57:25 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <stdlib.h>
# include <unistd.h>
int ft_atoi(const char *str);
void *ft_memset(void *dest, int val, size_t num);
void ft_bzero(void *s, size_t num);
void *ft_memcpy(void *destination, const void *source, size_t n);
void *ft_memccpy(void *dst, const void *src, int ch, size_t n);
void *ft_memmove(void *dst, const void *source, size_t n);
void *ft_memchr(const void *arr, int c, size_t n);
int ft_memcmp(const void *buf1, const void *buf2, size_t n);
int ft_strlen(char *str);
int ft_strncmp(const char *s1, const char *s2, size_t a);
int ft_isalpha(int c);
int ft_isdigit(int ch);
int ft_isalnum(int c);
int ft_isascii(int c);
int ft_isprint(int c);
int ft_toupper(int a);
int ft_tolower(int a);
char *ft_strchr(const char *str, int ch);
char *ft_strrchr(const char *str, int ch);
size_t ft_strlcpy(char *dst, const char *src, size_t size);
size_t ft_strlcat(char *dst, const char *src, size_t size);
char *ft_strnstr(const char *hst, const char *ndl, size_t size);
void *ft_calloc(size_t num, size_t size);
char *ft_strdup(const char *str);
char *ft_substr(char const *s, unsigned int start, size_t len);
char *ft_strjoin(char const *s1, char const *s2);
char *ft_strtrim(char const *s1, char const *set);
char **ft_split(char const *s, char c);
char *ft_itoa(int n);
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
void ft_putchar(char c);
void ft_putchar_fd(char c, int fd);
void ft_putstr_fd(char *s, int fd);
void ft_putendl_fd(char *s, int fd);
void ft_putnbr_fd(long int n, int fd);
void ft_putnbr_u_fd(unsigned int n, int fd);
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
t_list *ft_lstnew(void *content);
void ft_lstadd_front(t_list **lst, t_list *new);
int ft_lstsize(t_list *lst);
t_list *ft_lstlast(t_list *lst);
void ft_lstadd_back(t_list **lst, t_list *new);
void ft_lstdelone(t_list *lst, void (*del)(void *));
void ft_lstiter(t_list *lst, void (*f)(void *));
void ft_lstclear(t_list **lst, void (*del)(void*));
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), \
void (*del)(void *));
#endif

View File

@ -0,0 +1,116 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/23 18:47:42 by mdenys #+# #+# */
/* Updated: 2020/12/16 05:50:42 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_printf.h"
void szero(t_printf *params)
{
params->is_left = 0;
params->is_prec = 0;
params->is_zero = 0;
params->prec = 0;
params->rec_prec = 0;
params->to_up = 0;
params->wh = 0;
params->v_min = 0;
params->count = 0;
}
int ft_parser(char **str, va_list *arg)
{
t_printf params;
int result;
result = 0;
szero(&params);
if (**str == '%')
{
result += write(1, "%", 1);
(*str) += 1;
return (result);
}
while (**str)
{
if (is_flag(str, &params))
continue ;
else if (is_star(str, arg, &params))
continue ;
else if (is_digit(str, &params))
continue ;
else if (is_type(str, &result, arg, &params))
break ;
else
(*str) += 1 && result++;
}
return (result);
}
int is_flag(char **str, t_printf *params)
{
int result;
result = 0;
if (**str == '-' && ++result)
{
if (params->rec_prec)
{
params->is_prec = 0;
params->rec_prec = 0;
}
params->is_left = 1;
}
else if (**str == '.' && ++result)
{
params->prec = 0;
params->is_prec = 1;
params->rec_prec = 1;
}
(*str) += result;
return (result);
}
void is_star2(int *value, t_printf *params)
{
if (params->rec_prec)
{
if (*value < 0)
params->is_prec = 0;
else
params->prec = *value;
params->rec_prec = 0;
}
else
{
if (*value < 0)
{
params->is_left = 1;
params->wh = *value * -1;
}
else
params->wh = *value;
}
}
int is_star(char **str, va_list *arg, t_printf *params)
{
int result;
int value;
result = 0;
if (**str == '*' && ++result)
{
value = va_arg(*arg, int);
is_star2(&value, params);
}
(*str) += result;
return (result);
}

View File

@ -0,0 +1,141 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parser_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/25 13:33:20 by mdenys #+# #+# */
/* Updated: 2020/12/16 05:48:22 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_printf.h"
void printf_value(t_printf *list, int value)
{
int len;
long int cpval;
cpval = value;
len = len_value(list, value);
if (list->v_min == 1)
{
ft_putchar_fd_count(list, '-', 1);
list->v_min = 0;
cpval *= -1;
}
if (list->rec_prec == 1 && list->prec == 0 && list->wh == 0)
ft_putnbr_fd_count(list, cpval, 1);
else if (list->prec < lennbr(value))
ft_putnbr_fd_count(list, cpval, 1);
else
{
while_c(list, '0', len - lennbr(cpval));
ft_putnbr_fd_count(list, cpval, 1);
}
}
void printf_u_value(t_printf *list, unsigned int value)
{
unsigned int len;
unsigned int cpval;
cpval = value;
len = len_u_value(list, value);
if (list->v_min == 1)
{
ft_putchar_fd_count(list, '-', 1);
list->v_min = 0;
cpval *= -1;
}
if (list->rec_prec == 1 && list->prec == 0 && list->wh == 0)
;
else if (list->prec < u_lennbr(value))
ft_putnbr_u_fd_count(list, cpval, 1);
else
{
while_c(list, '0', len - u_lennbr(cpval));
ft_putnbr_u_fd_count(list, cpval, 1);
}
}
void printf_x_value(t_printf *list, unsigned int value)
{
unsigned int len;
unsigned int cpval;
cpval = value;
len = len_x_value(list, value);
if (list->is_prec == 1 && list->prec == 0 && list->wh == 1 && value == 0)
while_c(list, ' ', list->wh);
else if (list->prec == 0 && list->is_prec == 1 && \
value == 0 && list->wh == 0)
;
else if (list->rec_prec == 1 && list->prec == 0 && list->wh == 0)
;
else if (list->prec < len_hex(value))
print_hex(cpval, list);
else
{
while_c(list, '0', len - len_hex(cpval));
print_hex(cpval, list);
}
}
int is_digit(char **str, t_printf *params)
{
int result;
int value;
result = 0;
if (ft_isdigit(**str) && ++result)
{
while (**str == '0')
{
params->is_zero = 1;
(*str)++;
}
if (ft_isdigit(**str))
{
value = ft_atoi(*str);
(*str) += lennbr(value);
if (params->rec_prec)
{
params->rec_prec = 0;
params->prec = value;
}
else
params->wh = value;
}
}
return (result);
}
int is_type(char **str, int *cout, va_list *arg, t_printf *params)
{
int result;
result = 0;
if (**str == 'c' && ++result)
*cout = ft_put_c(params, arg);
else if (**str == 's' && ++result)
*cout = ft_put_s(params, arg);
else if (**str == 'p' && ++result)
*cout = ft_put_p(params, arg);
else if ((**str == 'd' || **str == 'i') && ++result)
*cout = ft_put_d(params, arg);
else if (**str == 'u' && ++result)
*cout = ft_put_u(params, arg);
else if (**str == '%' && ++result)
*cout = ft_put_percent(params);
else if (**str == 'x' && ++result)
*cout = ft_put_x(params, arg);
else if (**str == 'X' && ++result)
{
params->to_up = 1;
*cout = ft_put_x(params, arg);
}
(*str) += result;
return (result);
}

View File

@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_put_c.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/02 09:51:16 by mdenys #+# #+# */
/* Updated: 2020/12/14 21:41:40 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_printf.h"
int ft_put_c(t_printf *list, va_list *name)
{
char c;
int d;
d = list->wh - 1;
c = va_arg(*name, int);
if (list->wh != 0 && list->is_left == 0)
{
while_c(list, ' ', d);
ft_putchar_fd_count(list, c, 1);
return (list->count);
}
else if (list->wh != 0 && list->is_left == 1)
{
ft_putchar_fd_count(list, c, 1);
while_c(list, ' ', d);
return (list->count);
}
else
{
ft_putchar_fd_count(list, c, 1);
return (list->count);
}
}
int ft_put_percent(t_printf *list)
{
int d;
d = list->wh - 1;
if (list->wh != 0 && list->is_left == 0)
{
list->is_zero == 0 ? while_c(list, ' ', d) : while_c(list, '0', d);
ft_putchar_fd_count(list, '%', 1);
return (list->wh);
}
else if (list->wh != 0 && list->is_left == 1)
{
ft_putchar_fd_count(list, '%', 1);
while_c(list, ' ', d);
return (list->wh);
}
else
{
ft_putchar_fd_count(list, '%', 1);
return (1);
}
}

View File

@ -0,0 +1,71 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_put_p.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/02 09:51:16 by mdenys #+# #+# */
/* Updated: 2020/12/14 21:55:46 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_printf.h"
void print_hep(t_printf *list, long int value)
{
long int remainder;
int i;
int j;
char hepa[16];
i = 0;
j = 0;
while (value)
{
remainder = value % 16;
if (remainder < 10)
hepa[j++] = 48 + remainder;
else
hepa[j++] = 55 + remainder;
value = value / 16;
}
i = j - 1;
while (i >= 0)
{
ft_putchar_fd_count(list, ft_tolower(hepa[i]), 1);
i--;
}
}
int len_hep(long int value)
{
long int remainder;
int i;
int j;
long int cpval;
char hepa[16];
i = 0;
j = 0;
cpval = value;
while (cpval)
{
remainder = cpval % 16;
if (remainder < 10)
hepa[j++] = 48 + remainder;
else
hepa[j++] = 55 + remainder;
cpval = cpval / 16;
}
return (value == 0 ? 2 : j + 2);
}
int ft_put_p(t_printf *list, va_list *name)
{
long int value;
value = va_arg(*name, long int);
ft_write_p(list, value);
return (list->count);
}

View File

@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_put_s.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/02 09:51:16 by mdenys #+# #+# */
/* Updated: 2020/12/16 05:30:38 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_printf.h"
int ft_prints(t_printf *list, char *value)
{
int i;
int len;
len = ft_strlen(value);
i = 0;
if (list->is_prec == 1)
while (i < list->prec && i < len)
{
ft_putchar_fd_count(list, value[i], 1);
i++;
}
else
while (value[i])
{
ft_putchar_fd_count(list, value[i], 1);
i++;
}
return (i);
}
int count_tab(t_printf *list, char *value)
{
int len;
int i;
i = 0;
len = ft_strlen(value);
if (list->wh > 0 && list->prec == 0 && list->is_prec == 0)
i = list->wh - (list->prec > len ? list->prec : len);
else
i = list->wh - (list->prec > len ? len : list->prec);
return (i);
}
char *get_line(va_list *name)
{
char *line;
if ((line = va_arg(*name, char *)) == NULL)
line = "(null)";
return (line);
}
int put_string(t_printf *list, va_list *name)
{
int temp;
int count;
char *line;
count = 0;
line = get_line(name);
temp = count_tab(list, line);
if (list->is_left == 1)
{
count = ft_prints(list, line) + temp;
while_c(list, ' ', temp);
}
else if ((list->wh > 0 && list->is_prec >= 0) || (list->wh > 0))
{
list->is_zero == 0 ? while_c(list, ' ', temp) \
: while_c(list, '0', temp);
count = ft_prints(list, line) + temp;
}
else
count = ft_prints(list, line);
return (count);
}
int ft_put_s(t_printf *list, va_list *name)
{
put_string(list, name);
return (list->count);
}

View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_put_u.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/02 09:51:16 by mdenys #+# #+# */
/* Updated: 2020/12/16 00:57:51 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_printf.h"
int ft_put_u(t_printf *list, va_list *name)
{
unsigned int value;
value = va_arg(*name, unsigned int);
ft_write_u(list, value);
return (list->count);
}

View File

@ -0,0 +1,73 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_put_x.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/02 09:51:16 by mdenys #+# #+# */
/* Updated: 2020/12/16 02:03:54 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_printf.h"
void print_hex(unsigned int value, t_printf *list)
{
long remainder;
int i;
int j;
char hexa[8];
i = 0;
j = 0;
value == 0 ? ft_putchar_fd_count(list, '0', 1) : 0;
while (value)
{
remainder = value % 16;
if (remainder < 10)
hexa[j++] = 48 + remainder;
else
hexa[j++] = 55 + remainder;
value = value / 16;
}
i = j - 1;
while (i >= 0)
{
list->count += ft_printf("%c", list->to_up == 1 ? hexa[i] : \
ft_tolower(hexa[i]));
i--;
}
}
int len_hex(unsigned int value)
{
long remainder;
int i;
int j;
unsigned int cpval;
char hexa[8];
i = 0;
j = 0;
cpval = value;
while (cpval)
{
remainder = cpval % 16;
if (remainder < 10)
hexa[j++] = 48 + remainder;
else
hexa[j++] = 55 + remainder;
cpval = cpval / 16;
}
return (value == 0 ? 1 : j);
}
int ft_put_x(t_printf *list, va_list *name)
{
unsigned int value;
value = va_arg(*name, unsigned int);
ft_write_x(list, value);
return (list->count);
}

View File

@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_put_d.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/02 09:51:16 by mdenys #+# #+# */
/* Updated: 2020/12/16 05:21:31 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../ft_printf.h"
void d_iszero(t_printf *list, int value)
{
int c;
int tmp;
c = value;
tmp = list->wh - len_value(list, value);
if (list->v_min == 1)
for_min_diszero(list, value, tmp, &c);
else if (list->rec_prec == 1 && value == 0)
while_c(list, ' ', list->wh);
else if (value == 0 && list->wh != 0 && list->prec == 0)
while_c(list, '0', list->wh);
else
for_diszero(list, value, tmp, c);
}
void d_isprec(t_printf *list, int value)
{
int tmp;
tmp = list->wh - len_value(list, value);
if (list->v_min == 1)
tmp--;
if (list->wh != 0 && list->prec == 0 && list->rec_prec == 1)
while_c(list, ' ', list->wh);
else
{
while_c(list, ' ', tmp);
printf_value(list, value);
}
}
void ft_write_d(t_printf *list, int value)
{
if ((list->wh > len_value(list, value)))
ft_write_d_2(list, value);
else if (list->is_left == 1 || (list->is_prec == 1 && list->wh > 0))
d_isleft(list, value);
else if (list->wh == 0 && list->is_prec == 1 && list->rec_prec == 1)
d_iszero(list, value);
else if (list->is_prec == 1 && list->rec_prec == 1)
d_iszero(list, value);
else if (list->rec_prec == 1)
d_isprec(list, value);
else if (list->is_prec == 1 && list->prec == 0 && value == 0)
;
else
printf_value(list, value);
}
void ft_write_d_2(t_printf *list, int value)
{
if (list->is_left == 1)
d_isleft(list, value);
else if (list->is_left == 0 && list->is_prec == 1 && value != 0)
d_isprec(list, value);
else if (list->is_prec == 1 && list->prec == 0)
d_isleft(list, value);
else if (list->is_zero == 1)
d_iszero(list, value);
else if (list->is_prec == 1)
d_isprec(list, value);
else
d_isprec(list, value);
}
int ft_put_d(t_printf *list, va_list *name)
{
int value;
value = va_arg(*name, int);
ft_write_d(list, value);
return (list->count);
}

View File

@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_put_d_undersize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/02 09:51:16 by mdenys #+# #+# */
/* Updated: 2020/12/16 05:22:28 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../ft_printf.h"
void d_isleft(t_printf *list, int value)
{
int tmp;
tmp = list->wh - len_value(list, value);
if (list->is_prec == 1 && list->prec == 0 && value == 0)
while_c(list, ' ', list->wh);
else if (list->wh > 0 && list->prec == 0 && list->rec_prec == 0 && \
value == 0)
{
if (list->is_left == 1)
{
printf_value(list, value);
while_c(list, ' ', tmp);
}
else
{
while_c(list, ' ', tmp);
printf_value(list, value);
}
}
else if (list->prec == 0 && value == 0 && list->wh != 0)
while_c(list, ' ', list->wh);
else
d_isleft_2(list, value);
}
void d_isleft_2(t_printf *list, int value)
{
if (list->v_min == 1)
{
printf_value(list, value);
while_c(list, ' ', (list->wh - len_value(list, value) - 1));
}
else
{
if (list->is_left == 1)
{
printf_value(list, value);
while_c(list, ' ', (list->wh - len_value(list, value)));
}
else
{
while_c(list, ' ', (list->wh - len_value(list, value)));
printf_value(list, value);
}
}
}

View File

@ -0,0 +1,88 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_put_d_utilc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/02 09:51:16 by mdenys #+# #+# */
/* Updated: 2020/12/15 17:12:10 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../ft_printf.h"
void for_diszero(t_printf *list, int value, int tmp, int c)
{
if (value == 0)
while_c(list, ' ', (list->wh - len_value(list, c)));
else if (list->is_prec == 1)
while_c(list, ' ', tmp);
else
while_c(list, '0', (list->wh - len_value(list, c)));
printf_value(list, c);
}
void for_min_diszero(t_printf *list, int value, int tmp, int *c)
{
if (list->is_prec == 0)
{
ft_putchar_fd_count(list, '-', 1);
list->v_min = 0;
*c *= -1;
while_c(list, '0', tmp - 1);
printf_value(list, *c);
}
else
{
while_c(list, ' ', tmp - 1);
printf_value(list, value);
}
}
int lennbr(long int n)
{
int digits;
digits = 1;
if (n < 0)
n *= -1;
while (n >= 10)
{
n /= 10;
digits++;
}
return (digits);
}
void while_c(t_printf *list, char c, int i)
{
if (i > 0)
{
while (i--)
ft_putchar_fd_count(list, c, 1);
}
}
int len_value(t_printf *list, long int value)
{
int count;
count = 0;
if (list->prec == 0 || list->prec <= lennbr(value))
{
count = lennbr(value);
value < 0 ? list->v_min = 1 : 0;
}
else if (value < 0)
{
list->v_min = 1;
count = lennbr(value) + ((list->prec - lennbr(value) > 0) ?\
list->prec - lennbr(value) : 0);
}
else if (list->prec > lennbr(value))
{
count = list->prec;
}
return (count);
}

View File

@ -0,0 +1,69 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_put_p.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/02 09:51:16 by mdenys #+# #+# */
/* Updated: 2020/12/16 05:55:32 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../ft_printf.h"
void p_isleft(t_printf *list, long int value)
{
printf_p_value(list, value);
if (value == 0)
while_c(list, ' ', list->wh - len_hep(value) - 1);
else
while_c(list, ' ', list->wh - len_hep(value));
}
void p_iszero(t_printf *list, long int value)
{
long int c;
long int temp;
c = value;
temp = list->wh - len_hep(value);
if (list->rec_prec == 1 && list->prec == 0)
list->count += ft_printf("0x");
else if (value == 0)
while_c(list, ' ', temp);
else if (value == 0 && list->wh != 0 && list->prec == 0)
{
while_c(list, '0', temp);
}
else
for_piszero(list, value, temp, c);
}
void p_isprec(t_printf *list, long int value)
{
if (list->wh > 0 && list->prec == 0 && value == 0)
while_c(list, ' ', list->wh - 3);
else
while_c(list, ' ', list->wh - len_hep(value));
printf_p_value(list, value);
}
void ft_write_p(t_printf *list, long int value)
{
if (list->is_left == 1)
p_isleft(list, value);
else if (list->is_zero == 1)
p_iszero(list, value);
else if (list->rec_prec == 1 || list->wh > 0)
p_isprec(list, value);
else
printf_p_value(list, value);
}
void printf_p_value_2(t_printf *list, long int value, int b)
{
list->count += ft_printf("0x");
while_c(list, '0', b);
print_hep(list, value);
}

View File

@ -0,0 +1,103 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_put_p_utilc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/02 09:51:16 by mdenys #+# #+# */
/* Updated: 2020/12/16 05:26:52 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../ft_printf.h"
int p_counter(t_printf *list, long int value)
{
int count;
count = 0;
if (list->wh > len_hep(value))
count = list->wh;
if (value < 0)
count += 1;
else
{
count += len_hep(value);
}
return (count);
}
void for_piszero(t_printf *list, long int value, long int tmp, long int c)
{
if (value == 0)
{
while_c(list, ' ', (list->wh - len_hep(c)));
list->count += ft_printf("0x");
}
else if (list->is_prec == 1)
{
while_c(list, ' ', tmp);
list->count += ft_printf("0x");
}
else
{
list->count += ft_printf("0x");
while_c(list, '0', (list->wh - len_hep(c)));
}
printf_p_value(list, c);
}
void for_min_piszero(t_printf *list, long int value, \
long int tmp, long int *c)
{
if (list->is_prec == 0)
{
ft_putchar('-');
list->v_min = 0;
*c *= -1;
list->count += ft_printf("0x");
while_c(list, '0', tmp - 1);
printf_p_value(list, *c);
}
else
{
while_c(list, ' ', tmp - 1);
list->count += ft_printf("0x");
printf_p_value(list, value);
}
}
int check_zero(t_printf *list, int value)
{
if (list->is_left == 0 && list->is_prec == 0 && \
list->is_zero == 0 && list->prec == 0 \
&& list->rec_prec == 0 && list->to_up == 0 \
&& list->wh == 0 && list->v_min == 0 && value == 0)
return (1);
return (0);
}
void printf_p_value(t_printf *list, long int value)
{
long int len;
len = list->wh - len_hep(value);
if (list->rec_prec == 1 && list->prec == 0 && list->wh == 0)
{
list->count += ft_printf("0x");
print_hep(list, value);
}
else if (check_zero(list, value))
list->count += ft_printf("0x0");
else if (list->wh != 0 && list->prec == 0 && list->is_zero == 0)
{
value == 0 ? (list->count += ft_printf("0x0")) : \
(list->count += ft_printf("0x"));
print_hep(list, value);
}
else if (list->is_prec == 1 || list->rec_prec == 1)
printf_p_value_2(list, value, list->prec - len_hep(value) + 2);
else
printf_p_value_2(list, value, len);
}

View File

@ -0,0 +1,100 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_put_u.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/02 09:51:16 by mdenys #+# #+# */
/* Updated: 2020/12/16 05:28:39 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../ft_printf.h"
void u_isleft(t_printf *list, unsigned int value)
{
unsigned int tmp;
tmp = list->wh - len_u_value(list, value);
if (list->wh > 0 && list->is_prec == 1 && list->prec == 0 && value == 0)
while_c(list, ' ', list->wh);
else if (list->wh > 0 && list->prec == 0 && list->rec_prec == 0 && \
value == 0)
{
printf_u_value(list, value);
while_c(list, ' ', tmp);
}
else if (list->prec == 0 && value == 0 && list->wh != 0)
while_c(list, ' ', list->wh);
else
u_isleft_ext(list, value);
}
void u_isleft_ext(t_printf *list, unsigned int value)
{
if (list->v_min == 1)
{
printf_u_value(list, value);
while_c(list, ' ', (list->wh - len_u_value(list, value) - 1));
}
else
{
printf_u_value(list, value);
while_c(list, ' ', (list->wh - len_u_value(list, value)));
}
}
void u_iszero(t_printf *list, unsigned int value)
{
unsigned int c;
unsigned int tmp;
c = value;
tmp = list->wh - len_u_value(list, value);
if (list->wh > 0 && list->is_prec == 1 && list->prec == 0 && value == 0)
while_c(list, ' ', list->wh);
else if (list->rec_prec == 1 && value == 0)
while_c(list, ' ', list->wh);
else if (value == 0 && list->wh != 0 && list->prec == 0)
while_c(list, '0', list->wh);
else
for_uiszero(list, value, tmp, c);
}
void u_isprec(t_printf *list, unsigned int value)
{
unsigned int tmp;
tmp = list->wh - len_u_value(list, value);
if (list->wh > 0 && list->is_prec == 1 && list->prec == 0 && value == 0)
while_c(list, ' ', list->wh);
else if (list->wh != 0 && list->prec == 0 && list->rec_prec == 1)
while_c(list, ' ', list->wh);
else
{
while_c(list, ' ', tmp);
printf_u_value(list, value);
}
}
void ft_write_u(t_printf *list, unsigned int value)
{
if ((list->wh > len_u_value(list, value)))
{
if (list->is_left == 1)
u_isleft(list, value);
else if (list->is_zero == 1)
u_iszero(list, value);
else if (list->is_prec == 1)
u_isprec(list, value);
else
u_isprec(list, value);
}
else if (list->rec_prec == 1)
u_isprec(list, value);
else if (list->is_prec == 1 && list->prec == 0 && value == 0)
while_c(list, ' ', list->wh);
else
printf_u_value(list, value);
}

View File

@ -0,0 +1,80 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_put_u_utilc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/02 09:51:16 by mdenys #+# #+# */
/* Updated: 2020/12/16 05:29:15 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../ft_printf.h"
void for_uiszero(t_printf *list, unsigned int value, int tmp, unsigned int c)
{
if (value == 0)
while_c(list, ' ', (list->wh - len_value(list, c)));
else if (list->is_prec == 1)
while_c(list, ' ', tmp);
else
while_c(list, '0', (list->wh - len_value(list, c)));
printf_u_value(list, c);
}
void for_min_uiszero(t_printf *list, unsigned int value,\
int tmp, unsigned int *c)
{
if (list->is_prec == 0)
{
ft_putchar('-');
list->v_min = 0;
*c *= -1;
while_c(list, '0', tmp - 1);
printf_u_value(list, *c);
}
else
{
while_c(list, ' ', tmp - 1);
printf_u_value(list, value);
}
}
int len_u_value(t_printf *list, unsigned int value)
{
unsigned int count;
count = 0;
if (list->prec == 0 || list->prec <= u_lennbr(value))
{
count = u_lennbr(value);
value < 0 ? list->v_min = 1 : 0;
}
else if (value < 0)
{
list->v_min = 1;
count = u_lennbr(value) + ((list->prec - u_lennbr(value) > 0) ?\
list->prec - u_lennbr(value) : 0);
}
else if (list->prec > u_lennbr(value))
{
count = list->prec;
}
return (count);
}
int u_lennbr(unsigned int n)
{
int digits;
digits = 1;
if (n < 0)
n *= -1;
while (n >= 10)
{
n /= 10;
digits++;
}
return (digits);
}

View File

@ -0,0 +1,109 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_put_x.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/02 09:51:16 by mdenys #+# #+# */
/* Updated: 2020/12/16 05:15:12 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../ft_printf.h"
void x_isleft(t_printf *list, unsigned int value)
{
unsigned int tmp;
tmp = list->wh - len_x_value(list, value);
if (list->rec_prec == 0 && list->prec == 0 && \
list->wh > 0 && list->is_left == 1 && list->is_prec == 1 && value == 0)
while_c(list, ' ', list->wh);
else if (list->wh > 0 && list->prec == 0 && list->rec_prec == 0 && \
value == 0)
{
printf_x_value(list, value);
while_c(list, ' ', tmp);
}
else if (list->prec == 0 && value == 0 && list->wh != 0)
while_c(list, ' ', list->wh);
else
{
printf_x_value(list, value);
while_c(list, ' ', tmp);
}
}
void x_iszero(t_printf *list, unsigned int value)
{
unsigned int c;
unsigned int tmp;
c = value;
tmp = list->wh - len_x_value(list, value);
if (list->rec_prec == 0 && list->prec == 0 && \
list->wh > 0 && list->is_zero == 1 && list->is_prec == 1 && value == 0)
while_c(list, ' ', list->wh);
else if (list->rec_prec == 1 && value == 0)
while_c(list, ' ', list->wh);
else if (value == 0 && list->wh != 0 && list->prec == 0)
while_c(list, '0', list->wh);
else
for_xiszero(list, value, tmp, c);
}
void x_isprec(t_printf *list, unsigned int value)
{
unsigned int tmp;
tmp = list->wh - len_x_value(list, value);
if (list->wh != 0 && list->prec == 0 && list->rec_prec == 1)
while_c(list, ' ', list->wh);
else if (list->wh > 0 && list->is_prec == 1 \
&& list->prec == 0 && value == 0)
while_c(list, ' ', list->wh);
else
{
while_c(list, ' ', tmp);
printf_x_value(list, value);
}
}
int len_x_value(t_printf *list, unsigned int value)
{
unsigned int count;
count = 0;
if (list->prec == 0 || list->prec <= len_hex(value))
{
count = len_hex(value);
value < 0 ? list->v_min = 1 : 0;
}
else if (list->prec > len_hex(value))
{
count = list->prec;
}
return (count);
}
void ft_write_x(t_printf *list, unsigned int value)
{
if ((list->wh > len_x_value(list, value)))
{
if (list->is_left == 1)
x_isleft(list, value);
else if (list->is_zero == 1)
x_iszero(list, value);
else if (list->is_prec == 1)
x_isprec(list, value);
else
x_isprec(list, value);
}
else if (list->is_left == 1)
x_isleft(list, value);
else if (list->rec_prec == 1)
x_isprec(list, value);
else
printf_x_value(list, value);
}

View File

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_put_x_utilc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/02 09:51:16 by mdenys #+# #+# */
/* Updated: 2020/12/16 05:30:04 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../ft_printf.h"
void for_xiszero(t_printf *list, unsigned int value, int tmp, unsigned int c)
{
if (value == 0)
while_c(list, ' ', (list->wh - len_x_value(list, c)));
else if (list->is_prec == 1)
while_c(list, ' ', tmp);
else
while_c(list, '0', (list->wh - len_x_value(list, c)));
printf_x_value(list, c);
}
void for_min_xiszero(t_printf *list, unsigned int value, \
int tmp, unsigned int *c)
{
if (list->is_prec == 0)
{
ft_putchar('-');
list->v_min = 0;
*c *= -1;
while_c(list, '0', tmp - 1);
printf_x_value(list, *c);
}
else
{
while_c(list, ' ', tmp - 1);
printf_x_value(list, value);
}
}

View File

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* putmaket_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/02 09:51:16 by mdenys #+# #+# */
/* Updated: 2020/12/16 05:39:25 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_printf.h"
void ft_putnbr_fd_count(t_printf *list, long int n, int fd)
{
long int nbr;
if (n < 0)
{
ft_putchar_fd_count(list, '-', fd);
nbr = (unsigned int)(n * -1);
}
else
nbr = (unsigned int)n;
if (nbr >= 10)
ft_putnbr_fd_count(list, nbr / 10, fd);
ft_putchar_fd_count(list, (char)(nbr % 10 + '0'), fd);
}
void ft_putnbr_u_fd_count(t_printf *list, unsigned int n, int fd)
{
unsigned int nbr;
if (n < 0)
{
ft_putchar_fd_count(list, '-', fd);
nbr = (unsigned int)(n * -1);
}
else
nbr = (unsigned int)n;
if (nbr >= 10)
ft_putnbr_u_fd_count(list, nbr / 10, fd);
ft_putchar_fd_count(list, (char)(nbr % 10 + '0'), fd);
}

View File

@ -0,0 +1,92 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/12 13:47:35 by mdenys #+# #+# */
/* Updated: 2020/11/17 23:54:27 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include <stdio.h>
char *check_reminder(char *reminder)
{
char *rtn;
int i;
int j;
i = 0;
j = 0;
if (!reminder)
return (0);
while (reminder[i] && reminder[i] != '\n')
i++;
if (!reminder[i])
{
free(reminder);
return (0);
}
if (!(rtn = malloc(sizeof(char) * ((ft_strlen(reminder) - i) + 1))))
return (0);
i++;
while (reminder[i])
rtn[j++] = reminder[i++];
rtn[j] = '\0';
free(reminder);
return (rtn);
}
char *gnl(char *str)
{
int i;
char *rtn;
i = 0;
if (!str)
return (0);
while (str[i] && str[i] != '\n')
i++;
if (!(rtn = malloc(sizeof(char) * (i + 1))))
return (0);
i = 0;
while (str[i] && str[i] != '\n')
{
rtn[i] = str[i];
i++;
}
rtn[i] = '\0';
return (rtn);
}
int get_next_line(int fd, char **line)
{
char *buff;
static char *reminder;
int r_d;
r_d = 1;
if (fd < 0 || !line || BUFFER_SIZE <= 0)
return (-1);
if (!(buff = malloc(sizeof(char) * (BUFFER_SIZE + 1))))
return (-1);
while (!ft_rn(reminder) && r_d != 0)
{
if ((r_d = read(fd, buff, BUFFER_SIZE)) == -1)
{
free(buff);
return (-1);
}
buff[r_d] = '\0';
reminder = ft_strjoin(reminder, buff);
}
free(buff);
*line = gnl(reminder);
reminder = check_reminder(reminder);
if (r_d == 0)
return (0);
return (1);
}

View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/13 21:55:13 by mdenys #+# #+# */
/* Updated: 2020/11/17 23:22:55 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# include <stdlib.h>
# include <unistd.h>
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 48
# endif
int get_next_line(int fd, char **line);
int ft_rn(char *str);
size_t ft_strlen(const char *s);
char *ft_strjoin(const char *s1, const char *s2);
#endif

View File

@ -0,0 +1,122 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/12 13:47:35 by mdenys #+# #+# */
/* Updated: 2020/11/17 23:55:45 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
char *check_reminder(char *reminder)
{
char *rtn;
int i;
int j;
i = 0;
j = 0;
if (!reminder)
return (0);
while (reminder[i] && reminder[i] != '\n')
i++;
if (!reminder[i])
{
free(reminder);
return (0);
}
if (!(rtn = malloc(sizeof(char) * ((ft_strlen(reminder) - i) + 1))))
return (0);
i++;
while (reminder[i])
rtn[j++] = reminder[i++];
rtn[j] = '\0';
free(reminder);
return (rtn);
}
char *gnl(char *str)
{
int i;
char *rtn;
i = 0;
if (!str)
return (0);
while (str[i] && str[i] != '\n')
i++;
if (!(rtn = malloc(sizeof(char) * (i + 1))))
return (0);
i = 0;
while (str[i] && str[i] != '\n')
{
rtn[i] = str[i];
i++;
}
rtn[i] = '\0';
return (rtn);
}
int get_line(int fd, char **line, char **reminder)
{
char *buff;
int r_d;
r_d = 1;
if (fd < 0 || !line || BUFFER_SIZE <= 0)
return (-1);
if (!(buff = malloc(sizeof(char) * (BUFFER_SIZE + 1))))
return (-1);
while (!ft_rn(*reminder) && r_d != 0)
{
if ((r_d = read(fd, buff, BUFFER_SIZE)) == -1)
{
free(buff);
return (-1);
}
buff[r_d] = '\0';
*reminder = ft_strjoin(*reminder, buff);
}
free(buff);
*line = gnl(*reminder);
*reminder = check_reminder(*reminder);
if (r_d == 0)
return (0);
return (1);
}
t_netnext_line *new_linked_list_el(const int fd)
{
t_netnext_line *new;
new = (t_netnext_line *)malloc(sizeof(t_netnext_line));
new->fd = fd;
new->reminder = NULL;
new->next = NULL;
return (new);
}
int get_next_line(int fd, char **line)
{
static t_netnext_line *head;
t_netnext_line *temp;
if (fd < 0)
return (-1);
if (fd == 0)
return (0);
if (head == NULL)
head = new_linked_list_el(fd);
temp = head;
while (temp->fd != fd)
{
if (temp->next == NULL)
temp->next = new_linked_list_el(fd);
temp = temp->next;
}
return (get_line(fd, line, &temp->reminder));
}

View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/13 21:55:13 by mdenys #+# #+# */
/* Updated: 2020/11/17 23:58:39 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_BONUS_H
# define GET_NEXT_LINE_BONUS_H
# include <stdlib.h>
# include <unistd.h>
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 5000
# endif
typedef struct s_netnext_line
{
int fd;
char *reminder;
struct s_netnext_line *next;
} t_netnext_line;
int get_next_line(int fd, char **line);
int ft_rn(char *str);
size_t ft_strlen(const char *s);
char *ft_strjoin(const char *s1, const char *s2);
#endif

View File

@ -0,0 +1,83 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/14 10:50:53 by mdenys #+# #+# */
/* Updated: 2020/11/17 23:56:51 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
size_t ft_strlen(const char *s)
{
int i;
i = 0;
if (!s)
return (0);
while (s[i] != '\0')
i++;
return (i);
}
void *ft_memmove(void *dst, const void *src, size_t len)
{
char *d;
char *s;
d = (char *)dst;
s = (char *)src;
if (dst == src)
return (dst);
if (s < d)
{
while (len--)
*(d + len) = *(s + len);
return (dst);
}
while (len--)
*d++ = *s++;
return (dst);
}
char *ft_strjoin(char const *s1, char const *s2)
{
size_t s1_len;
size_t s2_len;
size_t stot_len;
char *rtn;
if (!s1 && !s2)
return (0);
s1_len = ft_strlen((char *)s1);
s2_len = ft_strlen((char *)s2);
stot_len = s1_len + s2_len + 1;
rtn = malloc(sizeof(char) * stot_len);
if (!rtn)
return (0);
ft_memmove(rtn, s1, s1_len);
ft_memmove(rtn + s1_len, s2, s2_len);
rtn[stot_len - 1] = '\0';
free((char *)s1);
return (rtn);
}
int ft_rn(char *str)
{
int i;
i = 0;
if (!str)
return (0);
while (str[i])
{
if (str[i] == '\n')
return (1);
i++;
}
return (0);
}

View File

@ -0,0 +1,83 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/14 10:50:53 by mdenys #+# #+# */
/* Updated: 2020/11/17 23:56:22 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
size_t ft_strlen(const char *s)
{
int i;
i = 0;
if (!s)
return (0);
while (s[i] != '\0')
i++;
return (i);
}
void *ft_memmove(void *dst, const void *src, size_t len)
{
char *d;
char *s;
d = (char *)dst;
s = (char *)src;
if (dst == src)
return (dst);
if (s < d)
{
while (len--)
*(d + len) = *(s + len);
return (dst);
}
while (len--)
*d++ = *s++;
return (dst);
}
char *ft_strjoin(char const *s1, char const *s2)
{
size_t s1_len;
size_t s2_len;
size_t stot_len;
char *rtn;
if (!s1 && !s2)
return (0);
s1_len = ft_strlen((char *)s1);
s2_len = ft_strlen((char *)s2);
stot_len = s1_len + s2_len + 1;
rtn = malloc(sizeof(char) * stot_len);
if (!rtn)
return (0);
ft_memmove(rtn, s1, s1_len);
ft_memmove(rtn + s1_len, s2, s2_len);
rtn[stot_len - 1] = '\0';
free((char *)s1);
return (rtn);
}
int ft_rn(char *str)
{
int i;
i = 0;
if (!str)
return (0);
while (str[i])
{
if (str[i] == '\n')
return (1);
i++;
}
return (0);
}

0
test.c
View File