-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_pointer.c
More file actions
83 lines (76 loc) · 2.28 KB
/
ft_pointer.c
File metadata and controls
83 lines (76 loc) · 2.28 KB
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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_pointer.c :+: :+: */
/* +:+ */
/* By: greed <greed@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2019/11/18 15:37:14 by greed #+# #+# */
/* Updated: 2019/11/21 13:05:33 by greed ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "libftprintf.h"
void ft_print_pointer(t_conv *conv, va_list a_list, int *lv)
{
unsigned long ptr;
ptr = (unsigned long)va_arg(a_list, void*);
conv->numlen = (ft_ptr_size(ptr) + 2);
if (conv->precision == -2 ||
(conv->precision < conv->numlen && conv->precision != 0))
conv->precision = conv->numlen;
if (conv->left)
{
ft_putstr_c_fd("0x", 1, 2, lv);
ft_pad_width(conv->precision,
conv->numlen - ((conv->precision > conv->numlen) ? 2 : 0), '0', lv);
if (conv->precision)
ft_ptr_res_fd(ptr, lv);
}
(!conv->left && conv->padzero) ? ft_pad_width(conv->width, conv->precision,
'0', lv) : ft_pad_width(conv->width, conv->precision, ' ', lv);
if (!(conv->left))
{
ft_putstr_c_fd("0x", 1, 2, lv);
ft_pad_width(conv->precision,
conv->numlen - ((conv->precision > conv->numlen) ? 2 : 0), '0', lv);
if (conv->precision)
ft_ptr_res_fd(ptr, lv);
}
}
unsigned long ft_ptr_size(unsigned long ptr)
{
unsigned long tmp;
unsigned long size;
size = 0;
tmp = ptr;
if (ptr == 0)
size++;
while (tmp)
{
tmp = tmp / 16;
size++;
}
return (size);
}
void ft_ptr_res_fd(unsigned long ptr, int *lv)
{
unsigned long res;
unsigned long power;
char *hex;
hex = "0123456789abcdef";
res = ptr;
power = 1;
while (res / 16)
{
res /= 16;
power *= 16;
}
res = ptr;
while (power)
{
ft_putchar_c_fd(hex[ptr / power], 1, lv);
ptr %= power;
power /= 16;
}
}