minishell
print_all.c File Reference
#include <unistd.h>
#include "ft_printf_local.h"
#include "../ft_math/ft_math.h"
Include dependency graph for print_all.c:

Go to the source code of this file.

Functions

static int calc_pad_zero_len (t_fmt *fmt, int *pad_len, int *zero_len)
 
static int print_buf (int dstfd, t_fmt *fmt)
 
static int print_char (int dstfd, t_fmt *fmt)
 
static int print_str (int dstfd, t_fmt *fmt)
 
int print_all (int dstfd, t_list *list)
 

Function Documentation

◆ calc_pad_zero_len()

static int calc_pad_zero_len ( t_fmt fmt,
int *  pad_len,
int *  zero_len 
)
static

Definition at line 17 of file print_all.c.

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 }
int ft_max(int a, int b)
Definition: ft_max.c:16
bool f_minus
t_data data
int max_len
int str_len
t_uint8 head_len
int min_len
bool f_dot
bool f_zero
Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_all()

int print_all ( int  dstfd,
t_list list 
)

Definition at line 106 of file print_all.c.

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 }
@ CHAR
@ STR
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 print_buf(int dstfd, t_fmt *fmt)
Definition: print_all.c:40
t_dtype type
void * content
Definition: ft_lst.h:19
struct s_list * next
Definition: ft_lst.h:20
Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_buf()

static int print_buf ( int  dstfd,
t_fmt fmt 
)
static

Definition at line 40 of file print_all.c.

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 }
static int calc_pad_zero_len(t_fmt *fmt, int *pad_len, int *zero_len)
Definition: print_all.c:17
char header[3]
char str_buf[STR_BUF_LEN]
Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_char()

static int print_char ( int  dstfd,
t_fmt fmt 
)
static

Definition at line 58 of file print_all.c.

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 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_str()

static int print_str ( int  dstfd,
t_fmt fmt 
)
static

Definition at line 80 of file print_all.c.

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 }
int ft_min(int a, int b)
Definition: ft_min.c:16
char * str
Here is the call graph for this function:
Here is the caller graph for this function: