1
0
mirror of https://github.com/KeyZox71/ft_minipowershell.git synced 2025-05-15 08:38:46 +02:00

🔨」 fix(libft): replaced libft by a better one :D

This commit is contained in:
y-syo
2024-04-29 13:53:00 +02:00
parent 77786466e3
commit 73bb992b01
77 changed files with 1511 additions and 1320 deletions

View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:00:18 by mmoussou #+# #+# */
/* Updated: 2023/11/13 19:29:30 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *str)
{
int r;
int i;
int s;
r = 0;
i = 0;
s = 1;
if (!str)
return (0);
while (str[i] == 32 || (str[i] >= 9 && str[i] <= 13))
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i] == '-')
s *= -1;
i++;
}
while (str[i] && (str[i] >= '0' && str[i] <= '9'))
{
r = (r * 10) + (str[i] - '0');
i++;
}
return (r * s);
}

View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atol.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:00:18 by mmoussou #+# #+# */
/* Updated: 2024/01/18 00:39:13 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
long long ft_atol(const char *str)
{
long long r;
int i;
char s;
r = 0;
i = 0;
s = 1;
if (!str)
return (0);
while (str[i] == 32 || (str[i] >= 9 && str[i] <= 13))
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i] == '-')
s *= -1;
i++;
}
while (str[i] && (str[i] >= '0' && str[i] <= '9'))
{
r = (r * 10) + (str[i] - '0');
i++;
}
return (r * s);
}

View File

@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/05 12:45:53 by mmoussou #+# #+# */
/* Updated: 2023/11/11 11:41:53 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int n_size(int n)
{
int s;
s = 0;
if (n <= 0)
{
s++;
n *= -1;
}
while (n >= 1)
{
s++;
n /= 10;
}
return (s);
}
static size_t pwten(int n)
{
int i;
i = 1;
while (n)
{
i *= 10;
n--;
}
return (i);
}
static char *itoa_rec(char *r, int n)
{
int i;
if (n > 9)
{
r[0] = n / (pwten(n_size(n) - 1)) + '0';
i = 1;
while (n_size(n) - (n_size(n % pwten(n_size(n) - 1))) != i)
{
r[i] = '0';
i++;
}
itoa_rec(&r[n_size(n) - (n_size(n % pwten(n_size(n) - 1)))], \
n % pwten(n_size(n) - 1));
return (r);
}
r[0] = n + '0';
return (r);
}
char *ft_itoa(int n)
{
char *r;
if (n == -2147483648)
return (ft_strdup("-2147483648"));
r = ft_calloc(sizeof(char), n_size(n) + 1);
if (!r)
return (NULL);
if (n == 0)
{
r[0] = '0';
return (r);
}
if (n < 0)
{
r[0] = '-';
itoa_rec(&r[1], n * -1);
return (r);
}
itoa_rec(r, n);
return (r);
}

View File

@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ltoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/05 12:45:53 by mmoussou #+# #+# */
/* Updated: 2024/01/12 00:52:12 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int n_size(int n)
{
int s;
s = 0;
if (n <= 0)
{
s++;
n *= -1;
}
while (n >= 1)
{
s++;
n /= 10;
}
return (s);
}
static size_t pwten(int n)
{
int i;
i = 1;
while (n)
{
i *= 10;
n--;
}
return (i);
}
static char *itoa_rec(char *r, int n)
{
int i;
if (n > 9)
{
r[0] = n / (pwten(n_size(n) - 1)) + '0';
i = 1;
while (n_size(n) - (n_size(n % pwten(n_size(n) - 1))) != i)
{
r[i] = '0';
i++;
}
itoa_rec(&r[n_size(n) - (n_size(n % pwten(n_size(n) - 1)))], \
n % pwten(n_size(n) - 1));
return (r);
}
r[0] = n + '0';
return (r);
}
char *ft_ltoa(long long n)
{
char *r;
if (n == -9223372036854775807)
return (ft_strdup("-9223372036854775807"));
r = ft_calloc(sizeof(char), n_size(n) + 1);
if (!r)
return (NULL);
if (n == 0)
{
r[0] = '0';
return (r);
}
if (n < 0)
{
r[0] = '-';
itoa_rec(&r[1], n * -1);
return (r);
}
itoa_rec(r, n);
return (r);
}

119
libft/src/str/ft_split.c Normal file
View File

@ -0,0 +1,119 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/31 17:59:11 by mmoussou #+# #+# */
/* Updated: 2023/11/13 19:39:13 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int free_list(char **result, int j)
{
while (j)
{
free(result[j]);
j--;
}
return (1);
}
static int word_counter(const char *str, char ch)
{
int i;
int c;
int bool_c;
i = 0;
c = 0;
bool_c = 1;
while (str[i])
{
if (str[i] == ch)
{
bool_c = 1;
}
else if (str[i] != ch && bool_c)
{
c++;
bool_c = 0;
}
i++;
}
return (c);
}
static int per_word_fill(char **result, const char *str, char ch)
{
int i;
int j;
int c;
i = 0;
j = 0;
while (str[i])
{
c = 0;
while (str[i] != ch && str[i])
{
c++;
i++;
}
if (c != 0)
{
result[j] = ft_calloc(sizeof(char), c + 1);
if (result[j++] == NULL)
return (free_list(result, j - 2));
}
if (str[i])
i++;
}
result[j] = NULL;
return (0);
}
static void ft_split_resolver(char **result, const char *str, char ch)
{
int i;
int j;
int wi;
int bool_w;
i = 0;
j = 0;
wi = 0;
bool_w = 0;
while (str[i])
{
if (str[i] == ch && bool_w)
{
bool_w = 0;
wi = 0;
j++;
}
else if (str[i] != ch)
{
bool_w = 1;
result[j][wi] = str[i];
wi++;
}
i++;
}
}
char **ft_split(const char *str, char c)
{
char **result;
result = malloc(sizeof(char *) * (word_counter(str, c) + 1));
if (result == NULL)
return (NULL);
if (per_word_fill(result, str, c))
return (NULL);
ft_split_resolver(result, str, c);
return (result);
}

31
libft/src/str/ft_strchr.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:01:50 by mmoussou #+# #+# */
/* Updated: 2023/11/13 19:39:47 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strchr(const char *s, int c)
{
size_t i;
i = 0;
if (!s)
return (NULL);
while (s[i])
{
if (s[i] == (unsigned char)c)
return (&((char *)s)[i]);
i++;
}
if (!c)
return (&((char *)s)[i]);
return (NULL);
}

27
libft/src/str/ft_strcmp.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:03:09 by mmoussou #+# #+# */
/* Updated: 2024/01/11 18:03:15 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strcmp(const char *s1, const char *s2)
{
unsigned int i;
i = 0;
while ((s1[i] || s2[i]))
{
if (((unsigned char *)s1)[i] != ((unsigned char *)s2)[i])
return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]);
i++;
}
return (0);
}

33
libft/src/str/ft_strdup.c Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:02:20 by mmoussou #+# #+# */
/* Updated: 2023/11/13 19:41:29 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strdup(char *src)
{
char *rst;
int i;
i = 0;
if (!src)
return (NULL);
rst = malloc(sizeof(char) * (ft_strlen(src) + 1));
if (!rst)
return (NULL);
while (src[i])
{
rst[i] = src[i];
i++;
}
rst[i] = 0;
return (rst);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striteri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 13:59:56 by mmoussou #+# #+# */
/* Updated: 2023/11/11 11:45:06 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_striteri(char *s, void (*f)(unsigned int, char *))
{
size_t i;
if (!s || !f)
return ;
i = 0;
while (s[i])
{
f(i, s + i);
i++;
}
}

View File

@ -0,0 +1,82 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 19:35:01 by mmoussou #+# #+# */
/* Updated: 2024/02/26 03:07:16 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strjoin(const char *s1, const char *s2)
{
char *fstr;
if (!s1)
s1 = "";
if (!s2)
s2 = "";
fstr = ft_calloc(sizeof(char), ft_strlen(s1) + ft_strlen(s2) + 1);
if (!fstr)
return (NULL);
ft_strlcpy(fstr, s1, ft_strlen(s1) + 1);
ft_strlcat(fstr, s2, ft_strlen(s1) + ft_strlen(s2) + 1);
return (fstr);
}
char *ft_strjoin_free_s1(char *s1, const char *s2)
{
char *fstr;
if (!s1)
s1 = "";
if (!s2)
s2 = "";
fstr = ft_calloc(sizeof(char), ft_strlen(s1) + ft_strlen(s2) + 1);
if (!fstr)
return (NULL);
ft_strlcpy(fstr, s1, ft_strlen(s1) + 1);
ft_strlcat(fstr, s2, ft_strlen(s1) + ft_strlen(s2) + 1);
if (s1[0])
free(s1);
return (fstr);
}
char *ft_strjoin_free_s2(const char *s1, char *s2)
{
char *fstr;
if (!s1)
s1 = "";
if (!s2)
s2 = "";
fstr = ft_calloc(sizeof(char), ft_strlen(s1) + ft_strlen(s2) + 1);
if (!fstr)
return (NULL);
ft_strlcpy(fstr, s1, ft_strlen(s1) + 1);
ft_strlcat(fstr, s2, ft_strlen(s1) + ft_strlen(s2) + 1);
free(s2);
return (fstr);
}
char *ft_strjoin_free(char *s1, char *s2)
{
char *fstr;
if (!s1)
s1 = "";
if (!s2)
s2 = "";
fstr = ft_calloc(sizeof(char), ft_strlen(s1) + ft_strlen(s2) + 1);
if (!fstr)
return (NULL);
ft_strlcpy(fstr, s1, ft_strlen(s1) + 1);
ft_strlcat(fstr, s2, ft_strlen(s1) + ft_strlen(s2) + 1);
free(s1);
free(s2);
return (fstr);
}

View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:02:48 by mmoussou #+# #+# */
/* Updated: 2023/11/11 13:27:31 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcat(char *dst, const char *src, size_t size)
{
size_t dl;
if (!size)
return (ft_strlen(src));
dl = ft_strlen(dst);
if (size <= dl)
return (size + ft_strlen(src));
return (dl + ft_strlcpy(dst + dl, src, size - dl));
}

View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:02:43 by mmoussou #+# #+# */
/* Updated: 2023/11/03 16:25:20 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcpy(char *dst, const char *src, size_t size)
{
size_t i;
i = 0;
while (i + 1 < size && src[i])
{
dst[i] = src[i];
i++;
}
if (i < size)
dst[i] = 0;
while (src[i])
i++;
return (i);
}

21
libft/src/str/ft_strlen.c Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/31 12:42:36 by mmoussou #+# #+# */
/* Updated: 2024/04/16 18:09:42 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
uint ft_strlen(const char *s)
{
const char *endptr;
endptr = s;
while (*endptr)
endptr++;
return (endptr - s);
}

View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 13:53:45 by mmoussou #+# #+# */
/* Updated: 2023/11/07 15:36:32 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strmapi(const char *s, char (*f)(unsigned int, char))
{
char *r;
int i;
if (!s || !f)
return ((char *)s);
r = ft_calloc(sizeof(char), ft_strlen(s) + 1);
if (!r)
return (NULL);
i = 0;
while (s[i])
{
r[i] = (*f)(i, s[i]);
i++;
}
return (r);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:03:09 by mmoussou #+# #+# */
/* Updated: 2023/11/11 13:19:18 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strncmp(const char *s1, const char *s2, unsigned int n)
{
unsigned int i;
i = 0;
while ((s1[i] || s2[i]) && i < n)
{
if (((unsigned char *)s1)[i] != ((unsigned char *)s2)[i])
return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]);
i++;
}
return (0);
}

View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 21:55:45 by mmoussou #+# #+# */
/* Updated: 2023/11/13 19:46:01 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *big, const char *little, size_t len)
{
size_t i;
size_t j;
i = 0;
if (!little || !little[0] || big == little)
return ((char *)big);
if (!len || !big)
return (NULL);
while (i < len && big[i])
{
j = 0;
while (i + j < len && big[i + j] == little[j])
j++;
if (j == (size_t)ft_strlen(little))
return (&((char *)big)[i]);
i++;
}
return (NULL);
}

View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 21:57:57 by mmoussou #+# #+# */
/* Updated: 2023/11/13 19:46:30 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strrchr(const char *s, int c)
{
size_t i;
if (!s)
return (NULL);
i = ft_strlen(s);
if (!c)
return (&((char *)s)[i]);
while (i + 1)
{
if (s[i] == (unsigned char)c)
return (&((char *)s)[i]);
i--;
}
return (NULL);
}

View File

@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/31 13:39:05 by mmoussou #+# #+# */
/* Updated: 2023/11/12 00:25:11 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int is_charset(char c, char const *set)
{
size_t i;
i = 0;
while (set[i])
{
if (c == set[i])
return (1);
i++;
}
return (0);
}
char *ft_strtrim(const char *s1, const char *set)
{
size_t o;
int i;
char *r;
if (!s1 || !set)
return ((char *)s1);
o = 0;
while (s1[o] && is_charset(s1[o], set))
o++;
i = ft_strlen(s1) - o - 1;
while (i > 0 && is_charset(s1[o + i], set))
i--;
if (o == ft_strlen(s1) - 1)
return (malloc(0));
r = ft_calloc(sizeof(char), i + 2);
if (!r)
return (NULL);
while (i + 1)
{
r[i] = s1[o + i];
i--;
}
return (r);
}

39
libft/src/str/ft_substr.c Normal file
View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/30 17:46:28 by mmoussou #+# #+# */
/* Updated: 2023/11/11 11:45:52 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t l;
size_t nl;
char *r;
if (!s)
return (NULL);
l = ft_strlen(s);
if (start > l)
{
r = ft_calloc(1, 1);
return (r);
}
nl = ft_strlen(s + start);
if (nl > len)
nl = len;
r = malloc((nl + 1) * sizeof(char));
if (!r)
return (NULL);
r[nl] = 0;
while (nl--)
r[nl] = s[start + nl];
return (r);
}

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tolower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 21:56:42 by mmoussou #+# #+# */
/* Updated: 2023/11/01 21:56:43 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_tolower(int c)
{
if (c >= 'A' && c <= 'Z')
return (c + ('a' - 'A'));
return (c);
}

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_toupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 21:58:40 by mmoussou #+# #+# */
/* Updated: 2023/11/01 21:58:55 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_toupper(int c)
{
if (c >= 'a' && c <= 'z')
return (c - ('a' - 'A'));
return (c);
}