-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallign.c
81 lines (72 loc) · 2.59 KB
/
allign.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* allign.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rklein <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/03/10 14:44:59 by rklein #+# #+# */
/* Updated: 2020/06/05 12:32:15 by rklein ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_ls.h"
/*
** Functions to allign the text in the long format (-l), by creating spacepads.
** First the length of the spacepad is determined with ft_scan_len, recursively
** going through the binary tree, finding the longest string in each column.
** The function ft_space_allign goes through the binary tree recursively
** adding a spacepad through the ft_create_pad function, adding padding to the
** front or back ('f' or 'b')
*/
static void ft_scan_len(t_file *ftree, t_flag *flags, int *len)
{
int tmp;
if (ftree)
{
ft_scan_len(ftree->left, flags, len);
if (flags->a == 1 || (flags->a == 0 && ftree->name[0] != '.'))
{
tmp = ft_strlen(ftree->links);
len[0] = tmp > len[0] ? tmp : len[0];
tmp = ft_strlen(ftree->user);
len[1] = tmp > len[1] ? tmp : len[1];
tmp = ft_strlen(ftree->group);
len[2] = tmp > len[2] ? tmp : len[2];
tmp = ft_strlen(ftree->bytes);
len[3] = tmp > len[3] ? tmp : len[3];
}
ft_scan_len(ftree->right, flags, len);
}
}
static void ft_create_pad(char **str, int len, char c)
{
char *tmp1;
char *tmp2;
tmp1 = ft_strmake(' ', len - ft_strlen(*str));
tmp2 = (c == 'f') ? ft_strjoin(tmp1, *str) : ft_strjoin(*str, tmp1);
free(*str);
free(tmp1);
*str = tmp2;
}
static void ft_space_allign(t_file *ftree, int *len)
{
if (ftree)
{
ft_space_allign(ftree->left, len);
ft_create_pad(&ftree->links, len[0], 'f');
ft_create_pad(&ftree->user, len[1], 'b');
ft_create_pad(&ftree->group, len[2], 'b');
ft_create_pad(&ftree->bytes, len[3], 'f');
ft_space_allign(ftree->right, len);
}
}
void ft_allign_field(t_file *ftree, t_flag *flags)
{
int len[4];
len[0] = 0;
len[1] = 0;
len[2] = 0;
len[3] = 0;
ft_scan_len(ftree, flags, len);
ft_space_allign(ftree, len);
}