-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_e.c
92 lines (84 loc) · 2.85 KB
/
convert_e.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* convert_e.c :+: :+: */
/* +:+ */
/* By: mraasvel <[email protected]> +#+ */
/* +#+ */
/* Created: 2020/11/21 13:17:37 by mraasvel #+# #+# */
/* Updated: 2020/11/22 13:19:43 by mraasvel ######## odam.nl */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "ft_double.h"
#include "libft.h"
static int get_echars_count(t_double nbr, t_flags flags, char *digits)
{
int len;
len = ft_strlen(digits);
if (flags.precision == 0 && flags.hash == 0)
len--;
if (nbr.bits.sign == 1 || flags.plus == 1 || flags.space == 1)
len++;
return (len);
}
static int put_double_sn(t_double nbr, t_flags flags, char *digits, int chars)
{
if (nbr.bits.sign == 1)
if (write(1, "-", 1) == -1)
return (-1);
if (nbr.bits.sign == 0 && flags.plus == 1)
if (write(1, "+", 1) == -1)
return (-1);
if (nbr.bits.sign == 0 && flags.space == 1)
if (write(1, " ", 1) == -1)
return (-1);
if (flags.zero == 1 && flags.field_width > chars)
if (put_fw(flags.field_width - chars, flags.zero) == -1)
return (-1);
if (flags.hash == 0 && flags.precision == 0)
{
if (write(1, digits, 1) == -1)
return (-1);
if (ft_putstr(digits + 2) == -1)
return (-1);
}
else if (ft_putstr(digits) == -1)
return (-1);
return (chars);
}
static int output_e(t_double nbr, t_flags flags, char *digits)
{
int chars;
chars = get_echars_count(nbr, flags, digits);
if (flags.minus == 0 && flags.zero == 0 && flags.field_width > chars)
if (put_fw(flags.field_width - chars, 0) == -1)
return (-1);
if (put_double_sn(nbr, flags, digits, chars) == -1)
return (-1);
if (flags.minus == 1 && flags.field_width > chars)
if (put_fw(flags.field_width - chars, 0) == -1)
return (-1);
return (chars > flags.field_width ? chars : flags.field_width);
}
int convert_e(va_list start, t_flags flags)
{
t_double nbr;
char *digits;
int chars;
nbr.value = va_arg(start, double);
if (flags.minus == 1 || nbr.bits.exponent == 2047)
flags.zero = 0;
if (flags.plus == 1)
flags.space = 0;
if (flags.precision < 0)
flags.precision = 6;
if (nbr.bits.exponent == 2047)
return (inf_nan(nbr, flags));
digits = ft_dtoa_e(nbr.value, flags.precision);
if (digits == NULL)
return (-1);
chars = output_e(nbr, flags, digits);
free(digits);
return (chars);
}