-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
118 lines (109 loc) · 2.66 KB
/
ft_printf.c
File metadata and controls
118 lines (109 loc) · 2.66 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_printf.c :+: :+: */
/* +:+ */
/* By: novan-ve <marvin@codam.nl> +#+ */
/* +#+ */
/* Created: 2019/12/16 13:24:22 by novan-ve #+# #+# */
/* Updated: 2019/12/27 14:54:56 by novan-ve ######## odam.nl */
/* */
/* ************************************************************************** */
/*
** cspdiuxX% -0.*
*/
#include "ft_printf.h"
void ft_printc(t_print *p)
{
int i;
i = 0;
p->tmplen++;
if (p->just == 'r')
ft_putchar_fd((unsigned int)va_arg(p->args, char*), 1);
while (i < (p->tmpwidth - 1))
{
ft_putchar_fd(p->padchar, 1);
i++;
}
if (p->just == 'l')
ft_putchar_fd((unsigned int)va_arg(p->args, char*), 1);
}
void ft_format2(t_print *p)
{
p->i++;
if (p->format[p->i] == '-' || p->format[p->i] == '.')
ft_calcwidth(p);
else if (p->format[p->i] >= '0' && p->format[p->i] <= '9')
ft_calcwidth(p);
else if (p->format[p->i] == '*')
ft_calcwidth(p);
}
void ft_format(t_print *p)
{
ft_format2(p);
if (p->format[p->i] == 's')
ft_prints(p);
else if (p->format[p->i] == 'i' || p->format[p->i] == 'd')
ft_printi(p);
else if (p->format[p->i] == 'c')
ft_printc(p);
else if (p->format[p->i] == 'p')
ft_printp(p);
else if (p->format[p->i] == 'u')
ft_printu(p);
else if (p->format[p->i] == '%')
ft_printper(p);
else if (p->format[p->i] == 'x')
ft_printx(p, 0);
else if (p->format[p->i] == 'X')
ft_printx(p, 1);
else
p->error = 1;
}
void ft_resetlst(t_print *p, int i)
{
if (i == 0)
{
p->i = 0;
p->len = 0;
p->padchar = ' ';
p->width = 0;
p->tmpwidth = 0;
}
else if (i == 1)
{
p->i++;
p->width += p->tmpwidth;
p->tmpwidth = 0;
p->len += p->tmplen;
}
p->tmplen = 0;
p->error = 0;
p->minus = 0;
p->prec = -1;
p->just = 'l';
}
int ft_printf(const char *format, ...)
{
t_print p;
ft_resetlst(&p, 0);
p.format = (char*)format;
va_start(p.args, format);
while (p.format[p.i] != '\0')
{
if (p.format[p.i] == '%')
ft_format(&p);
else
{
ft_putchar_fd(p.format[p.i], 1);
p.len++;
}
if (p.error == 1)
return (-1);
ft_resetlst(&p, 1);
if (p.len < p.width)
p.len = p.width;
}
va_end(p.args);
return (p.len);
}