init with functionnal works

This commit is contained in:
Hugo LAMY
2019-12-11 01:00:33 +01:00
commit 7fd709fcac
5 changed files with 513 additions and 0 deletions

62
.gitignore vendored Normal file
View File

@@ -0,0 +1,62 @@
# Prerequisites
*.d
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
# OS generated files #
*.swp
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ethumbs.db
Thumbs.db

140
get_next_line.c Normal file
View File

@@ -0,0 +1,140 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/11 00:27:09 by hulamy #+# #+# */
/* Updated: 2019/12/11 00:56:54 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;
** }
** while (1);
** return (0);
** }
*/
int multi_fd(int fd, t_gnlist **lst)
{
t_gnlist *tmp;
tmp = *lst;
while (*lst && (*lst)->lfd != fd && (*lst)->next != tmp)
*lst = (*lst)->next;
if (!tmp || ((*lst)->next == tmp && (*lst)->lfd != fd))
{
if (!(tmp = (t_gnlist*)malloc(sizeof(*tmp))))
return (0);
tmp->lfd = fd;
if (!(tmp->string = ft_strdup("")))
return (0);
if (*lst)
{
tmp->next = (*lst)->next;
(*lst)->next = tmp;
}
else
tmp->next = tmp;
*lst = tmp;
}
return (1);
}
char *ft_strjoinfree(char const *s1, char const *s2)
{
char *str;
int len;
int j;
if (!s1 || !s2)
return (NULL);
len = ft_strlen(s1) + ft_strlen(s2);
if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
return (NULL);
len = 0;
j = 0;
while (s1[j] != '\0')
str[len++] = s1[j++];
j = 0;
while (s2[j] != '\0')
str[len++] = s2[j++];
str[len] = '\0';
free((char*)s1);
return (str);
}
int hasnewline(char *str, t_gnlist *lst, char **line)
{
str[0] = '\0';
if (!(*line = ft_strdup(lst->string)))
return (-1);
ft_memmove(lst->string, str + 1, ft_strlen(str + 1) + 1);
return (1);
}
int get_next_line(const int fd, char **line)
{
char buf[BUFF_SIZE + 1];
int ret;
static t_gnlist *lst = NULL;
char *str;
if (!(multi_fd(fd, &lst)) || !(ret = 1) || !line || fd < 0)
return (-1);
while (ret > 0)
{
if ((str = ft_strchr(lst->string, '\n')))
return (hasnewline(str, lst, line));
if ((ret = read(fd, buf, BUFF_SIZE)) < 0)
return (-1);
buf[ret] = '\0';
if (!(lst->string = ft_strjoinfree(lst->string, buf)))
return (-1);
}
if (*(lst->string) && !(*line = ft_strdup(lst->string)))
return (-1);
if (*(lst->string) && ++ret == 1)
(lst->string)[0] = '\0';
return (ret);
}

41
get_next_line.h Normal file
View File

@@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/29 10:19:37 by hulamy #+# #+# */
/* Updated: 2019/12/11 00:56:04 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# include <string.h>
# include <unistd.h>
# include <stdlib.h>
# ifndef BUFF_SIZE
# define BUFF_SIZE 9999
# endif
typedef struct s_gnlist
{
int lfd;
char *string;
struct s_gnlist *next;
} t_gnlist;
int get_next_line(const int fd, char **line);
int multi_fd(int fd, t_gnlist **lst);
char *ft_strjoinfree(char const *s1, char const *s2);
int hasnewline(char *str, t_gnlist *lst, char **line);
size_t ft_strlen(const char *str);
void *ft_memmove(void *dst, const void *src, size_t len);
char *ft_strchr(const char *s, int c);
char *ft_strdup(const char *src);
#endif

73
get_next_line_utils.c Normal file
View File

@@ -0,0 +1,73 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/11 00:26:54 by hulamy #+# #+# */
/* Updated: 2019/12/11 00:57:19 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *ft_strdup(const char *src)
{
int i;
char *str;
i = 0;
while (src[i] != '\0')
i++;
if (!(str = (char*)malloc(sizeof(*str) * (i + 1))))
return (NULL);
while (i-- >= 0)
str[i + 1] = src[i + 1];
return (str);
}
char *ft_strchr(const char *s, int c)
{
int i;
int j;
i = 0;
j = -1;
while (s[i])
i++;
while (++j < i + 1)
if (s[j] == c)
return ((char *)s + j);
return (NULL);
}
void *ft_memmove(void *dst, const void *src, size_t len)
{
size_t i;
char *cpsrc;
char *cpdst;
i = -1;
cpsrc = (char *)src;
cpdst = (char *)dst;
if (dst == src)
return (dst);
if (cpsrc < cpdst)
while (len--)
cpdst[len] = cpsrc[len];
else
while (++i < len)
cpdst[i] = cpsrc[i];
return (dst);
}
size_t ft_strlen(const char *str)
{
size_t i;
i = 0;
while (str[i])
i++;
return (i);
}

197
testt/get_next_line.c Normal file
View File

@@ -0,0 +1,197 @@
#include <libc.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]);
return (0);
}
int get_next_line(int fd, char **line)
{
static char *str[OPEN_MAX];
char buf[BUFFER_SIZE + 1];
int i;
char *tmp;
if (fd < 0 || fd >= OPEN_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 fd2;
int i;
char *line;
line = NULL;
fd1 = open(av[1], O_RDONLY);
fd2 = open(av[2], O_RDONLY);
if (ac < 2)
return (0);
while ((i = get_next_line(fd1, &line)) > 0)
{
printf("%i", i);
printf("%s\n",line);
free(line);
if ((i = get_next_line(fd2, &line)) > 0)
{
printf("%s\n",line);
free(line);
}
printf("%i", i);
}
printf("%i", i);
while (1);
return (0);
}