-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconversion_pcs.c
121 lines (110 loc) · 2.77 KB
/
conversion_pcs.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* conversion_pcs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: araiva <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/29 22:21:40 by araiva #+# #+# */
/* Updated: 2022/03/29 22:21:42 by araiva ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include "libft.h"
#include "myutils.h"
static char *format_pcs_str(char *cstr, t_format *f);
static char *format_pcs_digit(char *cstr, t_format *f);
static char *pcs_digit_operation(char *cstr, t_format *f);
static char *pcs_digit_fill(char *cfstr, char *cstr, t_format *f);
char *conversion_pcs(char *cstr, t_format *f)
{
char *cfstr;
if (!f->dot)
return (cstr);
if (f->type == 's' || f->type == 'c')
cfstr = format_pcs_str(cstr, f);
else
cfstr = format_pcs_digit(cstr, f);
if (!cfstr)
return (NULL);
return (cfstr);
}
static char *format_pcs_str(char *cstr, t_format *f)
{
char *cfstr;
if ((f->dot && f->pcs == 0)
|| (IS_LINUX
&& (ft_strncmp(cstr, S_EMPTY, S_EMPTY_L) == 0 && f->pcs < 6)))
cfstr = ft_calloc(sizeof(char), 1);
else
cfstr = ft_substr(cstr, 0, f->pcs);
if (!cfstr)
return (NULL);
free(cstr);
return (cfstr);
}
static char *format_pcs_digit(char *cstr, t_format *f)
{
char *cfstr;
if (cstr[0] == '0' && f->pcs == 0)
{
cfstr = ft_calloc(sizeof(char), f->pcs + 1);
if (!cfstr)
return (NULL);
free(cstr);
return (cfstr);
}
else if (f->pcs >= ft_strlen(cstr))
{
cfstr = pcs_digit_operation(cstr, f);
if (!cfstr)
return (NULL);
free(cstr);
return (cfstr);
}
return (cstr);
}
static char *pcs_digit_operation(char *cstr, t_format *f)
{
char *cfstr;
if (cstr[0] == '-')
{
cfstr = ft_calloc(sizeof(char), f->pcs + 2);
if (!cfstr)
return (NULL);
cfstr[0] = '-';
}
else
{
cfstr = ft_calloc(sizeof(char), f->pcs + 1);
if (!cfstr)
return (NULL);
}
pcs_digit_fill(cfstr, cstr, f);
return (cfstr);
}
static char *pcs_digit_fill(char *cfstr, char *cstr, t_format *f)
{
int i;
int j;
int len;
len = ft_strlen(cstr);
i = 0;
j = 0;
if (cstr[0] == '-')
{
i++;
j++;
len--;
f->pcs++;
}
while (i < f->pcs)
{
if (i < f->pcs - len)
cfstr[i] = '0';
else
cfstr[i] = cstr[j++];
i++;
}
return (cfstr);
}