-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_print_pos.c
More file actions
141 lines (130 loc) · 2.99 KB
/
ft_print_pos.c
File metadata and controls
141 lines (130 loc) · 2.99 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_print_pos.c :+: :+: */
/* +:+ */
/* By: ivork <ivork@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2021/01/15 23:11:44 by ivork #+# #+# */
/* Updated: 2021/02/12 10:42:55 by ivork ######## odam.nl */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "ft_printf.h"
extern int g_count;
void ft_fill_width(char *str, t_flags flag)
{
int len;
len = 0;
if (str != NULL)
len = (int)ft_strlen(str);
if (flag.left_align == false)
{
while (flag.width - len > 0)
{
ft_putchar_fd(' ', 1);
g_count++;
flag.width--;
}
}
ft_putstr_fd(str, 1);
g_count += len;
if (flag.left_align == true)
{
while (flag.width - len > 0)
{
ft_putchar_fd(' ', 1);
g_count++;
flag.width--;
}
}
}
void ft_fill_preci(char *str, t_flags flag)
{
int len;
if (str == NULL)
len = 0;
else
len = (int)ft_strlen(str);
if (flag.width != 0)
flag.precision = flag.width;
while (flag.precision - len > 0)
{
ft_putchar_fd('0', 1);
g_count++;
flag.precision--;
}
ft_putstr_fd(str, 1);
g_count += len;
flag.precision = 0;
}
void ft_fill_both(char *str, t_flags flag)
{
int len;
if (str == NULL)
len = 0;
else
len = (int)ft_strlen(str);
while ((flag.width - flag.precision > 0)
&& (flag.width - len > 0))
{
ft_putchar_fd(' ', 1);
g_count++;
flag.width--;
}
while (flag.precision - len > 0)
{
ft_putchar_fd('0', 1);
g_count++;
flag.precision--;
}
ft_putstr_fd(str, 1);
g_count += len;
}
void ft_fill_both_left(char *str, t_flags flag)
{
int count;
int len;
if (str == NULL)
len = 0;
else
len = (int)ft_strlen(str);
count = 0;
while (flag.precision - len + count > 0)
{
ft_putchar_fd('0', 1);
g_count++;
count--;
}
ft_putstr_fd(str, 1);
g_count += len;
while ((flag.width - flag.precision > 0)
&& (flag.width - len > 0))
{
ft_putchar_fd(' ', 1);
g_count++;
flag.width--;
}
}
void ft_print_pos(char *str, t_flags flag)
{
if (flag.width == 0 && flag.preci == false && flag.zero == false)
{
ft_putstr_fd(str, 1);
g_count += (int)ft_strlen(str);
}
if (flag.width != 0 && flag.zero == false && flag.preci == false)
ft_fill_width(str, flag);
if ((flag.preci == true && flag.width == 0) || (flag.zero == true))
{
ft_fill_preci(str, flag);
return ;
}
if (flag.width != 0 && flag.preci == true)
{
if (flag.left_align == false)
ft_fill_both(str, flag);
else
ft_fill_both_left(str, flag);
}
}