From 38d24f4915c1228f36147aee50d6dd748e1acc6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=ABy=20Noort?= Date: Sun, 11 May 2025 19:22:47 +0200 Subject: [PATCH 1/8] feat: added ft_putaddr --- srcs/ft_putaddr.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 srcs/ft_putaddr.c diff --git a/srcs/ft_putaddr.c b/srcs/ft_putaddr.c new file mode 100644 index 0000000..4c66ce0 --- /dev/null +++ b/srcs/ft_putaddr.c @@ -0,0 +1,12 @@ +#include "libft.h" + +int ft_putaddr(unsigned long n) +{ + int count = 0; + char *digits = "0123456789abcdef"; + + if (n >= 16) + count += ft_putaddr(n / 16); + count += ft_putchar(digits[n % 16]); + return count; +} \ No newline at end of file From 2bee25020c082c0a82c259ae6b02e56767595e1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=ABy=20Noort?= Date: Sun, 11 May 2025 19:22:53 +0200 Subject: [PATCH 2/8] feat: added ft_putchar --- srcs/ft_putchar.c | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 srcs/ft_putchar.c diff --git a/srcs/ft_putchar.c b/srcs/ft_putchar.c new file mode 100644 index 0000000..c1b9f17 --- /dev/null +++ b/srcs/ft_putchar.c @@ -0,0 +1,8 @@ +#include "libft.h" +#include + +int ft_putchar(char c) +{ + write(1, &c, 1); + return 1; +} From 9bf197acbd603df877c54d1b33b18dcb224e39ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=ABy=20Noort?= Date: Sun, 11 May 2025 19:23:01 +0200 Subject: [PATCH 3/8] feat: added ft_puthex --- srcs/ft_puthex.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 srcs/ft_puthex.c diff --git a/srcs/ft_puthex.c b/srcs/ft_puthex.c new file mode 100644 index 0000000..6958c8c --- /dev/null +++ b/srcs/ft_puthex.c @@ -0,0 +1,12 @@ +#include "libft.h" // for ft_putchar + +int ft_puthex(unsigned int n, int uppercase) +{ + int count = 0; + char *digits = uppercase ? "0123456789ABCDEF" : "0123456789abcdef"; + + if (n >= 16) + count += ft_puthex(n / 16, uppercase); + count += ft_putchar(digits[n % 16]); + return count; +} \ No newline at end of file From 2a9b0421225270a5172ac6c241f5e161799d899e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=ABy=20Noort?= Date: Sun, 11 May 2025 19:23:16 +0200 Subject: [PATCH 4/8] feat: added ft_putnbr and ft-putunbr --- srcs/ft_putnbr.c | 21 +++++++++++++++++++++ srcs/ft_putunbr.c | 14 ++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 srcs/ft_putnbr.c create mode 100644 srcs/ft_putunbr.c diff --git a/srcs/ft_putnbr.c b/srcs/ft_putnbr.c new file mode 100644 index 0000000..2710ba1 --- /dev/null +++ b/srcs/ft_putnbr.c @@ -0,0 +1,21 @@ +#include "libft.h" +#include + +int ft_putnbr(int n) +{ + int count = 0; + char c; + + if (n == -2147483648) + return write(1, "-2147483648", 11); + if (n < 0) + { + count += ft_putchar('-'); + n = -n; + } + if (n >= 10) + count += ft_putnbr(n / 10); + c = '0' + (n % 10); + count += ft_putchar(c); + return count; +} diff --git a/srcs/ft_putunbr.c b/srcs/ft_putunbr.c new file mode 100644 index 0000000..40c9dea --- /dev/null +++ b/srcs/ft_putunbr.c @@ -0,0 +1,14 @@ +#include "libft.h" +#include + +int ft_putunbr(unsigned int n) +{ + int count = 0; + char c; + + if (n >= 10) + count += ft_putunbr(n / 10); + c = '0' + (n % 10); + count += ft_putchar(c); + return count; +} From 5276d36f0c02feae50225c044b965c244f90e742 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=ABy=20Noort?= Date: Sun, 11 May 2025 19:23:23 +0200 Subject: [PATCH 5/8] feat: added ft_putstr --- srcs/ft_putstr.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 srcs/ft_putstr.c diff --git a/srcs/ft_putstr.c b/srcs/ft_putstr.c new file mode 100644 index 0000000..e5b6bd1 --- /dev/null +++ b/srcs/ft_putstr.c @@ -0,0 +1,12 @@ +#include "libft.h" +#include + +int ft_putstr(const char *s) +{ + int len = 0; + if (!s) + return write(1, "(null)", 6); + while (s[len]) + write(1, &s[len++], 1); + return len; +} From 6910e9e96f5e657eea31cfcaf064464720b9b3d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=ABy=20Noort?= Date: Sun, 11 May 2025 19:23:53 +0200 Subject: [PATCH 6/8] feat: added ft_printf --- srcs/ft_printf.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 srcs/ft_printf.c diff --git a/srcs/ft_printf.c b/srcs/ft_printf.c new file mode 100644 index 0000000..5b065c3 --- /dev/null +++ b/srcs/ft_printf.c @@ -0,0 +1,45 @@ +#include "libft.h" +#include +#include + +static int ft_print_format(const char *fmt, va_list args) +{ + if (*fmt == 'c') + return (ft_putchar(va_arg(args, int))); + else if (*fmt == 's') + return (ft_putstr(va_arg(args, char *))); + else if (*fmt == 'p') + return (ft_putaddr((uintptr_t)va_arg(args, void *))); + else if (*fmt == 'd' || *fmt == 'i') + return (ft_putnbr(va_arg(args, int))); + else if (*fmt == 'u') + return (ft_putunbr(va_arg(args, unsigned int))); + else if (*fmt == 'x') + return (ft_puthex(va_arg(args, unsigned int), 0)); + else if (*fmt == 'X') + return (ft_puthex(va_arg(args, unsigned int), 1)); + else if (*fmt == '%') + return (ft_putchar('%')); + return (0); +} + +int ft_printf(const char *format, ...) +{ + va_list args; + int i = 0; + int count = 0; + + va_start(args, format); + while (format[i]) + { + if (format[i] == '%' && format[i + 1]) + { + count += ft_print_format(&format[++i], args); + } + else + count += ft_putchar(format[i]); + i++; + } + va_end(args); + return (count); +} From 8c933384bbb422ed70c05d9f5191052bc089536e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=ABy=20Noort?= Date: Sun, 11 May 2025 19:24:07 +0200 Subject: [PATCH 7/8] chore: added new functions to libft.h --- hdrs/libft.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/hdrs/libft.h b/hdrs/libft.h index 8b65d36..696caf1 100644 --- a/hdrs/libft.h +++ b/hdrs/libft.h @@ -4,6 +4,8 @@ # include int ft_strcmp(const char *s1, const char *s2); +size_t ft_strlen(const char *s); +int ft_putstr(const char *s); int ft_islower(int c); int ft_isupper(int c); int ft_tolower(int c); @@ -18,5 +20,12 @@ void *ft_memcpy(void *dst, const void *src, size_t n); void *ft_memcpy32(void *dst, const void *src, size_t n); void *ft_memcpy64(void *dst, const void *src, size_t n); int ft_atoi(const char *str); +int ft_putchar(char c); +int ft_putnbr(int n); +int ft_putunbr(unsigned int n); +int ft_puthex(unsigned int n, int uppercase); +void ft_putptr(void *ptr); +int ft_printf(const char *format, ...); +int ft_putaddr(unsigned long n); #endif From a7b7cda49626594feb48c96ecf70573e06b8aa2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=ABy=20Noort?= Date: Sun, 11 May 2025 19:24:24 +0200 Subject: [PATCH 8/8] feat: added string struct in libft.h --- hdrs/libft.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/hdrs/libft.h b/hdrs/libft.h index 696caf1..c9f6803 100644 --- a/hdrs/libft.h +++ b/hdrs/libft.h @@ -3,6 +3,12 @@ # include +typedef struct s_string +{ + size_t len; + char *data; +} t_string; + int ft_strcmp(const char *s1, const char *s2); size_t ft_strlen(const char *s); int ft_putstr(const char *s);