-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_address.c
91 lines (82 loc) · 2.12 KB
/
print_address.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_address.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abkssiba <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/07 18:06:26 by abkssiba #+# #+# */
/* Updated: 2019/12/13 15:36:56 by abkssiba ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int print_resrt(const char *str, char *tmp, int len, t_flags flg)
{
int i;
int count;
count = 0;
i = 0;
if (flg.minus == 0)
{
count += apply_width(flg.width, len);
if (flg.precision != -1)
count += ft_putstr(tmp);
}
else
{
count += ft_putstr(tmp);
count += apply_width(flg.width, len);
i++;
}
if ((str[i] == 'p' && !flg.minus))
return (count + ft_putstr(tmp));
else if (i == 0)
return (count + ft_putstr(tmp));
return (count);
}
static int print_result(const char *str, char *res, t_flags flg)
{
int count;
char *tmp;
int len;
count = 0;
tmp = res;
len = ft_strlen(tmp);
count += print_resrt(str, tmp, len, flg);
free(res);
return (count);
}
static char *create_adr(char *str)
{
char *res;
int len;
int i;
int j;
j = 0;
i = 2;
len = ft_strlen(str);
if (!(res = (char*)malloc(len + 3)))
return (NULL);
res[0] = '0';
res[1] = 'x';
while (i < len + 2)
{
res[i] = str[j];
j++;
i++;
}
res[i] = '\0';
return (res);
}
int print_address(va_list *arg, t_flags flg)
{
uintmax_t tmp;
char *res;
char *tmp_s;
tmp = va_arg(*arg, uintmax_t);
res = to_hexa_uint((uintmax_t)tmp);
tmp_s = res;
res = create_adr(res);
free(tmp_s);
return (print_result(res, res, flg));
}