-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
131 lines (121 loc) · 3.96 KB
/
ft_printf.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: guilmira <guilmira@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/06/12 08:46:55 by guilmira #+# #+# */
/* Updated: 2021/09/23 08:33:02 by guilmira ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/** PURPOSE : print variable on screen.
* 1. Takes into account the details of the flag and converter.
* 2. Selects function. */
static void variable_printer(t_flag *flag, va_list x)
{
int lenght;
lenght = 0;
if (flag->signal == 'c')
print_char(va_arg(x, int), flag);
else if (flag->signal == '%')
print_char('%', flag);
else if (flag->signal == 's')
print_string(va_arg(x, char *), flag);
else if (flag->signal == 'p')
print_pointer((unsigned long long) va_arg(x, void *), flag);
else if (flag->signal == 'i' || flag->signal == 'd')
print_integer(va_arg(x, int), flag);
else if (flag->signal == 'u')
print_integer_unsigned(va_arg(x, unsigned int), flag);
else if (flag->signal == 'x')
print_hexa(va_arg(x, unsigned int), flag, HEXADECIMAL, PREFIX);
else if (flag->signal == 'X')
print_hexa(va_arg(x, unsigned int), flag, \
HEXADECIMAL_MAYUS, PREFIX_MAYUS);
}
/** PURPOSE : identifies the signal previous to the converter.
* 1. Iterative on the flag while is not converter (i. e: i, d, s, c ...)
* 2. Registers in struct flag details. */
static void identify_flag(char *str, t_flag *flag, va_list x)
{
char *precision;
if (!str[0])
return ;
get_flags(str, flag, x);
if (flag->alternative || flag->invisible_sign || flag->plus_sign)
get_flags(str + 1, flag, x);
if (!flag->zerofilled)
get_allignment(str, flag, x);
precision = ft_strchr(str, '.');
if (precision)
get_precision(++precision, flag, x);
}
/** STRUCTURE PF = %[parameter][flags][width][.precision][length]type */
static void advance_string(char **str, char *new_position)
{
if (new_position)
*str = new_position;
}
/** PURPOSE : to read the string passed as an argument and identify flag.
* 1. Reads the string and prints single characters.
* 2. Stops at '%'
* 3. Applies 'identify_flag' to obtain flag details. */
static void read_mainstring(char **str, t_flag *flag, va_list x)
{
int i;
char *flag_string;
char *converter;
flag->signal = 0;
i = -1;
if (!(*str)[0])
return ;
while ((*str)[++i])
{
if ((*str)[i] == '%')
break ;
else
pf_putchar_fd((*str)[i], 1, flag);
}
if ((*str)[i] == '%' && ft_strchr_plus(&(*str)[i + 1], CONVERTERS))
{
converter = ft_strchr_plus(&(*str)[i + 1], CONVERTERS);
flag_string = get_flag_string(&(*str)[i + 1]);
identify_flag(flag_string, flag, x);
if (flag_string)
free (flag_string);
flag->signal = converter[0];
advance_string(str, (ft_strchr_plus(&(*str)[i + 1], CONVERTERS) + 1));
}
}
/** PURPOSE : ft_printf will recreate the behavior of printf.
* Recieves a varying number of arguments and types. Then prints. Iterative.
* 1. Read argument.
* 2. Define the flag and identify converter.
* 3. Print the argument. */
int ft_printf(const char *c, ...)
{
va_list x;
t_flag *flag;
char *ptr;
int result;
flag = ft_calloc(1, sizeof(*flag));
init_flag(flag);
flag->counter = 0;
ptr = (char *) c;
if (!c)
return (0);
va_start(x, c);
while (flag->signal)
{
init_flag(flag);
read_mainstring(&ptr, flag, x);
if (flag->signal)
variable_printer(flag, x);
}
va_end(x);
result = flag->counter;
free(flag);
return (result);
}