-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils.c
125 lines (114 loc) · 2.57 KB
/
get_next_line_utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: muhozkan <muhozkan@student.42.tr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/27 14:23:43 by muhozkan #+# #+# */
/* Updated: 2022/02/27 14:24:26 by muhozkan ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
size_t ft_strlen(char *s)
{
size_t i;
i = 0;
if (!s)
return (0);
while (s[i] != '\0')
i++;
return (i);
}
char *ft_strchr(char *s, int c)
{
int i;
i = 0;
if (!s)
return (0);
if (c == '\0')
return ((char *)&s[ft_strlen(s)]);
while (s[i] != '\0')
{
if (s[i] == (char) c)
return ((char *)&s[i]);
i++;
}
return (0);
}
char *ft_strjoin(char *left_str, char *buff)
{
size_t i;
size_t j;
char *str;
if (!left_str)
{
left_str = (char *)malloc(1 * sizeof(char));
left_str[0] = '\0';
}
if (!left_str || !buff)
return (NULL);
str = malloc(sizeof(char) * ((ft_strlen(left_str) + ft_strlen(buff)) + 1));
if (str == NULL)
return (NULL);
i = -1;
j = 0;
if (left_str)
while (left_str[++i] != '\0')
str[i] = left_str[i];
while (buff[j] != '\0')
str[i++] = buff[j++];
str[ft_strlen(left_str) + ft_strlen(buff)] = '\0';
free(left_str);
return (str);
}
char *ft_get_line(char *left_str)
{
int i;
char *str;
i = 0;
if (!left_str[i])
return (NULL);
while (left_str[i] && left_str[i] != '\n')
i++;
str = (char *)malloc(sizeof(char) * (i + 2));
if (!str)
return (NULL);
i = 0;
while (left_str[i] && left_str[i] != '\n')
{
str[i] = left_str[i];
i++;
}
if (left_str[i] == '\n')
{
str[i] = left_str[i];
i++;
}
str[i] = '\0';
return (str);
}
char *ft_new_left_str(char *left_str)
{
int i;
int j;
char *str;
i = 0;
while (left_str[i] && left_str[i] != '\n')
i++;
if (!left_str[i])
{
free(left_str);
return (NULL);
}
str = (char *)malloc(sizeof(char) * (ft_strlen(left_str) - i + 1));
if (!str)
return (NULL);
i++;
j = 0;
while (left_str[i])
str[j++] = left_str[i++];
str[j] = '\0';
free(left_str);
return (str);
}