-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
149 lines (137 loc) · 3.41 KB
/
Copy pathft_printf.c
File metadata and controls
149 lines (137 loc) · 3.41 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: stoupin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/04/24 11:17:41 by stoupin #+# #+# */
/* Updated: 2017/04/24 14:07:45 by stoupin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
t_assoc g_assoc[N_ASSOC] = {
{'%', &ft_print_percent},
{'s', &ft_print_string},
{'d', &ft_print_int},
{'p', &ft_print_ptr},
{'S', &ft_print_string_unicode},
{'D', &ft_print_long_int},
{'i', &ft_print_int},
{'o', &ft_print_octal},
{'O', &ft_print_long_octal},
{'u', &ft_print_uint},
{'U', &ft_print_ulong_int},
{'x', &ft_print_hex},
{'X', &ft_print_hex_uppercase},
{'c', &ft_print_char},
{'C', &ft_print_char_unicode},
{'f', &ft_print_float},
{'F', &ft_print_float},
{'e', &ft_print_float_exp},
{'E', &ft_print_float_bigexp}
};
static int conversion_assoc(t_buf *buffer, va_list *ap,
char conv_spec, t_par *p)
{
int i;
int found;
int len;
len = 0;
found = 0;
i = 0;
while (i < N_ASSOC)
{
if (g_assoc[i].conv_spec == conv_spec)
{
len = (*(g_assoc[i].f))(buffer, ap, p);
found = 1;
}
i++;
}
if (!found)
len = pad_left(buffer, p->width1, p->flags, 1)
+ buf_putchar(buffer, conv_spec)
+ pad_right(buffer, p->width1, p->flags, 1);
return (len);
}
static void read_params(const char *format, int *i, t_par *p, va_list *ap)
{
char c;
char c_next;
int advance;
p->flags = 0;
p->modifiers = 0;
parse_flags(format, i, &(p->flags));
parse_width(format, i, p, ap);
advance = 1;
while (advance > 0)
{
c = format[*i];
c_next = format[*i + 1];
advance = parse_modifier(c, c_next, &(p->modifiers));
(*i) += advance;
}
parse_flags(format, i, &(p->flags));
}
static int handle_conversion(t_buf *buffer, int *i,
const char *format, va_list *ap)
{
int len;
t_par p;
len = 0;
if (format[*i] == '\0')
return (0);
(*i)++;
read_params(format, i, &p, ap);
if (format[*i] == '\0')
len = 0;
else
{
if (p.width2 >= 0 && format[*i] == 'd')
p.flags &= ~FLAG_ZERO;
if (p.flags & FLAG_PLUS)
p.flags &= ~FLAG_SPACE;
len = conversion_assoc(buffer, ap, format[*i], &p);
}
return (len);
}
static int do_the_job(t_buf *buffer, const char *format, va_list *ap)
{
int i;
int pos;
int len;
int total;
int end;
total = 0;
pos = 0;
end = 0;
i = 0;
while (!end)
{
if (format[i] == '%' || format[i] == '\0')
{
end = (format[i] == '\0');
total += buf_write(buffer, &(format[pos]), i - pos);
if ((len = handle_conversion(buffer, &i, format, ap)) == -1)
return (-1);
total += len;
end = (format[i] == '\0');
pos = i + 1;
}
i++;
}
return (total);
}
int ft_printf(const char *format, ...)
{
va_list ap;
t_buf buffer;
int len;
buffer.i = 0;
va_start(ap, format);
len = do_the_job(&buffer, format, &ap);
if (len >= 0)
buf_print(&buffer);
return (len);
}