-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_cspf.c
116 lines (105 loc) · 3.23 KB
/
format_cspf.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* format_cspf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jfelty <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/11 13:09:09 by jfelty #+# #+# */
/* Updated: 2019/11/06 23:14:54 by jfelty ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int format_c(t_format *fmt, va_list args)
{
char *c;
char *pad;
c = NULL;
if (fmt->type != '%')
c = ft_fillstrnew(1, va_arg(args, int));
else
c = ft_fillstrnew(1, '%');
if (!(fmt->has_width))
fmt->width = 1;
pad = ft_fillstrnew(fmt->width - 1, ' ');
fmt->retstr = join_pad(c, pad, fmt->flag->minus);
free_strings(&c, &pad, NULL);
return (0);
}
int format_s(t_format *fmt, va_list args)
{
char *s;
char *pad;
if (!(s = ft_strdup(va_arg(args, char *))))
{
fmt->retstr = ft_strdup("(null)");
return (0);
}
if (fmt->has_prec)
s[fmt->prec] = '\0';
pad = ft_fillstrnew(fmt->width - ft_strlen(s), ' ');
fmt->retstr = join_pad(s, pad, fmt->flag->minus);
free_strings(&s, &pad, NULL);
return (0);
}
int format_p(t_format *fmt, va_list args)
{
uint64_t num;
char *ret;
char *pad;
char *p;
int chlen;
p = NULL;
num = va_arg(args, uint64_t);
ret = (fmt->prec == 0 && num == 0) ? ft_strnew(0) : \
ft_ull_itoa_base(num, 16, 0);
chlen = ft_largest_int(fmt->prec, ft_strlen(ret));
if (!fmt->has_width)
fmt->width = ft_strlen(ret) + 2;
pad = ft_fillstrnew(fmt->width - chlen - 2, ' ');
while ((int)ft_strlen(ret) < chlen && (int)ft_strlen(ret) < fmt->prec)
ret = ft_push_to_str_front(ret, '0');
fmt->retstr = join_pad(\
p = ft_strjoin("0x", ret), pad, fmt->flag->minus);
free_strings(&ret, &pad, &p);
return (0);
}
char *get_decimal(long double num, int prec)
{
int i;
uint64_t first;
long double second;
int check;
i = -1;
first = (uint64_t)num;
second = num - (long double)first;
prec = (prec == -1) ? 6 : prec;
while (++i <= prec)
second *= 10;
check = second;
second /= 10;
if (check % 10 > 4)
second += 1;
return (ft_ull_itoa_base((uint64_t)second, 10, 0));
}
int format_f(t_format *fmt, va_list args)
{
long double num;
char *first;
char *second;
char *pad;
int chlen;
if (fmt->lmod == 5)
num = va_arg(args, long double);
else
num = (long double)va_arg(args, double);
first = ft_ull_itoa_base(ft_makepos((uint64_t)num), 10, 0);
chlen = ft_largest_int(fmt->prec, 6);
chlen += ft_strlen(first) + 1 + has_lead(fmt, num);
pad = ft_fillstrnew(fmt->width - chlen - has_lead(fmt, num), ' ');
second = get_decimal(num > 0 ? num : -num, fmt->prec);
first = f_help(fmt, num, &first, second);
fmt->retstr = join_pad(first, pad, fmt->flag->minus);
free_strings(&first, &second, &pad);
return (0);
}