-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf_aux.c
More file actions
99 lines (91 loc) · 2.15 KB
/
ft_printf_aux.c
File metadata and controls
99 lines (91 loc) · 2.15 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_aux.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: manufern <manufern@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/02 15:52:33 by manufern #+# #+# */
/* Updated: 2023/11/27 18:34:32 by manufern ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdarg.h>
#include <unistd.h>
#include "ft_printf.h"
void ft_putchar(char c, int fd, int *len)
{
if (write(fd, &c, 1) == -1)
{
*len = -1;
}
else
(*len)++;
}
int check_base(char *base)
{
unsigned int i;
unsigned int j;
i = 0;
if (base[i] == '\0')
return (0);
while (base[i] != '\0')
{
j = i;
while (base[j] != '\0')
{
if (base[j] == '+' || base[j] == '-')
return (0);
j ++;
if (base[j] == base[i])
return (0);
}
i ++;
}
return (i);
}
void ft_putnbr_base(int nbr, char *base, int *len)
{
unsigned int size;
unsigned int num;
size = check_base (base);
if (size <= 1)
return ;
if (nbr < 0)
{
ft_putchar ('-', 1, len);
num = nbr * -1;
}
else
num = nbr;
if (*len == -1)
return ;
if (num >= size)
{
ft_putnbr_base (num / size, base, len);
if (*len == -1)
return ;
ft_putchar (base[num % size], 1, len);
}
else
ft_putchar (base[num % size], 1, len);
}
void ft_unsing(int nbr, char *base, int *len)
{
unsigned int size;
unsigned int num;
size = check_base (base);
if (size <= 1)
return ;
num = nbr;
if (num >= size)
{
ft_putnbr_base (num / size, base, len);
if (*len == -1)
return ;
ft_putchar (base[num % size], 1, len);
}
else
ft_putchar (base[num % size], 1, len);
if (*len == -1)
return ;
}