All Projects → librity → ft_get_next_line

librity / ft_get_next_line

Licence: MIT license
42 São Paulo - get_next_line

Programming Languages

c
50402 projects - #5 most used programming language
shell
77523 projects
Makefile
30231 projects

Projects that are alternatives of or similar to ft get next line

42 Projects
What is 42? How does curriculum look like? A manual reference for all 42 projects I've done so far (Zappy, Malloc, C++ Piscine... check out for more)! 💡📈
Stars: ✭ 111 (+552.94%)
Mutual labels:  42born2code, 42, 42school
passport-42
Passport strategy for authenticating with 42 using the OAuth 2.0 API
Stars: ✭ 26 (+52.94%)
Mutual labels:  42born2code, 42, 42school
minishell tester
42 | Complex tester for minishell (42cursus)
Stars: ✭ 31 (+82.35%)
Mutual labels:  42born2code, 42, 42school
42header.vim
Add and update the 42 comment header at the top of your files
Stars: ✭ 15 (-11.76%)
Mutual labels:  42born2code, 42, 42school
42MapGenerator
Generate FdF maps of the real world, the Earth's moon and the planet Venus for the project Fil de Fer
Stars: ✭ 58 (+241.18%)
Mutual labels:  42born2code, 42, 42school
42ShellTester
Integration test suite for Shell implementation
Stars: ✭ 35 (+105.88%)
Mutual labels:  42born2code, 42, 42school
1337
This repository contains a collection of 42School cursus projects in addition to the pool/piscine days (with a detailed step by step explanation). Enjoy!
Stars: ✭ 35 (+105.88%)
Mutual labels:  42born2code, 42, 42school
42tools
Making 42 life better
Stars: ✭ 43 (+152.94%)
Mutual labels:  42born2code, 42
ftprintfdestructor
A script that destroys the school 42 project ft_printf
Stars: ✭ 20 (+17.65%)
Mutual labels:  42born2code, 42
init
School 42 project // DevOps project
Stars: ✭ 13 (-23.53%)
Mutual labels:  42, 42school
IRC-Server
IRC server based on TCP/IP protocol to rfc1459 standard
Stars: ✭ 27 (+58.82%)
Mutual labels:  42born2code, 42school
42-piscine-exam
This repo has all exercises of "C Exam Alone In The Dark - Beginner" sorted from level_00 to Level_05
Stars: ✭ 218 (+1182.35%)
Mutual labels:  42born2code, 42
examrank-02-03-04-05-06
exam project 2020
Stars: ✭ 195 (+1047.06%)
Mutual labels:  42born2code, 42school
gnlTester
Tester for the get_next_line project of 42 school
Stars: ✭ 87 (+411.76%)
Mutual labels:  gnl, 42
libft-war-machine
forked repository of libftest (by jtoty) for libft at 42
Stars: ✭ 167 (+882.35%)
Mutual labels:  42
printfTester
Tester for the ft_printf project of 42 school
Stars: ✭ 94 (+452.94%)
Mutual labels:  42
42cursus
42 project!
Stars: ✭ 27 (+58.82%)
Mutual labels:  42born2code
BetterBlackHole
Feeling pressured by the Black Hole and the counter that comes with it? With this extension, you are able to hide the demotivating countdown and sad emote, replacing it by just a date and a motivating message for extra motivation.
Stars: ✭ 17 (+0%)
Mutual labels:  42
coalibot
Slack bot for the school 42 using nlopes/slack Slack api client. This bot provide tools for student to access school informations.
Stars: ✭ 22 (+29.41%)
Mutual labels:  42
NormEZ
Coding-style checker for Epitech students. This program analyzes your C source files for Epitech coding-style violations.
Stars: ✭ 73 (+329.41%)
Mutual labels:  norminette

42 São Paulo - ft_get_next_line

norminette 42 São Paulo License Code size in bytes Lines of code Top language Last commit

A function that reads a file line-by-line.


📜 Table of Contents

🧐 About

What can I say that @harm-smits hasn't said already?

get_next_line is a function that reads an entire line from a file indexed by a file descriptor fd. It then alocates a string with the contents of that line without the linebreak '\n' and points line to it.

We don't have to pass anything allocated to get_next_line, we just pass the address of a pointer that will point to the allocated string. line should be freeable with free() after the function call unless an error occured.

We will incrementally read the file with read(), which advances its position in the file dexample escriptor automatically.

We need to use a static pointer as a read buffer to access what was read in previous calls.

We need to handle the following situations:

  1. If the read buffer doesn't have a '\n', we concatenate with the previous buffer and call read again.
  2. If the read buffer has a '\n', we concatenate with the previous buffer up to '\n'.
  3. If we reach the end of the file (read() == 0), we concatenate with the previous buffer.
  4. We finally point line to an allocated string that contains the entire line without the '\n'. Then we release the memory allocated in the intermediate strings and return 1 or 0 for '\n' and end_of_file respectively.
  5. If the parameters have any problems (BUFFER_SIZE <= 0), or if in any operation we were unable to allocate memory, we free whatever memory was allocated and return -1.

pt-br

Tem essa documentacao bem legal do @harm-smits.

O get_next_line lê uma linha inteira do arquivo indexado por fd, e faz o ponterio line apontar para uma string allocada com os conteudos dessa linha sem a quebra de linha '\n'.

Nao temos que passar nada allocado para o get_next_line, apenas passar o endereco de um ponteiro que vai apontar para a string allocada. Precisamos poder dar free() nessa string allocada depois da chamada.

A gente vai ler o arquivo com a funcao read(), que avanca a sua posicao no file descriptor automaticamente.

A gente precisa usar um ponteiro estatico como buffer do read para poder acessar o que foi lido pelo read nas chamadas anteriores.

Precisamos tratar as seguintes situacoes:

  1. Se o buffer lido nao tem '\n', concatenamos com o buffer anterior e chamamos read novamente.
  2. Se o buffer lido tem '\n', concatenamos com o buffer anterior ate o '\n'.
  3. Se chegamos no final do arquivo (read() = 0), concatenamos com o buffer anterior.
  4. Finalmente temos que apontar o ponteiro line passado para uma string allocada que contenha a linha inteira sem o '\n'. Depois liberamos a memoria allocada nas strings intermediarias e retornamos 1 ou 0 para '\n' ou final do arquivo respectivamente.
  5. Se os parametros tem algum problema (BUFFER_SIZE <= 0), ou em alguma dessas operacaoes nao conseguimos allocar memoria, liberamos toda a memoria allocada e retornamos -1.

🏁 Getting Started

⚙️ Prerequisites

All you need is a shell and a C compiler like gcc or clang.

🖥️ Installing

To compile the entire thing just clone the repo and run make with a positive BUFFER_SIZE:

$ git clone https://github.com/librity/ft_get_next_line.git
$ cd ft_get_next_line
$ make d="'BUFFER_SIZE=42'"

This will generate a get_next_line.a archive, which you can compile with the example file:

$ gcc -g -D BUFFER_SIZE=42 -I ./includes ./examples/example.c get_next_line.a
$ ./a.out

🐙 Github Actions

Norminette Github Action by @AdrianWR

🛸 42 São Paulo

Part of the larger 42 Network, 42 São Paulo is a software engineering school that offers a healthy alternative to traditional education:

  • It doesn't have any teachers and classes.
  • Students learn by cooperating and correcting each other's work (peer-to-peer learning).
  • Its focus is as much on social skills as it is on technical skills.
  • It's completely free to anyone that passes its selection process - The Piscine

It's an amazing school, and I'm grateful for the opportunity.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].