]> git.dkaiser.de - 42/ft_printf.git/commitdiff
Add all functions but %p
authorDominik Kaiser <dominik@dominik-XPS.fritz.box>
Wed, 13 Mar 2024 15:05:39 +0000 (16:05 +0100)
committerDominik Kaiser <dominik@dominik-XPS.fritz.box>
Wed, 13 Mar 2024 15:05:39 +0000 (16:05 +0100)
ft_printf.h
src/ft_printf.c
src/ft_printhex.c [new file with mode: 0644]
src/ft_printnbr.c [new file with mode: 0644]

index a148f7339becd6fb9dd5cf0edbacb553dbd017a6..2a3fbb981b138f8d849148def87a373f3a29d4ba 100644 (file)
@@ -6,13 +6,18 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/12 18:19:47 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/03/12 18:20:41 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/03/13 16:01:09 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #ifndef FT_PRINTF_H
 # define FT_PRINTF_H
 
-int    ft_printf(char *fmt, ...);
+#include "libft/libft.h"
+#include <stdarg.h>
 
+int    ft_printf(char *fmt, ...);
+int ft_printnbr(int nbr);
+int ft_printunbr(unsigned int nbr);
+int ft_printhex(unsigned int nbr, char fmt);
 #endif // FT_PRINTF_H
index fe152f66918229d1182b20c5917dc0e2eb1b876c..f6984c78459db61cfa65b0e269b46872b68f16db 100644 (file)
@@ -6,13 +6,69 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/12 18:18:59 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/03/12 18:20:15 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/03/13 16:02:04 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "../ft_printf.h"
+#include <stdarg.h>
+
+static int print_char(char c)
+{
+    ft_putchar_fd(c, 1);
+    return 1;
+}
+
+static int print_str(char *str)
+{
+    if (str)
+    {
+        ft_putstr_fd(str, 1);
+        return ft_strlen(str);
+    }
+    else
+    {
+        ft_putstr_fd("(null)", 1);
+        return 6;
+    }
+}
+
+static int print_conv(va_list args, char c)
+{
+    if (c == 'c')
+        return print_char(va_arg(args, int));
+    if (c == 's')
+        return print_str(va_arg(args, char*));
+    if (c == 'p')
+        ;
+    if (c == 'd' || c == 'i')
+        return ft_printnbr(va_arg(args, int));
+    if (c == 'u')
+        return ft_printunbr(va_arg(args, unsigned int));
+    if (c == 'x' || c == 'X')
+        return ft_printhex(va_arg(args, unsigned int), c);
+    if (c == '%')
+        return print_char('%');
+    return -2147483648;
+}
 
 int ft_printf(char *fmt, ...)
 {
+    int result;
+    va_list args;
 
+   result = 0;
+   va_start(args, fmt);
+   while(*fmt)
+   {
+       if (*fmt == '%')
+           result += print_conv(args, *(++fmt));
+       else
+           result++;
+       fmt++;
+       if (result < 0)
+           return -1;
+   }
+   va_end(args);
+   return (result);
 }
diff --git a/src/ft_printhex.c b/src/ft_printhex.c
new file mode 100644 (file)
index 0000000..e96b322
--- /dev/null
@@ -0,0 +1,47 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_printhex.c                                      :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/13 15:50:35 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/13 16:00:38 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "../ft_printf.h"
+
+static int get_len(unsigned int n)
+{
+    int len;
+
+    len = 0;
+    if (n == 0)
+        return 1;
+    while (n)
+    {
+        len++;
+        n /= 16;
+    }
+    return len;
+}
+
+static void printhex_rec(unsigned int nbr, char fmt)
+{
+    char c;
+
+    if (nbr % 16 < 10)
+        c = '0' + (nbr % 16);
+    else
+        c = fmt - 35 + (nbr % 16);
+    if (nbr > 15)
+        printhex_rec(nbr, fmt);
+    write(1, &c, 1);
+}
+
+int ft_printhex(unsigned int nbr, char fmt)
+{
+    printhex_rec(nbr, fmt);
+    return get_len(nbr);
+}
diff --git a/src/ft_printnbr.c b/src/ft_printnbr.c
new file mode 100644 (file)
index 0000000..cd20025
--- /dev/null
@@ -0,0 +1,53 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_printnbr.c                                      :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/13 15:18:40 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/13 15:49:00 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "../ft_printf.h"
+#include <unistd.h>
+
+static int get_len(long n)
+{
+    int len;
+
+    len = 0;
+    if (n == 0)
+        return 1;
+    if (n < 0)
+        len++;
+    while (n)
+    {
+        len++;
+        n /= 10;
+    }
+    return len;
+}
+
+int ft_printnbr(int nbr)
+{
+    ft_putnbr_fd(nbr, 1);
+    return get_len(nbr);
+}
+
+static void    printunbr_rec(unsigned int n)
+{
+       char    c;
+
+       c = '0' + n % 10;
+       if (n > 9)
+               printunbr_rec(n / 10);
+       write(1, &c, 1);
+}
+
+int ft_printunbr(unsigned int nbr)
+{
+    printunbr_rec(nbr);
+    return get_len(nbr);
+}