-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_strings.c
69 lines (62 loc) · 2.03 KB
/
print_strings.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_strings.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alex <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/13 17:36:03 by alex #+# #+# */
/* Updated: 2021/01/13 17:39:36 by alex ### ########.fr */
/* */
/* ************************************************************************** */
#include "printfhead.h"
void ft_order_to_print_string(char *s, t_struct *blocks)
{
static char nul[7] = "(null)\0";
if (!s)
s = nul;
if (blocks->f_minus)
{
ft_print_precision_string(s, blocks);
ft_print_width_string(s, blocks);
}
else
{
ft_print_width_string(s, blocks);
ft_print_precision_string(s, blocks);
}
}
void ft_print_width_string(char *s, t_struct *blocks)
{
char w_ch;
int w_amount;
w_ch = ' ';
if (!blocks->f_prec || blocks->precision < 0)
w_amount = blocks->width - ft_strlen(s);
else if (blocks->f_prec && !blocks->precision)
w_amount = blocks->width;
else
w_amount = blocks->width - ft_find_min(blocks->precision, ft_strlen(s));
if (blocks->f_zero && !blocks->f_minus)
w_ch = '0';
while (w_amount > 0)
{
blocks->total_printed += write(1, &w_ch, 1);
w_amount--;
}
}
void ft_print_precision_string(char *s, t_struct *blocks)
{
int p_amount;
if (blocks->f_prec > 0 && blocks->precision > 0)
p_amount = blocks->precision;
else if (blocks->f_prec && !blocks->precision)
p_amount = 0;
else
p_amount = ft_strlen(s);
while (p_amount > 0 && *s)
{
blocks->total_printed += write(1, s++, 1);
p_amount--;
}
}