-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.c
87 lines (82 loc) · 2.61 KB
/
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* string.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: moboustt <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/07 18:36:25 by moboustt #+# #+# */
/* Updated: 2019/12/20 20:20:28 by moboustt ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int is_string(t_struct *list, va_list ap)
{
char *out_str;
int str_len;
out_str = (char *)va_arg(ap, char*);
if (out_str == NULL)
out_str = "(null)";
str_len = ft_strlen(out_str);
if (str_len == 0)
out_str = ft_strdup("");
if (list->width == -1 && list->precision == -1 && !list->dot)
{
list->n += write(1, out_str, str_len);
return (0);
}
else
{
if (list->minus == 1)
goto_right(list, out_str, str_len);
else
goto_left(list, out_str, str_len);
}
return (0);
}
void goto_right(t_struct *list, char *out_str, int str_len)
{
if (list->precision > 0 && str_len == 0)
list->precision = 0;
if ((list->precision >= 0 || list->precision == -1) && list->dot == 1)
{
list->precision == -1 ? list->precision = 0 : list->precision;
list->precision > str_len ? list->precision = str_len : list->precision;
list->n += write(1, out_str, list->precision);
}
else
list->n += write(1, out_str, str_len);
if (list->precision > str_len)
list->precision = str_len;
else if (list->dot == 0)
list->width -= str_len;
while (list->width > list->precision && list->width > 0)
{
list->n += write(1, " ", 1);
list->width--;
}
}
void goto_left(t_struct *list, char *out_str, int str_len)
{
if (list->dot && str_len > list->precision)
str_len = list->precision;
if (list->dot == 1)
list->precision == -1 ? str_len = 0 : str_len;
if (list->width > str_len)
{
while ((list->width - str_len) > 0)
{
list->n += write(1, " ", 1);
list->width--;
}
}
if (str_len != 0)
{
if (list->precision > str_len)
list->precision = str_len;
if ((list->precision > 0 || list->precision == -1) && list->dot == 1)
list->n += write(1, out_str, list->precision);
else
list->n += write(1, out_str, str_len);
}
}