├── get_next_line.c ├── get_next_line.h ├── get_next_line_bonus.c ├── get_next_line_bonus.h ├── get_next_line_utils.c └── get_next_line_utils_bonus.c /get_next_line.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: abello-r +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/09 17:18:13 by abello-r #+# #+# */ 9 | /* Updated: 2020/02/09 23:56:39 by abello-r ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "get_next_line.h" 14 | 15 | /* 16 | ** La función FT_STRCHR, busca un caracter (c) en la string (s). 17 | */ 18 | 19 | char *ft_strchr(const char *s, int c) 20 | { 21 | while (*s) 22 | { 23 | if (*s == (char)c) 24 | return ((char *)s); 25 | s++; 26 | } 27 | if ((char)c == '\0') 28 | return ((char *)s); 29 | return (NULL); 30 | } 31 | 32 | /* 33 | ** La funcion ft_negative, se cumple cuando no se consigue un \n, y por ende 34 | ** busca el \0 ò EOF, una vez encontrado, guarda en (line) una cadena de (x), 35 | ** luego le hhace free a la variable estatica (x) para evitar leaks, y 36 | ** la apunta a NULL porque no se utilizara más, como llegó al final del (fd) 37 | ** se retorna (0); 38 | */ 39 | 40 | int ft_negative(char **x, char **line) 41 | { 42 | if (ft_strchr(*x, '\0')) 43 | { 44 | *line = ft_strdup(*x); 45 | free(*x); 46 | *x = NULL; 47 | return (0); 48 | } 49 | return (0); 50 | } 51 | 52 | /* 53 | ** La funcion ft_nbytesnegativo, es una gestion de errores, en caso de 54 | ** que nbytes < 0, si es así y la variable estatica (x) NO esta vacia se 55 | ** libera y se apunta a NULL para evitar Leaks, y se retorna (-1) porque 56 | ** es un error. 57 | */ 58 | 59 | int ft_nbytesnegativo(ssize_t *nbytes, char **x) 60 | { 61 | if (*nbytes < 0) 62 | { 63 | if (*x != NULL) 64 | { 65 | free(*x); 66 | *x = NULL; 67 | } 68 | return (-1); 69 | } 70 | return (0); 71 | } 72 | 73 | int ft_aux(ssize_t nbytes, char **x, char **line) 74 | { 75 | char *tmp; 76 | char *tmp2; 77 | 78 | if (!nbytes && !*x) 79 | { 80 | *line = ft_strdup(""); 81 | return (0); 82 | } 83 | if (ft_nbytesnegativo(&nbytes, &*x)) 84 | return (-1); 85 | if ((tmp = ft_strchr(*x, '\n'))) 86 | { 87 | *tmp = '\0'; 88 | *line = ft_strdup(*x); 89 | tmp2 = ft_strdup(tmp + 1); 90 | free(*x); 91 | *x = tmp2; 92 | return (1); 93 | } 94 | else if (ft_negative(&*x, &*line)) 95 | return (0); 96 | return (0); 97 | } 98 | 99 | /* 100 | ** Esta es la función principal 101 | */ 102 | 103 | int get_next_line(int fd, char **line) 104 | { 105 | char *buff; 106 | static char *x[4096]; 107 | ssize_t nbytes; 108 | char *tmp; 109 | 110 | if (fd < 0 || line == NULL || BUFFER_SIZE <= 0 || 111 | (!(buff = (char *)malloc(sizeof(char) * (BUFFER_SIZE + 1))))) 112 | return (-1); 113 | while ((nbytes = read(fd, buff, BUFFER_SIZE)) > 0) 114 | { 115 | buff[nbytes] = '\0'; 116 | if (!x[fd]) 117 | x[fd] = ft_strdup(buff); 118 | else 119 | { 120 | tmp = ft_strjoin(x[fd], buff); 121 | free(x[fd]); 122 | x[fd] = tmp; 123 | } 124 | if (ft_strchr(buff, '\n')) 125 | break ; 126 | } 127 | free(buff); 128 | buff = NULL; 129 | return (ft_aux(nbytes, &x[fd], &*line)); 130 | } 131 | -------------------------------------------------------------------------------- /get_next_line.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: abello-r +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/09 18:59:21 by abello-r #+# #+# */ 9 | /* Updated: 2020/02/09 18:59:33 by abello-r ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef GET_NEXT_LINE_H 14 | # define GET_NEXT_LINE_H 15 | 16 | # include 17 | # include 18 | # include 19 | # include 20 | # include 21 | # include 22 | 23 | int get_next_line(int fd, char **line); 24 | char *ft_strdup(const char *s1); 25 | char *ft_strchr(const char *s, int c); 26 | size_t ft_strlen(const char *s); 27 | char *ft_substr(char const *s, unsigned int start, size_t len); 28 | char *ft_strjoin(char const *s1, char const *s2); 29 | void *ft_memcpy(void *dst, const void *src, size_t n); 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /get_next_line_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: abello-r +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/09 19:07:19 by abello-r #+# #+# */ 9 | /* Updated: 2020/02/09 19:07:24 by abello-r ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "get_next_line_bonus.h" 14 | 15 | /* 16 | ** La función FT_STRCHR, busca un caracter (c) en la string (s). 17 | */ 18 | 19 | char *ft_strchr(const char *s, int c) 20 | { 21 | while (*s) 22 | { 23 | if (*s == (char)c) 24 | return ((char *)s); 25 | s++; 26 | } 27 | if ((char)c == '\0') 28 | return ((char *)s); 29 | return (NULL); 30 | } 31 | 32 | /* 33 | ** La funcion ft_negative, se cumple cuando no se consigue un \n, y por ende 34 | ** busca el \0 ò EOF, una vez encontrado, guarda en (line) una cadena de (x), 35 | ** luego le hhace free a la variable estatica (x) para evitar leaks, y 36 | ** la apunta a NULL porque no se utilizara más, como llegó al final del (fd) 37 | ** se retorna (0); 38 | */ 39 | 40 | int ft_negative(char **x, char **line) 41 | { 42 | if (ft_strchr(*x, '\0')) 43 | { 44 | *line = ft_strdup(*x); 45 | free(*x); 46 | *x = NULL; 47 | return (0); 48 | } 49 | return (0); 50 | } 51 | 52 | /* 53 | ** La funcion ft_nbytesnegativo, es una gestion de errores, en caso de 54 | ** que nbytes < 0, si es así y la variable estatica (x) NO esta vacia se 55 | ** libera y se apunta a NULL para evitar Leaks, y se retorna (-1) porque 56 | ** es un error. 57 | */ 58 | 59 | int ft_nbytesnegativo(ssize_t *nbytes, char **x) 60 | { 61 | if (*nbytes < 0) 62 | { 63 | if (*x != NULL) 64 | { 65 | free(*x); 66 | *x = NULL; 67 | } 68 | return (-1); 69 | } 70 | return (0); 71 | } 72 | 73 | int ft_aux(ssize_t nbytes, char **x, char **line) 74 | { 75 | char *tmp; 76 | char *tmp2; 77 | 78 | if (!nbytes && !*x) 79 | { 80 | *line = ft_strdup(""); 81 | return (0); 82 | } 83 | if (ft_nbytesnegativo(&nbytes, &*x)) 84 | return (-1); 85 | if ((tmp = ft_strchr(*x, '\n'))) 86 | { 87 | *tmp = '\0'; 88 | *line = ft_strdup(*x); 89 | tmp2 = ft_strdup(tmp + 1); 90 | free(*x); 91 | *x = tmp2; 92 | return (1); 93 | } 94 | else if (ft_negative(&*x, &*line)) 95 | return (0); 96 | return (0); 97 | } 98 | 99 | /* 100 | ** Esta es la función principal 101 | */ 102 | 103 | int get_next_line(int fd, char **line) 104 | { 105 | char *buff; 106 | static char *x[4096]; 107 | ssize_t nbytes; 108 | char *tmp; 109 | 110 | if (fd < 0 || line == NULL || BUFFER_SIZE <= 0 || 111 | (!(buff = (char *)malloc(sizeof(char) * (BUFFER_SIZE + 1))))) 112 | return (-1); 113 | while ((nbytes = read(fd, buff, BUFFER_SIZE)) > 0) 114 | { 115 | buff[nbytes] = '\0'; 116 | if (!x[fd]) 117 | x[fd] = ft_strdup(buff); 118 | else 119 | { 120 | tmp = ft_strjoin(x[fd], buff); 121 | free(x[fd]); 122 | x[fd] = tmp; 123 | } 124 | if (ft_strchr(buff, '\n')) 125 | break ; 126 | } 127 | free(buff); 128 | buff = NULL; 129 | return (ft_aux(nbytes, &x[fd], &*line)); 130 | } 131 | -------------------------------------------------------------------------------- /get_next_line_bonus.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line_bonus.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: abello-r +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/08 19:07:17 by abello-r #+# #+# */ 9 | /* Updated: 2020/02/09 19:09:16 by abello-r ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef GET_NEXT_LINE_BONUS_H 14 | # define GET_NEXT_LINE_BONUS_H 15 | 16 | # ifndef BUFFER_SIZE 17 | # define BUFFER_SIZE 1 18 | # endif 19 | 20 | # include 21 | # include 22 | # include 23 | # include 24 | # include 25 | # include 26 | 27 | int get_next_line(int fd, char **line); 28 | char *ft_strdup(const char *s1); 29 | char *ft_strchr(const char *s, int c); 30 | size_t ft_strlen(const char *s); 31 | char *ft_substr(char const *s, unsigned int start, size_t len); 32 | char *ft_strjoin(char const *s1, char const *s2); 33 | void *ft_memcpy(void *dst, const void *src, size_t n); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /get_next_line_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: abello-r +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/09 19:08:26 by abello-r #+# #+# */ 9 | /* Updated: 2020/02/09 19:08:39 by abello-r ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "get_next_line.h" 14 | 15 | /* 16 | ** La funcion FT_STRDUP, crea una nueva string y le asigna memoria con malloc 17 | ** (x), luego (x) recibe los valores de (s1), y se le agrega '\0' al final. 18 | */ 19 | 20 | char *ft_strdup(const char *s1) 21 | { 22 | char *s2; 23 | size_t i; 24 | 25 | i = ft_strlen(s1); 26 | if (!(s2 = malloc(i + 1))) 27 | return (NULL); 28 | ft_memcpy(s2, s1, i); 29 | s2[i] = '\0'; 30 | return (s2); 31 | } 32 | 33 | size_t ft_strlen(const char *s) 34 | { 35 | size_t i; 36 | 37 | i = 0; 38 | while (s[i] != '\0') 39 | { 40 | i++; 41 | } 42 | return (i); 43 | } 44 | 45 | /* 46 | ** la funcion FT_SUBSTR, crea una nueva string con malloc (x) y luego 47 | ** empieza a guardar los valores de (s) empezando desde s[start] hasta 48 | ** s[len] y al final de (x) se agrega '\0'. 49 | */ 50 | 51 | char *ft_substr(char const *s, unsigned int start, size_t len) 52 | { 53 | char *x; 54 | size_t i; 55 | 56 | i = 0; 57 | if (!s) 58 | return (NULL); 59 | if (*s == '\0') 60 | return (ft_strdup("")); 61 | if (start > ft_strlen(s)) 62 | return (ft_strdup("")); 63 | if (!(x = (char *)malloc(sizeof(char) * len + 1))) 64 | return (NULL); 65 | while (i < len) 66 | { 67 | x[i] = s[start + i]; 68 | i++; 69 | } 70 | x[i] = '\0'; 71 | return (x); 72 | } 73 | 74 | /* 75 | ** la funcion FT_STRJOIN, crea una nueva string (x) como 76 | ** resultado de la concatenacion de la (s1) con la (s2) y 77 | ** agregando '\0' al final de (x). 78 | */ 79 | 80 | char *ft_strjoin(char const *s1, char const *s2) 81 | { 82 | char *x; 83 | size_t c1; 84 | size_t c2; 85 | size_t i; 86 | 87 | i = 0; 88 | c2 = ft_strlen(s1); 89 | c1 = 0; 90 | if (s1 == NULL || s2 == NULL) 91 | return (NULL); 92 | if (!(x = (char *)malloc(sizeof(char) * ft_strlen(s1) + ft_strlen(s2) + 1))) 93 | return (NULL); 94 | while (c1 != c2) 95 | { 96 | x[i] = s1[c1]; 97 | ++i && ++c1; 98 | } 99 | c2 = 0; 100 | c1 = ft_strlen(s2); 101 | while (c2 != c1) 102 | x[i++] = s2[c2++]; 103 | x[i] = '\0'; 104 | return (x); 105 | } 106 | 107 | /* 108 | ** la funcion FT_MEMCPY, copia los caracteres de (src) en (dst) 109 | ** hasta llegar a (n). 110 | */ 111 | 112 | void *ft_memcpy(void *dst, const void *src, size_t n) 113 | { 114 | size_t i; 115 | 116 | if (n == 0 || dst == src) 117 | return (dst); 118 | i = 0; 119 | while (i < n) 120 | { 121 | ((char *)dst)[i] = ((char *)src)[i]; 122 | i++; 123 | } 124 | return (dst); 125 | } 126 | -------------------------------------------------------------------------------- /get_next_line_utils_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line_utils_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: abello-r +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2020/02/08 19:06:50 by abello-r #+# #+# */ 9 | /* Updated: 2020/02/09 19:03:35 by abello-r ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "get_next_line_bonus.h" 14 | 15 | /* 16 | ** La funcion FT_STRDUP, crea una nueva string y le asigna memoria con malloc 17 | ** (x), luego (x) recibe los valores de (s1), y se le agrega '\0' al final. 18 | */ 19 | 20 | char *ft_strdup(const char *s1) 21 | { 22 | char *s2; 23 | size_t i; 24 | 25 | i = ft_strlen(s1); 26 | if (!(s2 = malloc(i + 1))) 27 | return (NULL); 28 | ft_memcpy(s2, s1, i); 29 | s2[i] = '\0'; 30 | return (s2); 31 | } 32 | 33 | size_t ft_strlen(const char *s) 34 | { 35 | size_t i; 36 | 37 | i = 0; 38 | while (s[i] != '\0') 39 | { 40 | i++; 41 | } 42 | return (i); 43 | } 44 | 45 | /* 46 | ** la funcion FT_SUBSTR, crea una nueva string con malloc (x) y luego 47 | ** empieza a guardar los valores de (s) empezando desde s[start] hasta 48 | ** s[len] y al final de (x) se agrega '\0'. 49 | */ 50 | 51 | char *ft_substr(char const *s, unsigned int start, size_t len) 52 | { 53 | char *x; 54 | size_t i; 55 | 56 | i = 0; 57 | if (!s) 58 | return (NULL); 59 | if (*s == '\0') 60 | return (ft_strdup("")); 61 | if (start > ft_strlen(s)) 62 | return (ft_strdup("")); 63 | if (!(x = (char *)malloc(sizeof(char) * len + 1))) 64 | return (NULL); 65 | while (i < len) 66 | { 67 | x[i] = s[start + i]; 68 | i++; 69 | } 70 | x[i] = '\0'; 71 | return (x); 72 | } 73 | 74 | /* 75 | ** la funcion FT_STRJOIN, crea una nueva string (x) como 76 | ** resultado de la concatenacion de la (s1) con la (s2) y 77 | ** agregando '\0' al final de (x). 78 | */ 79 | 80 | char *ft_strjoin(char const *s1, char const *s2) 81 | { 82 | char *x; 83 | size_t c1; 84 | size_t c2; 85 | size_t i; 86 | 87 | i = 0; 88 | c2 = ft_strlen(s1); 89 | c1 = 0; 90 | if (s1 == NULL || s2 == NULL) 91 | return (NULL); 92 | if (!(x = (char *)malloc(sizeof(char) * ft_strlen(s1) + ft_strlen(s2) + 1))) 93 | return (NULL); 94 | while (c1 != c2) 95 | { 96 | x[i] = s1[c1]; 97 | ++i && ++c1; 98 | } 99 | c2 = 0; 100 | c1 = ft_strlen(s2); 101 | while (c2 != c1) 102 | x[i++] = s2[c2++]; 103 | x[i] = '\0'; 104 | return (x); 105 | } 106 | 107 | /* 108 | ** la funcion FT_MEMCPY, copia los caracteres de (src) en (dst) 109 | ** hasta llegar a (n). 110 | */ 111 | 112 | void *ft_memcpy(void *dst, const void *src, size_t n) 113 | { 114 | size_t i; 115 | 116 | if (n == 0 || dst == src) 117 | return (dst); 118 | i = 0; 119 | while (i < n) 120 | { 121 | ((char *)dst)[i] = ((char *)src)[i]; 122 | i++; 123 | } 124 | return (dst); 125 | } 126 | --------------------------------------------------------------------------------