This commit is contained in:
Camille Chauvet 2022-10-23 19:01:50 +02:00
parent 593de9941a
commit 31feb3fe3c
26 changed files with 254 additions and 378 deletions

BIN
.ft_vsprintf.c.swp Normal file

Binary file not shown.

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

View File

@ -6,29 +6,30 @@
# By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/09/27 08:39:27 by cchauvet #+# #+# #
# Updated: 2022/10/10 14:34:40 by cchauvet ### ########.fr #
# Updated: 2022/10/23 19:00:59 by cchauvet ### ########.fr #
# #
# **************************************************************************** #
SRCS = ft_ctoa.c \
ft_itoa.c \
ft_ltoabase.c \
SRCS = ft_dprintarg.c \
ft_dprintf.c \
ft_dprintflag.c \
ft_dprintl_base.c \
ft_dprintx.c \
ft_dprintX.c \
ft_dprintptr.c \
ft_dprintul_base.c \
ft_dprintul.c \
ft_isarg.c \
ft_isdigit.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_skipflag.c \
ft_strlen.c \
ft_ultoabase.c \
ft_vdprintf.c \
ft_vprintf.c \
ft_vsprintf.c
ft_vdprintf.c
OBJS = ${SRCS:.c=.o}
NAME = libftprintf
NAME = libftprintf.a
CFLAG = -Wall -Werror -Wextra
@ -38,13 +39,13 @@ CFLAG = -Wall -Werror -Wextra
all: ${NAME}
${NAME}: ${OBJS}
ar -rc ${NAME}.a ${OBJS}
ar -rc ${NAME} ${OBJS}
clean:
rm -f ${OBJS} ${BOBJS}
fclean: clean
rm -f ${NAME}.a
rm -f ${NAME}
re: fclean all

BIN
a.out

Binary file not shown.

View File

@ -1,21 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* ft_dprintX.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 22:26:36 by cchauvet #+# #+# */
/* Updated: 2022/10/10 14:42:06 by cchauvet ### ########.fr */
/* Created: 2022/10/23 17:44:38 by cchauvet #+# #+# */
/* Updated: 2022/10/23 18:22:43 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "printf.h"
#include "ft_printf.h"
void ft_putendl_fd(char *s, int fd)
int ft_dprint_upperx(int fd, unsigned int n)
{
if (s == NULL)
return ;
ft_putstr_fd(s, fd);
ft_putchar_fd('\n', fd);
return (ft_dprintul_base(fd, n, "0123456789ABCDEF"));
}

34
ft_dprintarg.c Normal file
View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dprintarg.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/23 18:08:31 by cchauvet #+# #+# */
/* Updated: 2022/10/23 18:54:41 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_dprintarg(int fd, int arg, va_list args)
{
if (arg == 'i' || arg == 'd')
return (ft_dprintl_base(fd, va_arg(args, int), "0123456789"));
if (arg == 'X')
return (ft_dprint_upperx(fd, va_arg(args, unsigned int)));
if (arg == 'x')
return (ft_dprintx(fd, va_arg(args, unsigned int)));
if (arg == 'u')
return (ft_dprintul(fd, va_arg(args, unsigned int)));
if (arg == 'c')
return (ft_putchar_fd(fd, va_arg(args, int)));
if (arg == 's')
return (ft_putstr_fd(fd, va_arg(args, char *)));
if (arg == '%')
return (ft_putchar_fd(fd, '%'));
if (arg == 'p')
return (ft_dprintptr(fd, va_arg(args, void *)));
return (0);
}

View File

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

23
ft_dprintflag.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dprintflag.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/11 16:30:39 by cchauvet #+# #+# */
/* Updated: 2022/10/23 18:59:55 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_dprintflag(int fd, const char *flag, va_list va)
{
if (ft_skipflag(flag) == 0)
return (-1);
flag++;
while (ft_isdigit(flag[0]))
flag += 1;
return (ft_dprintarg(fd, flag[0], va));
}

View File

@ -1,38 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ltoabase.c :+: :+: :+: */
/* ft_dprintl_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/10 03:26:21 by cchauvet #+# #+# */
/* Updated: 2022/10/10 14:41:28 by cchauvet ### ########.fr */
/* Updated: 2022/10/21 15:54:31 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "printf.h"
#include "ft_printf.h"
char *ft_ltoabase(long int n, char *base)
int ft_dprintl_base(int fd, long long int n, char *base)
{
char *mid_out;
char *out;
int i;
unsigned long nb;
if (base == NULL)
return (NULL);
return (-1);
i = 0;
if (n < 0)
{
nb = -n;
mid_out = ft_ultoabase(nb, base);
if (mid_out == NULL)
return (NULL);
out = ft_strjoin("-", mid_out);
free(mid_out);
i += ft_putchar_fd(fd, '-');
}
else
{
nb = n;
return (ft_ultoabase(nb, base));
}
return (out);
i += ft_dprintul_base(fd, nb, base);
if (i < 1)
return (-1);
return (i);
}

View File

@ -1,37 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ptoa.c :+: :+: :+: */
/* ft_dprintptr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 17:41:04 by cchauvet #+# #+# */
/* Updated: 2022/10/10 15:54:00 by cchauvet ### ########.fr */
/* Updated: 2022/10/12 15:22:40 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "printf.h"
#include "ft_printf.h"
char *ft_ptoa(void *ptr)
int ft_dprintptr(int fd, void *ptr)
{
unsigned long address;
size_t i;
char *out;
char *s;
int i;
if (ptr == NULL)
return (ft_strdup("(nil)"));
return (ft_putstr_fd(fd, "(nil)"));
address = (unsigned long) ptr;
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++;
}
return (out);
i = 2;
ft_putstr_fd(fd, "0x");
i += ft_dprintul_base(fd, address, "0123456789abcdef");
return (i);
}

View File

@ -1,24 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* ft_dprintul.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 14:44:15 by cchauvet #+# #+# */
/* Updated: 2022/10/10 14:42:21 by cchauvet ### ########.fr */
/* Created: 2022/10/12 15:46:14 by cchauvet #+# #+# */
/* Updated: 2022/10/21 15:53:07 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "printf.h"
#include "ft_printf.h"
char *ft_strchr(const char *s, int c)
int ft_dprintul(int fd, unsigned long long n)
{
while (*s != (char) c)
{
if (*s == 0)
return (NULL);
s++;
}
return ((char *) s);
return (ft_dprintul_base(fd, n, "0123456789"));
}

View File

@ -1,18 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ultoabase.c :+: :+: :+: */
/* ft_dprintul_base.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 */
/* Updated: 2022/10/23 14:23:44 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "printf.h"
#include "ft_printf.h"
static size_t ft_str_size(unsigned long n, size_t base_size)
static size_t ft_str_size(unsigned long long n, size_t base_size)
{
size_t size;
@ -59,26 +59,23 @@ static size_t ft_base_size(char *base)
return (len);
}
char *ft_ultoabase(unsigned long n, char *base)
int ft_dprintul_base(int fd, unsigned long long n, char *base)
{
size_t base_size;
size_t str_size;
char *out;
int str_size;
if (base == NULL)
return (NULL);
return (-1);
base_size = ft_base_size(base);
if (base_size == 0)
return (NULL);
return (-1);
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)
if (n > base_size - 1)
{
out[--str_size] = base[n % base_size];
n = n / base_size;
ft_dprintul_base(fd, n / base_size, base);
ft_putchar_fd(fd, base[n % base_size]);
}
return (out);
else
ft_putchar_fd(fd, base[n]);
return (str_size - 1);
}

18
ft_dprintx.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dprintx.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/23 17:44:38 by cchauvet #+# #+# */
/* Updated: 2022/10/23 18:04:56 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_dprintx(int fd, unsigned int n)
{
return (ft_dprintul_base(fd, n, "0123456789abcdef"));
}

View File

@ -1,25 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ctoa.c :+: :+: :+: */
/* ft_isarg.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 21:42:53 by cchauvet #+# #+# */
/* Updated: 2022/10/10 14:41:19 by cchauvet ### ########.fr */
/* Created: 2022/10/11 22:37:41 by cchauvet #+# #+# */
/* Updated: 2022/10/12 16:09:22 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "printf.h"
#include "ft_printf.h"
char *ft_ctoa(int c)
int ft_isarg(int c)
{
char *s;
char *flags;
s = malloc(sizeof(char) * 2);
if (s == NULL)
return (NULL);
s[0] = (char) c;
s[1] = 0;
return (s);
if (c == '\0')
return (1);
flags = "cspdiuxX%";
while (*flags)
{
if (*flags == c)
return (1);
flags++;
}
return (0);
}

View File

@ -1,20 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/10 17:32:31 by cchauvet #+# #+# */
/* Updated: 2022/10/10 19:29:03 by cchauvet ### ########.fr */
/* Created: 2022/10/11 16:34:14 by cchauvet #+# #+# */
/* Updated: 2022/10/11 16:35:40 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "printf.h"
#include <stdio.h>
#include "ft_printf.h"
int main(void)
int ft_isdigit(int c)
{
ft_printf("%% %@ %X\n", 255);
if (c <= '9' && c >= '0')
return (1);
return (0);
}

View File

@ -1,55 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 13:49:45 by cchauvet #+# #+# */
/* Updated: 2022/10/10 14:41:18 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "printf.h"
static int ft_nb_digit(int n)
{
int out;
out = 0;
while (n)
{
n /= 10;
out++;
}
return (out);
}
char *ft_itoa(int n)
{
char *out;
unsigned int nb[2];
if (!n)
return (ft_strdup("0"));
nb[0] = ft_nb_digit(n);
if (n < 0)
{
nb[1] = -n;
nb[0]++;
}
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]--] = nb[1] % 10 + 48;
nb[1] /= 10;
}
return (out);
}

View File

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

View File

@ -1,40 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* printf.h :+: :+: :+: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/26 14:47:54 by cchauvet #+# #+# */
/* Updated: 2022/10/10 19:11:15 by cchauvet ### ########.fr */
/* Updated: 2022/10/23 19:00:30 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PRINTF_H
# define PRINTF_H
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
# include <stdlib.h>
# include <unistd.h>
# include <stdarg.h>
# include <limits.h>
size_t ft_strlen(const char *s);
char *ft_strchr(const char *s, int c);
char *ft_strjoin(char *s1, char *s2);
int ft_isdigit(int c);
int ft_skipflag(const char *str);
int ft_isarg(int c);
char *ft_itoa(int n);
char *ft_ctoa(int c);
char *ft_strdup(const char *s);
char *ft_ptoa(void *ptr);
char *ft_utoa(unsigned int n);
char *ft_ltoabase(long int n, char *base);
char *ft_ultoabase(long unsigned int n, char *base);
int ft_dprintptr(int fd, void *ptr);
int ft_dprintl_base(int fd, long long n, char *base);
int ft_dprintul_base(int fd, unsigned long long n, char *base);
int ft_dprintul(int fd, unsigned long long n);
int ft_dprintx(int fd, unsigned int n);
int ft_dprint_upperx(int fd, unsigned int n);
int ft_dprintflag(int fd, const char *flag, va_list va);
int ft_dprintarg(int fd, int c, va_list va);
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);
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);
int ft_putchar_fd(int fd, char c);
int ft_putstr_fd(int fd, char *str);
#endif

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

View File

@ -6,13 +6,14 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 22:23:35 by cchauvet #+# #+# */
/* Updated: 2022/10/10 14:41:55 by cchauvet ### ########.fr */
/* Updated: 2022/10/12 15:21:42 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "printf.h"
#include "ft_printf.h"
void ft_putchar_fd(char c, int fd)
int ft_putchar_fd(int fd, char c)
{
write(fd, &c, 1);
return (1);
}

View File

@ -6,16 +6,20 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 22:25:08 by cchauvet #+# #+# */
/* Updated: 2022/10/10 14:42:15 by cchauvet ### ########.fr */
/* Updated: 2022/10/12 16:51:51 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "printf.h"
#include "ft_printf.h"
void ft_putstr_fd(char *s, int fd)
int ft_putstr_fd(int fd, char *str)
{
if (s == NULL)
return ;
while (*s)
write(fd, s++, 1);
int i;
if (str == NULL)
str = "(null)";
i = 0;
while (str[i] != '\0')
ft_putchar_fd(fd, str[i++]);
return (i);
}

View File

@ -1,33 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* ft_skipflag.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 16:00:49 by cchauvet #+# #+# */
/* Updated: 2022/10/10 18:48:37 by cchauvet ### ########.fr */
/* Created: 2022/10/12 14:47:52 by cchauvet #+# #+# */
/* Updated: 2022/10/12 16:14:07 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "printf.h"
#include "ft_printf.h"
char *ft_strdup(const char *s)
int ft_skipflag(const char *str)
{
char *out;
size_t i;
if (s == NULL)
return (NULL);
out = malloc((ft_strlen(s) + 1) * sizeof(char));
if (out == NULL)
return (NULL);
i = 0;
while (s[i])
{
out[i] = s[i];
if (str[i] != '%')
return (0);
i++;
if (str[i] == '\0')
return (-1);
while (ft_isdigit(str[i]))
i++;
}
out[i] = 0;
return (out);
if (ft_isarg(str[i]))
return (i + 1);
return (0);
}

View File

@ -1,38 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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);
}

View File

@ -6,11 +6,11 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 17:39:05 by cchauvet #+# #+# */
/* Updated: 2022/10/10 15:43:27 by cchauvet ### ########.fr */
/* Updated: 2022/10/11 00:01:22 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "printf.h"
#include "ft_printf.h"
size_t ft_strlen(const char *s)
{

View File

@ -5,22 +5,45 @@
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 16:39:25 by cchauvet #+# #+# */
/* Updated: 2022/10/10 19:12:04 by cchauvet ### ########.fr */
/* Created: 2022/10/12 14:34:28 by cchauvet #+# #+# */
/* Updated: 2022/10/12 16:47:32 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "printf.h"
#include "ft_printf.h"
static int ft_dprintseg(int fd, const char *str)
{
int i;
i = 0;
while (str[i] != '\0' && ft_skipflag(str + i) == 0)
ft_putchar_fd(fd, str[i++]);
return (i);
}
int ft_vdprintf(int fd, const char *format, va_list va)
{
char *str;
int rv;
int i;
int upi;
str = ft_strdup("");
str = ft_vsprintf(str, format, va);
rv = ft_strlen(str);
ft_putstr_fd(str, fd);
free(str);
return (rv);
i = 0;
while (format[0] != 0)
{
if (ft_skipflag(format) == 0)
{
upi = ft_dprintseg(fd, format);
format += upi;
i += upi;
}
else
{
upi = ft_dprintflag(fd, format, va);
if (upi == -1)
return (-1);
format += ft_skipflag(format);
i += upi;
}
}
return (i);
}

View File

@ -1,105 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_vsprintf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 10:44:36 by cchauvet #+# #+# */
/* Updated: 2022/10/10 19:32:04 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#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')
return (ft_strdup(va_arg(args, char *)));
if (c == 'd' || c == 'i')
return (ft_itoa(va_arg(args, long int)));
if (c == 'p')
return (ft_ptoa(va_arg(args, void *)));
if (c == 'x')
return (ft_ltoabase(va_arg(args, long int), "0123456789abcdef"));
if (c == 'X')
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_strdup("%"));
out = ft_strdup("% ");
if (out == NULL)
return (NULL);
out[1] = c;
return (out);
}
char *ft_vsprintf(char *str, const char *format, va_list va)
{
char *cformat;
char *ccformat;
char *mid;
cformat = ft_strdup(format);
ccformat = cformat;
if (str == NULL || cformat == NULL)
return (0);
while (cformat != NULL)
{
if (cformat[0] != '%')
str = ft_strfjoin(str, ft_strdup(cformat));
cformat = ft_strchr(cformat, '%');
if (cformat == NULL)
break ;
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);
}