-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_printf.c
More file actions
124 lines (113 loc) · 2.56 KB
/
ft_printf.c
File metadata and controls
124 lines (113 loc) · 2.56 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yyefimov <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/16 16:51:44 by yyefimov #+# #+# */
/* Updated: 2017/02/18 15:44:10 by yyefimov ### ########.fr */
/* */
/* ************************************************************************** */
#include "printf.h"
t_pf *add_list(void)
{
t_pf *list;
list = (t_pf *)malloc(sizeof(t_pf));
list->format = "";
list->str = "";
list->type = 0;
list->print = "";
list->flags = "";
list->p = -1;
list->w = 0;
list->len = "";
list->next = NULL;
return (list);
}
char *get_text(char **ptr, int i)
{
char *res;
char *pt;
char *exe;
char *ex;
exe = "sSpdDioOuUxXcCb%";
ex = "sSpdDioOuUxXcCb+-0 #hlLjzt.%123456789";
i = 0;
pt = *ptr;
while (!ft_strchr(exe, *pt) && *pt != '\0' && ft_strchr(ex, *pt))
{
pt++;
i++;
}
pt = pt - i;
res = ft_strsub(pt, 0, i + 1);
pt++;
res = ft_strjoin("%", res);
return (res);
}
char *get_str(char **format)
{
int i;
char *res;
char *ptr;
ptr = *format;
i = 0;
if (*ptr != '%' && *ptr != '\0')
{
while (*ptr != '%' && *ptr != '\0')
{
ptr++;
i++;
}
ptr = ptr - i;
res = ft_strsub(ptr, 0, i);
return (res);
}
else
{
ptr++;
return (get_text(&ptr, 0));
}
}
void make_format(char *format, t_pf **begin_list)
{
char *str;
t_pf *list;
list = *begin_list;
while (*format)
{
str = get_str(&format);
list->str = ft_strdup(str);
list->next = add_list();
list = list->next;
if (ft_strlen(str))
format += ft_strlen(str);
else if (str[0] == '%' || *format == '%')
format++;
}
}
int ft_printf(const char *format, ...)
{
int len;
va_list args;
static t_pf *list;
t_pf *begin_list;
char *res;
len = 0;
res = "";
if (!format)
return (-1);
va_start(args, format);
if (!list)
list = add_list();
begin_list = list;
make_format((char *)format, &begin_list);
make_cast(&begin_list, args, "sSpdDioOuUxXcCib%", 0);
while (list)
{
final_print(list, &len);
list = list->next;
}
return (len);
}