-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintf_utils.c
95 lines (88 loc) · 3.07 KB
/
printf_utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* printf_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mpernia- <mpernia-@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/12 17:35:53 by mpernia- #+# #+# */
/* Updated: 2020/01/15 22:02:47 by mpernia- ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
#include "libft/libft.h"
int is_specifier(char a)
{
return (a == 'c' || a == 's' || a == 'p' || a == 'd' || a == 'i' ||
a == 'u' || a == 'x' || a == 'X' || a == '%');
}
int check_to_print(t_case *aux, va_list args)
{
if (aux->var_type == 'c' || aux->var_type == '%')
return (print_singlechar(aux, args));
else if (aux->var_type == 's')
return (print_s(aux, args));
else if (aux->var_type == 'd' || aux->var_type == 'i' ||
aux->var_type == 'u')
return (print_int_preffix(aux, args));
else if (aux->var_type == 'x' || aux->var_type == 'X')
return (print_x(aux, args));
else if (aux->var_type == 'p')
return (print_p(aux, args));
else
return (print_0(aux->flags));
}
t_case *check_wp(t_case *format, char *aux, va_list args)
{
format->width = (*aux == '*') ? va_arg(args, int) : ft_atoi(aux);
if (format->width < 0)
{
format->width *= -1;
format->left_align = 1;
format->zero_filler = 0;
}
if ((aux = ft_strchr(format->flags, '.')))
{
++aux;
format->precision = (*aux == '*') ? va_arg(args, int) : ft_atoi(aux);
format->zero_filler = 0;
}
return (format);
}
t_case *check_flags(t_case *format, va_list args)
{
int i;
i = -1;
while (*(format->flags + ++i))
{
if (*(format->flags + i) == '-')
format->left_align = 1;
if (*(format->flags + i) == '#')
format->hex_pref = 1;
if (*(format->flags + i) == '+')
format->plus_sign = 1;
if (*(format->flags + i) == ' ')
format->plus_space = 1;
if (*(format->flags + i) == '0')
format->zero_filler = 1;
if ((ft_isdigit(*(format->flags + i)) && *(format->flags + i) != '0')
|| (*(format->flags + i) == '*') || (*(format->flags + i) == '.'))
{
check_wp(format, format->flags + i, args);
break ;
}
}
return (format);
}
t_case *get_wp(t_case *format, int len)
{
format->precision = (ft_strchr(format->flags, '.') == 0 ? len :
format->precision);
if (format->var_type == 's' && format->precision >= 0)
format->precision = (format->precision < len) ? format->precision : len;
else
format->precision = (format->precision > len) ? format->precision : len;
format->width = (format->width > format->precision) ? format->width :
format->precision;
return (format);
}