-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_p_chr_bonus.c
65 lines (56 loc) · 1.71 KB
/
ft_p_chr_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_p_chr_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rhsu <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/13 20:46:32 by rhsu #+# #+# */
/* Updated: 2023/10/13 20:47:07 by rhsu ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_pchr(char *s)
{
if (s[0] != 0)
ft_putchar_fd(s[0], 1);
return (1);
}
/*
if right-justified, only min width
Logic:
if min width > cnt
print ' ' (min - cnt) times
if '#', print '0x/0X'
print int len
return tot bytes printed.
*/
static int ft_right_justified_p_ptr(int min, char *s)
{
int cnt;
int pcnt;
cnt = 1;
pcnt = 0;
if (min > cnt)
pcnt += ft_pad_char(min - cnt, ' ');
pcnt += ft_pchr(s);
return (pcnt);
}
/*
only '-' flag and min width length allowed
expect int input, however converted to s string, s[0] holds the char
if input char is 0, return ""
*/
int ft_p_chr(char *flag, int min, char *s)
{
int cnt;
if (ft_strchr(flag, '-'))
{
cnt = ft_pchr(s);
if (min > cnt)
cnt += ft_pad_char(min - cnt, ' ');
}
else
cnt = ft_right_justified_p_ptr(min, s);
return (cnt);
}