-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconversion.c
102 lines (95 loc) · 3.7 KB
/
conversion.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* conversion.c :+: :+: */
/* +:+ */
/* By: mraasvel <[email protected]> +#+ */
/* +#+ */
/* Created: 2020/11/14 09:01:17 by mraasvel #+# #+# */
/* Updated: 2020/11/22 17:36:00 by mraasvel ######## odam.nl */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "ft_conversion.h"
static int invalid_conversion(char conversion_type, t_flags flags)
{
if (flags.minus == 0 && flags.field_width > 1)
if (put_fw(flags.field_width - 1, flags.zero) == -1)
return (-1);
if (write(1, &conversion_type, 1) == -1)
return (-1);
if (flags.minus == 1 && flags.field_width > 1)
if (put_fw(flags.field_width - 1, 0) == -1)
return (-1);
return (1 > flags.field_width ? 1 : flags.field_width);
}
/*
** Reads conversion char and calls it's function.
** Returns bytes written if conversion char is valid.
** Returns -1 if conversion char is invalid.
*/
static int conversion_caller(char conversion_type,
va_list start, t_flags flags, int n)
{
if (conversion_type == 'c')
return (convert_character(start, flags));
if (conversion_type == 's')
return (convert_string(start, flags));
if (conversion_type == 'p')
return (convert_pointer(start, flags));
if (conversion_type == 'd')
return (convert_digit(start, flags));
if (conversion_type == 'i')
return (convert_digit(start, flags));
if (conversion_type == 'u')
return (convert_udigit(start, flags));
if (conversion_type == 'x')
return (convert_hex(start, flags, 0));
if (conversion_type == 'X')
return (convert_hex(start, flags, 1));
if (conversion_type == 'n')
return (convert_n(start, flags, n));
if (conversion_type == 'f')
return (convert_f(start, flags));
if (conversion_type == 'g')
return (0);
if (conversion_type == 'e')
return (convert_e(start, flags));
return (-2);
}
/*
** t_flags contains boolean values for each flag.
** Reads in flags in sequence like real printf:
** 1. Sequence one: '#0- +'.
** 2. Sequence two: Minimum Field Width and Precision.
** if the values are too large, it will return an error.
** 3. Sequence three: Length Modifiers ('l', 'll', 'h' and 'hh').
** In the sequence functions:
** index i is incremented to point to the conversion char.
** Definitions of these functions are in flagsequence.c
**
** If the conversion character is invalid, it will print the S1 and S2 flags,
** only one copy of each flag, in the expected order.
**
** If the flags and conversion character are correct,
** the appropriate conversion function is called.
** Return value is equal to the bytes written, or -1 on error.
** Index i will be incremented to point to the next char.
*/
int conversion(const char *format, size_t *i, va_list start, int n)
{
t_flags flags;
int return_value;
flags = flags_sequence_one(format, i);
flags = flags_sequence_two(format, i, flags, start);
if (flags.field_width == -2 || flags.precision == -2)
return (-1);
flags = flags_sequence_three(format, i, flags);
return_value = conversion_caller(format[*i], start, flags, n);
if (return_value == -2)
return_value = invalid_conversion(format[*i], flags);
else if (return_value == -1)
return (-1);
(*i)++;
return (return_value);
}