Initial commit

This commit is contained in:
2026-01-09 15:20:33 +01:00
commit d0a9d550e5
31 changed files with 2248 additions and 0 deletions

46
srcs/utils/ft_atoi.c Normal file
View File

@@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lejulien <lejulien@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/05 13:06:04 by lejulien #+# #+# */
/* Updated: 2021/04/01 13:38:37 by lejulien ### ########.fr */
/* */
/* ************************************************************************** */
static int ft_isspace(char c)
{
if ((c == '\f') || (c == '\t') || (c == '\n') || (c == '\r')
|| (c == '\v') || (c == ' '))
{
return (1);
}
return (0);
}
int ft_atoi(const char *str)
{
int nbr;
int sign;
int index;
nbr = 0;
sign = 1;
index = 0;
while ((ft_isspace(str[index]) == 1) && str[index] != '\0')
index++;
if ((str[index] == '+') || (str[index] == '-'))
{
if (str[index] == '-')
sign = -1;
index++;
}
while ((str[index] >= '0') && (str[index] <= '9'))
{
nbr = (nbr * 10) + (str[index] - '0');
index++;
}
return (sign * nbr);
}

62
srcs/utils/ft_itoa.c Normal file
View File

@@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/31 16:22:36 by lejulien #+# #+# */
/* Updated: 2021/03/31 16:22:41 by lejulien ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
static char *ft_itoa2(int iszero, unsigned int nu, int i)
{
char *res;
if (!(res = malloc((i + 1) * sizeof(char))))
return (0);
res[i] = '\0';
while (nu > 0)
{
i--;
res[i] = nu % 10 + '0';
nu = nu / 10;
}
i--;
if (iszero)
{
res[i] = nu + '0';
i--;
}
if (i == 0)
res[i] = '-';
return (res);
}
char *ft_itoa(int n)
{
unsigned int nu;
int i;
int iszero;
i = 0;
if (n <= 0)
{
nu = -n;
i++;
}
else
nu = n;
iszero = 0;
while (n != 0)
{
n = n / 10;
i++;
}
if (nu == 0)
iszero++;
return (ft_itoa2(iszero, nu, i));
}

38
srcs/utils/ft_putnbr_fd.c Normal file
View File

@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lejulien <lejulien@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/05 13:05:29 by lejulien #+# #+# */
/* Updated: 2021/03/05 13:05:39 by lejulien ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
static void ft_putchar(char c, int fd)
{
write(fd, &c, 1);
}
void ft_putnbr_fd(int nb, int fd)
{
unsigned int dnb;
if (nb < 0)
{
ft_putchar('-', fd);
dnb = nb * (-1);
}
else
dnb = nb;
if (dnb >= 10)
{
ft_putnbr_fd(dnb / 10, fd);
ft_putchar(dnb % 10 + '0', fd);
}
else
ft_putchar(dnb + '0', fd);
}

26
srcs/utils/ft_putstr.c Normal file
View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lejulien <lejulien@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/05 13:08:09 by lejulien #+# #+# */
/* Updated: 2021/03/05 13:09:54 by lejulien ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void
ft_putstr(const char *str)
{
int i;
i = 0;
while (str[i] != '\0')
{
write(1, &str[i], 1);
i++;
}
}

28
srcs/utils/ft_sqrt.c Normal file
View File

@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_sqrt.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/08/12 13:38:59 by lejulien #+# #+# */
/* Updated: 2021/03/29 17:40:39 by lejulien ### ########.fr */
/* */
/* ************************************************************************** */
float ft_sqrt(float n)
{
float x;
float y;
float e;
x = n;
y = 1;
e = 0.00001;
while (x - y > e)
{
x = (x + y) / 2;
y = n / x;
}
return (x);
}

28
srcs/utils/ft_strcmp.c Normal file
View File

@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lejulien <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/31 15:46:04 by lejulien #+# #+# */
/* Updated: 2021/04/01 13:38:20 by lejulien ### ########.fr */
/* */
/* ************************************************************************** */
int my_strcmp(char *s1, char *s2)
{
int index;
index = 0;
while (s2[index] && s1[index])
{
if (s1[index] == s2[index])
index++;
else
return (1);
}
if (s1[index] == '\0' && s2[index] == '\0')
return (0);
return (1);
}

22
srcs/utils/utils.h Normal file
View File

@@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lejulien <lejulien@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/05 13:06:31 by lejulien #+# #+# */
/* Updated: 2021/03/31 16:01:35 by lejulien ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef UTILS_H
# define UTILS_H
int ft_atoi(const char *str);
char *ft_itoa(int n);
void ft_putnbr_fd(int nb, int fd);
void ft_putstr(const char *str);
float ft_sqrt(float nb);
int my_strcmp(char *s1, char *s2);
#endif