Do You Coding?
[utility] get_next_line 구ííêž° 볞묞

ð ë§€ëŽìŒ (in subject)
Function name
get_next_line
Prototype
char *get_next_line(int fd);
Parameters
fd: The file descriptor to read from
: ìœìŽìš íìŒ ëì€í¬ëŠœí°
Return value
Read line: correct behavior / NULL: there is nothing else to read, or an error occurred
: ì¬ë°ë¥ž ëìì ì€ 1ì€ ìœìŽì€ë ê²ìŽê³ , ìœì ê² ìê±°ë ìë¬ê° ë°ìí멎 NULLì ë°ííë€
External functs
read, malloc, free
Description
Write a function that returns a line read from a file descriptor
: íìŒ ëì€í¬ëŠœí°ë¡ë¶í° ìœìŽì íì€ì ë°ííë íšì륌 ìì±íë€
ð ìì± ìœë (1) - header file code
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 10
# endif
# include <stdlib.h>
# include <unistd.h>
size_t do_strlen(const char *s);
char *do_strdup_s(char *str, char *buff);
char *do_strljoin(char *s1, char *s2, int rs);
size_t do_strlcpy(char *dst, const char *src, size_t size);
char *get_next_line(int fd);
#endif
ð ìì± ìœë (2) - utils code
#include "get_next_line.h"
size_t do_strlen(const char *s)
{
size_t i;
i = 0;
while (s[i] != '\0')
i ++;
return (i);
}
char *do_strdup_s(char *str, char *buff)
{
char *dup;
size_t buff_len;
size_t i;
buff_len = do_strlen(buff);
i = 0;
dup = (char *)malloc(sizeof(char) * (buff_len + 1));
if (dup == NULL)
return (NULL);
while (buff[i] != '\0')
{
dup[i] = buff[i];
i ++;
}
dup[i] = '\0';
if (str != NULL)
free (str);
return (dup);
}
char *do_strljoin(char *s1, char *s2, int rs)
{
char *s3;
size_t i;
size_t len_s1;
i = 0;
len_s1 = do_strlen(s1);
if (!s1 || !s2)
return (NULL);
s3 = (char *)malloc(sizeof(char) * (len_s1 + rs + 1));
if (s3 == NULL)
return (NULL);
while (i < len_s1)
{
s3[i] = s1[i];
i ++;
}
while (i < len_s1 + rs)
{
s3[i] = s2[i - len_s1];
i ++;
}
free (s1);
s3[i] = '\0';
return (s3);
}
size_t do_strlcpy(char *dst, const char *src, size_t size)
{
size_t i;
i = 0;
while (i + 1 < size && src[i] != '\0')
{
dst[i] = src[i];
i ++;
}
if (size != 0)
dst[i] = '\0';
return (do_strlen(src));
}
ð ìì± ìœë (3) - main code
#include "get_next_line.h"
static int read_and_store(int fd, char **str)
{
char buff[BUFFER_SIZE + 1];
int rs;
rs = read(fd, buff, BUFFER_SIZE);
if (rs <= 0)
return (rs);
buff[rs] = '\0';
if (*str == NULL)
*str = do_strdup_s(*str, buff);
else
*str = do_strljoin(*str, buff, rs);
return (rs);
}
static char *put_next_line(char **str, size_t len, size_t i)
{
char *restr;
restr = NULL;
i ++;
restr = malloc(i + 1);
if (restr == NULL)
return (NULL);
do_strlcpy(restr, *str, i + 1);
if (i - 1 == len)
{
free (*str);
*str = NULL;
}
else if (i - 1 != len)
*str = do_strdup_s(*str, *str + i);
return (restr);
}
char *get_next_line(int fd)
{
static char *str = NULL;
int rs;
size_t len;
size_t i;
while (1)
{
rs = read_and_store(fd, &str);
if (str[fd] != NULL)
len = do_strlen(str);
if (rs == -1 || (rs == 0 && str == NULL) || (len == 0))
{
free (str);
str = NULL;
return (NULL);
}
i = 0;
while (str[i] != '\n' && i < len)
i ++;
if ((i == len && (rs < BUFFER_SIZE)) || (i != len))
return (put_next_line(&str, len, i));
}
}
ð ìœë 늬뷰
- char *get_next_line(int fd)
â ëì : ìžìë¡ ë°ììš íìŒ ëì€í¬ëŠœí°(fd)ë¡ ìœìŽìš íìŒìì, ê°íì ì°Ÿìê°ë©° 1ì€ ì© ë°ííŽì£Œë íšì
â 죌ì 곌ì : staticìŒë¡ ëŽë¶ì ì ë³ì strì ì ìžíê³ , while(1)ì íµíŽ ë¬Ží ë°ë³µë¬žì ë늰ë€.
ê·ž í, read_and_store íšììì BUFFER_SIZE ë§íŒ readíì¬ buff 묞ììŽì ëŽìì€ë€.
strìŽ ë¹ìŽììŒë©Ž (=NULL) strdupìŒë¡ ìë¡ ëŽìì£Œê³ , ë¹ìŽìì§ ììŒë©Ž strljoinìŒë¡ ë¶ì¬ì€ë€.
ê·žëŠ¬ê³ ìœì read_size륌 ë°ííì¬ íŽë¹ ê°ì ë°ëŒ ììž ì²ëŠ¬ë¥Œ íŽì£Œê³ , ê°íìŽ íŽë¹ strì í¬íšëìŽ ììŒë©Ž
put_next_line íšì륌 ì€ííë€. íŽë¹ íšìììë íì¬ strìì ê°í ì ê¹ì§ 묞ììŽì ë°ííê³ ,
ê°í ìŽíì 묞ììŽë§ strì ëšê²šëë ìí ì íë€. ë§ìœ ììì strì ê°íìŽ í¬íš ëìŽìì§ ìë€ë©Ž,
while묞ì ë€ì ì€ííì¬ ë€ì read_and_store íšì륌 ì€ííì¬ BUFFER_SIZEë§íŒ readíë 곌ì ì ë°ë³µíë€
ê°íì ë§ë ë°íí ëê¹ì§ ìííë€.
â ì€ëª
- ì°ì , staticì íµíŽ íšì ëŽì ë§ë ëŽë¶ì ì ë³ìë íë¡ê·žëšìŽ ììë ë í ë¹ëê³ , ì¢
ë£í ë íŽì ëë€.
ë°ëŒì ì¬ë¬ë² íšì륌 ì€ííŽë, íë²ë§ ìŽêž°íëë¯ë¡ ìŽë¥Œ íì©íŽ ê°í ìŽíì 묞ììŽì ëŽìëë ìí ì íë€.
- while묞ì 묎í ë°ë³µìŒë¡ íì¬ ê³ì ì€ííê² ëëë°, ê°íì ë§ëì put_next_line íšìì ë°íê°ì ë°íí멎
ê·žë ì¢ ë£ëë€. get_next_line íšìê° ì¢ ë£ëìŽë ìììì ìžêžì²ëŒ ëŽë¶ì ì ë³ììŽë¯ë¡,
íë¡ê·žëš ì¢ ë£ê¹ì§ staticìŽ ì ì§ê° ëëë°, 첫 ì€ìŽ ê°íì ë§ë get_ next_lineì ì²ì ë°ííë€ê³ íŽë,
static ë³ìì ê·ž ê°í ìŽíì ê°ìŽ ëšììì ì ìë€.
(Buffer í¬êž°ë§íŒ read륌 ë°ë³µíë¯ë¡, buffer í¬êž°ì ë°ëŒ ê°í ìŽíì 묞ììŽìŽ ëšì ìì ì ìë€.)
- ìŽ static ìŒë¡ ì ìží 묞ììŽì ê°íìŽ ì€ê°ì ìë€ë©Ž ê·ž ìŽíì 묞ììŽë§ staticë³ìì ëšê²šëë 곌ì ìŽ íìíë€.
ë€ì get_next_lineì ë¶ë¬ìì ë, staticì ìë 묞ììŽ ë€ë¡ ìë¡ ìœìŽìš 묞ììŽì ë¶ì¬ìŒ ì ìì ìŒë¡
ë€ì ì€ì ê°ì žì¬ ì ìêž° ë묞ìŽë€.
- ì€í ì, buffer size륌 í°ë¯žëìì íì€ì ë ¥ìŒë¡ ë°ìì ì ì©íëë°, ìŽ ì ë ¥ê°ìŽ ì¡Žì¬íì§ìì ëë
headerìì defineìŽ ëì§ ììì ë 10ìŒë¡ Ʞ볞ê°ì ì§ì íŽëìë€.
- utilë€ì íìžíŽë³Žë©Ž, Ʞ졎 ëŒìŽëžë¬ëЬì ë§ë€ìŽëë str êŽë š íšìë€ê³Œ ë€ë¥Žê² ê°ì žìš stringì freeíŽì£Œë 곌ì ì
ì¶ê°íŽëìë€. Ʞ졎ì 졎ì¬íë stringì freeíŽì£Œê³ ìë¡ í ë¹ë 묞ììŽì ë°ííë ìì ë±ì íêž°ìíŽ ì ì©íìë€.
â íê³
- 3 ~ 4ë¬ ì ì ì§ íšìëŒ ì§êž 볎ë ì ëŠ¬ê° ê¹ëíê² ëìŽìì§ ìë€ë ëëìŽ ìꞎ íë€.
ëí, ë¶íìí ë¶ë¶ìŽ ì¡Žì¬íë©°, bufferë§íŒ ê³ì ìœë ê²ë³Žë€ë, ìŽë¯ž strì ê°íìŽ ì¡Žì¬í멎 ìœì§ ìê³ ë°ë¡
put_next_lineìŒë¡ ë겚죌ë ê²ìŽ ì¢ì ê² ê°ë€. ì묎ëë ìŽë°ììŒë¡ ë°êŸžë €ë©Ž ìë¡ ë€ ë°ê¿ìŒí ê² ê°ìë°,
ì¶íì ìê°ìŽ ë멎 'get_next_line_renewal_ver'ë¡ ìë¡ ë§ë€ìŽëŽìŒê² ë€. (...)
'CS & Engineering > C' 칎í ê³ ëŠ¬ì ë€ë¥ž êž
| [function] pthread_join íšì ìì볎Ʞ (8) | 2024.09.07 |
|---|---|
| [function] pthread_create íšì ìì볎Ʞ (5) | 2024.09.07 |
| [library] itoa 구ííêž° (0) | 2024.06.29 |
| [library] split 구ííêž° (0) | 2024.06.29 |
| [library] strtrim 구ííêž° (0) | 2024.06.27 |