-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsing1.c
102 lines (94 loc) · 2.36 KB
/
parsing1.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vmiachko <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/02/18 19:52:27 by vmiachko #+# #+# */
/* Updated: 2018/03/10 16:55:29 by vmiachko ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
char *minus_flag(char *tmp, t_flags flag)
{
char *tmp1;
size_t length;
int i;
length = ft_strlen(tmp);
tmp1 = ft_memalloc(length + 1);
ft_strcpy(tmp1, tmp);
i = 0;
while (i < flag.width - 1)
{
tmp[i + 1] = tmp1[i];
++i;
}
tmp[0] = ' ';
free(tmp1);
return (tmp);
}
static char *skip_spaces(char *tmp, t_flags flag)
{
int i;
i = 0;
while (tmp[i] == ' ' && tmp[i])
++i;
if (flag.sign_minus)
tmp[i - 1] = '-';
else if (flag.plus)
tmp[i - 1] = '+';
else
tmp[i - 1] = ' ';
return (tmp);
}
static char *add_signs(char *tmp, t_flags flag)
{
int i;
char *tmp1;
tmp1 = ft_memalloc(ft_strlen(tmp) + 1);
ft_strcpy(tmp1, tmp);
tmp = ft_realloc(tmp, 2);
i = 0;
while (tmp1[i])
{
tmp[i + 1] = tmp1[i];
++i;
}
if (flag.sign_minus)
tmp[0] = '-';
else if (flag.plus)
tmp[0] = '+';
else
tmp[0] = ' ';
free(tmp1);
return (tmp);
}
char *add_character_before(char *tmp, t_flags flag)
{
if ((find_spec("idD", flag.specifier)) &&
(flag.space || flag.sign_minus || flag.plus))
{
if (flag.minus && flag.width >= (int)ft_strlen(tmp))
tmp = minus_flag(tmp, flag);
if (tmp[0] != ' ')
{
if (ft_strcmp(tmp, "0") && tmp[0] == '0'
&& (flag.width > flag.precision
|| compare_width_precision(flag)))
{
if (flag.sign_minus)
tmp[0] = '-';
else if (flag.plus)
tmp[0] = '+';
else
tmp[0] = ' ';
return (tmp);
}
tmp = add_signs(tmp, flag);
}
else
tmp = skip_spaces(tmp, flag);
}
return (tmp);
}