-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
82 lines (75 loc) · 2.59 KB
/
ft_printf.c
File metadata and controls
82 lines (75 loc) · 2.59 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: minjukim <minjukim@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/03 16:54:10 by minkim3 #+# #+# */
/* Updated: 2023/01/26 21:26:43 by minjukim ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int conversion(const char *format, \
size_t *index, t_options *string_info)
{
(*index)++;
ft_memset(string_info, 0, sizeof(t_options));
while (ft_flag_checker(*(format + (*index))))
ft_flag_check(*(format + (*index)), string_info, index);
ft_width_check(format, index, string_info);
if (*(format + (*index)) == '.')
ft_precision_check(format, index, string_info);
if (ft_type_checker(*(format + (*index))) == 0)
return (-1);
string_info->type = *(format + (*index));
if (string_info->type == '%')
string_info->flag_zero = 0;
(*index)++;
return (ft_option_error(string_info));
}
static int printer(const char *format, va_list *ap, \
t_options *string_info, int *strlen)
{
size_t index;
int check;
index = 0;
while (*(format + index))
{
if (ft_percent_or_other(*(format + index)) == -1)
break ;
else if (ft_percent_or_other(*(format + index)) == 1)
{
if (conversion(format, &index, string_info) == -1)
return (-1);
check = ft_apply_option(ap, string_info);
ft_free_options(string_info);
if (check == -1)
return (-1);
(*strlen) += check;
}
else if (ft_percent_or_other(*(format + index)) == 2)
ft_putchar(*(format + index++), strlen);
}
return (0);
}
static int ft_free_fin(va_list *ap, t_options *string_info, int strlen)
{
free(string_info);
va_end(*ap);
return (strlen);
}
int ft_printf(const char *format, ...)
{
va_list ap;
t_options *string_info;
int strlen;
strlen = 0;
string_info = (t_options *)malloc(sizeof(t_options));
if (string_info == NULL)
return (-1);
va_start(ap, format);
if (printer(format, &ap, string_info, &strlen) == -1)
return (ft_error_quit(&ap, string_info));
return (ft_free_fin(&ap, string_info, strlen));
}