diff --git a/get_next_line.c b/get_next_line.c index 9301892..d604dca 100644 --- a/get_next_line.c +++ b/get_next_line.c @@ -12,48 +12,49 @@ #include "get_next_line.h" -/* -** #include //for printf -** #include //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); -** } -*/ + + #include //for printf + #include //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) { diff --git a/testt/get_next_line_bonus.c b/testt/get_next_line_bonus.c new file mode 100644 index 0000000..1e3e188 --- /dev/null +++ b/testt/get_next_line_bonus.c @@ -0,0 +1,78 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jamrabhi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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)); +} diff --git a/testt/get_next_line_bonus.h b/testt/get_next_line_bonus.h new file mode 100644 index 0000000..7b7a358 --- /dev/null +++ b/testt/get_next_line_bonus.h @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_bonus.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jamrabhi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 +# include +# include +# include +# include +# include +# include + +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 diff --git a/testt/get_next_line_utils_bonus.c b/testt/get_next_line_utils_bonus.c new file mode 100644 index 0000000..b0e36ba --- /dev/null +++ b/testt/get_next_line_utils_bonus.c @@ -0,0 +1,104 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jamrabhi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/testt/get_next_line.c b/testt/get_next_one_line.c similarity index 90% rename from testt/get_next_line.c rename to testt/get_next_one_line.c index 0b4b559..a3e654e 100644 --- a/testt/get_next_line.c +++ b/testt/get_next_one_line.c @@ -1,4 +1,10 @@ -#include +#include +#include +#include +#include +#include +#include +#include void *ft_memalloc(size_t size) { @@ -131,17 +137,18 @@ static int separate_lines(char **line, char **str, int fd) return (1); } // free_it(&str[fd]); +// free(*line); return (0); } int get_next_line(int fd, char **line) { - static char *str[OPEN_MAX]; + static char *str[FOPEN_MAX]; char buf[BUFFER_SIZE + 1]; int i; char *tmp; - if (fd < 0 || fd >= OPEN_MAX || !line || BUFFER_SIZE <= 0 + if (fd < 0 || fd >= FOPEN_MAX || !line || BUFFER_SIZE <= 0 || read(fd, buf, 0) < 0) return (-1); if (!str[fd]) @@ -181,7 +188,7 @@ int get_next_line(int fd, char **line) printf("%s\n",line); free(line); } - while (1); + // while (1); return (0); } diff --git a/testt/leaks.txt b/testt/leaks.txt new file mode 100644 index 0000000..b0e36ba --- /dev/null +++ b/testt/leaks.txt @@ -0,0 +1,104 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jamrabhi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/testt/leaksfree.txt b/testt/leaksfree.txt new file mode 100644 index 0000000..b0e36ba --- /dev/null +++ b/testt/leaksfree.txt @@ -0,0 +1,104 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jamrabhi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/testt/main.c b/testt/main.c new file mode 100644 index 0000000..03ca8cd --- /dev/null +++ b/testt/main.c @@ -0,0 +1,20 @@ +#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); +}