-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_str.c
More file actions
80 lines (72 loc) · 2.07 KB
/
Copy pathprint_str.c
File metadata and controls
80 lines (72 loc) · 2.07 KB
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
/* ************************************************************************** */
/* */
/* :::::::: */
/* print_str.c :+: :+: */
/* +:+ */
/* By: rvan-sch <rvan-sch@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2020/01/29 19:04:13 by rvan-sch #+# #+# */
/* Updated: 2020/01/29 19:04:14 by rvan-sch ######## odam.nl */
/* */
/* ************************************************************************** */
#include "header.h"
static void ft_str_set_edges(t_flag *flag, char *s, int *s_len)
{
if (flag->zero && flag->dash)
flag->zero = 0;
if (flag->width < 0)
{
flag->zero = 0;
flag->dash = 1;
flag->width *= -1;
}
*s_len = ft_strlen(s);
if (flag->prec < 0)
flag->prec = *s_len;
}
static void ft_print_str_dash(t_flag *flag, char *s, int s_len)
{
int i;
i = 0;
if (flag->dot)
{
while (s[i] && i < flag->prec)
{
ft_putchar(s[i]);
i++;
}
if (flag->prec < s_len)
ft_putchar_amount(' ', flag->width - flag->prec);
else
ft_putchar_amount(' ', flag->width - s_len);
}
else
ft_putstr_chars(s, ' ', flag->width - s_len);
}
static void ft_print_str_dot(t_flag *flag, char *s, int s_len)
{
int i;
i = 0;
if (flag->prec < s_len)
ft_putchar_amount(' ', flag->width - flag->prec);
else
ft_putchar_amount(' ', flag->width - s_len);
while (s[i] && i < flag->prec)
{
ft_putchar(s[i]);
i++;
}
}
void print_str(t_flag *flag, char *s)
{
int s_len;
if (s == NULL)
s = "(null)";
ft_str_set_edges(flag, s, &s_len);
if (flag->dash)
ft_print_str_dash(flag, s, s_len);
else if (flag->dot)
ft_print_str_dot(flag, s, s_len);
else
ft_putchars_str(' ', flag->width - s_len, s);
}