-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_di.c
73 lines (62 loc) · 2.39 KB
/
format_di.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* format_di.c :+: :+: */
/* +:+ */
/* By: kawish <[email protected]> +#+ */
/* +#+ */
/* Created: 2021/02/05 14:51:41 by kawish #+# #+# */
/* Updated: 2021/03/06 10:45:25 by kawish ######## odam.nl */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/* Gets data needed to format string according to fields.
1. Sets data->a to a string representation of the variadic argument.
2. Sets data->a_dup to data->a to move around the string while data->a
remains at the original position.
3. Sets data->a_len to be the length of data->a.
4. Sets data->a_digits to be the number of digits in data->a, in case
there's a minus sign.
With the above mentioned data you can take care of all formating
requirements for this project.
t_fields *fields -- pointer to struct that contains information
on fields
t_data *data -- pointer to struct that contains information on
the variadic argument to be formatted and printed
int dval -- variadic argument
*/
void get_data_di(t_fields *fields, t_data *data, int dval)
{
data->a = ft_itoa(dval);
if (!data->a)
{
fields->count = -1;
return ;
}
data->a_dup = data->a;
data->a_len = ft_strlen(data->a);
data->a_digits = get_alnum(data->a);
}
/* Handles %d and %i conversion specifiers.
Converts integers to formatted string. And outputs it
to the standard output.
t_fields *fields -- pointer to struct that contains information
on fields
int dval -- variadic argument
*/
void format_di(t_fields *fields, int dval)
{
t_data data;
get_data_di(fields, &data, dval);
if (fields->count == -1)
return ;
precision_diuxp(fields, &data);
if (fields->count == -1)
return ;
width_diuxp(&data, fields);
if (fields->count == -1)
return ;
ft_putstr_fd(data.a, 1);
fields->count = fields->count + data.a_len;
free(data.a);
}