├── get_next_line.h ├── get_next_line_bonus.h ├── get_next_line_utils.c ├── get_next_line_utils_bonus.c ├── get_next_line.c └── get_next_line_bonus.c /get_next_line.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: eelhafia +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/11/16 14:48:07 by eelhafia #+# #+# */ 9 | /* Updated: 2022/11/20 19:54:24 by eelhafia ### ########.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 | 22 | # ifndef BUFFER_SIZE 23 | # define BUFFER_SIZE 42 24 | # endif 25 | 26 | size_t ft_strlen(const char *s); 27 | int ft_strchr(char *s, int c); 28 | char *get_next_line(int fd); 29 | char *ft_strjoin(char *s1, char *s2); 30 | 31 | #endif -------------------------------------------------------------------------------- /get_next_line_bonus.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line_bonus.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: eelhafia +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/11/19 11:36:56 by eelhafia #+# #+# */ 9 | /* Updated: 2022/11/19 11:38:51 by eelhafia ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef GET_NEXT_LINE_BONUS_H 14 | # define GET_NEXT_LINE_BONUS_H 15 | 16 | # include 17 | # include 18 | # include 19 | # include 20 | # include 21 | 22 | # ifndef BUFFER_SIZE 23 | # define BUFFER_SIZE 10 24 | # endif 25 | 26 | size_t ft_strlen(const char *s); 27 | int ft_strchr(char *s, int c); 28 | char *get_next_line(int fd); 29 | char *ft_strjoin(char *s1, char *s2); 30 | 31 | #endif -------------------------------------------------------------------------------- /get_next_line_utils.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line_utils.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: eelhafia +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/11/18 13:36:50 by eelhafia #+# #+# */ 9 | /* Updated: 2022/11/18 22:36:14 by eelhafia ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "get_next_line.h" 14 | 15 | size_t ft_strlen(const char *s) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (*s++) 21 | i++; 22 | return (i); 23 | } 24 | 25 | char *ft_strdup(char *s) 26 | { 27 | char *str; 28 | size_t len; 29 | int i; 30 | 31 | i = 0; 32 | len = ft_strlen(s); 33 | str = malloc(len + 1); 34 | if (!str) 35 | return (NULL); 36 | while (*s) 37 | str[i++] = *s++; 38 | str[i] = '\0'; 39 | return (str); 40 | } 41 | 42 | char *ft_strjoin(char *s1, char *s2) 43 | { 44 | int i; 45 | int x; 46 | char *str; 47 | 48 | i = 0; 49 | x = 0; 50 | if (!s1) 51 | s1 = ft_strdup(""); 52 | if (!s2 && !s1) 53 | return (NULL); 54 | str = malloc((ft_strlen(s1) + ft_strlen(s2) +1) * sizeof(char)); 55 | if (!str) 56 | return (free(s1), NULL); 57 | while (s1[i]) 58 | { 59 | str[i] = s1[i]; 60 | i++; 61 | } 62 | while (s2[x]) 63 | str[i++] = s2[x++]; 64 | str[i] = '\0'; 65 | free(s1); 66 | return (str); 67 | } 68 | 69 | int ft_strchr(char *s, int c) 70 | { 71 | int i; 72 | 73 | i = 0; 74 | if (!s) 75 | return (0); 76 | while (s[i] && s[i] != (char )c) 77 | i++; 78 | if (s[i] == (char )c) 79 | return (1); 80 | return (0); 81 | } 82 | -------------------------------------------------------------------------------- /get_next_line_utils_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line_utils_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: eelhafia +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/11/19 11:36:34 by eelhafia #+# #+# */ 9 | /* Updated: 2022/11/19 11:39:06 by eelhafia ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "get_next_line_bonus.h" 14 | 15 | size_t ft_strlen(const char *s) 16 | { 17 | int i; 18 | 19 | i = 0; 20 | while (*s++) 21 | i++; 22 | return (i); 23 | } 24 | 25 | char *ft_strdup(char *s) 26 | { 27 | char *str; 28 | size_t len; 29 | int i; 30 | 31 | i = 0; 32 | len = ft_strlen(s); 33 | str = malloc(len + 1); 34 | if (!str) 35 | return (NULL); 36 | while (*s) 37 | str[i++] = *s++; 38 | str[i] = '\0'; 39 | return (str); 40 | } 41 | 42 | char *ft_strjoin(char *s1, char *s2) 43 | { 44 | int i; 45 | int x; 46 | char *str; 47 | 48 | i = 0; 49 | x = 0; 50 | if (!s1) 51 | s1 = ft_strdup(""); 52 | if (!s2 && !s1) 53 | return (NULL); 54 | str = malloc((ft_strlen(s1) + ft_strlen(s2) +1) * sizeof(char)); 55 | if (!str) 56 | return (free(s1), NULL); 57 | while (s1[i]) 58 | { 59 | str[i] = s1[i]; 60 | i++; 61 | } 62 | while (s2[x]) 63 | str[i++] = s2[x++]; 64 | str[i] = '\0'; 65 | free(s1); 66 | return (str); 67 | } 68 | 69 | int ft_strchr(char *s, int c) 70 | { 71 | int i; 72 | 73 | i = 0; 74 | if (!s) 75 | return (0); 76 | while (s[i] && s[i] != (char )c) 77 | i++; 78 | if (s[i] == (char )c) 79 | return (1); 80 | return (0); 81 | } 82 | -------------------------------------------------------------------------------- /get_next_line.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: eelhafia +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/11/16 14:47:45 by eelhafia #+# #+# */ 9 | /* Updated: 2022/11/20 19:56:28 by eelhafia ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "get_next_line.h" 14 | 15 | void *alloc(size_t size) 16 | { 17 | unsigned char *pt; 18 | size_t i; 19 | 20 | i = 0; 21 | pt = malloc(size); 22 | if (pt == NULL) 23 | return (NULL); 24 | while (i < size) 25 | { 26 | pt[i] = 0; 27 | i++; 28 | } 29 | return (pt); 30 | } 31 | 32 | char *nexline(char *save) 33 | { 34 | char *defnext; 35 | int i; 36 | int j; 37 | 38 | i = 0; 39 | j = 0; 40 | while (save[i] && save[i] != '\n') 41 | i++; 42 | if (!save[i]) 43 | return (free(save), NULL); 44 | defnext = alloc((ft_strlen(save) - i +1) * sizeof(char)); 45 | if (!defnext) 46 | return (free(save), NULL); 47 | i++; 48 | while (save[i]) 49 | defnext[j++] = save[i++]; 50 | defnext[j] = '\0'; 51 | free(save); 52 | return (defnext); 53 | } 54 | 55 | char *linre(char *save) 56 | { 57 | char *res; 58 | int i; 59 | int a; 60 | 61 | a = 0; 62 | i = 0; 63 | while ((save[i] && save[i] != '\n')) 64 | i++; 65 | res = alloc((i +2) * sizeof(char)); 66 | if (!res) 67 | return (NULL); 68 | if (save[i] == '\n') 69 | i++; 70 | while (save[a] && a < i) 71 | { 72 | res[a] = save[a]; 73 | a++; 74 | } 75 | res[a] = '\0'; 76 | return (res); 77 | } 78 | 79 | char *readline(char *save, int fd) 80 | { 81 | int i; 82 | char *rd; 83 | 84 | i = 1; 85 | rd = alloc((BUFFER_SIZE +1) * sizeof(char)); 86 | if (!rd) 87 | return (NULL); 88 | while (i > 0 && !(ft_strchr(save, '\n'))) 89 | { 90 | i = read(fd, rd, BUFFER_SIZE); 91 | if ((i < 0) || (i == 0 && !save) || (i == 0 && save[0] == 0)) 92 | return (free(rd), free(save), NULL); 93 | rd[i] = '\0'; 94 | save = ft_strjoin(save, rd); 95 | } 96 | free(rd); 97 | return (save); 98 | } 99 | 100 | char *get_next_line(int fd) 101 | { 102 | static char *save; 103 | char *res; 104 | 105 | if (fd < 0 || BUFFER_SIZE <= 0) 106 | return (0); 107 | save = readline(save, fd); 108 | if (!save) 109 | return (NULL); 110 | res = linre(save); 111 | save = nexline(save); 112 | return (res); 113 | } 114 | -------------------------------------------------------------------------------- /get_next_line_bonus.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* get_next_line_bonus.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: eelhafia +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2022/11/19 11:37:31 by eelhafia #+# #+# */ 9 | /* Updated: 2022/11/20 19:57:03 by eelhafia ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "get_next_line_bonus.h" 14 | 15 | void *alloc(size_t size) 16 | { 17 | unsigned char *pt; 18 | size_t i; 19 | 20 | i = 0; 21 | pt = malloc(size); 22 | if (pt == NULL) 23 | return (NULL); 24 | while (i < size) 25 | { 26 | pt[i] = 0; 27 | i++; 28 | } 29 | return (pt); 30 | } 31 | 32 | char *nexline(char *save) 33 | { 34 | char *defnext; 35 | int i; 36 | int j; 37 | 38 | i = 0; 39 | j = 0; 40 | while (save[i] && save[i] != '\n') 41 | i++; 42 | if (!save[i]) 43 | return (free(save), NULL); 44 | defnext = alloc((ft_strlen(save) - i +1) * sizeof(char)); 45 | if (!defnext) 46 | return (free(save), NULL); 47 | i++; 48 | while (save[i]) 49 | defnext[j++] = save[i++]; 50 | defnext[j] = '\0'; 51 | free(save); 52 | return (defnext); 53 | } 54 | 55 | char *linre(char *save) 56 | { 57 | char *res; 58 | int i; 59 | int a; 60 | 61 | a = 0; 62 | i = 0; 63 | while ((save[i] && save[i] != '\n')) 64 | i++; 65 | res = alloc((i +2) * sizeof(char)); 66 | if (!res) 67 | return (NULL); 68 | if (save[i] == '\n') 69 | i++; 70 | while (save[a] && a < i) 71 | { 72 | res[a] = save[a]; 73 | a++; 74 | } 75 | res[a] = '\0'; 76 | return (res); 77 | } 78 | 79 | char *readline(char *save, int fd) 80 | { 81 | int i; 82 | char *rd; 83 | 84 | i = 1; 85 | rd = alloc((BUFFER_SIZE +1) * sizeof(char)); 86 | if (!rd) 87 | return (NULL); 88 | while (i > 0 && !(ft_strchr(save, '\n'))) 89 | { 90 | i = read(fd, rd, BUFFER_SIZE); 91 | if ((i < 0) || (i == 0 && !save) || (i == 0 && save[0] == 0)) 92 | return (free(rd), free(save), NULL); 93 | rd[i] = '\0'; 94 | save = ft_strjoin(save, rd); 95 | } 96 | free(rd); 97 | return (save); 98 | } 99 | 100 | char *get_next_line(int fd) 101 | { 102 | static char *save[10240]; 103 | char *res; 104 | 105 | if (fd < 0 || BUFFER_SIZE <= 0) 106 | return (0); 107 | save[fd] = readline(save[fd], fd); 108 | if (!save[fd]) 109 | return (NULL); 110 | res = linre(save[fd]); 111 | save[fd] = nexline(save[fd]); 112 | return (res); 113 | } 114 | --------------------------------------------------------------------------------