-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf.c
77 lines (71 loc) · 1.87 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dalves-p <dalves-p@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/07/02 19:08:22 by dalves-p #+# #+# */
/* Updated: 2021/08/18 10:04:14 by dalves-p ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include "ft_printf.h"
#include "libft/libft.h"
void init_struct(t_spec *spec)
{
spec->negative = 0;
spec->minus = 0;
spec->zero = 0;
spec->hash = 0;
spec->space = 0;
spec->plus = 0;
spec->dot = 0;
spec->precision = 0;
spec->width = 0;
}
char *get_var(char *str, va_list *args, t_spec *spec)
{
int i;
char *var;
i = 0;
init_struct(spec);
while (str[i] != 'c' && str[i] != 's' && str[i] != 'p' && str[i] != 'd'
&& str[i] != 'i' && str[i] != 'u' && str[i] != 'x' && str[i] != 'X'
&& str[i] != '%')
i++;
var = ft_substr(str, 0, i);
spec->spec = str[i];
get_spec(spec, args, var);
while (i >= 0)
{
str++;
i--;
}
return (str);
}
int ft_printf(const char *str, ...)
{
va_list args;
static t_spec spec;
va_start(args, str);
spec.count = 0;
while (*str)
{
if (*str == '%')
{
str++;
str = get_var((char *)str, &args, &spec);
if (!str)
break ;
}
else
{
write (1, &*str, 1);
str++;
spec.count++;
}
}
va_end(args);
return (spec.count);
}