Skip to content

Instantly share code, notes, and snippets.

@mjy9088
Last active June 14, 2024 21:22
Show Gist options
  • Save mjy9088/c8e590a53a814ee3caa0b1f6399f467a to your computer and use it in GitHub Desktop.
Save mjy9088/c8e590a53a814ee3caa0b1f6399f467a to your computer and use it in GitHub Desktop.
get_next_line in 4 functions
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: Juyeong Maing <jmaing@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/16 16:47:34 by Juyeong Maing #+# #+# */
/* Updated: 2023/04/22 19:26:51 by Juyeong Maing ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
typedef enum e_h
{
j,
k,
l,
m,
n,
o,
} t_h;
typedef enum e_i
{
p,
q,
r,
s,
t,
u,
} t_i;
static size_t h(const char *a, t_h b, size_t c, char *d)
{
size_t e;
e = 0;
(b == j && *a) && (e = h(a + 1, b, c + 1, NULL));
(b == j && !*a) && (e = c);
(b == k && *a != '\n' && *a) && (e = h(a + 1, b, c + 1, NULL));
(b == k && !*a) && (e = -1);
(b == k && *a == '\n') && (e = c);
(b == l) && (e = c);
(b == l && *a && *a != '\n') && (e = h(a + 1, b, c + 1, NULL));
(b == l && *a == '\n') && (e++);
(b == m) && (*d = *a);
(b == m && *a) && (h(a + 1, b, 0, d + 1));
(b == n) && (*d = *a);
(b == n && *a && *a != '\n') && (h(a + 1, b, 0, d + 1));
(b == n && *a == '\n') && (d[1] = 0);
(b == o && !*a && !*d) && (e = 1);
(b == o && *a && *a == *d) && (e = h(a + 1, b, 0, d + 1));
return (e);
}
static bool i(t_i a, const char *b, const char *c, char **d)
{
bool e;
char *f;
e = false;
f = NULL;
(a == t) && (f = (char *)b);
free(f);
(a == t && c) && (i(a, c, NULL, NULL));
f = NULL;
(a == p) && (f = malloc(h(b, j, 0, NULL) + h(c, j, 0, NULL) + 1));
(a == p && f) && (h(b, m, 0, f) || h(c, m, 0, f + h(b, j, 0, NULL)));
(a == q) && (f = malloc(h(b, l, 0, NULL) + 1));
(a == q && f) && (h(b, n, 0, f));
(a == r) && (f = malloc(h(b + h(b, l, 0, NULL), j, 0, NULL) + 1));
(a == r && f) && (h(b + h(b, l, 0, NULL), m, 0, f));
(a == s) && (f = malloc(h(b, j, 0, NULL) + 1));
(a == s && f) && (h(b, m, 0, f));
((a == p || a == q || a == r || a == s) && !f) && (e = true);
(a == u) && (f = (char *)b);
(a == u && (!b || h("", o, 0, (char *)b))) && (e = true);
(a == u && e) && (i(t, b, NULL, NULL) || (f = NULL));
((a == p || a == q || a == r || a == s || a == u) && !d)
&& (i(t, f, NULL, NULL) || (e = true));
(d) && (*d = f);
return (e && a != u);
}
static char *v(char *a, int b)
{
char c[BUFFER_SIZE + 1];
const bool d = (a && h(a, k, 0, NULL) != (size_t)(-1));
bool e;
char *f;
ssize_t g;
f = NULL;
(d) && (f = a);
(d) && (a = NULL);
(!d) && (g = read(b, c, BUFFER_SIZE));
e = false;
(!d && g < 0) && (e = true);
(!d && !e) && (c[g] = '\0');
(!d && !e && !a) && (e = i(s, "", NULL, &a));
(!d && !e) && (e = i(p, a, c, &f));
i(t, a, NULL, NULL);
(f && h(f, k, 0, NULL) == (size_t)(-1) && g) && (f = v(f, b));
return (f);
}
char *get_next_line(int a)
{
static char *b;
char *c;
bool d;
char *e;
d = false;
c = NULL;
b = v(b, a);
(b && h(b, k, 0, NULL) == (size_t)(-1)) && (c = b);
(b && h(b, k, 0, NULL) == (size_t)(-1)) && (b = NULL);
e = NULL;
(b) && (d = i(q, b, NULL, &c) || i(r, b, NULL, &e));
(b && d) && (i(t, c, e, NULL) || i(t, b, NULL, NULL) || (b = NULL));
(d) && (c = NULL);
(b && !d) && (i(t, b, NULL, NULL) || (b = e));
(c) && (i(u, c, NULL, &c));
return (c);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: Juyeong Maing <jmaing@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/16 16:43:21 by Juyeong Maing #+# #+# */
/* Updated: 2023/04/16 20:15:49 by Juyeong Maing ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 1024
# elif BUFFER_SIZE < 1
# error BUFFER_SIZE must be greater than 0
# endif
char *get_next_line(int fd);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment