comment main
This commit is contained in:
@@ -6,55 +6,55 @@
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/12/11 00:27:09 by hulamy #+# #+# */
|
||||
/* Updated: 2019/12/15 19:00:37 by hulamy ### ########.fr */
|
||||
/* Updated: 2019/12/16 13:54:20 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "get_next_line.h"
|
||||
|
||||
|
||||
#include <stdio.h> //for printf
|
||||
#include <fcntl.h> //for open
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
int *fd;
|
||||
int i;
|
||||
int j;
|
||||
char *line;
|
||||
|
||||
if (ac < 2)
|
||||
return (0);
|
||||
line = NULL;
|
||||
i = 0;
|
||||
if (!(fd = (int *)malloc(sizeof(int) * ac)))
|
||||
return (0);
|
||||
while (++i <= ac - 1)
|
||||
fd[i - 1] = open(av[i], O_RDONLY);
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (j < ac - 1)
|
||||
{
|
||||
if (get_next_line(fd[i], &line) > 0)
|
||||
{
|
||||
printf("fd%i| %s\n", fd[i], line);
|
||||
free(line);
|
||||
j = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("fd%i|*FINI*\n", fd[i]);
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
if (i >= ac - 1)
|
||||
i = 0;
|
||||
}
|
||||
free (fd);
|
||||
// while (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
** #include <stdio.h> //for printf
|
||||
** #include <fcntl.h> //for open
|
||||
**
|
||||
** int main(int ac, char **av)
|
||||
** {
|
||||
** int *fd;
|
||||
** int i;
|
||||
** int j;
|
||||
** char *line;
|
||||
**
|
||||
** if (ac < 2)
|
||||
** return (0);
|
||||
** line = NULL;
|
||||
** i = 0;
|
||||
** if (!(fd = (int *)malloc(sizeof(int) * ac)))
|
||||
** return (0);
|
||||
** while (++i <= ac - 1)
|
||||
** fd[i - 1] = open(av[i], O_RDONLY);
|
||||
** i = 0;
|
||||
** j = 0;
|
||||
** while (j < ac - 1)
|
||||
** {
|
||||
** if (get_next_line(fd[i], &line) > 0)
|
||||
** {
|
||||
** printf("fd%i| %s\n", fd[i], line);
|
||||
** free(line);
|
||||
** j = 0;
|
||||
** }
|
||||
** else
|
||||
** {
|
||||
** printf("fd%i|*FINI*\n", fd[i]);
|
||||
** j++;
|
||||
** }
|
||||
** i++;
|
||||
** if (i >= ac - 1)
|
||||
** i = 0;
|
||||
** }
|
||||
** free (fd);
|
||||
** // while (1);
|
||||
** return (0);
|
||||
** }
|
||||
*/
|
||||
|
||||
int multi_fd(int fd, t_gnlist **lst)
|
||||
{
|
||||
|
||||
427
leaks.txt
427
leaks.txt
@@ -1,427 +0,0 @@
|
||||
fd3| /* ************************************************************************** */
|
||||
fd4| /* ************************************************************************** */
|
||||
fd5| /* ************************************************************************** */
|
||||
fd3| /* */
|
||||
fd4| /* */
|
||||
fd5| /* */
|
||||
fd3| /* ::: :::::::: */
|
||||
fd4| /* ::: :::::::: */
|
||||
fd5| /* ::: :::::::: */
|
||||
fd3| /* get_next_line.c :+: :+: :+: */
|
||||
fd4| /* get_next_line.h :+: :+: :+: */
|
||||
fd5| /* get_next_line_utils.c :+: :+: :+: */
|
||||
fd3| /* +:+ +:+ +:+ */
|
||||
fd4| /* +:+ +:+ +:+ */
|
||||
fd5| /* +:+ +:+ +:+ */
|
||||
fd3| /* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
fd4| /* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
fd5| /* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
fd3| /* +#+#+#+#+#+ +#+ */
|
||||
fd4| /* +#+#+#+#+#+ +#+ */
|
||||
fd5| /* +#+#+#+#+#+ +#+ */
|
||||
fd3| /* Created: 2019/12/11 00:27:09 by hulamy #+# #+# */
|
||||
fd4| /* Created: 2019/01/29 10:19:37 by hulamy #+# #+# */
|
||||
fd5| /* Created: 2019/12/11 00:26:54 by hulamy #+# #+# */
|
||||
fd3| /* Updated: 2019/12/12 23:01:04 by hulamy ### ########.fr */
|
||||
fd4| /* Updated: 2019/12/11 00:56:04 by hulamy ### ########.fr */
|
||||
fd5| /* Updated: 2019/12/11 00:57:19 by hulamy ### ########.fr */
|
||||
fd3| /* */
|
||||
fd4| /* */
|
||||
fd5| /* */
|
||||
fd3| /* ************************************************************************** */
|
||||
fd4| /* ************************************************************************** */
|
||||
fd5| /* ************************************************************************** */
|
||||
fd3|
|
||||
fd4|
|
||||
fd5|
|
||||
fd3| #include "get_next_line.h"
|
||||
fd4| #ifndef GET_NEXT_LINE_H
|
||||
fd5| #include "get_next_line.h"
|
||||
fd3|
|
||||
fd4| # define GET_NEXT_LINE_H
|
||||
fd5|
|
||||
fd3|
|
||||
fd4|
|
||||
fd5| char *ft_strdup(const char *src)
|
||||
fd3| #include <stdio.h> //for printf
|
||||
fd4| # include <string.h>
|
||||
fd5| {
|
||||
fd3| #include <fcntl.h> //for open
|
||||
fd4| # include <unistd.h>
|
||||
fd5| int i;
|
||||
fd3|
|
||||
fd4| # include <stdlib.h>
|
||||
fd5| char *str;
|
||||
fd3| int main(int ac, char **av)
|
||||
fd4|
|
||||
fd5|
|
||||
fd3| {
|
||||
fd4| # ifndef BUFF_SIZE
|
||||
fd5| i = 0;
|
||||
fd3| int *fd;
|
||||
fd4| # define BUFF_SIZE 9999
|
||||
fd5| while (src[i] != '\0')
|
||||
fd3| int i;
|
||||
fd4| # endif
|
||||
fd5| i++;
|
||||
fd3| int j;
|
||||
fd4|
|
||||
fd5| if (!(str = (char*)malloc(sizeof(*str) * (i + 1))))
|
||||
fd3| char *line;
|
||||
fd4| typedef struct s_gnlist
|
||||
fd5| return (NULL);
|
||||
fd3|
|
||||
fd4| {
|
||||
fd5| while (i-- >= 0)
|
||||
fd3| if (ac < 2)
|
||||
fd4| int lfd;
|
||||
fd5| str[i + 1] = src[i + 1];
|
||||
fd3| return (0);
|
||||
fd4| char *string;
|
||||
fd5| return (str);
|
||||
fd3| line = NULL;
|
||||
fd4| struct s_gnlist *next;
|
||||
fd5| }
|
||||
fd3| i = 0;
|
||||
fd4| } t_gnlist;
|
||||
fd5|
|
||||
fd3| if (!(fd = (int *)malloc(sizeof(int) * ac)))
|
||||
fd4|
|
||||
fd5| char *ft_strchr(const char *s, int c)
|
||||
fd3| return (0);
|
||||
fd4| int get_next_line(const int fd, char **line);
|
||||
fd5| {
|
||||
fd3| while (++i <= ac - 1)
|
||||
fd4| int multi_fd(int fd, t_gnlist **lst);
|
||||
fd5| int i;
|
||||
fd3| fd[i - 1] = open(av[i], O_RDONLY);
|
||||
fd4| char *ft_strjoinfree(char const *s1, char const *s2);
|
||||
fd5| int j;
|
||||
fd3| i = 0;
|
||||
fd4| int hasnewline(char *str, t_gnlist *lst, char **line);
|
||||
fd5|
|
||||
fd3| j = 0;
|
||||
fd4|
|
||||
fd5| i = 0;
|
||||
fd3| while (j < ac - 1)
|
||||
fd4| size_t ft_strlen(const char *str);
|
||||
fd5| j = -1;
|
||||
fd3| {
|
||||
fd4| void *ft_memmove(void *dst, const void *src, size_t len);
|
||||
fd5| while (s[i])
|
||||
fd3| if (get_next_line(fd[i], &line) > 0)
|
||||
fd4| char *ft_strchr(const char *s, int c);
|
||||
fd5| i++;
|
||||
fd3| {
|
||||
fd4| char *ft_strdup(const char *src);
|
||||
fd5| while (++j < i + 1)
|
||||
fd3| printf("fd%i| %s\n", fd[i], line);
|
||||
fd4|
|
||||
fd5| if (s[j] == c)
|
||||
fd3| free(line);
|
||||
fd4| #endif
|
||||
fd5| return ((char *)s + j);
|
||||
fd3| j = 0;
|
||||
fd4|*FINI*
|
||||
fd5| return (NULL);
|
||||
fd3| }
|
||||
fd4|*FINI*
|
||||
fd5| }
|
||||
fd3| else
|
||||
fd4|*FINI*
|
||||
fd5|
|
||||
fd3| {
|
||||
fd4|*FINI*
|
||||
fd5| void *ft_memmove(void *dst, const void *src, size_t len)
|
||||
fd3| printf("fd%i|*FINI*\n", fd[i]);
|
||||
fd4|*FINI*
|
||||
fd5| {
|
||||
fd3| j++;
|
||||
fd4|*FINI*
|
||||
fd5| size_t i;
|
||||
fd3| }
|
||||
fd4|*FINI*
|
||||
fd5| char *cpsrc;
|
||||
fd3| i++;
|
||||
fd4|*FINI*
|
||||
fd5| char *cpdst;
|
||||
fd3| if (i >= ac - 1)
|
||||
fd4|*FINI*
|
||||
fd5|
|
||||
fd3| i = 0;
|
||||
fd4|*FINI*
|
||||
fd5| i = -1;
|
||||
fd3| }
|
||||
fd4|*FINI*
|
||||
fd5| cpsrc = (char *)src;
|
||||
fd3| free (fd);
|
||||
fd4|*FINI*
|
||||
fd5| cpdst = (char *)dst;
|
||||
fd3| // while (1);
|
||||
fd4|*FINI*
|
||||
fd5| if (dst == src)
|
||||
fd3| return (0);
|
||||
fd4|*FINI*
|
||||
fd5| return (dst);
|
||||
fd3| }
|
||||
fd4|*FINI*
|
||||
fd5| if (cpsrc < cpdst)
|
||||
fd3|
|
||||
fd4|*FINI*
|
||||
fd5| while (len--)
|
||||
fd3|
|
||||
fd4|*FINI*
|
||||
fd5| cpdst[len] = cpsrc[len];
|
||||
fd3| int multi_fd(int fd, t_gnlist **lst)
|
||||
fd4|*FINI*
|
||||
fd5| else
|
||||
fd3| {
|
||||
fd4|*FINI*
|
||||
fd5| while (++i < len)
|
||||
fd3| t_gnlist *tmp;
|
||||
fd4|*FINI*
|
||||
fd5| cpdst[i] = cpsrc[i];
|
||||
fd3|
|
||||
fd4|*FINI*
|
||||
fd5| return (dst);
|
||||
fd3| tmp = *lst;
|
||||
fd4|*FINI*
|
||||
fd5| }
|
||||
fd3| while (*lst && (*lst)->lfd != fd && (*lst)->next != tmp)
|
||||
fd4|*FINI*
|
||||
fd5|
|
||||
fd3| *lst = (*lst)->next;
|
||||
fd4|*FINI*
|
||||
fd5| size_t ft_strlen(const char *str)
|
||||
fd3| if (!tmp || ((*lst)->next == tmp && (*lst)->lfd != fd))
|
||||
fd4|*FINI*
|
||||
fd5| {
|
||||
fd3| {
|
||||
fd4|*FINI*
|
||||
fd5| size_t i;
|
||||
fd3| if (!(tmp = (t_gnlist*)malloc(sizeof(*tmp))))
|
||||
fd4|*FINI*
|
||||
fd5|
|
||||
fd3| return (0);
|
||||
fd4|*FINI*
|
||||
fd5| i = 0;
|
||||
fd3| tmp->lfd = fd;
|
||||
fd4|*FINI*
|
||||
fd5| while (str[i])
|
||||
fd3| if (!(tmp->string = ft_strdup("")))
|
||||
fd4|*FINI*
|
||||
fd5| i++;
|
||||
fd3| return (0);
|
||||
fd4|*FINI*
|
||||
fd5| return (i);
|
||||
fd3| if (*lst)
|
||||
fd4|*FINI*
|
||||
fd5| }
|
||||
fd3| {
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| tmp->next = (*lst)->next;
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| (*lst)->next = tmp;
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| }
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| else
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| tmp->next = tmp;
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| *lst = tmp;
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| }
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| //free(tmp);
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| return (1);
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| }
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3|
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| char *ft_strjoinfree(char const *s1, char const *s2)
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| {
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| char *str;
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| int len;
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| int j;
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3|
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| if (!s1 || !s2)
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| return (NULL);
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| len = ft_strlen(s1) + ft_strlen(s2);
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| return (NULL);
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| len = 0;
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| j = 0;
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| while (s1[j] != '\0')
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| str[len++] = s1[j++];
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| j = 0;
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| while (s2[j] != '\0')
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| str[len++] = s2[j++];
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| str[len] = '\0';
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| free((char*)s1);
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| return (str);
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| }
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3|
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| int hasnewline(char *str, t_gnlist *lst, char **line)
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| {
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| str[0] = '\0';
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| if (!(*line = ft_strdup(lst->string)))
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| return (-1);
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| ft_memmove(lst->string, str + 1, ft_strlen(str + 1) + 1);
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| return (1);
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| }
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3|
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| int get_next_line(const int fd, char **line)
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| {
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| char buf[BUFF_SIZE + 1];
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| int ret;
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| static t_gnlist *lst = NULL;
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| char *str;
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3|
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| if (!(multi_fd(fd, &lst)) || !(ret = 1) || !line || fd < 0)
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| return (-1);
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| while (ret > 0)
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| {
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| if ((str = ft_strchr(lst->string, '\n')))
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| return (hasnewline(str, lst, line));
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| if ((ret = read(fd, buf, BUFF_SIZE)) < 0)
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| return (-1);
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| buf[ret] = '\0';
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| if (!(lst->string = ft_strjoinfree(lst->string, buf)))
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| return (-1);
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| }
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| if (*(lst->string) && !(*line = ft_strdup(lst->string)))
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| return (-1);
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| if (*(lst->string) && ++ret == 1)
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| (lst->string)[0] = '\0';
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| return (ret);
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3| }
|
||||
fd4|*FINI*
|
||||
fd5|*FINI*
|
||||
fd3|*FINI*
|
||||
@@ -1,78 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line_bonus.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jamrabhi <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/12/05 12:54:26 by jamrabhi #+# #+# */
|
||||
/* Updated: 2019/12/09 21:53:57 by jamrabhi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "get_next_line_bonus.h"
|
||||
|
||||
static void free_it(char **str)
|
||||
{
|
||||
if (str)
|
||||
{
|
||||
free(*str);
|
||||
*str = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static int separate_lines(char **line, char **str, int fd)
|
||||
{
|
||||
int i;
|
||||
char *rest;
|
||||
|
||||
i = 0;
|
||||
while (str[fd][i] && str[fd][i] != '\n')
|
||||
i++;
|
||||
if (!(*line = ft_substr(str[fd], 0, i)))
|
||||
{
|
||||
free_it(&str[fd]);
|
||||
return (-1);
|
||||
}
|
||||
if (str[fd][i])
|
||||
{
|
||||
if (!(rest = ft_substr(str[fd], i + 1, ft_strlen(str[fd]) - (i + 1))))
|
||||
{
|
||||
free_it(&str[fd]);
|
||||
return (-1);
|
||||
}
|
||||
free_it(&str[fd]);
|
||||
str[fd] = rest;
|
||||
return (1);
|
||||
}
|
||||
free_it(&str[fd]);
|
||||
// free(*line);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int get_next_line(int fd, char **line)
|
||||
{
|
||||
static char *str[FOPEN_MAX];
|
||||
char buf[BUFFER_SIZE + 1];
|
||||
int i;
|
||||
char *tmp;
|
||||
|
||||
if (fd < 0 || fd >= FOPEN_MAX || !line || BUFFER_SIZE <= 0
|
||||
|| read(fd, buf, 0) < 0)
|
||||
return (-1);
|
||||
if (!str[fd])
|
||||
if (!(str[fd] = (char*)ft_memalloc(sizeof(char) * 1)))
|
||||
return (-1);
|
||||
while (!ft_strchr(str[fd], '\n') && (i = read(fd, buf, BUFFER_SIZE)) > 0)
|
||||
{
|
||||
buf[i] = '\0';
|
||||
if (!(tmp = ft_strjoin(str[fd], buf)))
|
||||
{
|
||||
free_it(&str[fd]);
|
||||
return (-1);
|
||||
}
|
||||
free_it(&str[fd]);
|
||||
str[fd] = tmp;
|
||||
}
|
||||
return (separate_lines(line, str, fd));
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line_bonus.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jamrabhi <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/12/05 12:55:15 by jamrabhi #+# #+# */
|
||||
/* Updated: 2019/12/05 12:57:31 by jamrabhi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef GET_NEXT_LINE_BONUS_H
|
||||
# define GET_NEXT_LINE_BONUS_H
|
||||
|
||||
# include <unistd.h>
|
||||
# include <stdlib.h>
|
||||
# include <limits.h>
|
||||
# include <stdio.h>
|
||||
# include <sys/types.h>
|
||||
# include <sys/stat.h>
|
||||
# include <fcntl.h>
|
||||
|
||||
int get_next_line(int fd, char **line);
|
||||
char *ft_strjoin(char const *s1, char const *s2);
|
||||
char *ft_substr(char const *s, unsigned int start, size_t len);
|
||||
char *ft_strchr(const char *s, int c);
|
||||
size_t ft_strlen(const char *str);
|
||||
void *ft_memalloc(size_t size);
|
||||
|
||||
#endif
|
||||
@@ -1,104 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line_utils_bonus.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jamrabhi <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/12/05 12:55:34 by jamrabhi #+# #+# */
|
||||
/* Updated: 2019/12/05 12:58:26 by jamrabhi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "get_next_line_bonus.h"
|
||||
|
||||
void *ft_memalloc(size_t size)
|
||||
{
|
||||
void *mem;
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
if (!(mem = malloc(size)))
|
||||
return (NULL);
|
||||
while (i < size)
|
||||
{
|
||||
((char*)mem)[i] = 0;
|
||||
i++;
|
||||
}
|
||||
return (mem);
|
||||
}
|
||||
|
||||
size_t ft_strlen(const char *str)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (str[i])
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
char *ft_strjoin(char const *s1, char const *s2)
|
||||
{
|
||||
size_t i;
|
||||
size_t j;
|
||||
char *str;
|
||||
size_t str_len;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
if (!(s1 && s2))
|
||||
return (NULL);
|
||||
str_len = (ft_strlen(s1) + ft_strlen(s2));
|
||||
if (!(str = (char *)malloc(sizeof(*str) * (str_len + 1))))
|
||||
return (NULL);
|
||||
while (s1[i])
|
||||
{
|
||||
str[i] = s1[i];
|
||||
i++;
|
||||
}
|
||||
while (s2[j])
|
||||
str[i++] = s2[j++];
|
||||
str[i] = '\0';
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *ft_strchr(const char *s, int c)
|
||||
{
|
||||
int i;
|
||||
char ch;
|
||||
|
||||
i = 0;
|
||||
ch = c;
|
||||
while (s[i])
|
||||
{
|
||||
if (s[i] == ch)
|
||||
return ((char*)s + i);
|
||||
i++;
|
||||
}
|
||||
if (s[i] == '\0' && ch == '\0')
|
||||
return ((char*)s + i);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
char *ft_substr(char const *s, unsigned int start, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
char *rt;
|
||||
|
||||
i = 0;
|
||||
if (!s)
|
||||
return (NULL);
|
||||
if (start > ft_strlen(s))
|
||||
len = 0;
|
||||
if (!(rt = (char *)malloc(sizeof(*rt) * (len + 1))))
|
||||
return (NULL);
|
||||
while (*s + start && i < len)
|
||||
{
|
||||
rt[i] = s[start];
|
||||
i++;
|
||||
start++;
|
||||
}
|
||||
rt[i] = '\0';
|
||||
return (rt);
|
||||
}
|
||||
@@ -1,194 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
|
||||
void *ft_memalloc(size_t size)
|
||||
{
|
||||
void *mem;
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
if (!(mem = malloc(size)))
|
||||
return (NULL);
|
||||
while (i < size)
|
||||
{
|
||||
((char*)mem)[i] = 0;
|
||||
i++;
|
||||
}
|
||||
return (mem);
|
||||
}
|
||||
|
||||
size_t ft_strlen(const char *str)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (str[i])
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
char *ft_strjoin(char const *s1, char const *s2)
|
||||
{
|
||||
size_t i;
|
||||
size_t j;
|
||||
char *str;
|
||||
size_t str_len;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
if (!(s1 && s2))
|
||||
return (NULL);
|
||||
str_len = (ft_strlen(s1) + ft_strlen(s2));
|
||||
if (!(str = (char *)malloc(sizeof(*str) * (str_len + 1))))
|
||||
return (NULL);
|
||||
while (s1[i])
|
||||
{
|
||||
str[i] = s1[i];
|
||||
i++;
|
||||
}
|
||||
while (s2[j])
|
||||
str[i++] = s2[j++];
|
||||
str[i] = '\0';
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *ft_strchr(const char *s, int c)
|
||||
{
|
||||
int i;
|
||||
char ch;
|
||||
|
||||
i = 0;
|
||||
ch = c;
|
||||
while (s[i])
|
||||
{
|
||||
if (s[i] == ch)
|
||||
return ((char*)s + i);
|
||||
i++;
|
||||
}
|
||||
if (s[i] == '\0' && ch == '\0')
|
||||
return ((char*)s + i);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
char *ft_substr(char const *s, unsigned int start, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
char *rt;
|
||||
|
||||
i = 0;
|
||||
if (!s)
|
||||
return (NULL);
|
||||
if (start > ft_strlen(s))
|
||||
len = 0;
|
||||
if (!(rt = (char *)malloc(sizeof(*rt) * (len + 1))))
|
||||
return (NULL);
|
||||
while (*s + start && i < len)
|
||||
{
|
||||
rt[i] = s[start];
|
||||
i++;
|
||||
start++;
|
||||
}
|
||||
rt[i] = '\0';
|
||||
return (rt);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
|
||||
static void free_it(char **str)
|
||||
{
|
||||
if (str)
|
||||
{
|
||||
free(*str);
|
||||
*str = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static int separate_lines(char **line, char **str, int fd)
|
||||
{
|
||||
int i;
|
||||
char *rest;
|
||||
|
||||
i = 0;
|
||||
while (str[fd][i] && str[fd][i] != '\n')
|
||||
i++;
|
||||
if (!(*line = ft_substr(str[fd], 0, i)))
|
||||
{
|
||||
free_it(&str[fd]);
|
||||
return (-1);
|
||||
}
|
||||
if (str[fd][i])
|
||||
{
|
||||
if (!(rest = ft_substr(str[fd], i + 1, ft_strlen(str[fd]) - (i + 1))))
|
||||
{
|
||||
free_it(&str[fd]);
|
||||
return (-1);
|
||||
}
|
||||
free_it(&str[fd]);
|
||||
str[fd] = rest;
|
||||
return (1);
|
||||
}
|
||||
// free_it(&str[fd]);
|
||||
// free(*line);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int get_next_line(int fd, char **line)
|
||||
{
|
||||
static char *str[FOPEN_MAX];
|
||||
char buf[BUFFER_SIZE + 1];
|
||||
int i;
|
||||
char *tmp;
|
||||
|
||||
if (fd < 0 || fd >= FOPEN_MAX || !line || BUFFER_SIZE <= 0
|
||||
|| read(fd, buf, 0) < 0)
|
||||
return (-1);
|
||||
if (!str[fd])
|
||||
if (!(str[fd] = (char*)ft_memalloc(sizeof(char) * 1)))
|
||||
return (-1);
|
||||
while (!ft_strchr(str[fd], '\n') && (i = read(fd, buf, BUFFER_SIZE)) > 0)
|
||||
{
|
||||
buf[i] = '\0';
|
||||
if (!(tmp = ft_strjoin(str[fd], buf)))
|
||||
{
|
||||
free_it(&str[fd]);
|
||||
return (-1);
|
||||
}
|
||||
free_it(&str[fd]);
|
||||
str[fd] = tmp;
|
||||
}
|
||||
return (separate_lines(line, str, fd));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
int fd1;
|
||||
int i;
|
||||
char *line;
|
||||
|
||||
line = NULL;
|
||||
fd1 = open(av[1], O_RDONLY);
|
||||
if (ac < 2)
|
||||
return (0);
|
||||
while ((i = get_next_line(fd1, &line)) > 0)
|
||||
{
|
||||
printf("%s\n",line);
|
||||
free(line);
|
||||
}
|
||||
// while (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
104
testt/leaks.txt
104
testt/leaks.txt
@@ -1,104 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line_utils_bonus.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jamrabhi <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/12/05 12:55:34 by jamrabhi #+# #+# */
|
||||
/* Updated: 2019/12/05 12:58:26 by jamrabhi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "get_next_line_bonus.h"
|
||||
|
||||
void *ft_memalloc(size_t size)
|
||||
{
|
||||
void *mem;
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
if (!(mem = malloc(size)))
|
||||
return (NULL);
|
||||
while (i < size)
|
||||
{
|
||||
((char*)mem)[i] = 0;
|
||||
i++;
|
||||
}
|
||||
return (mem);
|
||||
}
|
||||
|
||||
size_t ft_strlen(const char *str)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (str[i])
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
char *ft_strjoin(char const *s1, char const *s2)
|
||||
{
|
||||
size_t i;
|
||||
size_t j;
|
||||
char *str;
|
||||
size_t str_len;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
if (!(s1 && s2))
|
||||
return (NULL);
|
||||
str_len = (ft_strlen(s1) + ft_strlen(s2));
|
||||
if (!(str = (char *)malloc(sizeof(*str) * (str_len + 1))))
|
||||
return (NULL);
|
||||
while (s1[i])
|
||||
{
|
||||
str[i] = s1[i];
|
||||
i++;
|
||||
}
|
||||
while (s2[j])
|
||||
str[i++] = s2[j++];
|
||||
str[i] = '\0';
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *ft_strchr(const char *s, int c)
|
||||
{
|
||||
int i;
|
||||
char ch;
|
||||
|
||||
i = 0;
|
||||
ch = c;
|
||||
while (s[i])
|
||||
{
|
||||
if (s[i] == ch)
|
||||
return ((char*)s + i);
|
||||
i++;
|
||||
}
|
||||
if (s[i] == '\0' && ch == '\0')
|
||||
return ((char*)s + i);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
char *ft_substr(char const *s, unsigned int start, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
char *rt;
|
||||
|
||||
i = 0;
|
||||
if (!s)
|
||||
return (NULL);
|
||||
if (start > ft_strlen(s))
|
||||
len = 0;
|
||||
if (!(rt = (char *)malloc(sizeof(*rt) * (len + 1))))
|
||||
return (NULL);
|
||||
while (*s + start && i < len)
|
||||
{
|
||||
rt[i] = s[start];
|
||||
i++;
|
||||
start++;
|
||||
}
|
||||
rt[i] = '\0';
|
||||
return (rt);
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line_utils_bonus.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jamrabhi <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/12/05 12:55:34 by jamrabhi #+# #+# */
|
||||
/* Updated: 2019/12/05 12:58:26 by jamrabhi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "get_next_line_bonus.h"
|
||||
|
||||
void *ft_memalloc(size_t size)
|
||||
{
|
||||
void *mem;
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
if (!(mem = malloc(size)))
|
||||
return (NULL);
|
||||
while (i < size)
|
||||
{
|
||||
((char*)mem)[i] = 0;
|
||||
i++;
|
||||
}
|
||||
return (mem);
|
||||
}
|
||||
|
||||
size_t ft_strlen(const char *str)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (str[i])
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
char *ft_strjoin(char const *s1, char const *s2)
|
||||
{
|
||||
size_t i;
|
||||
size_t j;
|
||||
char *str;
|
||||
size_t str_len;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
if (!(s1 && s2))
|
||||
return (NULL);
|
||||
str_len = (ft_strlen(s1) + ft_strlen(s2));
|
||||
if (!(str = (char *)malloc(sizeof(*str) * (str_len + 1))))
|
||||
return (NULL);
|
||||
while (s1[i])
|
||||
{
|
||||
str[i] = s1[i];
|
||||
i++;
|
||||
}
|
||||
while (s2[j])
|
||||
str[i++] = s2[j++];
|
||||
str[i] = '\0';
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *ft_strchr(const char *s, int c)
|
||||
{
|
||||
int i;
|
||||
char ch;
|
||||
|
||||
i = 0;
|
||||
ch = c;
|
||||
while (s[i])
|
||||
{
|
||||
if (s[i] == ch)
|
||||
return ((char*)s + i);
|
||||
i++;
|
||||
}
|
||||
if (s[i] == '\0' && ch == '\0')
|
||||
return ((char*)s + i);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
char *ft_substr(char const *s, unsigned int start, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
char *rt;
|
||||
|
||||
i = 0;
|
||||
if (!s)
|
||||
return (NULL);
|
||||
if (start > ft_strlen(s))
|
||||
len = 0;
|
||||
if (!(rt = (char *)malloc(sizeof(*rt) * (len + 1))))
|
||||
return (NULL);
|
||||
while (*s + start && i < len)
|
||||
{
|
||||
rt[i] = s[start];
|
||||
i++;
|
||||
start++;
|
||||
}
|
||||
rt[i] = '\0';
|
||||
return (rt);
|
||||
}
|
||||
20
testt/main.c
20
testt/main.c
@@ -1,20 +0,0 @@
|
||||
#include "get_next_line_bonus.h"
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
int fd1;
|
||||
int i;
|
||||
char *line;
|
||||
|
||||
line = NULL;
|
||||
fd1 = open(av[1], O_RDONLY);
|
||||
if (ac < 2)
|
||||
return (0);
|
||||
while ((i = get_next_line(fd1, &line)) > 0)
|
||||
{
|
||||
printf("%s\n",line);
|
||||
free(line);
|
||||
}
|
||||
// while (1);
|
||||
return (0);
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
asdfghjklasdfghjklasdfghjkl
|
||||
|
||||
Reference in New Issue
Block a user