/get_next_line

This project is about programming a function that returns a line read from a file descriptor.

Primary LanguageCOtherNOASSERTION


GET_NEXT_LINE

โ—ฆ Reading a line from fd is way too tedious

42 Barcelona

GitHub%20Actions GitHub code size GitHub top language

๐Ÿ“– Table of Contents


๐Ÿ“ Overview

The get_next_line project is a programming exercise that challenges you to implement a function capable of reading a line from a file descriptor. The primary goal is to develop a function that allows reading from a file descriptor efficiently, providing a line-by-line extraction from the given input. This project introduces the concept of static variables and encourages a deeper understanding of memory management and file I/O in the C programming language.


๐Ÿ“ฆ Features

  • Prototype: char *get_next_line(int fd);
  • Parameters:
    • ๐Ÿ“ fd: The file descriptor to read from.
  • Return value:
    • ๐Ÿ“œ Read line: Returns the next line from the specified file descriptor.
    • ๐Ÿšซ NULL: Indicates there is nothing else to read or an error occurred.
  • External functions: ๐Ÿ“– read, ๐Ÿ› ๏ธ malloc, ๐Ÿ—‘๏ธ free
  • Description: Writes a function that reads from a file descriptor (fd) and returns the next line of text.

๐Ÿ“‚ Repository Structure

โ””โ”€โ”€ get_next_line/
    โ”œโ”€โ”€ get_next_line.c
    โ”œโ”€โ”€ get_next_line.h
    โ”œโ”€โ”€ get_next_line_bonus.c
    โ”œโ”€โ”€ get_next_line_bonus.h
    โ”œโ”€โ”€ get_next_line_utils.c
    โ””โ”€โ”€ get_next_line_utils_bonus.c

โš™๏ธ Modules

Mandatory
File Summary
get_next_line.c Implements the core functionality of get_next_line. Reads a line from the given file descriptor and manages the linked list operations to handle line reading. It uses utility functions to efficiently handle dynamic memory allocation, buffer reading, and linked list manipulation. The function provides an interface for retrieving the next line from a file.
get_next_line_utils.c Contains utility functions used in the implementation of get_next_line. The key functionalities include checking for a newline character in a linked list node, finding the last node in a linked list, adding a new node to the end of a linked list, calculating the length of content in a linked list until a newline character, and clearing the memory allocated for nodes in a linked list. These utilities contribute to the overall functionality of reading lines from a file.
get_next_line.h Header file defining the struct t_list and function prototypes for get_next_line and associated utilities. The struct t_list represents a linked list node containing the content of a line and a pointer to the next node. The function prototypes include those for reading lines, managing linked lists, and handling memory. This header provides the necessary interface for using the get_next_line function and associated utilities.
Bonus
File Summary
get_next_line_bonus.c The get_next_line_bonus.c file extends the core functionality of get_next_line by introducing features that enable the simultaneous management of multiple file descriptors (fd). The primary change occurs in the get_next_line function, where it now maintains a static array of linked lists (lst) corresponding to different file descriptors. This allows the system to handle and track the state of each file descriptor independently, ensuring seamless reading of lines from multiple sources concurrently. The file orchestrates the flow and integration of these features, coordinating the initialization, reading, and cleanup processes for each file descriptor.
get_next_line_utils_bonus.c Contains utility functions unchanged from the mandatory implementation. These functions facilitate the core operations related to linked lists and memory management and are reused in the bonus implementation to maintain consistency and code modularity. They contribute to the effective management of linked lists within the extended get_next_line system, supporting operations like checking for newline characters, obtaining the last node, adding nodes, calculating length until newline, and clearing nodes.
get_next_line_bonus.h Header file for the bonus functionality, defining additional structs and function prototypes. The changes in the header are minimal and involve introducing the necessary structures and functions to support the concurrent management of multiple file descriptors. It extends the structures and functions defined in the mandatory header (get_next_line.h) to accommodate the new requirements. The header file ensures that the core functionality remains intact while providing an interface for the bonus features to seamlessly integrate with the existing system.

๐Ÿš€ Getting Started

Dependencies

Please ensure you have the following dependencies installed on your system:

  • โ„น๏ธ C Compiler: You will need a C compiler to build and run the project.

    • Example for GCC (GNU Compiler Collection):
      sudo apt-get install gcc  # Ubuntu
      sudo yum install gcc      # CentOS

๐Ÿ”ง Installation

  1. Clone the get_next_line repository:
git clone https://github.com/San-tito/get_next_line
  1. Change to the project directory:
cd get_next_line
  1. Compile the dependencies:
gcc -Wall -Werror -Wextra -D BUFFER_SIZE=42 get_next_line*.c -o gnl

๐Ÿค– Running get_next_line

./gnl file.txt

๐Ÿงช Tests

/* Not implemented */

๐Ÿ›ฃ Roadmap

  • โ„น๏ธ Task 1: Implement handling of multiple file descriptors at the same time
  • โ„น๏ธ Task 2: Implement tests
  • โ„น๏ธ ...

๐Ÿค Contributing

Contributions are welcome! Here are several ways you can contribute:


๐Ÿ“„ License

This project is protected under the UNLICENSE License. For more details, refer to the LICENSE file.


๐Ÿ‘ Acknowledgments

Return