-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_print_flag.c
78 lines (69 loc) · 2.32 KB
/
ft_print_flag.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_flag.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jkoupy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/21 17:19:55 by jkoupy #+# #+# */
/* Updated: 2023/09/24 14:28:14 by jkoupy ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int print_plus_space(const char *format, int *i, int num)
{
int printlen;
printlen = 0;
if (num >= 0)
printlen += ft_putchar_fd(format[*i + 1], 1);
printlen += ft_putnbr_fd(num, 1);
return (printlen);
}
int print_sharp(const char *format, int *i, int num)
{
int printlen;
printlen = 0;
if (num != 0)
{
printlen += ft_putchar_fd('0', 1);
printlen += ft_putchar_fd(format[*i + 2], 1);
}
printlen += ft_puthexa(num, format[*i + 2]);
return (printlen);
}
int print_space(const char *format, int *i, char *str)
{
int printlen;
printlen = 0;
if (format[*i + 2] == 's')
printlen += ft_putstr_fd(str, 1);
else if (ft_isdigit(format[*i + 2]) && format[*i + 3] == 's')
{
printlen += ft_putchar_fd(' ', 1);
printlen += ft_putstr_fd(str, 1);
*i += 2;
}
return (printlen);
}
int print_flag_va(const char *format, int *i, va_list args)
{
int printlen;
int num;
char *str;
printlen = 0;
if (format[*i + 2] == 's'
|| (ft_isdigit(format[*i + 2]) && format[*i + 3] == 's'))
str = va_arg(args, char *);
else
num = va_arg(args, int);
if ((format[*i + 1] == '+' || format[*i + 1] == ' ')
&& (format[*i + 2] == 'd' || format[*i + 2] == 'i'))
printlen += print_plus_space(format, i, num);
else if (format[*i + 1] == '#'
&& (format[*i + 2] == 'x' || format[*i + 2] == 'X'))
printlen += print_sharp(format, i, num);
else if (format[*i + 1] == ' ')
printlen += print_space(format, i, str);
(*i)++;
return (printlen);
}