-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhex_type.c
More file actions
115 lines (105 loc) · 2.56 KB
/
Copy pathhex_type.c
File metadata and controls
115 lines (105 loc) · 2.56 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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hex_type.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pgomez-a <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/11 08:44:17 by pgomez-a #+# #+# */
/* Updated: 2021/03/15 08:58:32 by pgomez-a ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void man_zero(char **itoa, char **pre)
{
if (**pre == '0' || **pre == '\0')
{
free(*itoa);
(*itoa) = ft_strdup("");
}
else if (**pre != '-')
(**itoa) = ' ';
}
static void man_neg_pre(char **itoa, char **pre)
{
char *aux;
char *temp;
int num_p;
int len_i;
aux = ft_strdup(*itoa + 1);
num_p = ft_atoi(*pre);
len_i = ft_strlen(aux);
while (num_p > len_i)
{
temp = aux;
aux = ft_strjoin("0", aux);
free(temp);
num_p--;
}
temp = *itoa;
(*itoa) = ft_strjoin("-", aux);
free(temp);
free(aux);
}
static int hex_pre(char **itoa, char **pre)
{
char *temp;
int num_p;
int len_i;
if (ft_isdigit(**itoa) && ft_atoi(*itoa) == 0
&& (**pre <= '0' || **pre > '9'))
man_zero(itoa, pre);
else if (**itoa == '-')
man_neg_pre(itoa, pre);
else
{
num_p = ft_atoi(*pre);
len_i = ft_strlen(*itoa);
while (num_p > len_i)
{
temp = *itoa;
(*itoa) = ft_strjoin("0", *itoa);
free(temp);
num_p--;
}
}
if (ft_strchr(*pre, '-'))
return (0);
return (1);
}
static void convert_to_lower(unsigned int num, char c, char **hextoa)
{
int len;
ft_hextoa(num, hextoa);
if (c == 'x')
{
len = 0;
while ((*hextoa)[len])
{
(*hextoa)[len] = ft_tolower((*hextoa)[len]);
len++;
}
}
}
void pf_find_hex(va_list *ap, char c, char **width, int **result)
{
char *hextoa;
char *pre;
int verif;
unsigned int num;
num = va_arg(*ap, unsigned int);
convert_to_lower(num, c, &hextoa);
pre = ft_strchr(*width, '.');
verif = 0;
if (pre)
{
(*pre) = '\0';
pre++;
verif = hex_pre(&hextoa, &pre);
}
if (ft_strchr(*width, '-'))
man_neg_width(&hextoa, width, result);
else
man_pos_width(verif, &hextoa, width, result);
free(hextoa);
}