-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils.c
101 lines (92 loc) · 2.33 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ibeliaie <ibeliaie@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/04 18:43:47 by ibeliaie #+# #+# */
/* Updated: 2023/06/19 12:52:37 by ibeliaie ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
/* get string length */
size_t ft_strlen(const char *str)
{
size_t count;
count = 0;
if (!str)
return (0);
while (str[count])
count++;
return (count);
}
/* locate first occurrence of character in string */
char *ft_strchr(char *str, int find)
{
int i;
i = 0;
if (!str)
return (0);
if (find == '\0')
return ((char *)&str[ft_strlen(str)]);
while (str[i] != '\0')
{
if (str[i] == (char) find)
return ((char *)&str[i]);
i++;
}
return (0);
}
/* combine 2 strings into single string */
char *ft_strjoin(char *rest, char *buff)
{
size_t i;
size_t j;
char *newstr;
if (!rest)
{
rest = (char *)malloc(1 * sizeof(char));
rest[0] = '\0';
}
if (!rest || !buff)
return (NULL);
newstr = malloc(sizeof(char) * ((ft_strlen(rest) + ft_strlen(buff)) + 1));
if (newstr == NULL)
return (NULL);
i = -1;
j = 0;
if (rest)
while (rest[++i] != '\0')
newstr[i] = rest[i];
while (buff[j] != '\0')
newstr[i++] = buff[j++];
newstr[ft_strlen(rest) + ft_strlen(buff)] = '\0';
free(rest);
return (newstr);
}
/* remove first line from rest and return remaining content */
char *ft_new_rest(char *rest)
{
int i;
int j;
char *str;
i = 0;
while (rest[i] && rest[i] != '\n')
i++;
if (!rest[i])
{
free(rest);
return (NULL);
}
str = (char *)malloc(sizeof(char) * (ft_strlen(rest) - i + 1));
if (!str)
return (NULL);
i++;
j = 0;
while (rest[i])
str[j++] = rest[i++];
str[j] = '\0';
free(rest);
return (str);
}