-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf_format.c
108 lines (100 loc) · 3.15 KB
/
ft_printf_format.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_format.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: guilmira <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/06/20 11:33:29 by guilmira #+# #+# */
/* Updated: 2021/09/23 08:37:59 by guilmira ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/** STRUCTURE of printf
* %[parameter][flags][width][.precision][length]type
* Evaluates the followinf flags:
* '.' // '#' // '+' // '-' // ' ' // '0' // '*' */
void get_flags(char *str, t_flag *flag, va_list x)
{
int i;
i = 0;
if (str[i] == '.')
return ;
if (str[i] == '0' && !flag->zerofilled && str[i + 1] != '-')
{
flag->zerofilled++;
if (str[i + 1] == '*')
flag->zerofilled_total_digits = va_arg(x, int);
else
flag->zerofilled_total_digits = \
get_number_from_string(&(str)[i + 1]);
}
if (str[i] == ' ')
flag->invisible_sign++;
if (str[i] == '+')
flag->plus_sign++;
if (str[i] == '#')
flag->alternative++;
}
/** PURPOSE : in case that the alignnment is < 0, check and correct. **/
static void correct_alignment(t_flag *flag)
{
if (flag->alignment_total_spaces < 0)
{
flag->alignment_total_spaces *= -1;
flag->alignment_sign = '-';
}
if (flag->alignment_total_spaces == 0)
flag->alignment = 0;
}
/** STRUCTURE of printf
* %[parameter][flags][width][.precision][length]type
* Width and allignment are synonyms */
void get_allignment(char *str, t_flag *flag, va_list x)
{
int i;
i = -1;
while (str[++i])
{
if (str[i] == '.')
break ;
if (str[i] == '-')
flag->alignment_sign = '-';
if (ft_isdigit(str[i]) && !(flag->alignment))
{
flag->alignment++;
flag->alignment_total_spaces = get_number_from_string(&(str)[i]);
}
if (str[i] == '*' && !(flag->alignment))
{
flag->alignment++;
flag->alignment_total_spaces = va_arg(x, int);
}
if (flag->alignment_total_spaces < 1)
correct_alignment(flag);
}
}
/** STRUCTURE of printf
* %[parameter][flags][width][.precision][length]type
* */
void get_precision(char *str, t_flag *flag, va_list x)
{
flag->precision++;
if (ft_isdigit(str[0]))
flag->precision_total_digits = get_number_from_string(str);
else if (str[0] == '*')
flag->precision_total_digits = va_arg(x, int);
else
flag->precision = -1;
if (flag->precision_total_digits < 0)
flag->precision = 0;
}
/** PURPOSE : gets a string that only contains format values.
* Format values are contained between % and converter [i. e: d, s, i...] */
char *get_flag_string(char *str)
{
if (ft_strchr_plus(str, CONVERTERS))
return (ft_substr(str, 0, ft_strchr_plus(str, CONVERTERS) - str));
else
return (NULL);
}