-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathints.c
More file actions
133 lines (124 loc) · 3.48 KB
/
ints.c
File metadata and controls
133 lines (124 loc) · 3.48 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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ints.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pnoutere <pnoutere@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/03 11:13:12 by pnoutere #+# #+# */
/* Updated: 2022/05/18 17:35:21 by pnoutere ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
char *int_handler_help(int size, va_list args, t_utils utils)
{
char *str;
if (size == 4)
{
utils._long_long = va_arg(args, long long);
return (itoa_base(10, utils._long_long));
}
if (size == 5)
{
utils._char = (char)va_arg(args, int);
str = ft_strnew(1);
str[0] = utils._char;
return (str);
}
return (NULL);
}
char *int_handler(int size, va_list args, t_utils utils)
{
if (size == 0)
{
utils._int = va_arg(args, int);
return (itoa_base(10, utils._int));
}
if (size == 1)
{
utils._short = va_arg(args, int);
return (itoa_base(10, utils._short));
}
if (size == 2)
{
utils._int = (char)va_arg(args, int);
return (itoa_base(10, utils._int));
}
if (size == 3)
{
utils._long = va_arg(args, long);
return (itoa_base(10, utils._long));
}
return (int_handler_help(size, args, utils));
}
char *int_adder_utils(char *str, char *add, t_utils utils)
{
char *temp;
if (utils.sign > 0 && utils.plus && add[0] != '0')
str = character_adder(&str, "+");
if (utils.sign < 0 && add[0] != '0')
str = character_adder(&str, "-");
if (utils.space && (str[0] != '-' && str[0] != '+') && !utils.zero)
str = character_adder(&str, " ");
if (utils.dash)
{
temp = str;
str = ft_strjoin(temp, add);
free(temp);
}
else
str = character_adder(&str, add);
if (utils.sign > 0 && utils.plus && add[0] == '0')
str = character_adder(&str, "+");
if (utils.sign < 0 && add[0] == '0')
str = character_adder(&str, "-");
if (utils.space && (str[0] != '-' && str[0] != '+') && utils.zero)
str = character_adder(&str, " ");
free (add);
return (str);
}
void int_flag_adder_ifs(int *len, t_utils *utils, char *ch, char **add)
{
if (utils->sign < 0 || (utils->plus && utils->sign > 0))
*len -= 1;
if (utils->space && utils->sign > 0)
*len -= 1;
if (utils->plus && utils->sign > 0 && !utils->zero)
*len += 1;
if (utils->plus && utils->sign > 0 && !utils->space && !utils->zero)
*len -= 1;
if (*len < 0)
*len = 0;
if (utils->zero && !utils->dash
&& ((utils->padding <= utils->precision
|| !utils->precision) || (utils->is_float)))
{
*ch = '0';
}
*add = ft_strnew(*len);
ft_memset(*add, *ch, *len);
}
char *int_flag_adders(char *str, t_utils utils, int len)
{
char *temp;
char *add;
char ch;
ch = ' ';
temp = str;
if (str[0] == '-')
{
utils.sign = -1;
str += 1;
}
len = utils.precision - ft_strlen(str);
if (len < 0)
len = 0;
add = ft_strnew(len);
ft_memset(add, '0', len);
str = ft_strjoin(add, str);
free (temp);
free (add);
len = utils.padding - ft_strlen(str);
int_flag_adder_ifs(&len, &utils, &ch, &add);
return (int_adder_utils(str, add, utils));
}