This commit is contained in:
Camille Chauvet 2022-10-10 19:33:47 +02:00
parent 780ac9ced3
commit af946dbf97
40 changed files with 286 additions and 137 deletions

1
42-printf-tester Submodule

@ -0,0 +1 @@
Subproject commit b6aeed792a811f305af27c8a140c77c14d3055fd

View File

@ -6,10 +6,25 @@
# By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/09/27 08:39:27 by cchauvet #+# #+# #
# Updated: 2022/10/06 16:37:11 by cchauvet ### ########.fr #
# Updated: 2022/10/10 14:34:40 by cchauvet ### ########.fr #
# #
# **************************************************************************** #
SRCS =
SRCS = ft_ctoa.c \
ft_itoa.c \
ft_ltoabase.c \
ft_printf.c \
ft_ptoa.c \
ft_putchar_fd.c \
ft_putendl_fd.c \
ft_putstr_fd.c \
ft_strchr.c \
ft_strdup.c \
ft_strjoin.c \
ft_strlen.c \
ft_ultoabase.c \
ft_vdprintf.c \
ft_vprintf.c \
ft_vsprintf.c
OBJS = ${SRCS:.c=.o}

BIN
a.out

Binary file not shown.

View File

@ -6,11 +6,11 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 21:42:53 by cchauvet #+# #+# */
/* Updated: 2022/10/07 00:49:07 by cchauvet ### ########.fr */
/* Updated: 2022/10/10 14:41:19 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
#include "printf.h"
char *ft_ctoa(int c)
{

BIN
ft_ctoa.o Normal file

Binary file not shown.

View File

@ -6,11 +6,11 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 13:49:45 by cchauvet #+# #+# */
/* Updated: 2022/10/06 21:25:27 by cchauvet ### ########.fr */
/* Updated: 2022/10/10 14:41:18 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
#include "printf.h"
static int ft_nb_digit(int n)
{

BIN
ft_itoa.o Normal file

Binary file not shown.

View File

@ -1,55 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoabase.c :+: :+: :+: */
/* ft_ltoabase.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 13:49:45 by cchauvet #+# #+# */
/* Updated: 2022/10/06 18:22:32 by cchauvet ### ########.fr */
/* Created: 2022/10/10 03:26:21 by cchauvet #+# #+# */
/* Updated: 2022/10/10 14:41:28 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
#include "printf.h"
static size_t ft_nb_digit(int n, size_t base_size)
{
int out;
out = 0;
while (n)
{
n /= base_size;
out++;
}
return (out);
}
char *ft_itoabase(int n, char *base)
char *ft_ltoabase(long int n, char *base)
{
char *mid_out;
char *out;
unsigned int nb[2];
unsigned long nb;
if (!n)
return (ft_ctoa(base[0]));
nb[0] = ft_nb_digit(n, ft_strlen(base));
if (base == NULL)
return (NULL);
if (n < 0)
{
nb[1] = -n;
nb[0]++;
nb = -n;
mid_out = ft_ultoabase(nb, base);
if (mid_out == NULL)
return (NULL);
out = ft_strjoin("-", mid_out);
free(mid_out);
}
else
nb[1] = n;
out = malloc(sizeof(char) * (nb[0] + 1));
if (out == NULL)
return (NULL);
out[nb[0]--] = 0;
if (n < 0)
out[0] = '-';
while (nb[1])
{
out[nb[0]--] = base[nb[1] % ft_strlen(base)];
nb[1] /= ft_strlen(base);
nb = n;
return (ft_ultoabase(nb, base));
}
return (out);
}

BIN
ft_ltoabase.o Normal file

Binary file not shown.

View File

@ -6,18 +6,19 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 22:01:28 by cchauvet #+# #+# */
/* Updated: 2022/10/07 01:43:13 by cchauvet ### ########.fr */
/* Updated: 2022/10/10 17:34:18 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
#include "printf.h"
int ft_printf(const char *s, ...)
{
va_list va;
int i;
va_start(va, s);
ft_vdprintf(1, s, va);
i = ft_vdprintf(1, s, va);
va_end(va);
return (ft_strlen(s));
return (i);
}

BIN
ft_printf.o Normal file

Binary file not shown.

1
ft_printf_tester Submodule

@ -0,0 +1 @@
Subproject commit a053a3500c9124a5c64c95cd8ff9e78a783932c4

View File

@ -6,11 +6,11 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 17:41:04 by cchauvet #+# #+# */
/* Updated: 2022/10/06 21:44:49 by cchauvet ### ########.fr */
/* Updated: 2022/10/10 15:54:00 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
#include "printf.h"
char *ft_ptoa(void *ptr)
{
@ -19,15 +19,19 @@ char *ft_ptoa(void *ptr)
char *out;
char *s;
if (ptr == NULL)
return (ft_strdup("(nil)"));
address = (unsigned long) ptr;
s = ft_itoabase(address, "0123456789ABCDEF");
out = malloc(22 * sizeof(char *));
out = "0x";
s = ft_ultoabase(address, "0123456789abcdef");
out = malloc((ft_strlen(s) + 2) * sizeof(char *));
out[ft_strlen(s) + 2] = 0;
out[0] = '0';
out[1] = 'x';
i = 0;
while (s[i] != 0)
{
out[i + 2] = s[i];
i++;
}
out = 0;
return (out);
}

BIN
ft_ptoa.o Normal file

Binary file not shown.

View File

@ -6,11 +6,11 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 22:23:35 by cchauvet #+# #+# */
/* Updated: 2022/09/29 22:24:53 by cchauvet ### ########.fr */
/* Updated: 2022/10/10 14:41:55 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
#include "printf.h"
void ft_putchar_fd(char c, int fd)
{

BIN
ft_putchar_fd.o Normal file

Binary file not shown.

View File

@ -6,11 +6,11 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 22:26:36 by cchauvet #+# #+# */
/* Updated: 2022/09/29 22:43:19 by cchauvet ### ########.fr */
/* Updated: 2022/10/10 14:42:06 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
#include "printf.h"
void ft_putendl_fd(char *s, int fd)
{

BIN
ft_putendl_fd.o Normal file

Binary file not shown.

View File

@ -6,11 +6,11 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 22:25:08 by cchauvet #+# #+# */
/* Updated: 2022/09/29 22:40:34 by cchauvet ### ########.fr */
/* Updated: 2022/10/10 14:42:15 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
#include "printf.h"
void ft_putstr_fd(char *s, int fd)
{

BIN
ft_putstr_fd.o Normal file

Binary file not shown.

View File

@ -6,11 +6,11 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 14:44:15 by cchauvet #+# #+# */
/* Updated: 2022/10/05 20:56:37 by cchauvet ### ########.fr */
/* Updated: 2022/10/10 14:42:21 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
#include "printf.h"
char *ft_strchr(const char *s, int c)
{

BIN
ft_strchr.o Normal file

Binary file not shown.

View File

@ -6,17 +6,19 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 16:00:49 by cchauvet #+# #+# */
/* Updated: 2022/10/06 16:53:02 by cchauvet ### ########.fr */
/* Updated: 2022/10/10 18:48:37 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
#include "printf.h"
char *ft_strdup(char *s)
char *ft_strdup(const char *s)
{
char *out;
size_t i;
if (s == NULL)
return (NULL);
out = malloc((ft_strlen(s) + 1) * sizeof(char));
if (out == NULL)
return (NULL);

BIN
ft_strdup.o Normal file

Binary file not shown.

38
ft_strjoin.c Normal file
View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 11:20:27 by cchauvet #+# #+# */
/* Updated: 2022/10/10 14:42:41 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "printf.h"
char *ft_strjoin(char *s1, char *s2)
{
size_t i;
size_t j;
char *out;
if (s1 == NULL || s2 == NULL)
return (NULL);
out = malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
i = 0;
while (s1[i] != 0)
{
out[i] = s1[i];
i++;
}
j = 0;
while (s2[j] != 0)
{
out[i + j] = s2[j];
j++;
}
out[i + j] = 0;
return (out);
}

BIN
ft_strjoin.o Normal file

Binary file not shown.

View File

@ -6,16 +6,18 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 17:39:05 by cchauvet #+# #+# */
/* Updated: 2022/10/06 17:40:41 by cchauvet ### ########.fr */
/* Updated: 2022/10/10 15:43:27 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
#include "printf.h"
size_t ft_strlen(const char *s)
{
size_t i;
if (s == NULL)
return (0);
i = 0;
while (s[i])
i++;

BIN
ft_strlen.o Normal file

Binary file not shown.

84
ft_ultoabase.c Normal file
View File

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ultoabase.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 13:49:45 by cchauvet #+# #+# */
/* Updated: 2022/10/10 14:45:46 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "printf.h"
static size_t ft_str_size(unsigned long n, size_t base_size)
{
size_t size;
size = 1;
if (n == 0)
return (2);
while (n != 0)
{
n = n / base_size;
size++;
}
return (size);
}
static int ft_isdup(char *str)
{
char c;
size_t i;
while (*str != 0)
{
c = *str;
i = 1;
while (str[i] != 0)
{
if (str[i] == c)
return (1);
i++;
}
str++;
}
return (0);
}
static size_t ft_base_size(char *base)
{
size_t len;
if (ft_isdup(base))
return (0);
len = ft_strlen(base);
if (len < 2)
return (0);
return (len);
}
char *ft_ultoabase(unsigned long n, char *base)
{
size_t base_size;
size_t str_size;
char *out;
if (base == NULL)
return (NULL);
base_size = ft_base_size(base);
if (base_size == 0)
return (NULL);
str_size = ft_str_size(n, base_size);
out = malloc(str_size * sizeof(char *));
if (out == NULL)
return (NULL);
out[--str_size] = 0;
while (0 < str_size)
{
out[--str_size] = base[n % base_size];
n = n / base_size;
}
return (out);
}

BIN
ft_ultoabase.o Normal file

Binary file not shown.

View File

@ -6,18 +6,21 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 16:39:25 by cchauvet #+# #+# */
/* Updated: 2022/10/07 01:38:18 by cchauvet ### ########.fr */
/* Updated: 2022/10/10 19:12:04 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
#include "printf.h"
char *ft_vdprintf(int fd, const char *format, va_list va)
int ft_vdprintf(int fd, const char *format, va_list va)
{
char *str;
int rv;
str = ft_strdup("");
str = ft_vsprintf(str, format, va);
rv = ft_strlen(str);
ft_putstr_fd(str, fd);
return (str);
free(str);
return (rv);
}

BIN
ft_vdprintf.o Normal file

Binary file not shown.

View File

@ -6,13 +6,13 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/07 01:05:23 by cchauvet #+# #+# */
/* Updated: 2022/10/07 01:43:58 by cchauvet ### ########.fr */
/* Updated: 2022/10/10 18:12:44 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
#include "printf.h"
int ft_vprintf(const char *format, va_list va)
{
return (ft_strlen(ft_vdprintf(1, format, va)));
return (ft_vdprintf(1, format, va));
}

BIN
ft_vprintf.o Normal file

Binary file not shown.

View File

@ -6,14 +6,53 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 10:44:36 by cchauvet #+# #+# */
/* Updated: 2022/10/07 02:26:50 by cchauvet ### ########.fr */
/* Updated: 2022/10/10 19:32:04 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
#include "printf.h"
static size_t ft_seglen(const char *str)
{
size_t i;
i = 0;
while ((str[i] != 0 && str[i] != '%') || (str[i] == '%' && i == 0))
i++;
return (i);
}
static char *ft_strfjoin(char *s1, char *s2)
{
size_t i;
size_t j;
char *out;
out = malloc((ft_strlen(s1) + ft_seglen(s2) + 1) * sizeof(char));
if (out == NULL)
return (NULL);
i = 0;
while (s1[i] != 0)
{
out[i] = s1[i];
i++;
}
free(s1);
j = 0;
while ((s2[j] != 0 && s2[j] != '%') || (s2[j] == '%' && j == 0))
{
out[i + j] = s2[j];
j++;
}
free(s2);
out[i + j] = 0;
return (out);
}
static char *ft_argtoa(int c, va_list args)
{
char *out;
if (c == 'c')
return (ft_ctoa(va_arg(args, int)));
if (c == 's')
@ -23,69 +62,44 @@ static char *ft_argtoa(int c, va_list args)
if (c == 'p')
return (ft_ptoa(va_arg(args, void *)));
if (c == 'x')
return (ft_itoabase(va_arg(args, long int), "0123456789abcdef"));
return (ft_ltoabase(va_arg(args, long int), "0123456789abcdef"));
if (c == 'X')
return (ft_itoabase(va_arg(args, long int), "0123456789ABCDEF"));
return (ft_ltoabase(va_arg(args, long int), "0123456789ABCDEF"));
if (c == 'u')
return (ft_ultoabase(va_arg(args, unsigned long int), "0123456789"));
if (c == '%')
return (ft_ctoa('%'));
return (NULL);
}
static size_t ft_seglen(const char *str)
{
size_t i;
i = 0;
while (str[i] != 0 && str[i] != '%')
i++;
return (i);
}
static char *ft_strfjoin(char *s1, const char *s2)
{
size_t i;
size_t j;
char *out;
if (s1 == NULL || s2 == NULL)
return (ft_strdup("%"));
out = ft_strdup("% ");
if (out == NULL)
return (NULL);
out = malloc((ft_seglen(s1) + ft_seglen(s2) + 1) * sizeof(char));
i = 0;
while (s1[i] != 0 && s2[i] != '%')
{
out[i] = s1[i];
i++;
}
free(s1);
j = 0;
while (s2[j] != 0 && s2[j] != '%')
{
out[i + j] = s2[j];
j++;
}
out[i + j] = 0;
out[1] = c;
return (out);
}
char *ft_vsprintf(char *str, const char *format, va_list va)
{
char *mid_out;
char *cformat;
char *ccformat;
char *mid;
while (format != NULL)
cformat = ft_strdup(format);
ccformat = cformat;
if (str == NULL || cformat == NULL)
return (0);
while (cformat != NULL)
{
str = ft_strfjoin(str, format);
format = ft_strchr(format, '%');
if (format == NULL)
if (cformat[0] != '%')
str = ft_strfjoin(str, ft_strdup(cformat));
cformat = ft_strchr(cformat, '%');
if (cformat == NULL)
break ;
else
format += 1;
mid_out = ft_argtoa(*format, va);
if (mid_out == NULL)
{
free(str);
return (0);
}
str = ft_strfjoin(str, mid_out);
cformat += 1;
mid = ft_argtoa(cformat[0], va);
while (mid == NULL)
mid = ft_strdup("(null)");
str = ft_strfjoin(str, mid);
cformat += 1;
}
free(ccformat);
return (str);
}

BIN
ft_vsprintf.o Normal file

Binary file not shown.

BIN
libftprintf.a Normal file

Binary file not shown.

Binary file not shown.

10
main.c
View File

@ -5,16 +5,16 @@
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 17:35:15 by cchauvet #+# #+# */
/* Updated: 2022/10/07 02:25:20 by cchauvet ### ########.fr */
/* Created: 2022/10/10 17:32:31 by cchauvet #+# #+# */
/* Updated: 2022/10/10 19:29:03 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
#include "printf.h"
#include <stdio.h>
int main(void)
{
ft_printf("%s", "gros peteur");
return (1);
ft_printf("%% %@ %X\n", 255);
return (0);
}

View File

@ -1,37 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libftprintf.h :+: :+: :+: */
/* printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
/* Updated: 2022/10/07 01:37:57 by cchauvet ### ########.fr */
/* Updated: 2022/10/10 19:11:15 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFTPRINTF_H
# define LIBFTPRINTF_H
#ifndef PRINTF_H
# define PRINTF_H
# include <stdlib.h>
# include <unistd.h>
# include <stdarg.h>
char *ft_strndup(const char *s, size_t n);
size_t ft_strlen(const char *s);
char *ft_strchr(const char *s, int c);
char *ft_strjoin(char *s1, char *s2);
char *ft_itoa(int n);
char *ft_ctoa(int c);
char *ft_strdup(char *s);
char *ft_strdup(const char *s);
char *ft_ptoa(void *ptr);
char *ft_utoa(unsigned int n);
char *ft_itoabase(int n, char *base);
char *ft_ltoabase(long int n, char *base);
char *ft_ultoabase(long unsigned int n, char *base);
int ft_printf(const char *format, ...);
int ft_vprintf(const char *format, va_list va);
char *ft_vsprintf(char *str, const char *format, va_list va);
char *ft_vdprintf(int fd, const char *format, va_list va);
int ft_vdprintf(int fd, const char *format, va_list va);
void ft_putchar_fd(char c, int fd);
void ft_putstr_fd(char *s, int f);