-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_action_bonus.c
125 lines (114 loc) · 3.2 KB
/
ft_action_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_action_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edelangh <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/01/01 12:41:21 by edelangh #+# #+# */
/* Updated: 2015/01/23 13:34:42 by edelangh ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include <time.h>
char *ft_itodate(char *i)
{
char *date;
int len;
date = NULL;
if ((len = ft_strlen(i)) % 2 == 1)
i = ft_strfjoin("0", i, 0, 1);
len = ft_strlen(i);
if (len >= 2)
date = ft_strsub(i, len - 2, 2);
if (len >= 2 + 2)
{
date = ft_strfjoinnchar(date, -1, '/');
date = ft_strfjoin(date, ft_strsub(i, len - 4, 2), 1, 1);
}
if (len >= 2 + 2 + 2)
{
date = ft_strfjoinnchar(date, -1, '/');
if (len >= 4 + 2 + 2)
date = ft_strfjoin(date, ft_strsub(i, 0, 4), 1, 1);
else
date = ft_strfjoin(date, ft_strsub(i, 0, 2), 1, 1);
}
free(i);
return (date);
}
int ft_action_t(char *f, int lenud, va_list ap)
{
t_printf info;
int len;
time_t nbr;
info.type = 't';
init_info(&info, f, lenud, ap);
nbr = va_arg(ap, time_t);
info.value = ctime(&nbr);
len = ft_strlen(info.value);
info.value[len - 1] = '\0';
info.add = '\0';
info.is_preci = 0;
ft_print_d(info, len, nbr, "");
free(info.value);
return (len);
}
int ft_action_k(char *f, int lenud, va_list ap)
{
t_printf info;
int len;
unsigned int nbr;
info.type = 'k';
init_info(&info, f, lenud, ap);
nbr = va_arg(ap, unsigned int);
info.value = ft_zutoa((size_t)nbr);
info.value = ft_itodate(info.value);
len = ft_strlen(info.value);
info.add = '\0';
info.preci = 0;
ft_print_d(info, len, nbr, "");
free(info.value);
return (len);
}
int ft_action_b(char *f, int lenud, va_list ap)
{
t_printf info;
int len;
unsigned int nbr;
char *tmp;
info.type = 'b';
init_info(&info, f, lenud, ap);
if ((nbr = ft_get_value_ou(ap, &info)) == 2)
{
nbr = va_arg(ap, unsigned int);
info.value = ft_zutoa((size_t)nbr);
}
tmp = info.value;
info.value = ft_convert_base(info.value, "0123456789", "01");
free(tmp);
len = ft_strlen(info.value);
info.add = '\0';
ft_print_d(info, len, nbr, (info.mark && nbr ? "b" : ""));
free(info.value);
return (len);
}
int ft_action_f(char *f, int lenud, va_list ap)
{
t_printf info;
int len;
long double nbr;
char *tmp;
info.type = 'f';
init_info(&info, f, lenud, ap);
nbr = ft_get_read(&info, ap);
info.value = ft_ftoa(nbr);
if (info.is_preci)
info.value = ft_fpreci(info.value, info.preci);
len = ft_strlen(info.value);
info.is_preci = 0;
ft_print_d(info, len, nbr != 0, (tmp = ft_strjoinnchar(NULL, 1, info.add)));
free(info.value);
free(tmp);
return (len);
}