base struct
This commit is contained in:
72
lib/libft/srcs/format/ft_atoi.c
Normal file
72
lib/libft/srcs/format/ft_atoi.c
Normal file
@@ -0,0 +1,72 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_atoi.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/14 14:00:41 by nalebrun #+# #+# */
|
||||
/* Updated: 2024/11/27 12:59:01 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../libft.h"
|
||||
|
||||
static int ft_isspace(char c)
|
||||
{
|
||||
if (c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f'
|
||||
|| c == '\r')
|
||||
return (1);
|
||||
else
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int ft_signer(char c, int *i)
|
||||
{
|
||||
int sign;
|
||||
|
||||
sign = 1;
|
||||
if (c == '-')
|
||||
{
|
||||
sign *= -1;
|
||||
(*i)++;
|
||||
}
|
||||
else if (c == '+')
|
||||
(*i)++;
|
||||
return (sign);
|
||||
}
|
||||
|
||||
static int ft_signed(int sign)
|
||||
{
|
||||
if (sign == -1)
|
||||
return (0);
|
||||
else
|
||||
return (-1);
|
||||
}
|
||||
|
||||
int ft_atoi(const char *str)
|
||||
{
|
||||
int i;
|
||||
int sign;
|
||||
long res;
|
||||
int current_digit;
|
||||
|
||||
if (!str)
|
||||
return (-1);
|
||||
res = 0;
|
||||
i = 0;
|
||||
while (ft_isspace(str[i]))
|
||||
i++;
|
||||
sign = ft_signer(str[i], &i);
|
||||
while (str[i] == '0')
|
||||
i++;
|
||||
while (ft_isdigit(str[i]))
|
||||
{
|
||||
current_digit = str[i] - '0';
|
||||
if (res > (LONG_MAX - current_digit) / 10)
|
||||
return (ft_signed(sign));
|
||||
res = (res * 10) + current_digit;
|
||||
i++;
|
||||
}
|
||||
return ((int)(res *= sign));
|
||||
}
|
||||
67
lib/libft/srcs/format/ft_itoa.c
Normal file
67
lib/libft/srcs/format/ft_itoa.c
Normal file
@@ -0,0 +1,67 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_itoa.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/15 14:36:46 by nalebrun #+# #+# */
|
||||
/* Updated: 2024/11/27 12:59:05 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../libft.h"
|
||||
|
||||
static char *ft_itoamem(long int n)
|
||||
{
|
||||
int i;
|
||||
char *res;
|
||||
int sign;
|
||||
|
||||
i = 0;
|
||||
sign = 0;
|
||||
if (n < 0)
|
||||
{
|
||||
sign = 1;
|
||||
n *= -1;
|
||||
}
|
||||
while (n > 9)
|
||||
{
|
||||
n /= 10;
|
||||
i++;
|
||||
}
|
||||
res = malloc(((i + 1) + sign + 1) * sizeof(char));
|
||||
if (!res)
|
||||
return (NULL);
|
||||
res[i + 1 + sign] = '\0';
|
||||
ft_memset(res, 48, ((i + 1) + sign) * sizeof(char));
|
||||
return (res);
|
||||
}
|
||||
|
||||
char *ft_itoa(int n)
|
||||
{
|
||||
char *res;
|
||||
int i;
|
||||
long int num;
|
||||
|
||||
num = n;
|
||||
res = ft_itoamem(num);
|
||||
if (!res)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
if (num < 0)
|
||||
{
|
||||
res[i] = '-';
|
||||
num *= -1;
|
||||
}
|
||||
i = ft_strlen(res);
|
||||
res[i] = '\0';
|
||||
i--;
|
||||
while (num > 9)
|
||||
{
|
||||
res[i--] = (num % 10) + '0';
|
||||
num /= 10;
|
||||
}
|
||||
res[i] = num + '0';
|
||||
return (res);
|
||||
}
|
||||
88
lib/libft/srcs/format/ft_split.c
Normal file
88
lib/libft/srcs/format/ft_split.c
Normal file
@@ -0,0 +1,88 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_split.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/11 16:15:47 by nalebrun #+# #+# */
|
||||
/* Updated: 2024/11/27 12:58:41 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../libft.h"
|
||||
|
||||
static int ft_word_count(const char *str, char c)
|
||||
{
|
||||
int count;
|
||||
|
||||
if (!str)
|
||||
return (0);
|
||||
count = 0;
|
||||
while (*str)
|
||||
{
|
||||
while (*str && *str == c)
|
||||
str++;
|
||||
if (*str && *str != c)
|
||||
count++;
|
||||
while (*str && *str != c)
|
||||
str++;
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
|
||||
static char *malloccpy(const char *str, char c)
|
||||
{
|
||||
char *result;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (str[i] && str[i] != c)
|
||||
i++;
|
||||
result = (char *)malloc(sizeof(char) * (i + 1));
|
||||
if (!result)
|
||||
return (NULL);
|
||||
ft_strlcpy(result, str, i + 1);
|
||||
return (result);
|
||||
}
|
||||
|
||||
void free_tab(char **tab)
|
||||
{
|
||||
char **pos;
|
||||
|
||||
if (!tab)
|
||||
return ;
|
||||
pos = tab;
|
||||
while (*pos != NULL)
|
||||
free(*(pos++));
|
||||
free(tab);
|
||||
}
|
||||
|
||||
char **ft_split(const char *s, char c)
|
||||
{
|
||||
char **result;
|
||||
int i;
|
||||
int strs_len;
|
||||
|
||||
if (!s)
|
||||
return (NULL);
|
||||
strs_len = ft_word_count(s, c);
|
||||
result = (char **)malloc(sizeof(char *) * (strs_len + 1));
|
||||
if (!result)
|
||||
return (NULL);
|
||||
i = -1;
|
||||
while (++i < strs_len)
|
||||
{
|
||||
while (*s && *s == c)
|
||||
s++;
|
||||
result[i] = malloccpy(s, c);
|
||||
if (!result[i])
|
||||
{
|
||||
free_tab(result);
|
||||
return (NULL);
|
||||
}
|
||||
s += ft_strlen(result[i]);
|
||||
}
|
||||
result[i] = 0;
|
||||
return (result);
|
||||
}
|
||||
Reference in New Issue
Block a user