-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
142 lines (131 loc) · 3.99 KB
/
ft_printf.c
File metadata and controls
142 lines (131 loc) · 3.99 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mbalon-s <mbalon-s@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/22 17:17:31 by mbalon-s #+# #+# */
/* Updated: 2019/03/01 15:25:02 by mbalon-s ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include "libftprintf.h"
static size_t update_mw_and_prec(const char *format,
t_specification *pspec)
{
const char *s;
unsigned long holder;
int wildcard;
s = format;
wildcard = *s == '*' ? 1 : 0;
s += wildcard ? 1 : ft_ulfromstr(s, &holder);
if (pspec->precision_set)
{
if (wildcard)
pspec->get_wildcard_precision = 1;
else
pspec->precision = holder;
pspec->wildcard_precision = wildcard;
}
else
{
if (wildcard)
pspec->get_wildcard_minwidth = 1;
else
pspec->minwidth = holder;
pspec->wildcard_minwidth = wildcard;
}
return (s - format);
}
static size_t get_specification(const char *format,
t_specification *pspec)
{
const char *s;
s = format;
while (ft_instr(*s, "-+ #hlLzj.0123456789*"))
{
pspec->align_left = *s == '-' ? 1 : pspec->align_left;
pspec->force_sign = *s == '+' ? 1 : pspec->force_sign;
pspec->force_spacing = *s == ' ' ? 1 : pspec->force_spacing;
pspec->force_zeroes = *s == '0' && !pspec->precision_set ?
1 : pspec->force_zeroes;
pspec->alt_print = *s == '#' ? 1 : pspec->alt_print;
pspec->short_short_mod = *s == 'h' && s[1] == 'h' ?
1 : pspec->short_short_mod;
pspec->short_mod = *s == 'h' ? 1 : pspec->short_mod;
pspec->long_long_mod = *s == 'l' && s[1] == 'l' ?
1 : pspec->long_long_mod;
pspec->long_mod = *s == 'l' ? 1 : pspec->long_mod;
pspec->long_double_mod = *s == 'L' ? 1 : pspec->long_double_mod;
pspec->size_t_mod = *s == 'z' ? 1 : pspec->size_t_mod;
pspec->intmax_t_mod = *s == 'j' ? 1 : pspec->intmax_t_mod;
pspec->precision_set = *s == '.' ? 1 : pspec->precision_set;
s += (ft_instr(*s, "0123456789*")) ? update_mw_and_prec(s, pspec) : 1;
}
s += ft_getspecificator(s, pspec);
return (s - format);
}
static void load_wildcards(t_specification *pspec, va_list ap)
{
int tmp;
if (pspec->get_wildcard_minwidth)
{
tmp = (int)va_arg(ap, int);
if (pspec->wildcard_minwidth)
pspec->minwidth = tmp;
}
if (pspec->get_wildcard_precision)
{
tmp = (int)va_arg(ap, int);
if (pspec->wildcard_precision)
pspec->precision = tmp;
}
if (pspec->minwidth < 0)
{
pspec->align_left = 1;
pspec->minwidth *= -1;
}
if (pspec->precision < 0)
pspec->precision_set = 0;
}
static size_t progress_buffer(const char *format,
t_smartstr *pbuf, va_list ap)
{
const char *s;
t_specification spec;
t_specification *pspec;
if (*format != '%')
{
s = ft_strchr(format, '%');
if (s == NULL)
s = ft_strchr(format, '\0');
ft_smartstrncat(pbuf, format, s - format);
return (s - format);
}
s = format + 1;
pspec = &spec;
ft_bzero(pspec, sizeof(spec));
s += get_specification(s, pspec);
load_wildcards(pspec, ap);
ft_getstrbyspec(spec, pbuf, ap);
return (s - format);
}
int ft_printf(const char *format, ...)
{
t_smartstr buffer;
ssize_t bytes;
va_list ap;
if (format == NULL)
return (-1);
va_start(ap, format);
ft_bzero(&buffer, sizeof(buffer));
while (*format != '\0')
format += progress_buffer(format, &buffer, ap);
va_end(ap);
bytes = write(1, buffer.str, buffer.len);
ft_flushsmartstr(&buffer);
return (bytes);
}