ft_printf
This commit is contained in:
commit
81d78420a1
12 changed files with 309 additions and 0 deletions
46
Makefile
Normal file
46
Makefile
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# ::: :::::::: #
|
||||||
|
# Makefile :+: :+: :+: #
|
||||||
|
# +:+ +:+ +:+ #
|
||||||
|
# By: rparodi <marvin@42.fr> +#+ +:+ +#+ #
|
||||||
|
# +#+#+#+#+#+ +#+ #
|
||||||
|
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
|
||||||
|
# Updated: 2023/11/16 15:14:07 by rparodi ### ########.fr #
|
||||||
|
# #
|
||||||
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
NAME=libftprintf.a
|
||||||
|
CC=cc
|
||||||
|
CFLAGS=-Wall -Wextra -Werror
|
||||||
|
RM=rm -f
|
||||||
|
LIBFT = ./libft/ft_strdup.c ./libft/ft_strlcpy.c ./libft/ft_strlen.c
|
||||||
|
SRC = ./src/ft_printf.c ./src/ft_put.c
|
||||||
|
OBJ = $(SRC:.c=.o)
|
||||||
|
OBJLibft = $(LIBFT:.c=.o)
|
||||||
|
|
||||||
|
$(NAME): $(OBJ) $(OBJLibft)
|
||||||
|
ar rc $(NAME) $(OBJ) $(OBJLibft)
|
||||||
|
ranlib $(NAME)
|
||||||
|
|
||||||
|
%.o: %.c
|
||||||
|
$(CC) -I. -o $@ -c $? $(CFLAGS)
|
||||||
|
|
||||||
|
all: $(NAME)
|
||||||
|
|
||||||
|
bonus: $(OBJ) $(OBJLibft)
|
||||||
|
ar rc $(NAME) $(OBJ) $(OBJLibft)
|
||||||
|
ranlib $(NAME)
|
||||||
|
|
||||||
|
|
||||||
|
dev: all bonus clean
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(RM) $(OBJ) $(OBJLibft)
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
$(RM) $(NAME)
|
||||||
|
|
||||||
|
re: fclean all bonus
|
||||||
|
|
||||||
|
.PHONY: clean clean
|
||||||
26
libft/ft_strdup.c
Normal file
26
libft/ft_strdup.c
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strdup.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/11/07 16:53:59 by rparodi #+# #+# */
|
||||||
|
/* Updated: 2023/11/16 15:12:05 by rparodi ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../src/ft_printf.h"
|
||||||
|
|
||||||
|
char *ft_strdup(const char *s)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
char *to_return;
|
||||||
|
|
||||||
|
len = ft_strlen(s) + 1;
|
||||||
|
to_return = (char *)malloc(sizeof(char) * len);
|
||||||
|
if (!to_return)
|
||||||
|
return (NULL);
|
||||||
|
ft_strlcpy(to_return, s, len);
|
||||||
|
return (to_return);
|
||||||
|
}
|
||||||
BIN
libft/ft_strdup.o
Normal file
BIN
libft/ft_strdup.o
Normal file
Binary file not shown.
31
libft/ft_strlcpy.c
Normal file
31
libft/ft_strlcpy.c
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strlcpy.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/11/07 16:55:25 by rparodi #+# #+# */
|
||||||
|
/* Updated: 2023/11/16 15:12:05 by rparodi ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../src/ft_printf.h"
|
||||||
|
|
||||||
|
size_t ft_strlcpy(char *dst, const char *src, size_t size)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (src[i] && i + 1 < size)
|
||||||
|
{
|
||||||
|
dst[i] = src[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
if (size > 0)
|
||||||
|
{
|
||||||
|
dst[i] = '\0';
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (ft_strlen(src));
|
||||||
|
}
|
||||||
BIN
libft/ft_strlcpy.o
Normal file
BIN
libft/ft_strlcpy.o
Normal file
Binary file not shown.
23
libft/ft_strlen.c
Normal file
23
libft/ft_strlen.c
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_strlen.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/11/07 16:56:24 by rparodi #+# #+# */
|
||||||
|
/* Updated: 2023/11/16 15:11:46 by rparodi ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../src/ft_printf.h"
|
||||||
|
|
||||||
|
size_t ft_strlen(const char *s)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (s[i] != '\0')
|
||||||
|
i++;
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
BIN
libft/ft_strlen.o
Normal file
BIN
libft/ft_strlen.o
Normal file
Binary file not shown.
66
src/ft_printf.c
Normal file
66
src/ft_printf.c
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_printf.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/11/14 17:27:44 by rparodi #+# #+# */
|
||||||
|
/* Updated: 2023/11/16 12:14:01 by rparodi ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_printf.h"
|
||||||
|
|
||||||
|
int ft_check_args(char c, va_list args, int *ret_value)
|
||||||
|
{
|
||||||
|
if (c == 'c')
|
||||||
|
ft_putchar((char)va_arg(args, int), ret_value);
|
||||||
|
else if (c == 's')
|
||||||
|
ft_putstr((char *)va_arg(args, char *), ret_value);
|
||||||
|
else if (c == 'i' || c == 'd')
|
||||||
|
ft_putnbr((int)va_arg(args, int), ret_value);
|
||||||
|
else if (c == '%')
|
||||||
|
ft_putchar('%', ret_value);
|
||||||
|
else if (c == 'u')
|
||||||
|
ft_putnbr_unsigned((unsigned int)va_arg(args, unsigned int), ret_value);
|
||||||
|
else if (c == 'x')
|
||||||
|
ft_putnbr_base((unsigned int)va_arg(args, unsigned int), \
|
||||||
|
"0123456789abcdef", ret_value, c);
|
||||||
|
else if (c == 'X')
|
||||||
|
ft_putnbr_base((unsigned int)va_arg(args, unsigned int), \
|
||||||
|
"0123456789ABCDEF", ret_value, c);
|
||||||
|
else if (c == 'p')
|
||||||
|
ft_putnbr_base((unsigned long long)va_arg(args, unsigned long long), \
|
||||||
|
"0123456789abcdef", ret_value, c);
|
||||||
|
va_end(args);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_printf(const char *s, ...)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
int nb_flag;
|
||||||
|
va_list args;
|
||||||
|
char *str;
|
||||||
|
int ret_value;
|
||||||
|
|
||||||
|
ret_value = 0;
|
||||||
|
str = ft_strdup(s);
|
||||||
|
va_start(args, s);
|
||||||
|
i = 0;
|
||||||
|
nb_flag = 0;
|
||||||
|
while (str[i])
|
||||||
|
{
|
||||||
|
if (str[i] == '%')
|
||||||
|
{
|
||||||
|
nb_flag += ft_check_args(str[i + 1], args, &ret_value);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
ft_putchar(str[i], &ret_value);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
free(str);
|
||||||
|
return (ret_value);
|
||||||
|
}
|
||||||
35
src/ft_printf.h
Normal file
35
src/ft_printf.h
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_printf.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/11/14 15:59:10 by rparodi #+# #+# */
|
||||||
|
/* Updated: 2023/11/16 15:10:41 by rparodi ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef FT_PRINTF_H
|
||||||
|
# define FT_PRINTF_H
|
||||||
|
# include <stdlib.h>
|
||||||
|
# include <stdarg.h>
|
||||||
|
# include <unistd.h>
|
||||||
|
|
||||||
|
# define UINT_MAX 4294967295
|
||||||
|
# define INT_MAX 2147483647
|
||||||
|
# define INT_MIN -2147483648
|
||||||
|
|
||||||
|
int ft_printf(const char *str, ...);
|
||||||
|
int ft_check_args(char c, va_list args, int *ret_value);
|
||||||
|
void ft_putnbr_unsigned(unsigned int nb, int *ret_value);
|
||||||
|
void ft_putnbr(int nb, int *ret_value);
|
||||||
|
void ft_putchar(char c, int *ret_value);
|
||||||
|
void ft_putstr(char *str, int *ret_value);
|
||||||
|
void ft_putnbr_base(\
|
||||||
|
unsigned long long nbr, char *base, int *ret_value, char c);
|
||||||
|
size_t ft_strlcpy(char *dst, const char *src, size_t size);
|
||||||
|
size_t ft_strlen(const char *s);
|
||||||
|
char *ft_strdup(const char *s);
|
||||||
|
|
||||||
|
#endif
|
||||||
BIN
src/ft_printf.o
Normal file
BIN
src/ft_printf.o
Normal file
Binary file not shown.
82
src/ft_put.c
Normal file
82
src/ft_put.c
Normal file
|
|
@ -0,0 +1,82 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_put.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/11/16 12:13:14 by rparodi #+# #+# */
|
||||||
|
/* Updated: 2023/11/16 12:14:35 by rparodi ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_printf.h"
|
||||||
|
|
||||||
|
void ft_putchar(char c, int *ret_value)
|
||||||
|
{
|
||||||
|
write(1, &c, 1);
|
||||||
|
(*ret_value)++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_putnbr(int nb, int *ret_value)
|
||||||
|
{
|
||||||
|
if (nb < 0)
|
||||||
|
{
|
||||||
|
if (nb == INT_MIN)
|
||||||
|
{
|
||||||
|
write(1, "-2147483648", 11);
|
||||||
|
*ret_value += 11;
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
nb = -nb;
|
||||||
|
ft_putchar('-', ret_value);
|
||||||
|
}
|
||||||
|
if (nb >= 10)
|
||||||
|
{
|
||||||
|
ft_putnbr(nb / 10, ret_value);
|
||||||
|
nb = nb % 10;
|
||||||
|
}
|
||||||
|
if (nb < 10)
|
||||||
|
ft_putchar(nb + 48, ret_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_putnbr_base(\
|
||||||
|
unsigned long long nbr, char *base, int *ret_value, char c)
|
||||||
|
{
|
||||||
|
if (c == 'p')
|
||||||
|
{
|
||||||
|
if (nbr != 0)
|
||||||
|
ft_putstr("0x", ret_value);
|
||||||
|
if (nbr == 0)
|
||||||
|
{
|
||||||
|
ft_putstr("(nil)", ret_value);
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
c++;
|
||||||
|
}
|
||||||
|
if (c != 'p')
|
||||||
|
{
|
||||||
|
if (nbr >= 16)
|
||||||
|
ft_putnbr_base(nbr / 16, base, ret_value, c);
|
||||||
|
ft_putchar(base[nbr % 16], ret_value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_putnbr_unsigned(unsigned int nb, int *ret_value)
|
||||||
|
{
|
||||||
|
if (nb >= 10)
|
||||||
|
{
|
||||||
|
ft_putnbr_unsigned(nb / 10, ret_value);
|
||||||
|
nb = nb % 10;
|
||||||
|
}
|
||||||
|
if (nb < 10)
|
||||||
|
ft_putchar(nb + 48, ret_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_putstr(char *str, int *ret_value)
|
||||||
|
{
|
||||||
|
if (!str)
|
||||||
|
*ret_value += write(1, "(null)", 6);
|
||||||
|
else
|
||||||
|
*ret_value += write(1, str, ft_strlen(str));
|
||||||
|
}
|
||||||
BIN
src/ft_put.o
Normal file
BIN
src/ft_put.o
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue