-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft_printf.c
143 lines (128 loc) · 3.32 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
132
133
134
135
136
137
138
139
140
141
142
143
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: angavrel <angavrel@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/28 19:18:44 by angavrel #+# #+# */
/* Updated: 2017/05/30 21:19:19 by angavrel ### ########.fr */
/* */
/* ************************************************************************** */
#include <ft_printf.h>
/*
** main function : printf will return the total len of chars displayed by printf
*/
int ft_printf(const char *format, ...)
{
t_printf p;
ft_bzero(&p, sizeof(p));
p.fd = 1;
p.format = (char *)format;
va_start(p.ap, format);
while (*p.format && p.len > -1)
{
if (*p.format == '%')
{
++p.format;
if (!*p.format || (*p.format == ' ' && (!p.format[1]
|| (!p.format[2] && p.format[1] == 'h'))))
break ;
else
parse_optionals(&p);
}
else if (++p.i)
buffer(&p, &*p.format, 1);
++p.format;
}
(p.len < 0) ? write(p.fd, p.buff, p.buffer_index - p.i)
: write(p.fd, p.buff, p.buffer_index);
va_end(p.ap);
return (p.len);
}
/*
** bonus function to specify a specific fd, similar to libc dprintf
*/
int ft_dprintf(int fd, const char *format, ...)
{
t_printf p;
ft_bzero(&p, sizeof(p));
p.fd = fd;
p.format = (char *)format;
va_start(p.ap, format);
while (*p.format && p.len > -1)
{
if (*p.format == '%')
{
++p.format;
if (!*p.format || (*p.format == ' ' && (!p.format[1]
|| (!p.format[2] && p.format[1] == 'h'))))
break ;
else
parse_optionals(&p);
}
else if (++p.i)
buffer(&p, &*p.format, 1);
++p.format;
}
(p.len < 0) ? write(p.fd, p.buff, p.buffer_index - p.i)
: write(p.fd, p.buff, p.buffer_index);
va_end(p.ap);
return (p.len);
}
/*
** sprintf returns the string
** Caution : intended for string < 64 (BUFF in define) characters.
*/
char *ft_sprintf(const char *format, ...)
{
t_printf p;
char *ret;
ft_bzero(&p, sizeof(p));
p.fd = 1;
p.format = (char *)format;
va_start(p.ap, format);
while (*p.format && p.len > -1)
{
if (*p.format == '%')
parse_optionals(&p);
else if (++p.i)
buffer(&p, &*p.format, 1);
++p.format;
}
p.buff[p.buffer_index + 1] = '\0';
if (!(ret = ft_strdup(p.buff)))
return (NULL);
va_end(p.ap);
return (ret);
}
/*
** function that displays pointer address
*/
void print_pointer_address(t_printf *p)
{
void *pointer;
pointer = va_arg(p->ap, void *);
p->f &= ~F_SHARP;
p->min_length -= (p->f & F_ZERO ? 2 : 0);
p->padding = (p->printed > p->min_length - 3) ? 0 :
p->min_length - 3 - p->printed;
p->f |= F_SHARP;
p->f |= F_POINTER;
p->printed = 0;
itoa_base_printf((__uintmax_t)pointer, 16, p);
}
/*
** function if no conversion specifier was found.
*/
void cs_not_found(t_printf *p)
{
if ((p->padding = p->min_length - 1) > 0)
{
padding(p, 0);
buffer(p, p->format, 1);
padding(p, 1);
}
else
buffer(p, p->format, 1);
}