-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_o.c
120 lines (112 loc) · 2.55 KB
/
ft_o.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_o.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: oltkache <oltkache@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/02/14 16:23:43 by oltkache #+# #+# */
/* Updated: 2018/02/16 17:37:22 by oltkache ### ########.fr */
/* */
/* ************************************************************************** */
#include "printf.h"
char *ft_octal(unsigned long long a, int *l)
{
int b;
char *str;
int i;
char *new;
i = 0;
str = ft_strnew(ft_num_len_u(a, 8));
if (a == 0)
str[i] = 0 + '0';
while (a >= 1)
{
b = a % 8;
if (b < 8 && a > 7)
if (b < 8)
str[i++] = b + '0';
if (a < 8)
str[i++] = a + '0';
a /= 8;
}
if (l != NULL)
*l += ft_strlen(str);
new = ft_strrev(str);
ft_strdel(&str);
return (new);
}
void ft_print_o(int *o, int *s, unsigned long long a, char *num)
{
int l;
l = ft_strlen(num);
if (a == 0 && o[4] != -1)
l = 0;
if (o[6] == 1)
l += 1;
if (o[6] == 1 && (o[1] != 1 || a == 0))
write(1, "0", 1);
if (o[4] != -1)
if (o[4] > l)
{
ft_spaces(o[4] - l, 2);
*s += (o[4] - l);
}
if (!(a == 0 && o[4] != -1) && !(a == 0 && o[6] != 0))
{
ft_putstr(num);
*s += ft_strlen(num);
}
if (!(a == 0 && o[6] != 0))
ft_strdel(&num);
}
void ft_space_o(int *o, int *s, int l)
{
if (o[3] - l > 0 && o[3] > o[4])
{
if (o[1] == 1 && o[0] != 1)
{
if (o[6] == 1)
write(1, "0", 1);
ft_spaces(o[3] - l, 2);
*s += (o[3] - l);
}
else
{
if (o[4] > l && (*s += (o[3] - o[4])))
ft_spaces(o[3] - o[4], 1);
else
{
ft_spaces(o[3] - l, 1);
*s += (o[3] - l);
}
}
}
}
void ft_o(char *str, unsigned long long a, int j, int *s)
{
int *o;
int c;
int l;
char *num;
l = 0;
c = 0;
if (str[j] == 'O')
c = 1;
o = ft_loop(j, str, 0);
ft_cast_u(&a, str, c, str[j]);
num = ft_octal(a, &l);
if (o[6] != 1 && a == 0 && o[4] != -1)
l = 0;
if (o[6] == 1)
{
*s += 1;
l += 1;
}
if (o[0] == 1)
ft_print_o(o, s, a, num);
ft_space_o(o, s, l);
if (o[0] != 1)
ft_print_o(o, s, a, num);
ft_memdel((void*)(&o));
}