This repository has been archived by the owner on Nov 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathft_print_str.c
104 lines (91 loc) · 2.72 KB
/
ft_print_str.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_str.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcombeau <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/11 16:01:33 by mcombeau #+# #+# */
/* Updated: 2021/12/17 14:26:33 by mcombeau ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int ft_print_string(const char *str, t_flags flags)
{
int count;
count = 0;
if (flags.precision >= 0)
{
count += ft_pad_width(flags.precision, ft_strlen(str), 0);
count += ft_print_s_pre(str, flags.precision);
}
else
count += ft_print_s_pre(str, ft_strlen(str));
return (count);
}
#if defined(__linux__) || defined(__gnu_linux__)
int ft_print_str(const char *str, t_flags flags)
{
int count;
count = 0;
if (str == NULL && flags.precision >= 0 && flags.precision < 6)
{
count += ft_pad_width(flags.width, 0, 0);
return (count);
}
if (str == NULL)
str = "(null)";
if (flags.precision >= 0 && (size_t)flags.precision > ft_strlen(str))
flags.precision = ft_strlen(str);
if (flags.left == 1)
count += ft_print_string(str, flags);
if (flags.precision >= 0)
count += ft_pad_width(flags.width, flags.precision, 0);
else
count += ft_pad_width(flags.width, ft_strlen(str), 0);
if (flags.left == 0)
count += ft_print_string(str, flags);
return (count);
}
#else
int ft_print_str(const char *str, t_flags flags)
{
int count;
count = 0;
if (str == NULL)
str = "(null)";
if (flags.precision >= 0 && (size_t)flags.precision > ft_strlen(str))
flags.precision = ft_strlen(str);
if (flags.left == 1)
count += ft_print_string(str, flags);
if (flags.precision >= 0)
count += ft_pad_width(flags.width, flags.precision, 0);
else
count += ft_pad_width(flags.width, ft_strlen(str), 0);
if (flags.left == 0)
count += ft_print_string(str, flags);
return (count);
}
#endif
int ft_print_s_pre(const char *str, int precision)
{
int count;
count = 0;
while (str[count] && count < precision)
write(1, &str[count++], 1);
return (count);
}
int ft_print_s(const char *str)
{
int len;
if (str == NULL)
{
write(1, "(null)", 6);
return (6);
}
len = 0;
while (str[len])
len++;
write(1, str, len);
return (len);
}