Skip to content

Jarnomer/gnl

Repository files navigation

get_next_line

Read file descriptors 📖 one line at the time.

score GitHub top language GitHub code size in bytes GitHub repo file or directory count

type GitHub last commit (branch)

📝 General

Goal of the project is to create a function get_next_line to read file descriptors line by line.

Files with _bonus suffix support reading multiple file descriptors in parallel.

Project passes many of the 42 testers, including franzinette strict, although timeout has to be modified.

Project uses linked list where nodes include read content and address to next node.

typedef struct s_gnl
{
  char         *content;
  struct s_gnl *next;
}  t_gnl;

BUFFER_SIZE defines the amount of characters read. If not included during compilation, it is defined by header.

# ifndef BUFFER_SIZE
#  define BUFFER_SIZE 100
# endif

🛠️ Build

GNU gcc is required to compile the project.

git clone https://github.com/Jarnomer/gnl.git

⚡ Usage

gnl has to be compiled with your main. Here is an example that takes a file name.

You can also read from STDIN_FILENO and end the input with Crtl + D.

gcc -D BUFFER_SIZE=42 main.c get_next_line.c get_next_line_utils.c -o gnl
./gnl <file_name>
#include "get_next_line.h"
#include <stdio.h>

int main(int argc, char **argv) {
  char *line;
  int fd;

  if (argc != 2)
    return 1;
  fd = open(argv[1], O_RDONLY);
  if (fd == -1)
    return 1;
  while (1) {
    line = get_next_line(fd);
    if (line == NULL)
      break;
    printf("%s", line);
    free(line);
  }
  close(fd);
  return 0;
}

🚀 Details

clean_list frees all unneeded nodes every time and moves head to last node created.

Returns read line unless malloc fails or EOF is reached, then whole list is freed and NULL is returned.

Before this every node's content is joined and last node's content gets shifted by prep_next_iter.

char *clean_list(t_gnl **lst, char *line)
{
  t_gnl  *temp;

  if (!lst || !*lst)
    return (NULL);
  while ((*lst)->next)
  {
    temp = (*lst)->next;
    free((*lst)->content);
    free(*lst);
    *lst = temp;
  }
  if (line && *line)
    return (line);
  free((*lst)->content);
  free(*lst);
  free(line);
  *lst = NULL;
  return (NULL);
}

♻️ Resources

franzinette amazing unit test framework for gnl and other 42 projects.

4️⃣2️⃣ Footer

For my other 42 projects and general information, please refer the Hive42 page.

I have also created error handling unit testers for pipex, so_long and cub3D.

Cheers and good luck! 🥳

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages