-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpadding_utility.c
More file actions
90 lines (83 loc) · 2.35 KB
/
padding_utility.c
File metadata and controls
90 lines (83 loc) · 2.35 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* padding_utility.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pnoutere <pnoutere@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/13 12:39:37 by pnoutere #+# #+# */
/* Updated: 2022/05/16 18:06:01 by pnoutere ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void adder_utils_help(char **str, char **add, t_utils utils)
{
char *temp;
if (utils.sign > 0 && utils.plus)
*str = character_adder(str, "+");
if (utils.sign < 0)
{
temp = *str;
*str = ft_strjoin("-", temp);
free(temp);
}
if (utils.space && (*str[0] != '-' && *str[0] != '+'))
*str = character_adder(str, " ");
if (utils.dash)
{
temp = *str;
*str = character_adder(add, temp);
free(temp);
}
else
{
*str = character_adder(str, *add);
free(*add);
}
}
static char *adder_utils(char *str, char *add, t_utils utils)
{
if ((utils.sign < 0 || utils.plus
|| utils.space || utils.dash) && !utils.zero)
adder_utils_help(&str, &add, utils);
else
{
str = character_adder(&str, add);
free(add);
str = check_helper(utils, utils.sign, str);
}
return (str);
}
static void flag_adder_utils(t_utils utils, int *len)
{
if (utils.plus && utils.sign > 0)
*len -= 1;
if (utils.sign < 0 && utils.space)
*len -= 1;
if (*len < 0)
*len = 0;
}
char *flag_adders(char *str, t_utils utils, int len)
{
char *temp;
char *add;
if (utils.padding)
utils.precision = utils.padding;
len = utils.precision - ft_strlen(str);
temp = str;
if (str[0] == '-')
{
utils.sign = -1;
str = str_maker(str);
}
else if (utils.space)
len--;
flag_adder_utils(utils, &len);
add = ft_strnew(len);
add = ft_memset(add, ' ', len);
if (utils.dash)
utils.zero = 0;
if (utils.zero)
add = ft_memset(add, '0', len);
return (adder_utils(str, add, utils));
}