-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf_utils_flag_struct.c
More file actions
107 lines (97 loc) · 3.38 KB
/
ft_printf_utils_flag_struct.c
File metadata and controls
107 lines (97 loc) · 3.38 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_printf_utils_flag_struct.c :+: :+: */
/* +:+ */
/* By: ohengelm <ohengelm@student.42.fr> +#+ */
/* +#+ */
/* Created: 2022/04/02 21:14:19 by ohengelm #+# #+# */
/* Updated: 2023/08/04 19:40:43 by ohengelm ######## odam.nl */
/* */
/* ************************************************************************** */
/* ====================================||==================================== *\
|| ||
|| Make Flag Structure ||
|| ||
|| Fills flag structure with syntax values. ||
|| ||
\* ==============ft_printf=============||==============©Othello============== */
#include "libft.h"
// void *ft_memset(void *b, int c, size_t len)
// char *ft_strchr(const char *s, int c)
// int ft_atoi(const char *str)
#include "ft_printf_utils.h"
//t_flags
#include <stdlib.h>
// void free(void *)
static void ftp_fill_flag_struct(const char *str, int *i, t_flags *flags);
static void ftp_fill_struct_flags(const char *str, int *i, t_flags *flags);
static void ftp_fill_struct_width_prec(const char *str, int *i, t_flags *flags);
static void ftp_fill_struct_length_mod(const char *str, int *i, t_flags *flags);
void ftp_make_flag_struct(const char *str, int *i, t_flags *flags)
{
flags->s = -1;
flags->form = -1;
flags->padding = -1;
flags->sign = -1;
flags->width = -1;
flags->prec = -1;
ft_memset(flags->len_mod, 0, 2);
flags->conv = 0;
ftp_fill_flag_struct(str, i, flags);
}
static void ftp_fill_flag_struct(const char *str, int *i, t_flags *flags)
{
if (ft_strchr("#0- +", str[*i]))
ftp_fill_struct_flags(str, i, flags);
if ((str[*i] >= '0' && str[*i] <= '9') || str[*i] == '.')
ftp_fill_struct_width_prec(str, i, flags);
if (ft_strchr("hlqLjzZt", str[*i]))
ftp_fill_struct_length_mod(str, i, flags);
if (ft_strchr("diouxXeEfFgGaAcsCSpnm%%", str[*i]))
flags->conv = str[*i];
}
static void ftp_fill_struct_flags(const char *str, int *i, t_flags *flags)
{
while (ft_strchr("#0- +", str[*i]))
{
if (str[*i] == '#')
flags->form = '#';
else if (str[*i] == '0' && flags->padding == -1)
flags->padding = '0';
else if (str[*i] == '-')
flags->padding = '-';
else if (str[*i] == ' ' && flags->sign == -1)
flags->sign = ' ';
else if (str[*i] == '+')
flags->sign = '+';
*i = *i + 1;
}
}
static void ftp_fill_struct_width_prec(const char *str, int *i, t_flags *flags)
{
if (str[*i] >= '1' || str[*i] <= '9')
{
flags->width = ft_atoi(&str[*i]);
while (str[*i] >= '0' && str[*i] <= '9')
*i = *i + 1;
}
if (str[*i] == '.')
{
*i = *i + 1;
flags->prec = ft_atoi(&str[*i]);
while (str[*i] >= '0' && str[*i] <= '9')
*i = *i + 1;
}
}
static void ftp_fill_struct_length_mod(const char *str, int *i, t_flags *flags)
{
int j;
j = 0;
while (ft_strchr("hlqLjzZt", str[*i]))
{
if (j < 2)
flags->len_mod[j] = str[*i];
*i = *i + 1;
}
}