/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_print_opts.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/11 08:05:56 by narnaud #+# #+# */ /* Updated: 2023/04/11 08:05:58 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #include "ft_printf.h" char *zero_fill(char *nbr, t_opts *opts) { int len; char *fill; fill = NULL; if (opts->zero) len = opts->width - ft_strlen(nbr); else if (opts->dot) len = opts->precision - ft_strlen(nbr); else return (NULL); if (len < 0 || (opts->zero && len == 0)) return (NULL); if ((nbr[0] == '-' || nbr[0] == '+' || nbr[0] == ' ') && opts->dot) fill = ft_strnew(len + 1, '0'); else fill = ft_strnew(len, '0'); if (nbr[0] == '-' || nbr[0] == '+' || nbr[0] == ' ') { fill[0] = nbr[0]; nbr[0] = '0'; } return (fill); } 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(zero_fill(nbr, opts), nbr); else if (opts->zero && !opts->dot) nbr = ft_append(zero_fill(nbr, 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(zero_fill(nbr, opts), nbr); else if (opts->zero && !opts->dot) nbr = ft_append(zero_fill(nbr, 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(zero_fill(nbr, opts), nbr); else if (opts->zero && !opts->dot) nbr = ft_append(zero_fill(nbr, opts), nbr); return (nbr); } char *str_opts_transform(char *str, t_opts *opts) { int len; char *tmp; char *sub; tmp = NULL; 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); }