mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
83 lines
2.5 KiB
C
83 lines
2.5 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_printf.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/11/06 09:32:35 by jguelen #+# #+# */
|
|
/* Updated: 2025/02/04 10:16:41 by jguelen ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "ft_printf.h"
|
|
|
|
static int check_minwidth_and_precision(t_printf_format *format)
|
|
{
|
|
if (format->minwidth == LONG_MAX || format->minwidth == LONG_MIN
|
|
|| format->precision == LONG_MAX || format->precision == LONG_MIN
|
|
|| format->minwidth == INT_MIN)
|
|
return (1);
|
|
if (format->minwidth < 0)
|
|
{
|
|
format->flags |= PRINTF_MINUSFLAG;
|
|
format->minwidth *= -1;
|
|
}
|
|
if (format->precision < 0)
|
|
format->flags &= ~PRINTF_DOTFLAG;
|
|
return (0);
|
|
}
|
|
|
|
int ft_print_printf_format(int fd, t_printf_format *format)
|
|
{
|
|
static int (*const printspec[])(int fd, t_printf_format *format) = {
|
|
[PRINTF_NOSPEC] = &ft_printf_badspec,
|
|
[PRINTF_CHARSPEC] = &ft_printf_char,
|
|
[PRINTF_STRINGSPEC] = &ft_printf_string,
|
|
[PRINTF_POINTERSPEC] = &ft_printf_ptr,
|
|
[PRINTF_INTSPEC] = &ft_printf_int,
|
|
[PRINTF_UINTSPEC] = &ft_printf_uint,
|
|
[PRINTF_HEXA_LOWSPEC] = &ft_printf_hexa,
|
|
[PRINTF_HEXA_UPSPEC] = &ft_printf_hexa,
|
|
[PRINTF_PERCENTSPEC] = &ft_printf_percent,
|
|
};
|
|
|
|
if (check_minwidth_and_precision(format))
|
|
return (-1);
|
|
return (printspec[format->specifier](fd, format));
|
|
}
|
|
|
|
static int ft_printf_fd(int fd, const char *s,
|
|
va_list *arg_list)
|
|
{
|
|
int len;
|
|
t_printf_format format;
|
|
|
|
if (!s)
|
|
return (-1);
|
|
len = ft_printf_parsing(fd, (char *)s, &format, arg_list);
|
|
return (len);
|
|
}
|
|
|
|
__attribute__((format(printf, 1, 2))) int ft_printf(const char *s, ...)
|
|
{
|
|
va_list arg_list;
|
|
int len;
|
|
|
|
va_start(arg_list, s);
|
|
len = ft_printf_fd(1, s, &arg_list);
|
|
va_end(arg_list);
|
|
return (len);
|
|
}
|
|
|
|
__attribute__((format(printf, 2, 3))) int ft_dprintf(int fd,
|
|
const char *s, ...)
|
|
{
|
|
va_list arg_list;
|
|
int len;
|
|
|
|
va_start(arg_list, s);
|
|
len = ft_printf_fd(fd, s, &arg_list);
|
|
va_end(arg_list);
|
|
return (len);
|
|
}
|