-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_calc.c
136 lines (127 loc) · 2.96 KB
/
ft_calc.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
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
130
131
132
133
134
135
136
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_calc.c :+: :+: */
/* +:+ */
/* By: novan-ve <novan-ve@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2019/12/18 12:27:09 by novan-ve #+# #+# */
/* Updated: 2019/12/27 15:39:28 by novan-ve ######## odam.nl */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_strrev(t_print *p, int len)
{
char tmp[len];
int i;
int j;
i = 0;
j = 0;
while (p->hex[i] != '\0')
{
tmp[i] = p->hex[i];
i++;
}
tmp[i] = '\0';
i--;
while (i >= 0)
{
p->hex[j] = tmp[i];
i--;
j++;
}
p->hex[j] = '\0';
p->tmplen = ft_strlen(p->hex);
}
void ft_dec_to_hex2(t_print *p, unsigned long dec, int x, int len)
{
unsigned long remainder;
int i;
i = 0;
if (dec == 4294967296 && len == 16)
{
dec = 0;
i = 1;
}
while (dec != 0)
{
remainder = dec % 16;
if (remainder < 10)
p->hex[i] = 48 + remainder;
else
p->hex[i] = x + remainder;
dec /= 16;
i++;
}
p->hex[i] = '\0';
ft_strrev(p, len);
}
void ft_dec_to_hex(t_print *p, unsigned long dec, int x, int len)
{
if (x == 0)
x = 87;
else if (x == 1)
x = 55;
if (dec != 0 || p->var != 'p')
ft_dec_to_hex2(p, dec, x, len);
else
{
p->hex[0] = '\0';
p->tmplen = 5;
}
}
void ft_calcwidth2(t_print *p, int x, int j)
{
if (x == 0)
{
p->var = p->format[j];
if (p->minus == 1)
{
p->just = 'r';
p->padchar = ' ';
}
}
else if (x == 1 && p->format[p->i] == '.')
{
j = 0;
p->padchar = ' ';
p->prec = 0;
while (ft_isalpha(p->format[p->i]) == 0 && p->format[p->i] != '\0')
{
if (p->format[p->i] == '%')
break ;
if (p->format[p->i] == '*')
p->prec = va_arg(p->args, int);
if (p->format[p->i] >= '0' && p->format[p->i] <= '9')
p->prec = p->prec * 10 + p->format[p->i] - '0';
p->i++;
}
}
}
void ft_calcwidth(t_print *p)
{
int j;
j = p->i;
while (ft_isalpha(p->format[j]) == 0 && p->format[j] != '\0')
{
if (p->format[j] == '0' && p->format[j - 1] == '%')
p->padchar = '0';
else if (p->format[j] == '-')
p->minus = 1;
j++;
}
ft_calcwidth2(p, 0, j);
while (ft_isalpha(p->format[p->i]) == 0 && p->format[p->i] != '\0')
{
if (p->format[p->i] == '%')
break ;
if (p->format[p->i] == '*')
p->tmpwidth = va_arg(p->args, int);
if (p->format[p->i] == '.')
break ;
if (p->format[p->i] >= '0' && p->format[p->i] <= '9')
p->tmpwidth = p->tmpwidth * 10 + p->format[p->i] - '0';
p->i++;
}
ft_calcwidth2(p, 1, j);
}