static int get_first_rounded_digit(long nbr, long divider) { long rest; char *reststr; char *nbrstr; int result; if (!divider) return (-1); nbrstr = ft_itoa_ll(nbr, 10); rest = nbr % divider; reststr = ft_itoa_ll(rest, 10); if (ft_strlen(reststr) == ft_strlen(nbrstr) - 1) result = rest / 10; else if (ft_strlen(reststr) == ft_strlen(nbrstr) - 2) { rest %= 10; if (rest / 10 >= 5) result = 1; else result = 0; } else result = 0; ft_strdel(&reststr); ft_strdel(&nbrstr); return (result); }
static int neg_and_prec(intmax_t d, t_data *t) { int ret; ret = 0; ft_putchar('-'); ret = handle_o_point(ft_strlen(ft_itoa_ll(d)) - 2, t, d); ret += ft_strlen(ft_itoa_ll(d)); ft_putnbr_ll(-d); return (ret); }
int handle_o_zero_d(intmax_t d, t_data *t, t_conv *c) { if (t->o_zero && d < 0 && !t->prec && !t->width && return_char(c->b_t_conv, '.')) return (ft_strlen(ft_itoa_ll(d))); else if (d < 0 && t->prec) return (neg_and_prec(d, t)); else if (d == 0 && !t->prec && return_char(c->b_t_conv, '.')) { if (t->width) ft_putchar(' '); return (t->width ? 1 : 0); } else if (t->o_zero && t->width && d < 0 && !t->o_minus) return (c->d ? ft_strlen((ft_itoa_ll(d))) : t->width - ft_strlen(ft_itoa_ll(d)) + 1); else ft_putnbr_ll(d); return (ft_strlen(ft_itoa_ll(d))); }
static void transform_size_into_str(off_t size, long divider, char *str, int i) { static char unit[5] = {'T', 'G', 'M', 'K', 'B'}; char *buf; buf = divider > 1 ? ft_itoa(size / divider) : ft_itoa_ll(size, 10); ft_strcpy(str, buf); ft_strdel(&buf); if (divider > 1 && ft_strlen(str) == 1) { ft_strlcat(str, ".", 100); buf = ft_itoa(get_first_rounded_digit((long)size, divider)); ft_strncat(str, buf, 1); ft_strdel(&buf); } ft_strncat(str, &unit[i], 1); }