-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
96 lines (86 loc) · 2.91 KB
/
ft_printf.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: smbaabu <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/02 20:42:30 by smbaabu #+# #+# */
/* Updated: 2019/07/14 12:27:57 by smbaabu ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static void apply_args(const char *format, va_list args, int *info[],
char **ret)
{
int i;
int idx;
int specifier;
int min_width;
int upto;
i = *info[INDEX];
specifier = check_specifier(format, i + 1, &upto);
*info[INDEX] = upto;
idx = check_modifier(format, i + 1, &upto);
*ret = apply_specifier(args, specifier, idx, info);
if ((idx = check_precision(format, i + 1, &upto)) != -1
|| specifier == F || specifier == E || specifier == G)
*ret = apply_precision(*ret, idx, specifier);
if ((min_width = check_width(format, i + 1, &upto)) != -1)
*ret = apply_width(*ret, min_width);
check_flags(format, i + 1, upto, info[FLGS]);
if (atleast_one(info[FLGS], NUM_FLAGS) != -1)
*ret = apply_flags(*ret, info[FLGS],
(int[]){*info[BASE], specifier, idx, min_width});
}
static void update_buf(char *buf, const char *src, int len, int *info[])
{
ft_memcpy(buf + *info[COUNT], src, len);
*info[COUNT] += len;
}
static void handle_args(const char *format, va_list args, int *info[],
char *buf)
{
char *ret;
int len;
*info[BASE] = 0;
*info[NUL_COUNT] = 0;
apply_args(format, args, info, &ret);
len = ft_strlen(ret);
pre_print(&ret, *info[NUL_COUNT]);
update_buf(buf, ret, len, info);
free(ret);
clear_flags(info[FLGS], NUM_FLAGS);
}
int ft_print(char *res, va_list args, const char *format)
{
int *info[NUM_INFO];
int count;
create_info(info, NUM_INFO);
free(info[FLGS]);
info[FLGS] = create_flags(NUM_FLAGS);
*info[COUNT] = 0;
*info[INDEX] = 0;
while (format[*info[INDEX]])
{
if (format[*info[INDEX]] == '%'
&& check_validity(format, *info[INDEX] + 1) != -1)
handle_args(format, args, info, res);
else if (format[*info[INDEX]] != '%')
update_buf(res, &format[*info[INDEX]], 1, info);
(*info[INDEX])++;
}
count = *info[COUNT];
destroy_info(info, NUM_INFO);
return (count);
}
int ft_printf(const char *restrict format, ...)
{
va_list args;
char buf[BUF_SIZE];
int count;
va_start(args, format);
count = ft_print(buf, args, format);
va_end(args);
return (write(1, buf, count));
}