minishell
print_all.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* print_all.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: kfujita <kfujita@student.42tokyo.jp> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2022/04/24 01:13:22 by kfujita #+# #+# */
9 /* Updated: 2023/01/30 16:56:38 by kfujita ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include <unistd.h>
14 #include "ft_printf_local.h"
15 #include "../ft_math/ft_math.h"
16 
17 static int calc_pad_zero_len(t_fmt *fmt, int *pad_len, int *zero_len)
18 {
19  int expected_print_len;
20 
21  if (fmt->f_dot && fmt->max_len == 0 && fmt->data.c == '0')
22  fmt->str_len = 0;
23  expected_print_len = ft_max(fmt->min_len,
24  ft_max(fmt->max_len, fmt->str_len) + fmt->head_len);
25  *pad_len = expected_print_len - fmt->str_len - fmt->head_len;
26  *zero_len = 0;
27  if (fmt->f_dot)
28  {
29  *zero_len = ft_max(0, fmt->max_len - fmt->str_len);
30  *pad_len = ft_max(0, *pad_len - *zero_len);
31  }
32  else if (!(fmt->f_minus) && fmt->f_zero)
33  {
34  *zero_len = ft_max(0, fmt->min_len - fmt->str_len - fmt->head_len);
35  *pad_len = 0;
36  }
37  return (expected_print_len);
38 }
39 
40 static int print_buf(int dstfd, t_fmt *fmt)
41 {
42  int pad_len;
43  int zero_len;
44  int print_len;
45 
46  print_len = calc_pad_zero_len(fmt, &pad_len, &zero_len);
47  while (!(fmt->f_minus) && pad_len-- > 0)
48  write(dstfd, " ", 1);
49  write(dstfd, fmt->header, fmt->head_len);
50  while (zero_len-- > 0)
51  write(dstfd, "0", 1);
52  write(dstfd, fmt->data.str_buf, fmt->str_len);
53  while (fmt->f_minus && pad_len-- > 0)
54  write(dstfd, " ", 1);
55  return (print_len);
56 }
57 
58 static int print_char(int dstfd, t_fmt *fmt)
59 {
60  int pad_len;
61 
62  pad_len = fmt->min_len - 1;
63  if (fmt->f_minus || !(fmt->f_zero))
64  {
65  while (!(fmt->f_minus) && pad_len-- > 0)
66  write(dstfd, " ", 1);
67  write(dstfd, fmt->data.str_buf, 1);
68  while (fmt->f_minus && pad_len-- > 0)
69  write(dstfd, " ", 1);
70  }
71  else
72  {
73  while (pad_len-- > 0)
74  write(dstfd, "0", 1);
75  write(dstfd, fmt->data.str_buf, 1);
76  }
77  return (ft_max(fmt->str_len + fmt->head_len, fmt->min_len));
78 }
79 
80 static int print_str(int dstfd, t_fmt *fmt)
81 {
82  int pad_len;
83  int print_len;
84 
85  print_len = fmt->str_len;
86  if (fmt->f_dot)
87  print_len = ft_min(fmt->max_len, print_len);
88  pad_len = fmt->min_len - print_len;
89  if (fmt->f_minus || !(fmt->f_zero))
90  {
91  while (!(fmt->f_minus) && pad_len-- > 0)
92  write(dstfd, " ", 1);
93  write(dstfd, fmt->data.str, print_len);
94  while (fmt->f_minus && pad_len-- > 0)
95  write(dstfd, " ", 1);
96  }
97  else
98  {
99  while (pad_len-- > 0)
100  write(dstfd, "0", 1);
101  write(dstfd, fmt->data.str, print_len);
102  }
103  return (ft_max(print_len, fmt->min_len));
104 }
105 
106 int print_all(int dstfd, t_list *list)
107 {
108  int written_count;
109  int tmp;
110  t_fmt *data;
111 
112  written_count = 0;
113  while (list != NULL)
114  {
115  data = list->content;
116  tmp = 0;
117  if (data->type == STR)
118  tmp = print_str(dstfd, data);
119  else if (data->type == CHAR)
120  tmp = print_char(dstfd, data);
121  else
122  tmp = print_buf(dstfd, data);
123  written_count += tmp;
124  list = list->next;
125  }
126  return (written_count);
127 }
int ft_max(int a, int b)
Definition: ft_max.c:16
int ft_min(int a, int b)
Definition: ft_min.c:16
@ CHAR
@ STR
int print_all(int dstfd, t_list *list)
Definition: print_all.c:106
static int print_str(int dstfd, t_fmt *fmt)
Definition: print_all.c:80
static int print_char(int dstfd, t_fmt *fmt)
Definition: print_all.c:58
static int calc_pad_zero_len(t_fmt *fmt, int *pad_len, int *zero_len)
Definition: print_all.c:17
static int print_buf(int dstfd, t_fmt *fmt)
Definition: print_all.c:40
t_dtype type
bool f_minus
t_data data
int max_len
int str_len
t_uint8 head_len
char header[3]
int min_len
bool f_dot
bool f_zero
Definition: ft_lst.h:18
void * content
Definition: ft_lst.h:19
struct s_list * next
Definition: ft_lst.h:20
char str_buf[STR_BUF_LEN]
char * str