-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprint_string.c
63 lines (58 loc) · 1.93 KB
/
print_string.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_string.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: parmarti <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/07/15 09:48:36 by parmarti #+# #+# */
/* Updated: 2020/07/25 18:22:17 by parmarti ### ########.fr */
/* */
/* ************************************************************************** */
#include "printf.h"
static char *precision_string(t_data *data, char *str)
{
char *ptr;
if (data->precision > -1 && str)
{
if (!(ptr = malloc(data->precision + 1)))
return (NULL);
ft_strlcpy(ptr, str, data->precision + 1);
return (ptr);
}
else if (data->precision == -1 && str)
return (ft_strdup(str));
else if (data->precision == -1 && !str)
return (ft_strdup("(null)"));
else if (data->precision > -1 && !str)
{
str = ft_strdup("(null)");
if (!(ptr = malloc(data->precision + 1)))
return (NULL);
ft_strlcpy(ptr, str, data->precision + 1);
free(str);
return (ptr);
}
return (str);
}
t_data *print_string(t_data *data)
{
char *str;
int len;
str = va_arg(data->args, char *);
str = precision_string(data, str);
len = ft_strlen(str);
data->length = data->length + len;
if (data->minus == 0)
{
if (data->zero == 0)
print_flags(data, ' ', data->width - len);
else
print_flags(data, '0', data->width - len);
}
ft_putstr_fd(str, 1);
if (data->minus == 1)
print_flags(data, ' ', data->width - len);
free(str);
return (data);
}