minishell/libft/ft_printf_str.c
2025-02-12 15:12:42 +01:00

93 lines
2.7 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_str.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/25 11:40:22 by jguelen #+# #+# */
/* Updated: 2025/02/04 10:03:36 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/* c points to the character used to pad*/
int ft_pad_char(int fd, t_printf_format *format, char *c)
{
while (format->total_len < format->minwidth)
{
if (write(fd, c, 1) == -1)
return (-1);
format->total_len++;
}
return (format->total_len);
}
int ft_printf_str(int fd, const char *str, int maxchar)
{
int len;
char *s;
len = 0;
s = (char *)str;
if (s == NULL)
{
if (maxchar >= 6)
return (ft_printf_str(fd, "(null)", INT_MAX));
return (0);
}
while (s[len] && len < maxchar)
len++;
return ((int)write(fd, s, len));
}
int ft_printf_string(int fd, t_printf_format *format)
{
int i;
i = INT_MAX;
if ((format->flags & ~PRINTF_MINUSFLAG) == format->flags)
{
if (ft_pad_char(fd, format, " ") == -1)
return (-1);
}
if (format->flags & PRINTF_DOTFLAG)
i = (int)format->precision;
if (ft_printf_str(fd, format->var.s, i) == -1)
return (-1);
if (ft_pad_char(fd, format, " ") == -1)
return (-1);
return (format->total_len);
}
int ft_printf_badspec(int fd, t_printf_format *format)
{
(void)fd;
return (format->total_len);
}
/*
//TODO write field width and precision
int ft_printf_badspec(int fd, t_printf_format *format)
{
int i;
i = 0;
if ((format->flags & PRINTF_HASHTAGFLAG) && write(fd, "#", 1) == -1)
return (-1);
if ((format->flags & PRINTF_PLUSFLAG) && write(fd, "+", 1) == -1)
return (-1);
if ((format->flags & PRINTF_SPACEFLAG)
&& (format->flags & ~PRINTF_PLUSFLAG) == format->flags
&& write(fd, " ", 1) == -1)
return (-1);
if ((format->flags & PRINTF_MINUSFLAG) && write(fd, "-", 1) == -1)
return (-1);
if ((format->flags & PRINTF_ZEROFLAG)
&& write(fd, "0", 1) == -1)
return (-1);
// WRITE FIELD WIDTH AND '.'-PRECEDED PRECISION
if (write(fd, &format->var.c, 1) == -1 && i++)
return (-1);
return (format->total_len);
}*/