minishell
get_next_line.c File Reference
#include "get_next_line.h"
#include "../ft_mem/ft_mem.h"
#include "../ft_vect/ft_vect.h"
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
Include dependency graph for get_next_line.c:

Go to the source code of this file.

Functions

t_gnl_state gen_gnl_state (int fd, ssize_t cap)
 
void dispose_gnl_state (t_gnl_state *state)
 
static bool check_and_update_state (t_vect *ret, t_gnl_state *state)
 
char * get_next_line (t_gnl_state *state)
 

Variables

static const t_gnl_state g_gnl_state_init_val
 

Function Documentation

◆ check_and_update_state()

static bool check_and_update_state ( t_vect ret,
t_gnl_state state 
)
static

Definition at line 57 of file get_next_line.c.

58 {
59  char *lf_ptr;
60  size_t append_len;
61 
62  if (state->len == 0)
63  return (false);
64  lf_ptr = (char *)ft_memchr(state->buf, '\n', state->len);
65  if (lf_ptr == NULL)
66  append_len = state->len;
67  else
68  append_len = lf_ptr - state->buf + 1;
69  vect_append_str(ret, state->buf, append_len);
70  if (lf_ptr != NULL)
71  {
72  ft_memmove(state->buf, state->buf + append_len,
73  state->len - append_len);
74  state->len -= append_len;
75  }
76  else
77  state->len = 0;
78  return (lf_ptr != NULL);
79 }
void * ft_memchr(const void *s, int c, size_t n)
Definition: ft_memchr.c:16
void * ft_memmove(void *dst, const void *src, size_t n)
Definition: ft_memmove.c:16
bool vect_append_str(t_vect *vect, const char *value, size_t count)
ssize_t len
Definition: get_next_line.h:26
Here is the call graph for this function:
Here is the caller graph for this function:

◆ dispose_gnl_state()

void dispose_gnl_state ( t_gnl_state state)

Definition at line 49 of file get_next_line.c.

50 {
51  free(state->buf);
52  *state = g_gnl_state_init_val;
53 }
static const t_gnl_state g_gnl_state_init_val
Definition: get_next_line.c:26
Here is the caller graph for this function:

◆ gen_gnl_state()

t_gnl_state gen_gnl_state ( int  fd,
ssize_t  cap 
)

Definition at line 37 of file get_next_line.c.

38 {
39  t_gnl_state state;
40 
41  state = g_gnl_state_init_val;
42  state.buf = ft_calloc(cap, sizeof(char));
43  if (state.buf != NULL)
44  state.cap = cap;
45  state.fd = fd;
46  return (state);
47 }
void * ft_calloc(size_t count, size_t size)
Definition: ft_calloc.c:29
ssize_t cap
Definition: get_next_line.h:27
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_next_line()

char* get_next_line ( t_gnl_state state)

Definition at line 81 of file get_next_line.c.

82 {
83  t_vect ret;
84  ssize_t read_result;
85 
86  if (state == NULL || state->buf == NULL || state->cap == 0)
87  return (NULL);
88  ret = vect_init(0, sizeof(char));
89  if (check_and_update_state(&ret, state))
90  return (ret.p);
91  while (true)
92  {
93  read_result = read(state->fd, state->buf, state->cap);
94  if (read_result <= 0)
95  {
96  dispose_gnl_state(state);
97  if (ret.len == 0)
98  vect_dispose(&ret);
99  return (ret.p);
100  }
101  state->len = read_result;
102  if (check_and_update_state(&ret, state))
103  return (ret.p);
104  }
105 }
void vect_dispose(t_vect *vect)
Definition: vect_dispose.c:16
t_vect vect_init(size_t cap, size_t elemsize)
Definition: vect_init.c:16
static bool check_and_update_state(t_vect *ret, t_gnl_state *state)
Definition: get_next_line.c:57
void dispose_gnl_state(t_gnl_state *state)
Definition: get_next_line.c:49
Definition: ft_vect.h:24
void * p
Definition: ft_vect.h:28
size_t len
Definition: ft_vect.h:26
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ g_gnl_state_init_val

const t_gnl_state g_gnl_state_init_val
static
Initial value:
= {
0,
NULL,
0,
0
}

Definition at line 26 of file get_next_line.c.