-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
91 lines (84 loc) · 2.56 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_printf.c :+: :+: */
/* +:+ */
/* By: rbakker <rbakker@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2019/12/10 15:53:47 by rbakker #+# #+# */
/* Updated: 2020/01/13 12:42:28 by rbakker ######## odam.nl */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void print_charachters(const char *str, int *spot, t_format *format)
{
while (str[*spot] != '\0')
{
if (str[*spot] != '%')
{
write_function(str[*spot], format);
*spot = *spot + 1;
}
if (str[*spot] == '%')
return ;
}
return ;
}
void reset_struct(t_format *format)
{
format->min_flag = 0;
format->zero_flag = 0;
format->width = 0;
format->precision = 0;
format->precision_nb = 0;
format->specifier = 0;
}
void reset_output(t_format *format)
{
format->output = 0;
}
void generate_output(t_format *format, va_list arguments)
{
if (format->specifier == 'c')
charachter_output(format, arguments);
if (format->specifier == 's')
string_output(format, arguments);
if (format->specifier == 'p')
pointer_output(format, arguments);
if (format->specifier == 'd' || format->specifier == 'i')
signed_integer_output(format, arguments, 0);
if (format->specifier == 'u')
unsigned_integer_output(format, arguments);
if (format->specifier == 'x')
hexa_lower_output(format, arguments);
if (format->specifier == 'X')
hexa_upper_output(format, arguments);
if (format->specifier == '%')
percentage_output(format);
}
int ft_printf(const char *str, ...)
{
int spot;
va_list arguments;
t_format format;
spot = 0;
va_start(arguments, str);
reset_output(&format);
while (str[spot] != '\0')
{
if (str[spot] == '%')
{
spot++;
reset_struct(&format);
scan_flags(str, &spot, &format, 0);
scan_wildcard(str, &spot, &format, arguments);
scan_specifier(str, &spot, &format, 0);
handeling_negatives(&format);
generate_output(&format, arguments);
}
else
print_charachters(str, &spot, &format);
}
va_end(arguments);
return (format.output);
}