02-05-2026
49 lines
1.6 kB
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* format_padding_bonus.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: mleitao <mleitao@student.42.fr> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2026/04/03 00:28:09 by mleitao #+# #+# */
9/* Updated: 2026/05/02 00:52:14 by mleitao ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "printers_bonus.h"
14#include "utils.h"
15
16int calculate_width(int width, int len)
17{
18 if (width > len)
19 return (width - len);
20 return (0);
21}
22
23int apply_width(t_data *d, int len)
24{
25 int padding;
26 int chars_written;
27
28 padding = calculate_width(d->format.width, len);
29 chars_written = putnchar(' ', padding);
30 if (chars_written != padding)
31 return (-1);
32 d->chars_written += chars_written;
33 return (chars_written);
34}
35
36void calculate_padding(t_data *d, t_numdata *n)
37{
38 int total;
39
40 if (d->format.precision > n->len)
41 n->zeros = d->format.precision - n->len;
42 total = n->len + n->zeros + (n->sign != 0) + n->prefix_len;
43 n->spaces = calculate_width(d->format.width, total);
44 if (d->format.zero)
45 {
46 n->zeros += n->spaces;
47 n->spaces = 0;
48 }
49}