-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_lib_fd.c
80 lines (71 loc) · 2.16 KB
/
ft_lib_fd.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_3lib_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: guilmira <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/06/19 10:02:57 by guilmira #+# #+# */
/* Updated: 2021/09/23 08:39:36 by guilmira ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void pf_putstr_fd(char *s, int fd, t_flag *flag)
{
if (s)
flag->counter += write(fd, s, ft_strlen(s));
}
void pf_putchar_fd(char c, int fd, t_flag *flag)
{
flag->counter += write(fd, &c, sizeof(char));
}
void pf_pointer_fd(unsigned long long n, char *base, int fd, t_flag *flag)
{
int i;
unsigned long long x;
i = -1;
if (!base || !base[0])
return ;
while (base[++i])
if (ft_strchr(base + 1 + i, base[i]) || \
ft_strchr(base, '+') || ft_strchr(base, '-'))
return ;
x = n;
if (x / ft_strlen(base) == 0)
pf_putchar_fd(base[x % ft_strlen(base)], fd, flag);
else
{
pf_pointer_fd(x / ft_strlen(base), base, fd, flag);
pf_putchar_fd(base[x % ft_strlen(base)], fd, flag);
}
}
/** PURPOSE : Selects number part in any strings, trims it and applies atoi */
int get_number_from_string(char *number)
{
int i;
int lenght;
int value;
char *number_string;
i = -1;
lenght = 0;
value = 0;
while (ft_isdigit(number[++i]))
lenght++;
number_string = ft_substr(number, 0, lenght);
value = ft_atoi(number_string);
free(number_string);
return (value);
}
/** PURPOSE : counts digits of an unsigned int (iterative) */
int ft_count_digits_unsigned(unsigned int n)
{
int digits;
digits = 1;
n /= 10;
while (n)
{
digits++;
n /= 10;
}
return (digits);
}