반응형
Mandatory part
get_next_line.h
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# include <unistd.h>
# include <stdlib.h>
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 42
# endif
char *ft_check_null(char *line);
char *ft_strdup(char *string);
char *ft_strjoin(char *s1, char *s2, int s2_len, int count);
char *ft_strcat(char *buff, char *prev);
char *ft_strchr(const char *s, int c);
int ft_strlen(const char *s);
char *ft_substr(char *s, unsigned int start, unsigned int len);
char *ft_substrab(char *s, unsigned int start, int len);
char *ft_nonewline(char *line);
char *ft_readline(int fd, char *buff, char *backup);
char *get_next_line(int fd);
#endif
get_next_line.c
#include "get_next_line.h"
char *readline(int fd, char *line)
{
char buff[BUFFER_SIZE + 1];
int readcount;
readcount = 1;
while (readcount)
{
readcount = read(fd, buff, BUFFER_SIZE);
if (readcount == 0)
break ;
else if (readcount == -1)
{
free(line);
line = NULL;
return (NULL);
}
buff[readcount] = '\\0';
line = ft_strjoin(line, buff, readcount, ft_strlen(line));
if (!line)
return (NULL);
if (ft_strchr(buff, '\\n'))
break ;
}
return (line);
}
static char *ft_checkline(char *line, char **backup)
{
int i;
int len_line;
i = 0;
if (!line)
{
line = NULL;
return (NULL);
}
while (line[i] != '\\0' && line[i] != '\\n')
i++;
if (line[i] == '\\0')
return (NULL);
len_line = ft_strlen(line);
*backup = ft_substr(line, i + 1, len_line - i);
if (!(*backup))
return (NULL);
if (*backup[0] == '\\0')
{
free(*backup);
line = NULL;
*backup = NULL;
return (NULL);
}
return (*backup);
}
int len_check(char *line)
{
int i;
i = 0;
if (line == 0)
return (0);
while (line[i] != '\\0')
{
if (line[i] == '\\n')
return (i);
i++;
}
return (-1);
}
char *change_line(char **line, char **backup)
{
int count;
char *temp;
if (!(*line))
return (NULL);
count = len_check(*line);
if (count == -1)
temp = ft_strdup(*line);
else
temp = ft_substr(*line, 0, count + 1);
if (!temp)
{
free(*backup);
*backup = NULL;
temp = NULL;
return (NULL);
}
return (temp);
}
char *get_next_line(int fd)
{
static char *backup;
char *line;
char *temp;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
if (backup)
{
line = ft_strdup(backup);
free(backup);
backup = NULL;
if (!line)
return (NULL);
}
else
line = NULL;
line = readline(fd, line);
if (!line)
return (NULL);
backup = ft_checkline(line, &backup);
temp = change_line(&line, &backup);
free(line);
if (!temp)
return (NULL);
return (temp);
}
get_next_line_utils.c
#include "get_next_line.h"
int ft_strlen(const char *s)
{
int i;
if (!s)
return (0);
i = 0;
while (s[i] != '\\0')
{
i++;
}
return (i);
}
char *ft_substr(char *s, unsigned int start, unsigned int len)
{
unsigned int i;
char *temp;
unsigned int cou;
cou = (unsigned int)ft_strlen(s);
if (cou <= start)
return (ft_strdup(""));
if (len >= cou - start)
len = cou - start;
temp = (char *)malloc(sizeof(char) * len + 1);
if (!temp)
return (NULL);
i = 0;
while (s[start] != '\\0' && i < len)
{
temp[i] = s[start];
start++;
i++;
}
temp[i] = '\\0';
return (temp);
}
char *ft_strdup(char *string)
{
char *temp;
int i;
int count;
i = 0;
if (!string)
return (0);
count = ft_strlen(string);
temp = (char *)malloc(sizeof(char) * (count + 1));
if (!temp)
return (0);
while (string[i])
{
temp[i] = string[i];
i++;
}
temp[i] = '\\0';
return (temp);
}
char *ft_strjoin(char *s1, char *s2, int s2_len, int count)
{
char *temp;
int i;
i = 0;
temp = (char *)malloc(sizeof(char) * (count + s2_len + 1));
if (!temp)
{
free(s1);
return (0);
}
while (i < count)
{
temp[i] = s1[i];
i++;
}
i = 0;
while (i < s2_len)
{
temp[count + i] = s2[i];
i++;
}
temp[count + i] = '\\0';
free(s1);
s1 = NULL;
return (temp);
}
char *ft_strchr(const char *s, int c)
{
char *temp;
int count;
unsigned char ca;
temp = (char *)s;
ca = (unsigned char)c;
if (ca == '\\0')
{
count = ft_strlen(s);
return (temp + count);
}
while (*temp)
{
if (*temp == ca)
return (temp);
temp++;
}
return (0);
}
Bonus part
get_next_line_bonus.h
#ifndef GET_NEXT_LINE_BONUS_H
# define GET_NEXT_LINE_BONUS_H
# include <unistd.h>
# include <stdlib.h>
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 42
# endif
# define OPENMAX 49152
char *ft_check_null(char *line);
char *ft_strdup(char *string);
char *ft_strjoin(char *s1, char *s2, int s2_len, int count);
char *ft_strcat(char *buff, char *prev);
char *ft_strchr(const char *s, int c);
int ft_strlen(const char *s);
char *ft_substr(char *s, unsigned int start, unsigned int len);
char *ft_substrab(char *s, unsigned int start, int len);
char *ft_nonewline(char *line);
char *ft_readline(int fd, char *buff, char *backup);
char *get_next_line(int fd);
#endif
get_next_line_bonus.c
#include "get_next_line_bonus.h"
char *readline(int fd, char *line)
{
char buff[BUFFER_SIZE + 1];
int readcount;
readcount = 1;
while (readcount)
{
readcount = read(fd, buff, BUFFER_SIZE);
if (readcount == 0)
break ;
else if (readcount == -1)
{
free(line);
line = NULL;
return (NULL);
}
buff[readcount] = '\\0';
line = ft_strjoin(line, buff, readcount, ft_strlen(line));
if (!line)
return (NULL);
if (ft_strchr(buff, '\\n'))
break ;
}
return (line);
}
static char *ft_checkline(char *line, char **backup)
{
int i;
int len_line;
i = 0;
if (!line)
{
line = NULL;
return (NULL);
}
while (line[i] != '\\0' && line[i] != '\\n')
i++;
if (line[i] == '\\0')
return (NULL);
len_line = ft_strlen(line);
*backup = ft_substr(line, i + 1, len_line - i);
if (!(*backup))
return (NULL);
if (*backup[0] == '\\0')
{
free(*backup);
line = NULL;
*backup = NULL;
return (NULL);
}
return (*backup);
}
int len_check(char *line)
{
int i;
i = 0;
if (line == 0)
return (0);
while (line[i] != '\\0')
{
if (line[i] == '\\n')
return (i);
i++;
}
return (-1);
}
char *change_line(char **line, char **backup)
{
int count;
char *temp;
if (!(*line))
return (NULL);
count = len_check(*line);
if (count == -1)
temp = ft_strdup(*line);
else
temp = ft_substr(*line, 0, count + 1);
if (!temp)
{
free(*backup);
*backup = NULL;
temp = NULL;
return (NULL);
}
return (temp);
}
char *get_next_line(int fd)
{
static char *backup[OPENMAX];
char *line;
char *temp;
if (fd < 0 || BUFFER_SIZE <= 0 || fd > OPENMAX)
return (NULL);
if (backup[fd])
{
line = ft_strdup(backup[fd]);
free(backup[fd]);
backup[fd] = NULL;
if (!line)
return (NULL);
}
else
line = NULL;
line = readline(fd, line);
if (!line)
return (NULL);
backup[fd] = ft_checkline(line, &backup[fd]);
temp = change_line(&line, &backup[fd]);
free(line);
if (!temp)
return (NULL);
return (temp);
}
get_next_line_utils_bonus.c
#include "get_next_line_bonus.h"
int ft_strlen(const char *s)
{
int i;
if (!s)
return (0);
i = 0;
while (s[i] != '\\0')
{
i++;
}
return (i);
}
char *ft_substr(char *s, unsigned int start, unsigned int len)
{
unsigned int i;
char *temp;
unsigned int cou;
cou = (unsigned int)ft_strlen(s);
if (cou <= start)
return (ft_strdup(""));
if (len >= cou - start)
len = cou - start;
temp = (char *)malloc(sizeof(char) * len + 1);
if (!temp)
return (NULL);
i = 0;
while (s[start] != '\\0' && i < len)
{
temp[i] = s[start];
start++;
i++;
}
temp[i] = '\\0';
return (temp);
}
char *ft_strdup(char *string)
{
char *temp;
int i;
int count;
i = 0;
if (!string)
return (0);
count = ft_strlen(string);
temp = (char *)malloc(sizeof(char) * (count + 1));
if (!temp)
return (0);
while (string[i])
{
temp[i] = string[i];
i++;
}
temp[i] = '\\0';
return (temp);
}
char *ft_strjoin(char *s1, char *s2, int s2_len, int count)
{
char *temp;
int i;
i = 0;
temp = (char *)malloc(sizeof(char) * (count + s2_len + 1));
if (!temp)
{
free(s1);
return (0);
}
while (i < count)
{
temp[i] = s1[i];
i++;
}
i = 0;
while (i < s2_len)
{
temp[count + i] = s2[i];
i++;
}
temp[count + i] = '\\0';
free(s1);
s1 = NULL;
return (temp);
}
char *ft_strchr(const char *s, int c)
{
char *temp;
int count;
unsigned char ca;
temp = (char *)s;
ca = (unsigned char)c;
if (ca == '\\0')
{
count = ft_strlen(s);
return (temp + count);
}
while (*temp)
{
if (*temp == ca)
return (temp);
temp++;
}
return (0);
}
반응형
'42Seoul > get_next_line' 카테고리의 다른 글
5. 운영체제에 따른 read (0) | 2023.04.08 |
---|---|
4. 정적 변수(static) (0) | 2023.04.08 |
3. 파일 디스크립트 함수(open, read, close) (0) | 2023.04.08 |
2. OPEN_MAX, 연결리스트 vs 배열 (2) | 2023.04.08 |
1. 파일 디스크립터(file Descriptors) (0) | 2023.04.08 |