aboutsummaryrefslogtreecommitdiffstats
path: root/libft/src/get_next_line_utils.c
blob: 584b6915f4543541b2fe8eb4ca5ef6caf1c4b3bc (plain)
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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   get_next_line_utils.c                              :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: joelecle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:07:20 by joelecle          #+#    #+#             */
/*   Updated: 2020/02/14 17:07:20 by joelecle         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <stdlib.h>
#include <stddef.h>

size_t
	ft_strlen_gnl(const char *s, char c)
{
	size_t	i;

	i = 0;
	if (!s)
		return (0);
	while (s[i] != c && s[i] != 0)
		i++;
	return (i);
}

int
	ft_free_gnl(int fd, t_gnl **list)
{
	t_gnl	*scout;
	t_gnl	*prev;

	prev = *list;
	if (prev->fd == fd)
	{
		scout = prev->next;
		ft_memdel((void**)&prev->rest);
		ft_memdel((void**)&prev);
		*list = scout;
		return (0);
	}
	scout = prev->next;
	while (scout->fd != fd)
	{
		prev = prev->next;
		scout = scout->next;
	}
	prev->next = scout->next;
	ft_memdel((void**)&scout->rest);
	ft_memdel((void**)&scout);
	return (0);
}

t_gnl
	*ft_find_fd(int fd, t_gnl **list)
{
	t_gnl	*curr;
	t_gnl	*new;

	if (!(new = malloc(sizeof(*new))))
		return (NULL);
	new->fd = fd;
	new->rest = 0;
	new->next = NULL;
	if (!*list)
	{
		*list = new;
		return (*list);
	}
	curr = *list;
	if (fd != curr->fd)
	{
		while (curr->next != NULL && fd != curr->fd)
			curr = curr->next;
		if (curr->next == NULL)
			return (curr->next = new);
	}
	ft_memdel((void**)&new);
	return (curr);
}

char
	*ft_strchr_gnl(const char *s, int c)
{
	unsigned int	i;

	i = 0;
	c = (char)c;
	if (!s)
		return (NULL);
	while (s[i] != 0 && s[i] != c)
		i++;
	if (s[i] != c)
		return (NULL);
	return ((char *)s + i);
}

char
	*ft_swap_gnl(char *s1, char *free_ft)
{
	char	*dst;
	int		i;

	if (!(dst = malloc(ft_strlen_gnl(s1, 0) + 1)))
		return (NULL);
	i = 0;
	while (s1[i])
	{
		dst[i] = s1[i];
		i++;
	}
	dst[i] = 0;
	ft_memdel((void**)&free_ft);
	return (dst);
}