Archived
1
0

start counting

This commit is contained in:
Adam Joly
2023-11-20 11:56:11 +01:00
parent 8f7c476835
commit 4188dfeb84
8 changed files with 152 additions and 20 deletions

@ -6,7 +6,7 @@
# By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/11/17 12:35:27 by adjoly #+# #+# #
# Updated: 2023/11/17 19:59:14 by adjoly ### ########.fr #
# Updated: 2023/11/20 10:07:45 by adjoly ### ########.fr #
# #
# **************************************************************************** #
@ -14,15 +14,17 @@ NAME = libftprintf.a
CC = cc
SRCS = ft_printf.c\
SRCS = ft_printf.c \
ft_putchar.c \
ft_putnbr.c \
ft_putnbrbase.c \
ft_putstr.c \
OBJS = $(SRCS:.c=.o)
FLAGS = -Werror -Wall -Wextra
HEADER = libftprintf.h \
$(NAME): $(OBJS)
ar -rcs $(NAME) $(OBJS)

@ -6,32 +6,33 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/17 16:48:37 by adjoly #+# #+# */
/* Updated: 2023/11/18 20:56:29 by adjoly ### ########.fr */
/* Updated: 2023/11/20 11:53:52 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft/libft.h"
#include "libftprintf.h"
#include <stdarg.h>
void ft_putaddr(void *ptr)
{
ft_putstr((char *)ptr);
}
void ft_putnbrulong(int n)
void ft_putnbrulong(unsigned long n)
{
if (n < 10)
write(1, &(char){n + '0'}, 1);
else
{
ft_putnbr_fd(n / 10, 1);
ft_putnbrulong(n / 10);
write(1, &(char){n % 10 + '0'}, 1);
}
}
int ft_printconversion(char conversion, va_list args)
{
int count;
count = 0;
if (conversion == '%')
ft_putchar('%');
else if (conversion == 's')
@ -48,26 +49,29 @@ int ft_printconversion(char conversion, va_list args)
ft_putnbrbase(va_arg(args, int), "0123456789abcdef");
else if (conversion == 'X')
ft_putnbrbase(va_arg(args, int), "0123456789ABCDEF");
return (count);
}
int ft_printf(const char *format, ...)
{
int i;
int j;
va_list args;
int i;
int j;
va_list args;
int count;
va_start(args, format);
count = 0;
while (format[i])
{
if (format[i] == '%')
{
i++;
ft_printconversion(format[i], args);
count += ft_printconversion(format[i], args);
}
else
ft_putchar_fd(format[i], i);
ft_putchar(format[i]);
i++;
}
va_end(args);
return (ft_strlen(result));
return (count);
}

19
ft_putchar.c Normal file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/18 10:49:00 by adjoly #+# #+# */
/* Updated: 2023/11/20 10:04:41 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
int ft_putchar(char c)
{
write(1, &c, 1);
return (1);
}

37
ft_putnbr.c Normal file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/31 11:52:46 by adjoly #+# #+# */
/* Updated: 2023/11/20 10:40:57 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
void ft_putnbr_fd(int n, int fd)
{
unsigned int nbr;
int i;
if (n < 0)
{
write(fd, "-", 1);
nbr = -n;
}
else
nbr = n;
if (nbr < 10)
{
write(fd, &(char){nbr + '0'}, 1);
i++;
}
else
{
ft_putnbr_fd(nbr / 10, fd);
write(fd, &(char){nbr % 10 + '0'}, 1);
}
}

35
ft_putnbrbase_fd.c Normal file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbrbase_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/18 10:57:44 by adjoly #+# #+# */
/* Updated: 2023/11/18 11:10:32 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putnbrbase_fd(int n, char *base, int fd)
{
unsigned int nbr;
size_t base_len;
base_len = ft_strlen(base);
if (n < 0)
{
write(fd, "-", 1);
nbr = -n;
}
else
nbr = n;
if (nbr < base_len)
write(fd, &base[nbr % base_len], 1);
else
{
ft_putnbrbase_fd(nbr / base_len, base, fd);
write(fd, &base[nbr % base_len], 1);
}
}

31
ft_putstr.c Normal file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/31 11:45:55 by adjoly #+# #+# */
/* Updated: 2023/11/20 10:07:08 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
int ft_putstr(char *s)
{
int i;
i = 0;
if (s == NULL)
{
write(1, "(null)", 6);
return (6);
}
while (s[i])
{
write(1, &s[i], 1);
i++;
}
return (i);
}

1
libft

Submodule libft deleted from 6486eb4b3f

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/17 16:50:36 by adjoly #+# #+# */
/* Updated: 2023/11/18 18:51:53 by adjoly ### ########.fr */
/* Updated: 2023/11/20 10:04:12 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,11 +15,16 @@
# include <stdlib.h>
# include <stdarg.h>
# include "libft/libft.h"
# include <unistd.h>
int ft_printf(const char *format, ...);
int ft_printconversion(char conversion, va_list args);
void ft_putnbrulong(int n);
void ft_putnbrulong(unsigned long n);
void ft_putaddr(void *ptr);
int ft_putstr(char *s);
void ft_putnbrbase(int n, char *base);
int ft_putchar(char c);
void ft_putnbr(int n);
#endif