Skip to content

Commit

Permalink
The source files
Browse files Browse the repository at this point in the history
  • Loading branch information
Sargis Hovsepyan committed Jul 13, 2021
1 parent d6303eb commit 773e303
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/get_next_line.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: shovsepy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/14 05:22:17 by shovsepy #+# #+# */
/* Updated: 2021/02/14 05:22:30 by shovsepy ### ########.fr */
/* */
/* ************************************************************************** */

#include "../includes/get_next_line.h"

char *ft_get_line(char *save)
{
char *new_line;
int i;

i = 0;
while (save && save[i] != '\0' && save[i] != '\n')
i++;
new_line = malloc((i + 1));
if (!new_line)
return (NULL);
i = 0;
while (save && save[i] != '\0' && save[i] != '\n')
{
new_line[i] = save[i];
i++;
}
new_line[i] = '\0';
return (new_line);
}

char *ft_clearmemory(char *memory)
{
char *temp;
int len;
int i;
int j;

i = 0;
if (!memory)
return (NULL);
while (memory[i] != '\0' && memory[i] != '\n')
i++;
if (memory[i] == '\0')
{
free(memory);
return (NULL);
}
len = ft_strlen(memory) - i;
if (!(temp = malloc(sizeof(char) * (len + 1))))
return (NULL);
i++;
j = 0;
while (memory[i] != '\0')
temp[j++] = memory[i++];
temp[j] = '\0';
free(memory);
return (temp);
}

int get_next_line(int fd, char **line)
{
int r;
char *buffer;
static char *save;

if (fd < 0 || !line || BUFFER_SIZE <= 0)
return (-1);
buffer = malloc(BUFFER_SIZE + 1);
if (!buffer)
return (-1);
r = read(fd, buffer, BUFFER_SIZE);
while (r > 0)
{
buffer[r] = '\0';
save = ft_strjoin(save, buffer);
if (ft_strchr(save, '\n') || ft_strchr(save, '\0'))
break ;
}
free(buffer);
if (r < 0)
return (-1);
*line = ft_get_line(save);
save = ft_clearmemory(save);
if (r == 0 && !save)
return (0);
return (1);
}
72 changes: 72 additions & 0 deletions src/get_next_line_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: shovsepy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/14 05:23:12 by shovsepy #+# #+# */
/* Updated: 2021/02/14 05:23:14 by shovsepy ### ########.fr */
/* */
/* ************************************************************************** */

#include "../includes/get_next_line.h"

unsigned long ft_strlen(const char *s)
{
unsigned long i;

i = 0;
if (!s)
return (0);
while (s[i])
i++;
return (i);
}

char *ft_strjoin(char *s1, char *s2)
{
char *join;
int i;
int j;

join = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1));
if (!join)
return (NULL);
i = 0;
j = 0;
if (s1)
while (s1[i] != '\0')
join[j++] = s1[i++];
i = 0;
if (s2)
while (s2[i] != '\0')
join[j++] = s2[i++];
join[j] = '\0';
free(s1);
return (join);
}

int ft_strchr(const char *s, int c)
{
size_t i;

i = 0;
while (s[i])
{
if (s[i] == c)
return (1);
i++;
}
return (0);
}

int ft_get_line_size(char *s)
{
int i;

i = 0;
while (s[i] != '\n' && s[i] != '\0')
i++;
return (i);
}

0 comments on commit 773e303

Please sign in to comment.