done
This commit is contained in:
commit
b8258db5b6
37
Makefile
Normal file
37
Makefile
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
# **************************************************************************** #
|
||||||
|
# #
|
||||||
|
# ::: :::::::: #
|
||||||
|
# Makefile :+: :+: :+: #
|
||||||
|
# +:+ +:+ +:+ #
|
||||||
|
# By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ #
|
||||||
|
# +#+#+#+#+#+ +#+ #
|
||||||
|
# Created: 2021/11/13 13:06:47 by apommier #+# #+# #
|
||||||
|
# Updated: 2021/11/18 11:12:59 by apommier ### ########.fr #
|
||||||
|
# #
|
||||||
|
# **************************************************************************** #
|
||||||
|
|
||||||
|
NAME = libftprintf.a
|
||||||
|
SRCS = ft_printf.c \
|
||||||
|
utils.c
|
||||||
|
OBJS = ${SRCS:.c=.o}
|
||||||
|
CFLAGS = -Wall -Wextra -Werror
|
||||||
|
RM = rm -rf
|
||||||
|
|
||||||
|
.c.o:
|
||||||
|
gcc ${CFLAGS} -c $< -o ${<:.c=.o}
|
||||||
|
|
||||||
|
${NAME}:${OBJS}
|
||||||
|
ar -rcs ${NAME} ${OBJS}
|
||||||
|
|
||||||
|
all: ${NAME}
|
||||||
|
|
||||||
|
clean:
|
||||||
|
${RM} ${OBJS}
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
${RM} ${NAME}
|
||||||
|
|
||||||
|
re: fclean all
|
||||||
|
|
||||||
|
.PHONY: all clean fclean re bonus
|
||||||
|
|
||||||
91
ft_printf.c
Normal file
91
ft_printf.c
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_printf.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/11/10 22:22:43 by apommier #+# #+# */
|
||||||
|
/* Updated: 2021/11/17 17:05:48 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_printf.h"
|
||||||
|
|
||||||
|
int set_type(char type, va_list *ap)
|
||||||
|
{
|
||||||
|
int printed;
|
||||||
|
|
||||||
|
printed = 0;
|
||||||
|
if (type == 'c')
|
||||||
|
printed += ft_putchar_fd(va_arg(*ap, int), 1);
|
||||||
|
else if (type == 's')
|
||||||
|
printed += ft_putstr_fd(va_arg(*ap, char *), 1);
|
||||||
|
else if (type == 'd' || type == 'i' || type == 'u')
|
||||||
|
printed += ft_putnbr_fd(va_arg(*ap, long), type, &printed);
|
||||||
|
else if (type == 'x' || type == 'X')
|
||||||
|
printed += ft_putnbr_hex((long)va_arg(*ap, long), type, &printed);
|
||||||
|
else if (type == 'p')
|
||||||
|
{
|
||||||
|
ft_putstr_fd("0x", 1);
|
||||||
|
printed += ft_putpointer((long)va_arg(*ap, void *), type, &printed) + 2;
|
||||||
|
}
|
||||||
|
else if (type == '%')
|
||||||
|
printed += ft_putchar_fd('%', 1);
|
||||||
|
return (printed);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_putpointer(unsigned long nbr, char type, int *i)
|
||||||
|
{
|
||||||
|
if (nbr >= 16)
|
||||||
|
ft_putpointer(nbr / 16, type, i);
|
||||||
|
if ((nbr % 16) < 10)
|
||||||
|
ft_putchar_fd(nbr % 16 + '0', 1);
|
||||||
|
else if (type == 'x' || type == 'p')
|
||||||
|
ft_putchar_fd(nbr % 16 - 10 + 'a', 1);
|
||||||
|
else if (type == 'X')
|
||||||
|
{
|
||||||
|
ft_putchar_fd(nbr % 16 - 10 + 'A', 1);
|
||||||
|
}
|
||||||
|
(*i)++;
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int next_char(const char *format, int i)
|
||||||
|
{
|
||||||
|
int printed;
|
||||||
|
|
||||||
|
printed = 0;
|
||||||
|
while (format[i] != '\0' && format[i] != '%')
|
||||||
|
{
|
||||||
|
write(1, &format[i], 1);
|
||||||
|
i++;
|
||||||
|
printed ++;
|
||||||
|
}
|
||||||
|
return (printed);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_printf(const char *format, ...)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
int printed;
|
||||||
|
va_list ap;
|
||||||
|
|
||||||
|
printed = 0;
|
||||||
|
i = 0;
|
||||||
|
va_start(ap, format);
|
||||||
|
while (format[i] > '\0')
|
||||||
|
{
|
||||||
|
j = next_char(format, i);
|
||||||
|
printed += j;
|
||||||
|
i += j;
|
||||||
|
if (format[i] == '%')
|
||||||
|
{
|
||||||
|
printed += set_type(format[i + 1], &ap);
|
||||||
|
i += 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
va_end(ap);
|
||||||
|
return (printed);
|
||||||
|
}
|
||||||
29
ft_printf.h
Normal file
29
ft_printf.h
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_printf.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/11/10 22:23:26 by apommier #+# #+# */
|
||||||
|
/* Updated: 2021/11/13 13:10:11 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef FT_PRINTF_H
|
||||||
|
# define FT_PRINTF_H
|
||||||
|
|
||||||
|
# include <unistd.h>
|
||||||
|
# include <stdarg.h>
|
||||||
|
|
||||||
|
int ft_putstr_fd(char *s, int fd);
|
||||||
|
int ft_putnbr_fd(int n, char type, int *i);
|
||||||
|
int ft_putnbr_hex(unsigned long n, char type, int *i);
|
||||||
|
int ft_putpointer(unsigned long nbr, char type, int *i);
|
||||||
|
int ft_putchar_fd(char c, int fd);
|
||||||
|
int ft_printf(const char *input, ...);
|
||||||
|
int next_char(const char *input, int i);
|
||||||
|
int set_type(char type, va_list *ap);
|
||||||
|
int ft_strlen(const char *s);
|
||||||
|
|
||||||
|
#endif
|
||||||
90
utils.c
Normal file
90
utils.c
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <alexpomms@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/11/11 07:40:39 by apommier #+# #+# */
|
||||||
|
/* Updated: 2021/11/21 16:53:20 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_printf.h"
|
||||||
|
|
||||||
|
int ft_putchar_fd(char c, int fd)
|
||||||
|
{
|
||||||
|
write(fd, &c, 1);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_strlen(const char *s)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (s == 0)
|
||||||
|
ft_putstr_fd("(null)", 1);
|
||||||
|
while (s && s[i] != 0)
|
||||||
|
i++;
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_putstr_fd(char *s, int fd)
|
||||||
|
{
|
||||||
|
if (s == 0)
|
||||||
|
{
|
||||||
|
write(1, "(null)", 6);
|
||||||
|
return (6);
|
||||||
|
}
|
||||||
|
write(fd, s, ft_strlen(s));
|
||||||
|
return (ft_strlen(s));
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_putnbr_fd(int n, char type, int *i)
|
||||||
|
{
|
||||||
|
long nbr;
|
||||||
|
unsigned int unbr;
|
||||||
|
|
||||||
|
nbr = n;
|
||||||
|
unbr = n;
|
||||||
|
if (nbr < 0 && type != 'u')
|
||||||
|
{
|
||||||
|
ft_putchar_fd('-', 1);
|
||||||
|
nbr *= -1;
|
||||||
|
(*i)++;
|
||||||
|
}
|
||||||
|
if (type != 'u')
|
||||||
|
{
|
||||||
|
if (nbr >= 10)
|
||||||
|
ft_putnbr_fd(nbr / 10, type, i);
|
||||||
|
ft_putchar_fd(nbr % 10 + '0', 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (unbr >= 10)
|
||||||
|
ft_putnbr_fd(unbr / 10, type, i);
|
||||||
|
ft_putchar_fd(unbr % 10 + '0', 1);
|
||||||
|
}
|
||||||
|
(*i)++;
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_putnbr_hex(unsigned long n, char type, int *i)
|
||||||
|
{
|
||||||
|
unsigned int nbr;
|
||||||
|
|
||||||
|
nbr = n;
|
||||||
|
if (nbr >= 16)
|
||||||
|
ft_putnbr_hex(nbr / 16, type, i);
|
||||||
|
if ((nbr % 16) < 10)
|
||||||
|
ft_putchar_fd(nbr % 16 + '0', 1);
|
||||||
|
else if (type == 'x' || type == 'p')
|
||||||
|
ft_putchar_fd(nbr % 16 - 10 + 'a', 1);
|
||||||
|
else if (type == 'X')
|
||||||
|
{
|
||||||
|
ft_putchar_fd(nbr % 16 - 10 + 'A', 1);
|
||||||
|
}
|
||||||
|
(*i)++;
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user