-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_format_s.c
88 lines (79 loc) · 2.21 KB
/
ft_format_s.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_format_s.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: afukuhar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/09/09 10:20:39 by afukuhar #+# #+# */
/* Updated: 2020/09/11 14:12:22 by afukuhar ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/*
** After analysing all attributes
** Based on left flag
** it prints pad than str or vice-versa
**
** when string is null, should print "(null)"
*/
void pf_s(t_format *arg, char *str)
{
pf_analyse_s(arg, str);
if (arg->left)
{
arg->len += ft_putnstr(str ? str : "(null)", arg->n_str);
arg->len += ft_putnchar(arg->pad, arg->n_pad);
}
else
{
arg->len += ft_putnchar(arg->pad, arg->n_pad);
arg->len += ft_putnstr(str ? str : "(null)", arg->n_str);
}
}
/*
** First check if string is null and update length accordinly
**
** Precision: should print at most p chars, if p < len,
** n_str that is the number of chars to print should
** be p or len (this prints whole string)
** Width: if w > n_str, should pad:
** - pad: '0' or ' '
** - n_pad: how many chars of pad
*/
void pf_analyse_s(t_format *arg, char *str)
{
int len;
len = !str ? 6 : ft_strlen(str);
if (arg->p >= 0 && arg->p < len)
arg->n_str = arg->p;
else
arg->n_str = len;
if (arg->w > arg->n_str)
{
arg->pad = (arg->zero && !arg->left) ? '0' : ' ';
arg->n_pad = arg->w - arg->n_str;
}
}
/*
** According to n, will print until n chars of string
*/
int ft_putnstr(char *str, int n)
{
int len;
len = 0;
if (!str)
{
len = ft_putstr("(null)");
}
else
{
while (n)
{
len += ft_putchar(*str);
str++;
n--;
}
}
return (len);
}