This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversions.c
84 lines (75 loc) · 2.47 KB
/
conversions.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
76
77
78
79
80
81
82
83
84
/* ************************************************************************** */
/* */
/* :::::::: */
/* conversions.c :+: :+: */
/* +:+ */
/* By: fbes <fbes@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2021/03/10 22:15:37 by fbes #+# #+# */
/* Updated: 2021/03/17 17:55:38 by fbes ######## odam.nl */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/*
new_conv creates a new conversion structure element, of a certain
type (char c). It also stores the end of the conversion in the
format string by calculating it with the starting pos and format_size.
Format_size is the amount of characters used for the conversion.
Ex: "%-11.6s" uses 7 characters, starting at % and ending at s+1.
*/
t_conv *new_conv(const char *pos, char c, int format_size)
{
t_conv *conv;
conv = malloc(sizeof(t_conv));
if (conv)
{
conv->position = pos;
conv->type = c;
conv->end = pos + format_size;
conv->precision = -1;
conv->width = 0;
conv->prepend = ' ';
conv->alignment = 1;
conv->input = NULL;
conv->output = NULL;
}
return (conv);
}
/*
del_conv gets called when deleting a conversion from a linked list,
freeing whatever has been allocated.
*/
void del_conv(void *conv)
{
if (((t_conv *)conv)->output)
free(((t_conv *)conv)->output);
free(conv);
}
/*
parse_convs loops through the format string, parsing a conversion
for every % character it finds. All conversions get added to a
linked list: t_list convs. This lists ends with a conversion of type
'\0', with a format_size of 0. In case the parsing fails, the linked
list gets cleared and freed, and the function will return a NULL pointer.
*/
t_list *parse_convs(va_list *params, const char *s)
{
t_list *convs;
t_conv *last;
convs = NULL;
while (*s)
{
if (*s == '%')
{
if (!parse_conv(params, &convs, &s))
{
ft_lstclear(&convs, &del_conv);
return (NULL);
}
}
s++;
}
last = new_conv(s, '\0', 0);
ft_lstadd_back(&convs, ft_lstnew(last));
return (convs);
}