-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_printf_utils2.c
139 lines (128 loc) · 4.7 KB
/
ft_printf_utils2.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
137
138
139
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf_utils2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lmmielgo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/27 18:24:13 by luciama2 #+# #+# */
/* Updated: 2023/11/02 21:55:44 by lmmielgo ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/** int ft_printnbr_vptr_base(void *nbr, char *base)
* @brief Function to handle the a void pointer (specifier %p) interpolated
* variable. Prints a memory address (on hex base) of an address passed on
* base 10.
* No flags apply to this specifier
* @param nbr, void *, memory address passed on base10
* @param base, number base 16 to print address as hex
* @return int, length printed on the standard output
*/
int ft_printnbr_vptr_base(void *nbr, char *base)
{
int len;
unsigned long long nb;
size_t base_size;
base_size = ft_strlen(base);
nb = (unsigned long long)nbr;
len = 0;
len += write(1, "0x", 2);
ft_putnbr_base(nb, base, &base_size, &len);
return (len);
}
/** int ft_print_base (t_interp *plh, int nbr, char *base)
* @brief Function to handle the specifier 'i' and 'd' on the printf function.
* It handles negative numbers updating the interpolated var struct and handles
* the sign flag and space flag, printing the corresponding char when applicable
* Prints the number using ft_putnbr_base funct.
* @param plh, ptr to the interpolated var strucct
* @param nbr, nbr to print
* @param base, base of number
* @return int
*/
int ft_printnbr_base(t_interp *plh, int nbr, char *base)
{
int len;
size_t base_size;
long long nb;
base_size = ft_strlen(base);
nb = (long long)nbr;
if (nb < 0)
{
plh->plus_flag = 1;
plh->sign = '-';
nb *= (-1);
}
len = 0;
if (plh->plus_flag == 1)
len += write(1, &(plh->sign), 1);
if (plh->space_flag == 1 && plh->plus_flag == 0)
len += write(1, " ", 1);
ft_putnbr_base((unsigned long long)nb, base, &base_size, &len);
return (len);
}
/** int ft_printnbr_u_base(t_interp *plh, unsginned nbr, char *base)
* @brief Function to handle the 'u' specifier on the printf function.
* Handles the applicable flags (only plus flag). Space flag not applicable.
* Prints the number using ft_putnbr_base.
* @param plh, pointer to the interpolated var struct
* @param nbr, nbr to print
* @param base, number base
* @return int, length of the string printed on the standard output
*/
int ft_printnbr_u_base(t_interp *plh, unsigned int nbr, char *base)
{
int len;
size_t base_size;
long long nb;
base_size = ft_strlen(base);
nb = (long long)nbr;
len = 0;
if (plh->plus_flag == 1)
len += write(1, &(plh->sign), 1);
ft_putnbr_base((unsigned long long)nb, base, &base_size, &len);
return (len);
}
/** ft_printnbr_x_base(t_interp *plh, unsinged int nbr, char *base)
* @brief Function to handle the 'x' specifier on the printf function.
* It also handles the applicable flags for the specifier (only '#').
* @param plh, pointer to the interpolated var struct
* @param nbr, nbr (on base 10) of the mem address to print
* @param base, base16 in lowercase
* @return int, length of the printed string
*/
int ft_printnbr_x_base(t_interp *plh, unsigned int nbr, char *base)
{
int len;
size_t base_size;
long long nb;
base_size = ft_strlen(base);
nb = (long long)nbr;
len = 0;
if (plh->hash_flag == 1 && nbr != 0)
len += write(1, "0x", 2);
ft_putnbr_base((unsigned long long)nb, base, &base_size, &len);
return (len);
}
/** ft_printnbr_xupp_base(t_interp *plh, unsinged int nbr, char *base)
* @brief Function to handle the 'X' specifier on the printf function.
* It also handles the applicable flags for the specifier (only '#').
* @param plh, pointer to the interpolated var struct
* @param nbr, nbr (on base 10) of the mem address to print
* @param base, base16 in uppercase
* @return int, length of the printed string
*/
int ft_printfnbr_xupp_base(t_interp *plh, unsigned int nbr, char *base)
{
int len;
size_t base_size;
long long nb;
base_size = ft_strlen(base);
nb = (long long)nbr;
len = 0;
if (plh->hash_flag == 1 && nbr != 0)
len += write(1, "0X", 2);
ft_putnbr_base((unsigned long long)nb, base, &base_size, &len);
return (len);
}