-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_format_di.c
More file actions
104 lines (94 loc) · 2.86 KB
/
ft_format_di.c
File metadata and controls
104 lines (94 loc) · 2.86 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_format_di.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: afukuhar <afukuhar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/09/09 10:24:13 by afukuhar #+# #+# */
/* Updated: 2020/09/11 12:15:33 by afukuhar ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
/*
** After analysing all attributes
** nbr is filled with the right format of the number to be printed
**
** Based on left flag
** it prints pad than nbr or vice-versa
*/
void pf_di(t_format *arg, long long int n)
{
char *nbr;
nbr = pf_analyse_di(arg, n);
if (arg->left)
{
arg->len += ft_putint(nbr, arg->pad_zero, arg->is_neg);
arg->len += ft_putnchar(arg->pad, arg->n_pad);
}
else
{
arg->len += ft_putnchar(arg->pad, arg->n_pad);
arg->len += ft_putint(nbr, arg->pad_zero, arg->is_neg);
}
ft_strdel(&nbr);
}
/*
** Checks if n is neg - change to pos and update is_neg flag if so
**
** Converting number (itoa):
** - there is a special case that should print nothing when
** n is NULL and we have precisio
** - otherwise just a common itoa base
**
** Precision: when p > len, should pad on left with zeroes
**
** len: updated if is_neg (+1) and pad_zero > 0;
**
** Width: After all updates inside number, we have new len
** based on that, if w > newlen we should pad
** - there is a special case when pad is '0' and we don't have
** precision .. zero should go in the number part
** - otherwise will be a ' ' pad and we update n_pad and pad
*/
char *pf_analyse_di(t_format *arg, long long int n)
{
char *nbr;
int len;
if (n < 0)
{
arg->is_neg = 1;
n *= -1;
}
nbr = (!n && !arg->p) ? ft_strnew(0) : ft_itoa_base(n, 10, 0);
len = ft_strlen(nbr);
arg->pad_zero = (arg->p > len) ? arg->p - len : 0;
len += arg->pad_zero + (arg->is_neg ? 1 : 0);
if (arg->w > len)
{
if (arg->zero && !arg->left && arg->p < 0)
arg->pad_zero += arg->w - len;
else
{
arg->pad = ' ';
arg->n_pad = arg->w - len;
}
}
return (nbr);
}
/*
** Printing the number part
** - if is_neg, should print a '-' at beggining
** - print the '0's inside number part
** - print the number string
*/
int ft_putint(char *nbr, int pad_zero, int is_neg)
{
int len;
len = 0;
if (is_neg)
len += ft_putstr("-");
len += ft_putnchar('0', pad_zero);
len += ft_putstr(nbr);
return (len);
}