minishell
ft_printf_local.h File Reference
#include <stddef.h>
#include <stdbool.h>
#include <stdarg.h>
#include "../ft_lst/ft_lst.h"
Include dependency graph for ft_printf_local.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

union  t_data
 
struct  t_fmt
 

Macros

#define STR_BUF_LEN   (19)
 

Typedefs

typedef unsigned char t_uint8
 

Enumerations

enum  t_dtype {
  STR , CHAR , PTR , INT_10BASE ,
  UINT_10BASE , UINT_16BASE
}
 

Functions

t_listparse_format (const char *fmt, va_list *args)
 
t_fmtparse_opt (size_t *len, char **fmt, va_list *args)
 
t_fmtcheck_no_opt_str (size_t *len, char **fmt, t_fmt *p_ret)
 
void parse_opt_flags (char **fmt, t_fmt *p_ret)
 
bool parse_opt_c_str (char fmt, va_list *args, t_fmt *p_ret)
 
bool parse_opt_num (char fmt, va_list *args, t_fmt *p_ret)
 
bool parse_opt_ptr (char fmt, va_list *args, t_fmt *p_ret)
 
bool is_valid_conv_char (char c)
 
bool is_valid_flag_char (char c)
 
int print_all (int fd, t_list *list)
 

Macro Definition Documentation

◆ STR_BUF_LEN

#define STR_BUF_LEN   (19)

Definition at line 22 of file ft_printf_local.h.

Typedef Documentation

◆ t_uint8

typedef unsigned char t_uint8

Definition at line 24 of file ft_printf_local.h.

Enumeration Type Documentation

◆ t_dtype

enum t_dtype
Enumerator
STR 
CHAR 
PTR 
INT_10BASE 
UINT_10BASE 
UINT_16BASE 

Definition at line 26 of file ft_printf_local.h.

27 {
28  STR,
29  CHAR,
30  PTR,
31  INT_10BASE,
34 } t_dtype;
t_dtype
@ PTR
@ CHAR
@ INT_10BASE
@ UINT_10BASE
@ UINT_16BASE
@ STR

Function Documentation

◆ check_no_opt_str()

t_fmt* check_no_opt_str ( size_t *  len,
char **  fmt,
t_fmt p_ret 
)

Definition at line 20 of file check_no_opt_str.c.

21 {
22  size_t str_len;
23 
24  str_len = 0;
25  if (p_ret == NULL)
26  p_ret = ft_calloc(1, sizeof(t_fmt));
27  if (p_ret == NULL)
28  return (p_ret);
29  p_ret->type = STR;
30  p_ret->data.str = *fmt;
31  while (*len <= INT_MAX && **fmt != '\0' && **fmt != '%')
32  {
33  str_len++;
34  *len += 1;
35  *fmt += 1;
36  }
37  if (*len > INT_MAX)
38  {
39  free(p_ret);
40  return (NULL);
41  }
42  p_ret->str_len = (int)str_len;
43  *len += ft_max(p_ret->min_len, p_ret->str_len);
44  return (p_ret);
45 }
void * ft_calloc(size_t count, size_t size)
Definition: ft_calloc.c:29
int ft_max(int a, int b)
Definition: ft_max.c:16
t_dtype type
t_data data
int str_len
int min_len
char * str
Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_valid_conv_char()

bool is_valid_conv_char ( char  c)

Definition at line 15 of file is_valid_conv_char.c.

16 {
17  return (c == 'c' || c == 's' || c == 'p' || c == 'd' || c == 'i' || c == 'u'
18  || c == 'x' || c == 'X' || c == '%');
19 }
Here is the caller graph for this function:

◆ is_valid_flag_char()

bool is_valid_flag_char ( char  c)

Definition at line 16 of file is_valid_flag_char.c.

17 {
18  return (c == '-' || ft_isdigit(c) || c == '.'
19  || c == '#' || c == ' ' || c == '+');
20 }
int ft_isdigit(int c)
Definition: ft_isdigit.c:15
Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_format()

t_list* parse_format ( const char *  fmt,
va_list *  args 
)

Definition at line 18 of file parse_format.c.

19 {
20  t_list *p_ret;
21  size_t ret_len;
22 
23  p_ret = NULL;
24  ret_len = 0;
25  while (*fmt != '\0' && ret_len < INT_MAX)
26  {
27  if (*fmt == '%')
28  ft_lstadd_back(&p_ret,
29  ft_lstnew(parse_opt(&ret_len, (char **)&fmt, args)));
30  else
31  ft_lstadd_back(&p_ret,
32  ft_lstnew(check_no_opt_str(&ret_len, (char **)&fmt, NULL)));
33  if (p_ret == NULL || ft_lstlast(p_ret)->content == NULL)
34  ret_len = UINTPTR_MAX;
35  }
36  if (ret_len >= INT_MAX)
37  ft_lstclear(&p_ret, free);
38  return (p_ret);
39 }
t_fmt * check_no_opt_str(size_t *len, char **fmt, t_fmt *p_ret)
void ft_lstadd_back(t_list **lst, t_list *new)
void ft_lstclear(t_list **lst, void(*del)(void *))
Definition: ft_lstclear.c:16
t_list * ft_lstnew(void *content)
Definition: ft_lstnew.c:16
t_list * ft_lstlast(t_list *lst)
Definition: ft_lstlast.c:16
t_fmt * parse_opt(size_t *len, char **fmt, va_list *args)
Definition: parse_opt.c:17
Definition: ft_lst.h:18
Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_opt()

t_fmt* parse_opt ( size_t *  len,
char **  fmt,
va_list *  args 
)

Definition at line 17 of file parse_opt.c.

18 {
19  t_fmt *p_ret;
20 
21  *fmt += 1;
22  if (!is_valid_conv_char(**fmt)
23  && !is_valid_flag_char(**fmt))
24  return (check_no_opt_str(len, fmt, NULL));
25  p_ret = ft_calloc(1, sizeof(t_fmt));
26  if (p_ret == NULL)
27  return (p_ret);
28  while (is_valid_flag_char(**fmt))
29  parse_opt_flags(fmt, p_ret);
30  if (!(parse_opt_c_str(**fmt, args, p_ret)
31  || parse_opt_num(**fmt, args, p_ret)
32  || parse_opt_ptr(**fmt, args, p_ret)))
33  return (check_no_opt_str(len, fmt, p_ret));
34  *fmt += 1;
35  *len += ft_max(p_ret->min_len, p_ret->str_len + p_ret->head_len);
36  return (p_ret);
37 }
bool parse_opt_c_str(char fmt, va_list *args, t_fmt *p_ret)
void parse_opt_flags(char **fmt, t_fmt *p_ret)
bool parse_opt_num(char fmt, va_list *args, t_fmt *p_ret)
Definition: parse_opt_num.c:60
bool is_valid_conv_char(char c)
bool is_valid_flag_char(char c)
bool parse_opt_ptr(char fmt, va_list *args, t_fmt *p_ret)
Definition: parse_opt_ptr.c:16
t_uint8 head_len
Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_opt_c_str()

bool parse_opt_c_str ( char  fmt,
va_list *  args,
t_fmt p_ret 
)

Definition at line 18 of file parse_opt_c_str.c.

19 {
20  if (fmt == 'c' || fmt == '%')
21  {
22  p_ret->type = CHAR;
23  if (fmt == 'c')
24  p_ret->data.c = (char)va_arg(*args, int);
25  else
26  p_ret->data.c = '%';
27  p_ret->str_len = 1;
28  }
29  else if (fmt == 's')
30  {
31  p_ret->type = STR;
32  p_ret->data.str = va_arg(*args, char *);
33  if (p_ret->data.str == NULL)
34  p_ret->data.str = (char *)g_nullstr;
35  p_ret->str_len = ft_strlen(p_ret->data.str);
36  }
37  else
38  return (false);
39  return (true);
40 }
size_t ft_strlen(const char *s)
Definition: ft_strlen.c:15
const char *const g_nullstr
Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_opt_flags()

void parse_opt_flags ( char **  fmt,
t_fmt p_ret 
)

Definition at line 45 of file parse_opt_flags.c.

46 {
47  if (**fmt == '-')
48  p_ret->f_minus = true;
49  else if (**fmt == '0')
50  p_ret->f_zero = true;
51  else if (**fmt == '.')
52  {
53  parse_opt_flag_dot(fmt, p_ret);
54  return ;
55  }
56  else if (**fmt == '#')
57  p_ret->f_hash = true;
58  else if (**fmt == ' ')
59  p_ret->f_space = true;
60  else if (**fmt == '+')
61  p_ret->f_plus = true;
62  else
63  {
64  parse_opt_flag_min_len(fmt, p_ret);
65  return ;
66  }
67  *fmt += 1;
68 }
static void parse_opt_flag_dot(char **fmt, t_fmt *p_ret)
static void parse_opt_flag_min_len(char **fmt, t_fmt *p_ret)
bool f_minus
bool f_plus
bool f_zero
bool f_space
bool f_hash
Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_opt_num()

bool parse_opt_num ( char  fmt,
va_list *  args,
t_fmt p_ret 
)

Definition at line 60 of file parse_opt_num.c.

61 {
62  if (!parse_num_type(fmt, p_ret))
63  return (false);
64  if (fmt == 'X')
65  p_ret->f_upper = true;
66  if (p_ret->type == INT_10BASE)
67  p_ret->data.s_digit = va_arg(*args, int);
68  else
69  p_ret->data.s_digit = va_arg(*args, unsigned int);
70  if (p_ret->type == INT_10BASE)
71  p_ret->str_len = calc_num_len(p_ret->data.s_digit, 10, p_ret);
72  else if (p_ret->type == UINT_10BASE)
73  p_ret->str_len = calc_num_len(p_ret->data.u_digit, 10, p_ret);
74  else
75  p_ret->str_len = calc_num_len(p_ret->data.u_digit, 16, p_ret);
76  return (true);
77 }
static int calc_num_len(long num, int base, t_fmt *p_ret)
Definition: parse_opt_num.c:16
static bool parse_num_type(char fmt, t_fmt *p_ret)
Definition: parse_opt_num.c:44
bool f_upper
unsigned int u_digit
Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_opt_ptr()

bool parse_opt_ptr ( char  fmt,
va_list *  args,
t_fmt p_ret 
)

Definition at line 16 of file parse_opt_ptr.c.

17 {
18  size_t num;
19 
20  if (fmt != 'p')
21  return (false);
22  p_ret->type = PTR;
23  num = (size_t)va_arg(*args, void *);
24  p_ret->header[0] = '0';
25  p_ret->header[1] = 'x';
26  p_ret->head_len = 2;
27  if (num == 0)
28  {
29  p_ret->str_len = 1;
30  p_ret->data.c = '0';
31  }
32  else
33  p_ret->str_len = get_numstr_base(p_ret->data.str_buf, num, 16, false);
34  return (true);
35 }
int get_numstr_base(char *buf, size_t num, int base, bool is_upper)
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_all()

int print_all ( int  fd,
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 }
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
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: