/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* convert_d_i.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2018/05/05 19:17:53 by tmaze #+# #+# */ /* Updated: 2018/05/05 19:40:49 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ #include "libftprintf.h" char *apply_precision_d(char *s, int prec) { char *ret; char *tmp; int nb_fill; nb_fill = prec - ft_strlen(s); if ((ret = ft_strsub(s, 0, prec)) == NULL) return (NULL); ft_strdel(&s); if (nb_fill > 0 && (s = ft_strnew(nb_fill)) != NULL) { ft_memset(s, '0', nb_fill); tmp = ft_strjoin(s, ret); ft_strdel(&s); ft_strdel(&ret); ret = tmp; tmp = NULL; } ft_strdel(&tmp); return (ret); } char *apply_width_d(char *s, int width, char align) { char *add; char *tmp; if ((add = ft_strnew(width - ft_strlen(s))) == NULL) return (NULL); ft_memset(add, ' ', width - ft_strlen(s)); if (align == '-') { tmp = add; add = s; s = tmp; } tmp = ft_strjoin(add, s); ft_strdel(&add); ft_strdel(&s); return (tmp); } char *convert_d_i(char *flags, int *dim, int nb) { char *ret; char *tmp; char *atoi; int atoi_len; atoi = ft_itoa(ft_abs(nb)); atoi_len = ft_strlen(atoi); if (dim[1] > atoi_len) tmp = apply_precision_d(atoi, dim[1]); else tmp = atoi; atoi = ft_strnew((((flags[1] == '+' || flags[1] == ' ') && nb >= 0) || nb < 0) ? 1 : 0); if (flags[1] == '+' && nb >= 0) atoi[0] = '+'; else if (flags[1] == ' ' && nb >= 0) atoi[0] = ' '; else if (nb <= 0) atoi[0] = '-'; ret = ft_strjoin(atoi, tmp); ft_strdel(&atoi); ft_strdel(&tmp); if ((size_t)dim[0] > ft_strlen(ret)) ret = apply_width_d(ret, dim[0], flags[0]); return (ret); }