-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
51 lines (47 loc) · 1.16 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
#include "ft_printf.h"
//Proceso para dirigir al tipo de variable requerido.
int process_format(const char *format, va_list ap, int *i)
{
int count;
count = 0;
if (!format || !ap)
return (-1);
if (format[*i] != 37)
count += ft_putchar(format[(*i)++], 1);
else
{
if (format[*i + 1] == 'c')
count += print_characters(1, va_arg(ap, int));
else if (format[*i + 1] == 's')
count += print_str(format, va_arg(ap, char *));
else if (format[*i + 1] == 'd' || format[*i + 1] == 'i')
count += print_ints(1, va_arg(ap, int));
else if (format[*i + 1] == 'X' || format[*i + 1] == 'x'
|| format[*i + 1] == 'u')
count += print_hex(*i, format, va_arg(ap, unsigned int));
else if (format[*i + 1] == 'p')
count += ft_putaddr(1, va_arg(ap, unsigned long int));
else if (format[*i] == 37 && format[*i + 1] == 37)
count += ft_putchar('%', 1);
*i += 2;
}
return (count);
}
//Función principal.
int ft_printf(char const *format, ...)
{
va_list ap;
int i;
int count;
if (write(1, "", 0) == -1)
return (-1);
va_start(ap, format);
i = 0;
count = 0;
while (format[i])
{
count += process_format(format, ap, &i);
}
va_end(ap);
return (count);
}