-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat_x.c
75 lines (64 loc) · 2.49 KB
/
format_x.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* format_x.c :+: :+: */
/* +:+ */
/* By: kawish <[email protected]> +#+ */
/* +#+ */
/* Created: 2021/02/20 20:50:23 by kawish #+# #+# */
/* Updated: 2021/03/06 10:51:44 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
unsigned int xval -- variadic argument
*/
void get_data_x(t_fields *fields, t_data *data, unsigned int xval)
{
data->a = uitoa_hex(xval);
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 %x and %X conversion specifiers.
Converts unsigned int to formatted string. And
outputs it to the standard output.
t_fields *fields -- pointer to struct that contains information
on fields
unsigned int xval -- variadic argument
*/
void format_x(t_fields *fields, unsigned int xval)
{
t_data data;
get_data_x(fields, &data, xval);
if (fields->count == -1)
return ;
precision_diuxp(fields, &data);
if (fields->count == -1)
return ;
width_diuxp(&data, fields);
if (fields->count == -1)
return ;
if (fields->conv_char == 'X')
data.a = str_toupper(data.a);
ft_putstr_fd(data.a, 1);
fields->count = fields->count + data.a_len;
free(data.a);
}