Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pipex Image

Get next line

Image

Summary

Get next line is a 42 school project with the goal of reading the next file from a given file descriptor. It should compile with the flag -D, which will define the buffer size for reading. For this, we need to learn what a static variable is and understand what file descriptors are and how to properly manage them.

Code Phases

Parsing
  1. First, we check if the file descriptor (fd) is smaller than 0. This happens because the first possible fd is 0, which is standard input. 1 means standard output and 2 means standard error. After that, all other fds are created by the user, as shown in the example I provided in the main function using open( ) to open some test files.
  2. Check if the macro BUFFER_SIZE is greater than 0, because its value will determine the number of characters we will read.
Get the line and save it to the static variable
  1. Call copy_fd( ) to read from the provided file descriptor.
    1. Alocate memory fot hte buffer variable to storage the character read
    2. Call read( ) with the parameters fd (the file descriptor from which it will be reading), buffer (which is the array of characters that we allocate memory for) and BUFFER_SIZE (the number of characters to be read).
    3. It will be on a loop that continues until reaches the end of the file or finds the '\n' (meaning a new line).
    4. Call add_to_strgs( ) on each iteration of the loop to add the content read to the static variable.
    5. Call check_for_new_line( ) to check if the static variable already contains the new line.
  2. Check if the document is empty or if all the characters have already been read, and if so, return NULL.
  3. Call verify_newline( ) to get the string for the program to return. There are a few possible outcomes for this function:
    1. There is a new line and nothing else after it. Therefore, the program will save everything in the variable next_line and the static variable will be empty.
    2. There is a new line and more content after it. The next_line variable gets all the content up to the new line and the rest is kept in the static variable.
    3. There is no new line, so it will save all the content stored in the static variable to next_line.
    4. The first character is already a new line, so it saves only the new line in the next_line variable and keeps the rest in the static variable.
Bonus The bonus part of this program follows the exact same structure as the mandatory one, with the only difference being the static variable. In this case, it will be an array of char arrays that stores the values for each file descriptor (fd) sent to the program.

Code Setup

Step 1: Clone the repository

git clone https://github.com/Hugofslopes/get_next_line

Step 2: Open the terminal on the repo folder and write

make

Step 3: Execute main

./main

Step 4: Execute main bonus

make bonus ; ./mainbonus

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages