diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-10-11 14:30:20 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-10-11 14:30:20 +0200 |
| commit | e0abc6358c4cacc840ac8b3a9a92900f8ce49f0a (patch) | |
| tree | 72d0cdedd11b10b593cf7547716a9e2e82f5c378 /get_next_line_utils.c | |
| parent | c24fb630859217dbf8cce9f834fe1c4a058d17a0 (diff) | |
| download | get_next_line-e0abc6358c4cacc840ac8b3a9a92900f8ce49f0a.tar.gz get_next_line-e0abc6358c4cacc840ac8b3a9a92900f8ce49f0a.tar.bz2 get_next_line-e0abc6358c4cacc840ac8b3a9a92900f8ce49f0a.zip | |
Rewrite with dynamic buffer
- ft_strappend: ft_strcat with resize
- still malloc issues in ft_strappend
Diffstat (limited to 'get_next_line_utils.c')
| -rw-r--r-- | get_next_line_utils.c | 70 |
1 files changed, 59 insertions, 11 deletions
diff --git a/get_next_line_utils.c b/get_next_line_utils.c index 8fbc78a..e024c12 100644 --- a/get_next_line_utils.c +++ b/get_next_line_utils.c @@ -6,20 +6,12 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/08 08:52:59 by cacharle #+# #+# */ -/* Updated: 2019/10/11 09:50:44 by cacharle ### ########.fr */ +/* Updated: 2019/10/11 14:29:34 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -int find_newline(char *str) -{ - int i; - - i = -1; - while (str[++i]) - if (str[i] == '\n') - return (i); - return (-1); -} +#include <stdlib.h> +#include "get_next_line.h" char *ft_strncpy(char *dest, const char *src, int n) { @@ -45,3 +37,59 @@ int ft_strlen(char *str) counter++; return (counter); } + +char *ft_strappend(char *dest, char *src) +{ + void *copy; + + if (dest == NULL) + { + if ((dest = (char*)malloc(sizeof(char) * (ft_strlen(src) + 1))) == NULL) + return (NULL); + ft_strcpy(dest, src); + return (dest); + } + if ((copy = (char*)malloc(sizeof(char) * (ft_strlen(dest) + 1))) == NULL) + return (NULL); + ft_strcpy(copy, dest); + /* free(dest); */ + if ((dest = (char*)malloc(sizeof(char) + * (ft_strlen(dest) + ft_strlen(src) + 1))) == NULL) + return (NULL); + ft_strcpy(dest, copy); + free(copy); + ft_strcat(dest, src); + return (dest); +} + +char *ft_strcpy(char *dest, const char *src) +{ + int i; + + i = 0; + while (src[i]) + { + dest[i] = src[i]; + i++; + } + dest[i] = '\0'; + return (dest); +} + +char *ft_strcat(char *dest, const char *src) +{ + int i; + int j; + + i = 0; + while (dest[i]) + i++; + j = 0; + while (src[j]) + { + dest[i + j] = src[j]; + j++; + } + dest[i + j] = '\0'; + return (dest); +} |
