Projet de l'école 42 : Printf
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.2 KiB

#include "ft_printf.h"
char *int_opts_transform(int n, char *nbr, t_opts *opts)
{
if (opts->plus && n>=0)
nbr = ft_strjoin("+", nbr);
else if (opts->space && n>=0)
nbr = ft_strjoin(" ", nbr);
else if (opts->precision == 0 && opts->dot && n == 0 &&
opts->len)
nbr = ft_strdup("");
else
nbr = ft_strdup(nbr);
if (opts->precision > 0)
nbr = ft_append(ft_zero_fill(nbr, '0', opts), nbr);
else if (opts->zero && !opts->dot)
nbr = ft_append(ft_zero_fill(nbr, '0', opts), nbr);
return (nbr);
}
char *uint_opts_transform(unsigned int n, char *nbr, t_opts *opts)
{
if (opts->plus)
nbr = ft_strjoin("+", nbr);
else if (opts->space)
nbr = ft_strjoin(" ", nbr);
else if (opts->precision == 0 && opts->dot && n == 0 &&
opts->len)
nbr = ft_strdup("");
else
nbr = ft_strdup(nbr);
if (opts->precision > 0)
nbr = ft_append(ft_zero_fill(nbr, '0', opts), nbr);
else if (opts->zero && !opts->dot)
nbr = ft_append(ft_zero_fill(nbr, '0', opts), nbr);
return (nbr);
}
char *ptr_opts_transform(char *nbr, t_opts *opts)
{
if (opts->plus)
nbr = ft_strjoin("+", nbr);
else if (opts->space)
nbr = ft_strjoin(" ", nbr);
else if (opts->precision == 0 && opts->dot &&
opts->len)
nbr = ft_strdup("");
else
nbr = ft_strdup(nbr);
if (opts->precision > 0)
nbr = ft_append(ft_zero_fill(nbr, '0', opts), nbr);
else if (opts->zero && !opts->dot)
nbr = ft_append(ft_zero_fill(nbr, '0', opts), nbr);
return (nbr);
}
char *str_opts_transform(char *str, t_opts *opts)
{
int len;
char *tmp = NULL;
char *sub = NULL;
if (opts->dot)
sub = ft_substr(str, 0, opts->precision);
if (sub)
str = sub;
len = opts->width - (int)ft_strlen(str);
tmp = ft_strnew(len, ' ');
if (!opts->minus && len > 0)
str = ft_strjoin(tmp, str);
else if (opts->minus && len > 0)
str = ft_strjoin(str, tmp);
else
str = ft_strdup(str);
if (tmp)
free(tmp);
if (sub)
free(sub);
return (str);
}