-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathint _printf(const char *format, ...cpp
More file actions
64 lines (55 loc) · 1.78 KB
/
Copy pathint _printf(const char *format, ...cpp
File metadata and controls
64 lines (55 loc) · 1.78 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
int _printf(const char *format, ...)
{
conversion_specifier conversion_table[] = {
{"%s", print_string}, {"%c", print_char}, {"%%", print_37},
{"%i", print_int}, {"%d", print_decimal}, {"%b", print_binary},
{"%u", print_unsigned}, {"%o", print_oct}, {"%x", print_lower_hexa},
{"%X", print_upper_hexa}, {"%p", print_pointer},
{"%S", print_string_with_escape}
};
int i = 0, length = 0;
int j;
va_list args;
va_start(args, format);
if (format == NULL || (format[0] == '%' && format[1] == '\0'))
return (-1);
while (format[i] != '\0')
{
if (format[i] == '%')
{
bool flag_plus = false;
bool flag_space = false;
bool flag_hash = false;
i++;
while (format[i] == '+' || format[i] == ' ' || format[i] == '#')
{
if (format[i] == '+')
flag_plus = true;
else if (format[i] == ' ')
flag_space = true;
else if (format[i] == '#')
flag_hash = true;
i++;
}
j = 10;
while (j >= 0)
{
if (conversion_table[j].specifier[0] == format[i] &&
conversion_table[j].specifier[1] == format[i + 1])
{
length += handle_plus_flag(flag_plus, handle_space_flag(flag_space, handle_hash_flag(flag_hash, conversion_table[j].fptr(args))));
i = i + 2;
break;
}
j--;
}
if (j >= 0)
continue;
}
_putchar(format[i]);
length++;
i++;
}
va_end(args);
return (length);
}