-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.c
109 lines (101 loc) · 3.2 KB
/
format.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* format.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kbelov <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/12 15:34:34 by kbelov #+# #+# */
/* Updated: 2019/11/08 20:41:53 by kbelov ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void reset_format(t_format *f)
{
f->minus = 0;
f->plus = 0;
f->space = 0;
f->hash = 0;
f->null = 0;
f->number = 0;
f->width = 0;
f->precision = -1;
f->length = 0;
f->specifier = 'a';
}
void get_d_str(t_format *f, va_list *ap, char **str)
{
if (f->length == 1)
*str = ft_strdup(ft_hitoa((short int)va_arg(*ap, int)));
else if (f->length == 2)
*str = ft_strdup(ft_hhitoa((signed char)va_arg(*ap, int)));
else if (f->length == 3)
*str = ft_strdup(ft_litoa(va_arg(*ap, long int)));
else if (f->length == 4 || f->length == 5)
*str = ft_strdup(ft_llitoa(va_arg(*ap, long long int)));
else if (f->length == 7)
*str = ft_strdup(ft_jitoa(va_arg(*ap, intmax_t)));
else if (f->length == 9)
*str = ft_strdup(ft_zitoa(va_arg(*ap, size_t)));
else
*str = ft_strdup(ft_itoa(va_arg(*ap, int)));
if (!(*str))
{
*str = malloc(sizeof(char) * 7);
*str = "(null)";
}
}
void get_u_str(t_format *f, va_list *ap, char **str)
{
if (f->length == 1)
*str = ft_strdup(ft_utoa((unsigned short)va_arg(*ap, unsigned int)));
else if (f->length == 2)
*str = ft_strdup(ft_utoa((unsigned char)va_arg(*ap, unsigned int)));
else if (f->length == 3)
*str = ft_strdup(ft_utoa(va_arg(*ap, unsigned long int)));
else if (f->length == 4 || f->length == 5)
*str = ft_strdup(ft_utoa(va_arg(*ap, unsigned long long)));
else if (f->length == 7)
*str = ft_strdup(ft_jitoa(va_arg(*ap, long long int)));
else if (f->length == 9)
*str = ft_strdup(ft_zitoa(va_arg(*ap, long long int)));
else
*str = ft_strdup(ft_utoa(va_arg(*ap, unsigned int)));
if (!(*str))
{
*str = malloc(sizeof(char) * 7);
*str = "(null)";
}
}
void apply_precision(t_format *f, int *len, char **str)
{
char *p_pad;
int pad_len;
char *first;
first = malloc(sizeof(char) * 2);
first = ft_strncpy(first, *str, 1);
if (SIGN(*str[0]) ? f->precision >= *len : f->precision > *len)
{
pad_len = f->precision - *len;
*len = f->precision;
if (SIGN(*str[0]))
pad_len++;
p_pad = ft_strnew(pad_len);
ft_memset(p_pad, '0', pad_len);
if (SIGN(*str[0]))
add_sign(str, first, p_pad, len);
else
*str = ft_strjoin(p_pad, *str);
ft_strdel(&p_pad);
}
ft_strdel(&first);
}
void add_sign(char **str, char *first, char *p_pad, int *len)
{
char *tmp;
tmp = ft_strjoin(first, p_pad);
(*str)++;
*str = ft_strjoin(tmp, *str);
(*len)++;
ft_strdel(&tmp);
}