From 603303a21bd8485961a0159f0aeefa629604cb4c Mon Sep 17 00:00:00 2001 From: asus Date: Thu, 11 Jan 2024 10:36:13 +0100 Subject: [PATCH] - re-added ft_putchar/putnbr/putstr without fd - added const for pointers in these functions --- Makefile | 6 +++++- includes/libft.h | 20 +++++++++++++---- srcs/ft_putchar.c | 7 ++++++ srcs/ft_putnbr.c | 17 +++++++++++++++ srcs/ft_putnbrbase.c | 4 ++-- srcs/ft_putnbrbase_fd.c | 48 +++++++++++++++++++++++++++++++++++++++++ srcs/ft_putstr.c | 8 +++++++ srcs/ft_putstr_fd.c | 2 +- 8 files changed, 104 insertions(+), 8 deletions(-) create mode 100644 srcs/ft_putchar.c create mode 100644 srcs/ft_putnbr.c create mode 100644 srcs/ft_putnbrbase_fd.c create mode 100644 srcs/ft_putstr.c diff --git a/Makefile b/Makefile index 2181f97..32c2809 100644 --- a/Makefile +++ b/Makefile @@ -69,10 +69,14 @@ SRCS = ft_memset.c \ ft_itoa.c \ ft_utoa.c \ \ + ft_putchar.c \ + ft_putstr.c \ + ft_putnbr.c \ + ft_putnbrbase.c \ ft_putchar_fd.c \ ft_putstr_fd.c \ ft_putnbr_fd.c \ - ft_putnbrbase.c \ + ft_putnbrbase_fd.c \ \ ft_lstcreate.c \ ft_lstpush_back.c \ diff --git a/includes/libft.h b/includes/libft.h index 2425bb0..7b42efc 100644 --- a/includes/libft.h +++ b/includes/libft.h @@ -53,16 +53,28 @@ char *ft_itoa(long int n); char *ft_utoa(unsigned long int n); char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +void ft_putchar(char c); +void ft_putstr(const char *s); +void ft_putnbr(int n); +void ft_putnbrbase(int nbr, const char *base); void ft_putchar_fd(char c, int fd); -void ft_putstr_fd(char *s, int fd); +void ft_putstr_fd(const char *s, int fd); void ft_putnbr_fd(int n, int fd); -void ft_putnbrbase(int nbr, char *base); +void ft_putnbrbase_fd(int nbr, const char *base, int fd); /* void ft_putchar(char c); +void ft_putstr(char *s); +void ft_putnbr(int n); +void ft_putnbrbase(int nbr, char *base); +void ft_putchar_fd(char c, int fd); +void ft_putstr_fd(char *s, int fd); +void ft_putnbr_fd(int n, int fd); +void ft_putnbrbase_fd(int nbr, char *base, int fd); +*/ + +/* void ft_putendl(char const *str); -void ft_putnbr(int nbr); -void ft_putstr(char const *str); void ft_putendl_fd(char *s, int fd); */ diff --git a/srcs/ft_putchar.c b/srcs/ft_putchar.c new file mode 100644 index 0000000..1911b69 --- /dev/null +++ b/srcs/ft_putchar.c @@ -0,0 +1,7 @@ +#include "libft.h" + +void ft_putchar(char c) +{ + write(1, &c, 1); +} + diff --git a/srcs/ft_putnbr.c b/srcs/ft_putnbr.c new file mode 100644 index 0000000..413d0da --- /dev/null +++ b/srcs/ft_putnbr.c @@ -0,0 +1,17 @@ +#include "libft.h" + +void ft_putnbr(int n) +{ + long l; + + l = n; + if (l < 0) + { + ft_putchar('-'); + l *= -1; + } + if (l >= 10) + ft_putnbr(l / 10); + ft_putchar((l % 10) + '0'); +} + diff --git a/srcs/ft_putnbrbase.c b/srcs/ft_putnbrbase.c index e98bac4..513f81c 100644 --- a/srcs/ft_putnbrbase.c +++ b/srcs/ft_putnbrbase.c @@ -12,7 +12,7 @@ #include "libft.h" -static int check(char *base) +static int check(const char *base) { int i; int j; @@ -36,7 +36,7 @@ static int check(char *base) return (0); } -void ft_putnbrbase(int nbr, char *base) +void ft_putnbrbase(int nbr, const char *base) { int i; long n; diff --git a/srcs/ft_putnbrbase_fd.c b/srcs/ft_putnbrbase_fd.c new file mode 100644 index 0000000..8f61da8 --- /dev/null +++ b/srcs/ft_putnbrbase_fd.c @@ -0,0 +1,48 @@ +#include "libft.h" + +static int check(const char *base) +{ + int i; + int j; + + i = 0; + while (base[i]) + { + j = i + 1; + while (base[j]) + { + if (base[i] == base[j]) + return (0); + j++; + } + if (base[i] == '-' || base[i] == '+') + return (0); + i++; + } + if (i >= 2) + return (1); + return (0); +} + +void ft_putnbrbase_fd(int nbr, const char *base, int fd) +{ + int i; + long n; + + i = 0; + n = nbr; + if (check(base)) + { + if (n < 0) + { + ft_putchar_fd('-', fd); + n = -n; + } + while (base[i]) + i++; + if (n >= i) + ft_putnbrbase_fd(n / i, base, fd); + ft_putchar_fd(base[n % i], fd); + } +} + diff --git a/srcs/ft_putstr.c b/srcs/ft_putstr.c new file mode 100644 index 0000000..d5ee66d --- /dev/null +++ b/srcs/ft_putstr.c @@ -0,0 +1,8 @@ +#include "libft.h" + +void ft_putstr(const char *s) +{ + while (s && *s) + ft_putchar(*s++); +} + diff --git a/srcs/ft_putstr_fd.c b/srcs/ft_putstr_fd.c index cf6ad12..4aca28c 100644 --- a/srcs/ft_putstr_fd.c +++ b/srcs/ft_putstr_fd.c @@ -16,7 +16,7 @@ #include "libft.h" -void ft_putstr_fd(char *s, int fd) +void ft_putstr_fd(const char *s, int fd) { while (s && *s) ft_putchar_fd(*s++, fd);