Initial commit
This commit is contained in:
74
philo_one/srcs/utils/ft_atoi.c
Executable file
74
philo_one/srcs/utils/ft_atoi.c
Executable file
@@ -0,0 +1,74 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_atoi.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <lejulien@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/03/05 13:06:04 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/15 10:34:00 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);
|
||||
}
|
||||
|
||||
unsigned long int
|
||||
ft_atouli(const char *str)
|
||||
{
|
||||
unsigned long int nbr;
|
||||
int sign;
|
||||
unsigned long 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);
|
||||
}
|
||||
111
philo_one/srcs/utils/ft_itoa.c
Normal file
111
philo_one/srcs/utils/ft_itoa.c
Normal file
@@ -0,0 +1,111 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_itoa.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <lejulien@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/04/15 10:35:00 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/15 11:15:45 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));
|
||||
}
|
||||
|
||||
static char *ft_ulitoa2(int iszero, unsigned long 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_ulitoa(unsigned long int n)
|
||||
{
|
||||
unsigned long 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));
|
||||
}
|
||||
28
philo_one/srcs/utils/ft_strcmp.c
Normal file
28
philo_one/srcs/utils/ft_strcmp.c
Normal 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/15 10:58:32 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_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);
|
||||
}
|
||||
21
philo_one/srcs/utils/utils.h
Executable file
21
philo_one/srcs/utils/utils.h
Executable file
@@ -0,0 +1,21 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lejulien <lejulien@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/04/03 15:42:26 by lejulien #+# #+# */
|
||||
/* Updated: 2021/04/15 10:54:39 by lejulien ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef UTILS_H
|
||||
# define UTILS_H
|
||||
|
||||
int ft_atoi(char *str);
|
||||
unsigned long int ft_atouli(char *str);
|
||||
char *ft_itoa(int n);
|
||||
char *ft_ulitoa(unsigned long int n);
|
||||
int ft_strcmp(char *s1, char *s2);
|
||||
#endif
|
||||
Reference in New Issue
Block a user