-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_printf.c
131 lines (121 loc) · 3.19 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
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
125
126
127
128
129
130
131
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vkinnune <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/02 13:26:28 by vkinnune #+# #+# */
/* Updated: 2022/02/08 12:27:55 by vkinnune ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_printf(const char *str, ...)
{
char *ptr;
va_list args;
t_info var;
int size;
va_start(args, str);
var.args = &args;
ptr = (char *)str;
size = 0;
while (*ptr != '\0')
{
if (*ptr != '%')
{
write(1, ptr, 1);
ptr++;
size++;
continue ;
}
setdefaultvalues(&var);
if (!init_print(&var, &ptr))
return (-1);
size += var.size;
}
va_end(args);
return (size);
}
/*
1. Check flags: Sets flags to struct and returns
pointer when no more flags found
2. Get width: Gets width or simply numbers and saves
them in the struct
3. Get precision: Finds a dot then gets gets numbers
for precision. Also returns a pointer
4. Do print: Does the print with given flags and options
5. Get modifier: Gets either ll, hh, l or h
*/
int init_print(t_info *var, char **ptr)
{
char *p;
*ptr = check_flags(var, *ptr);
*ptr = get_width(var, *ptr);
*ptr = get_precision(var, *ptr);
*ptr = get_modifier(var, *ptr);
if (!doprint(var, *ptr))
{
p = ft_strchr(*ptr, '%');
if (p)
{
printstring(var, "%", 1);
*ptr = p + 1;
return (1);
}
return (0);
}
*ptr += 1;
return (1);
}
int doprint(t_info *var, char *ptr)
{
if (*ptr == 'd' || *ptr == 'i')
return (signed_int(var));
else if (*ptr == 'o' || *ptr == 'u' || *ptr == 'x'
|| *ptr == 'X' || *ptr == 'p')
return (unsigned_int(var, ptr));
else if (*ptr == 'f')
return (decimal(var));
else if (*ptr == 'c')
return (character(var));
else if (*ptr == 's')
return (string(var));
return (0);
}
/*
1. buf[MAXBUF]: build number there
*/
int donumber(t_info *var, unsigned long long u)
{
char buf[MAXBUF];
char *p;
char digits[32];
ft_strcpy(digits, "0123456789abcdef0123456789ABCDEF");
p = &buf[MAXBUF - 1];
if (var->alt)
alt(var, u);
*p-- = digits[(u % var->base) + var->capitals];
u /= var->base;
while (u != 0)
{
*p-- = digits[(u % var->base) + var->capitals];
u /= var->base;
}
if (var->prec > 0)
var->padc = ' ';
return (printnum(var, buf, p, (int)((&buf[MAXBUF - 1]) - p)));
}
void alt(t_info *var, unsigned long long u)
{
if (var->base == 8 && var->alt)
{
var->prefix = "0";
if (var->prec != 0)
var->prec -= 1;
}
else if (var->base == 16 && var->capitals == 0 && u != 0)
var->prefix = "0x";
else if (var->base == 16 && var->capitals == 16 && u != 0)
var->prefix = "0X";
}