Compare commits

..

No commits in common. "9b1684a85acb2295b735e46e3dc5f7493ca46916" and "df151381bc64f134d008d2192677f8e574ba4adb" have entirely different histories.

10 changed files with 92 additions and 477 deletions

View File

@ -1,31 +0,0 @@
# get_next_line
This is my implementation of the get_next_line project for the 42 school.
## Description
get_next_line is a function that reads a file descriptor (FD) line by line until the end of file (EOF) or an error occurs.
The function returns each line as a string, without the line break character '\n'.
The purpose of the project is to provide a function for reading lines from a file descriptor, which can be useful for various applications.
## Usage
To use the function, simply include the header file and call the function with the file descriptor as the first argument.
The function will return:
- The line: if a line has been read
- Empty string: if the end of file has been reached
- NULL: if an error occurred
It is important to note that the function uses dynamic memory allocation to return the line as a string, so it is necessary to free the memory after use.
## Resources
- ![The project subject](./subject.pdf)
- The standard C library documentation
## Authors
This project was created by Camille CHAUVET. If you have any questions or suggestions, feel free to contact me.

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/26 00:52:47 by cchauvet #+# #+# */
/* Updated: 2022/11/14 15:37:48 by cchauvet ### ########.fr */
/* Updated: 2022/10/26 16:39:09 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,11 +17,11 @@ char *ft_getstash(int fd)
char *str;
int readed;
str = ft_calloc(BUFFER_SIZE + 1, sizeof(char));
str = ft_calloc(BUFFER_SIZE, sizeof(char));
if (str == NULL)
return (NULL);
readed = read(fd, str, BUFFER_SIZE);
if (readed < 1)
if (readed < 0)
{
free(str);
return (NULL);
@ -29,18 +29,39 @@ char *ft_getstash(int fd)
return (str);
}
ssize_t ft_strchr(char *str, char c)
{
size_t i;
if (str == NULL)
return (-1);
i = 0;
while (str[i] != c && str[i] != '\0')
i++;
if (str[i] == '\0')
return (-1);
return (i);
}
char *ft_getline(int fd)
{
char *stash;
char *buf;
size_t i;
stash = NULL;
buf = NULL;
while (ft_strchr(stash, '\n') == -1)
i = 0;
while (ft_strchr(stash + i, '\n') == -1)
{
i = i + ft_strlen(buf);
buf = ft_getstash(fd);
if (buf == NULL)
return (stash);
return (NULL);
if (buf[0] == '\0')
{
free(buf);
break ;
}
stash = ft_strfjoin(stash, buf);
if (stash == NULL)
return (NULL);
@ -48,53 +69,26 @@ char *ft_getline(int fd)
return (stash);
}
char *ft_getreturn(char *str)
{
int i;
if (str == NULL)
return (NULL);
i = ft_strchr(str, '\n') + 1;
if (i == 0)
i = ft_strlen(str);
return (ft_strndup(str, i));
}
char *ft_getextra(char *str)
{
int i;
int j;
if (str == NULL)
return (NULL);
i = ft_strchr(str, '\n') + 1;
if (i == 0)
return (NULL);
j = ft_strlen(str + i);
return (ft_strndup(str + i, j));
}
char *get_next_line(int fd)
{
static char *stash = NULL;
char *buf1;
char *buf2;
buf2 = stash;
if (ft_strchr(stash, '\n') == -1)
{
buf1 = ft_getline(fd);
buf2 = ft_strfjoin(stash, buf1);
if (buf2 == NULL)
{
free(buf1);
if (buf1 == NULL)
return (NULL);
stash = ft_strfjoin(stash, buf1);
if (stash == NULL)
return (NULL);
}
}
buf1 = ft_getreturn(buf2);
stash = ft_getextra(buf2);
free(buf2);
if (buf1 == NULL)
buf1 = ft_strndup(stash, ft_strchr(stash, '\n') + 1);
buf2 = ft_strndup(stash + ft_strchr(stash, '\n') + 1, ft_strlen(stash));
free(stash);
stash = buf2;
if (stash == NULL || buf1 == NULL)
{
free(stash);
free(buf1);

View File

@ -1,28 +1,14 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/14 15:38:06 by cchauvet #+# #+# */
/* Updated: 2022/11/14 17:28:39 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H
# include <stdlib.h>
# include <unistd.h>
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 42
# endif
# define BUFFER_SIZE 10
void *ft_calloc(size_t nmemb, size_t size);
void *ft_realloc(void *ptr, size_t size);
char *ft_strfjoin(char *dst, char *src);
char *ft_strndup(char *src, size_t n);
size_t ft_strlen(char *str);
ssize_t ft_strchr(char *str, char c);
char *get_next_line(int fd);
#endif

View File

@ -1,100 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/26 00:52:47 by cchauvet #+# #+# */
/* Updated: 2022/11/23 15:19:15 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
char *ft_getstash(int fd)
{
char *str;
int readed;
str = ft_calloc(BUFFER_SIZE + 1, sizeof(char));
if (str == NULL)
return (NULL);
readed = read(fd, str, BUFFER_SIZE);
if (readed < 1)
{
free(str);
return (NULL);
}
return (str);
}
char *ft_getline(int fd)
{
char *stash;
char *buf;
stash = NULL;
buf = NULL;
while (ft_strchr(stash, '\n') == -1)
{
buf = ft_getstash(fd);
if (buf == NULL)
return (stash);
stash = ft_strfjoin(stash, buf);
if (stash == NULL)
return (NULL);
}
return (stash);
}
char *ft_getreturn(char *str)
{
int i;
if (str == NULL)
return (NULL);
i = ft_strchr(str, '\n') + 1;
if (i == 0)
i = ft_strlen(str);
return (ft_strndup(str, i));
}
char *ft_getextra(char *str)
{
int i;
int j;
if (str == NULL)
return (NULL);
i = ft_strchr(str, '\n') + 1;
if (i == 0)
return (NULL);
j = ft_strlen(str + i);
return (ft_strndup(str + i, j));
}
char *get_next_line(int fd)
{
static char *stash[1024] = {0};
char *buf1;
char *buf2;
if (fd > 1023)
return (NULL);
buf2 = stash[fd];
if (ft_strchr(stash[fd], '\n') == -1)
{
buf1 = ft_getline(fd);
buf2 = ft_strfjoin(stash[fd], buf1);
}
buf1 = ft_getreturn(buf2);
stash[fd] = ft_getextra(buf2);
if (buf1 == NULL || buf2 == NULL)
{
free(stash[fd]);
free(buf1);
}
free(buf2);
return (buf1);
}

View File

@ -1,28 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/14 15:38:06 by cchauvet #+# #+# */
/* Updated: 2022/11/14 17:29:19 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_NEXT_LINE_BONUS_H
# define GET_NEXT_LINE_BONUS_H
# include <stdlib.h>
# include <unistd.h>
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 42
# endif
void *ft_calloc(size_t nmemb, size_t size);
void *ft_realloc(void *ptr, size_t size);
char *ft_strfjoin(char *dst, char *src);
char *ft_strndup(char *src, size_t n);
size_t ft_strlen(char *str);
ssize_t ft_strchr(char *str, char c);
char *get_next_line(int fd);
#endif

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/26 00:55:44 by cchauvet #+# #+# */
/* Updated: 2022/11/14 15:39:50 by cchauvet ### ########.fr */
/* Updated: 2022/10/26 16:33:23 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -31,6 +31,31 @@ void *ft_calloc(size_t nmemb, size_t size)
return ((void *) tab);
}
void *ft_realloc(void *ptr, size_t size)
{
char *tab;
char *in;
size_t i;
in = ptr;
tab = ft_calloc(sizeof(char), size);
if (tab == NULL)
return (NULL);
if (ptr == NULL)
{
free(ptr);
return (tab);
}
i = 0;
while (in[i] != 0 && i < size)
{
tab[i] = in[i];
i++;
}
free(ptr);
return (tab);
}
size_t ft_strlen(char *str)
{
size_t i;
@ -45,8 +70,8 @@ size_t ft_strlen(char *str)
char *ft_strfjoin(char *s1, char *s2)
{
ssize_t i;
ssize_t j;
size_t i;
size_t j;
char *out;
out = ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char));
@ -62,11 +87,11 @@ char *ft_strfjoin(char *s1, char *s2)
}
}
free(s1);
j = -1;
if (s2 != NULL)
j = 0;
while (s2[j] != '\0')
{
while (s2[++j] != '\0')
out[i + j] = s2[j];
out[i + j] = s2[j];
j++;
}
free(s2);
return (out);
@ -77,8 +102,6 @@ char *ft_strndup(char *src, size_t n)
char *out;
size_t i;
if (src[0] == '\0')
return (NULL);
out = ft_calloc(n + 1, sizeof(char));
if (out == NULL)
return (NULL);
@ -90,17 +113,3 @@ char *ft_strndup(char *src, size_t n)
}
return (out);
}
ssize_t ft_strchr(char *str, char c)
{
size_t i;
if (str == NULL)
return (-1);
i = 0;
while (str[i] != c && str[i] != '\0')
i++;
if (str[i] == '\0')
return (-1);
return (i);
}

View File

@ -1,106 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/26 00:55:44 by cchauvet #+# #+# */
/* Updated: 2022/11/14 15:50:27 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
void *ft_calloc(size_t nmemb, size_t size)
{
char *tab;
size_t i;
if (nmemb == 0 || size * nmemb / nmemb != size)
return (NULL);
tab = malloc(nmemb * size);
if (tab == NULL)
return (NULL);
i = 0;
while (i < size * nmemb)
{
tab[i] = 0;
i++;
}
return ((void *) tab);
}
size_t ft_strlen(char *str)
{
size_t i;
if (str == NULL)
return (0);
i = 0;
while (str[i] != '\0')
i++;
return (i);
}
char *ft_strfjoin(char *s1, char *s2)
{
ssize_t i;
ssize_t j;
char *out;
out = ft_calloc((ft_strlen(s1) + ft_strlen(s2) + 1), sizeof(char));
if (out == NULL)
return (NULL);
i = 0;
if (s1 != NULL)
{
while (s1[i] != '\0')
{
out[i] = s1[i];
i++;
}
}
free(s1);
j = -1;
if (s2 != NULL)
{
while (s2[++j] != '\0')
out[i + j] = s2[j];
}
free(s2);
return (out);
}
char *ft_strndup(char *src, size_t n)
{
char *out;
size_t i;
if (src[0] == '\0')
return (NULL);
out = ft_calloc(n + 1, sizeof(char));
if (out == NULL)
return (NULL);
i = 0;
while (src[i] != '\0' && i < n)
{
out[i] = src[i];
i++;
}
return (out);
}
ssize_t ft_strchr(char *str, char c)
{
size_t i;
if (str == NULL)
return (-1);
i = 0;
while (str[i] != c && str[i] != '\0')
i++;
if (str[i] == '\0')
return (-1);
return (i);
}

19
main.c Normal file
View File

@ -0,0 +1,19 @@
#include "get_next_line.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(void)
{
int fd = open("tester", O_RDONLY);
char *str = get_next_line(fd);
printf("%s", str);
free(str);
// printf("%s", get_next_line(fd));
// printf("%s", get_next_line(fd));
// printf("%s", get_next_line(fd));
// printf("%s", get_next_line(fd));
// printf("%s", get_next_line(fd));
}

View File

@ -1,131 +0,0 @@
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<meta content='IE=Edge,chrome=1' http-equiv='X-UA-Compatible'>
<meta content='width=device-width,height=device-height,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0' name='viewport'>
<meta content='yes' name='mobile-web-app-capable'>
<meta content='yes' name='apple-mobile-web-app-capable'>
<meta content='translucent-black' name='apple-mobile-web-app-status-bar-style'>
<meta content='Intranet 42' name='apple-mobile-web-app-title'>
<meta content='on' http-equiv='cleartype'>
<link href='https://cdn.intra.42.fr/' rel='preconnect'>
<link href='https://signin.intra.42.fr/assets/42_logo_black-684989d43d629b3c0ff6fd7e1157ee04db9bb7a73fba8ec4e01543d650a1c607.png' rel='icon' type='image/png'>
<link href='https://signin.intra.42.fr/assets/42_logo_black-684989d43d629b3c0ff6fd7e1157ee04db9bb7a73fba8ec4e01543d650a1c607.png' rel='shortcut icon' type='image/png'>
<title>Intra Signin New</title>
<meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="aQQWo5tAv+V6w2tSRFc4iq9f/kmWzgxP4ynnuIRicZeEQf8JSgoow9s4Bp9IQhfFzwPghzs3TeM8SxkQ6kWgQQ==" />
<link rel="stylesheet" media="all" href="/assets/application-0f8ff68ca8056b0b2e36230747f7e997b432658c4a215497757d1ab87ae73b0d.css" />
<style>
.deprecated {
-webkit-filter: grayscale(100%);
filter: grayscale(100%); }
</style>
<script>
(function() {
this._environment = "production";
this._git_commit = "";
this._release = this._git_commit;
}).call(this);
</script>
<body class=' '>
<div class='notifications-flash-top-bar'>
</div>
<div class='page'>
<div class='page-sidebar left-main-container page-sidebar-fixed-left under-main-navbar'></div>
<div class='login-page page-content page-content-fluid' data-turbolinks-scaffold>
<div class='row'>
<div class='main-top-menu'>
</div>
<div class='col-lg-12'>
<div class='login-container'>
<div class='login-logo'>
<img src="/assets/42_logo-7dfc9110a5319a308863b96bda33cea995046d1731cebb735e41b16255106c12.svg" alt="42 logo" />
</div>
<div class='login-between'></div>
<div class='login-main'>
<a class="btn btn-login-student" href="https://profile.intra.42.fr/users/auth/keycloak_student">Sign in as student</a>
<a class="btn btn-login-admin" href="https://profile.intra.42.fr/users/auth/keycloak_admin">Sign in as staff</a>
<a class="forgot-password-link" href="https://profile.intra.42.fr/legal">Read our terms and conditions</a><br />
</div>
</div>
</div>
</div>
</div>
</div>
<div aria-hidden='true' aria-labelledby='smartModalLabel' class='modal fade' id='smartModal' role='dialog' tabindex='-1'>
<div class='modal-dialog'>
<div class='modal-content'>
<div class='modal-header'>
<button aria-hidden='true' class='close' data-dismiss='modal' type='button'>
x
</button>
<h3 id='smartModalLabel'>
</h3>
</div>
<div class='modal-body'>
<h4 class='note-title'></h4>
</div>
<div class='modal-footer'>
<button aria-hidden='true' class='btn' data-dismiss='modal'>Cancel</button>
<button class='send btn btn-primary'>Send</button>
</div>
</div>
</div>
</div>
<div aria-hidden='true' aria-labelledby='flashModalLabel' class='modal fade' id='flashModal' role='dialog' tabindex='-1'>
<div class='modal-dialog modal-lg'>
<div class='modal-content'>
<div class='modal-header'>
<button aria-hidden='true' class='close' data-dismiss='modal' type='button'>
x
</button>
<h3 id='flashModalLabel'></h3>
</div>
<div class='modal-body'>
<h4 class='note-title'>Flash modal content (raw)</h4>
</div>
<div class='modal-footer'>
<button aria-hidden='true' class='btn btn-block btn-default' data-dismiss='modal'>Close</button>
</div>
</div>
</div>
</div>
<div data-cache="true" class="hide"></div>
</body>
<script async await src='https://www.googletagmanager.com/gtag/js?id=G-BJ34XNRJCV'></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-BJ34XNRJCV');
</script>
<script>
var jsClasses = ["new_message", "topic-reply"];
jsClasses.forEach(function(className) {
Array.prototype.forEach.call(document.getElementsByClassName(className), function (e) {
e.classList.add("js-hidden");
});
});
</script>
<script src="/assets/application-fed7f3dd771fa014c9bc9723023f686a0aef20cadbc8fdab7fce5ffd3d1e46b7.js"></script>
</head>
</html>

3
tester Normal file
View File

@ -0,0 +1,3 @@
test1
test2
test3