-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_print_num.c
More file actions
129 lines (117 loc) · 2.62 KB
/
ft_print_num.c
File metadata and controls
129 lines (117 loc) · 2.62 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_print_num.c :+: :+: */
/* +:+ */
/* By: ivork <ivork@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2021/01/15 23:10:02 by ivork #+# #+# */
/* Updated: 2021/02/14 14:06:54 by ivork ######## odam.nl */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "ft_printf.h"
extern int g_count;
char *ft_conv_hexa(unsigned int x, char *base, char *str)
{
char hexadecinum[100];
int i;
int n;
i = 0;
while (x != 0)
{
n = x % 16;
hexadecinum[i] = base[n];
x /= 16;
i++;
}
n = 0;
while (i > 0)
{
str[n] = hexadecinum[i - 1];
n++;
i--;
}
str[n] = '\0';
return (str);
}
void ft_print_hexa(unsigned int x, t_flags flag)
{
char *base;
char *str;
if (flag.preci == true && flag.precision >= 0)
flag.zero = false;
if (flag.specifier == 'x')
base = "0123456789abcdef";
else
base = "0123456789ABCDEF";
str = (char*)malloc(sizeof(char) * 9);
if (str == NULL)
{
g_count = -1;
return ;
}
str = ft_conv_hexa(x, base, str);
if (x == 0 && (flag.preci == false || flag.precision < 0))
ft_print_pos("0", flag);
else
ft_print_pos(str, flag);
free(str);
}
void ft_print_unsi(long long x, t_flags flag)
{
char *str;
if (flag.preci == true && flag.precision >= 0)
flag.zero = false;
if (x == 0 && flag.preci == true && flag.precision == 0)
str = 0;
else
{
str = ft_uitoa(x);
if (str == NULL)
{
g_count = -1;
return ;
}
}
ft_print_pos(str, flag);
free(str);
}
void ft_conv_str(int x, t_flags flag)
{
char *str;
if (x == 0 && flag.preci == true && flag.precision == 0)
str = NULL;
else
{
str = ft_itoa(x);
if (str == NULL)
{
g_count = -1;
return ;
}
}
ft_print_pos(str, flag);
free(str);
}
void ft_print_int(int x, t_flags flag)
{
char *str;
if ((flag.preci == true && flag.precision >= 0) || flag.left_align == true)
flag.zero = false;
if (x >= 0)
ft_conv_str(x, flag);
else
{
x *= -1;
str = ft_uitoa(x);
if (str == NULL)
{
g_count = -1;
return ;
}
else
ft_print_neg(str, flag);
free(str);
}
}