-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_pointer_format.c
More file actions
68 lines (63 loc) · 2.3 KB
/
ft_pointer_format.c
File metadata and controls
68 lines (63 loc) · 2.3 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_pointer_format.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mbalon-s <mbalon-s@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/24 21:17:46 by mbalon-s #+# #+# */
/* Updated: 2019/02/28 21:07:17 by mbalon-s ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <stdarg.h>
#include "libftprintf.h"
static void format_pointer(unsigned long long int nbr,
t_specification spec,
char *str)
{
size_t i;
size_t digits;
ft_memset(str, ' ', sizeof(char) * spec.minwidth);
i = spec.align_left ? 0 : spec.minwidth - spec.precision;
digits = spec.align_left ? spec.precision : spec.minwidth;
while (digits != i)
{
digits--;
str[digits] = (nbr & 0xF) + '0';
if (str[digits] > '9')
str[digits] = str[digits] - '0' - 10 + 'a';
nbr /= 16;
}
i = !(spec.align_left || spec.force_zeroes) ? digits : 0;
str[i] = '0';
str[i + 1] = 'x';
}
size_t ft_pointer_format(char **pdst, t_specification spec,
va_list ap)
{
unsigned long long int nbr;
int num_digits;
char *str;
nbr = (unsigned long long)va_arg(ap, void *);
num_digits = ft_count_digits_unsigned(nbr, 16);
if (nbr == 0)
if (spec.precision_set && spec.precision == 0)
spec.precision = 2;
else
spec.precision = (!spec.precision_set || spec.precision < 3) ?
3 : spec.precision + 2;
else if (!spec.precision_set || spec.precision < num_digits + 2)
spec.precision = num_digits + 2;
else
spec.precision += 2;
if (spec.minwidth < spec.precision)
spec.minwidth = spec.precision;
str = (char *)malloc(sizeof(char) * spec.minwidth + 1);
if (str == NULL)
return (0);
str[spec.minwidth] = '\0';
format_pointer(nbr, spec, str);
*pdst = str;
return (spec.minwidth);
}