Push the final version
This commit is contained in:
commit
d0fe1fb2fb
28 changed files with 1459 additions and 0 deletions
54
libft/ft_atoi.c
Executable file
54
libft/ft_atoi.c
Executable file
|
|
@ -0,0 +1,54 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_atoi.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/08 17:22:41 by rparodi #+# #+# */
|
||||
/* Updated: 2024/03/07 11:11:31 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
static int ft_check_space(int c)
|
||||
{
|
||||
if (c == 32 || (c >= 9 && c <= 13))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int ft_check_sign(const char *nptr, int *i)
|
||||
{
|
||||
while (ft_check_space(nptr[*i]))
|
||||
(*i)++;
|
||||
if (nptr[*i] == '-')
|
||||
{
|
||||
(*i)++;
|
||||
return (-1);
|
||||
}
|
||||
else if (nptr[*i] == '+')
|
||||
(*i)++;
|
||||
return (1);
|
||||
}
|
||||
|
||||
long ft_atoi(const char *nptr)
|
||||
{
|
||||
int i;
|
||||
int sign;
|
||||
long number;
|
||||
|
||||
i = 0;
|
||||
sign = ft_check_sign(nptr, &i);
|
||||
number = 0;
|
||||
while (nptr[i])
|
||||
{
|
||||
if (nptr[i] >= '0' && nptr[i] <= '9')
|
||||
number = (number * 10) + nptr[i] - '0';
|
||||
else
|
||||
break ;
|
||||
i++;
|
||||
}
|
||||
return (sign * number);
|
||||
}
|
||||
20
libft/ft_isdigit.c
Executable file
20
libft/ft_isdigit.c
Executable file
|
|
@ -0,0 +1,20 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_isdigit.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/06 12:44:28 by rparodi #+# #+# */
|
||||
/* Updated: 2024/03/06 11:55:32 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
int ft_isdigit(int c)
|
||||
{
|
||||
if (c >= '0' && c <= '9')
|
||||
return (c);
|
||||
return (0);
|
||||
}
|
||||
30
libft/ft_lstadd_back.c
Executable file
30
libft/ft_lstadd_back.c
Executable file
|
|
@ -0,0 +1,30 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstadd_back.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/13 12:07:29 by rparodi #+# #+# */
|
||||
/* Updated: 2024/03/06 12:15:50 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
void ft_lstadd_back(t_list **stack, t_list *new)
|
||||
{
|
||||
t_list *node;
|
||||
|
||||
if (*stack)
|
||||
{
|
||||
node = ft_lstlast(*stack);
|
||||
node->next = new;
|
||||
new->next = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
*stack = new;
|
||||
(*stack)->next = NULL;
|
||||
}
|
||||
}
|
||||
19
libft/ft_lstadd_front.c
Executable file
19
libft/ft_lstadd_front.c
Executable file
|
|
@ -0,0 +1,19 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstadd_front.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/13 12:07:31 by rparodi #+# #+# */
|
||||
/* Updated: 2024/03/06 12:15:50 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
void ft_lstadd_front(t_list **stack, t_list *new)
|
||||
{
|
||||
new->next = *stack;
|
||||
*stack = new;
|
||||
}
|
||||
27
libft/ft_lstlast.c
Executable file
27
libft/ft_lstlast.c
Executable file
|
|
@ -0,0 +1,27 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstlast.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/13 12:07:33 by rparodi #+# #+# */
|
||||
/* Updated: 2024/03/06 12:15:50 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
t_list *ft_lstlast(t_list *head)
|
||||
{
|
||||
t_list *tmp;
|
||||
|
||||
tmp = head;
|
||||
while (tmp->next)
|
||||
{
|
||||
tmp = tmp->next;
|
||||
if (tmp->next == NULL)
|
||||
return (tmp);
|
||||
}
|
||||
return (tmp);
|
||||
}
|
||||
26
libft/ft_lstnew.c
Executable file
26
libft/ft_lstnew.c
Executable file
|
|
@ -0,0 +1,26 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstnew.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/13 12:07:45 by rparodi #+# #+# */
|
||||
/* Updated: 2024/03/06 12:15:50 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
t_list *ft_lstnew(int value)
|
||||
{
|
||||
t_list *new;
|
||||
|
||||
new = (t_list *) malloc(sizeof(*new));
|
||||
if (!new)
|
||||
return (NULL);
|
||||
new->value = value;
|
||||
new->index = -1;
|
||||
new->next = NULL;
|
||||
return (new);
|
||||
}
|
||||
28
libft/ft_lstsize.c
Executable file
28
libft/ft_lstsize.c
Executable file
|
|
@ -0,0 +1,28 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstsize.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/13 12:07:48 by rparodi #+# #+# */
|
||||
/* Updated: 2024/03/06 12:15:49 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
int ft_lstsize(t_list *head)
|
||||
{
|
||||
size_t i;
|
||||
t_list *tmp;
|
||||
|
||||
tmp = head;
|
||||
i = 0;
|
||||
while (tmp)
|
||||
{
|
||||
tmp = tmp->next;
|
||||
i++;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
32
libft/ft_memcpy.c
Executable file
32
libft/ft_memcpy.c
Executable file
|
|
@ -0,0 +1,32 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_memcpy.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/07 16:49:46 by rparodi #+# #+# */
|
||||
/* Updated: 2024/03/06 11:55:32 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
void *ft_memcpy(void *dest, const void *src, size_t n)
|
||||
{
|
||||
char *d;
|
||||
const char *s;
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
if (!dest && !src)
|
||||
return (NULL);
|
||||
d = (char *)dest;
|
||||
s = (char *)src;
|
||||
while (i < n)
|
||||
{
|
||||
d[i] = s[i];
|
||||
i++;
|
||||
}
|
||||
return ((void *)d);
|
||||
}
|
||||
100
libft/ft_split.c
Executable file
100
libft/ft_split.c
Executable file
|
|
@ -0,0 +1,100 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_split.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/09 13:56:02 by rparodi #+# #+# */
|
||||
/* Updated: 2024/04/18 15:21:57 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
static int ft_check_space(int c)
|
||||
{
|
||||
if (c == 32 || (c >= 9 && c <= 13))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int count_words(const char *str)
|
||||
{
|
||||
int i;
|
||||
int count;
|
||||
|
||||
i = 0;
|
||||
count = 0;
|
||||
while (str[i])
|
||||
{
|
||||
while (ft_check_space(str[i]) == 1 && str[i])
|
||||
i++;
|
||||
if (ft_check_space(str[i]) == 0 && str[i])
|
||||
{
|
||||
while (ft_check_space(str[i]) == 0 && str[i])
|
||||
i++;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
|
||||
static char *ft_strndup(const char *s, int j)
|
||||
{
|
||||
int i;
|
||||
char *str;
|
||||
|
||||
i = 0;
|
||||
str = (char *)malloc((j + 1));
|
||||
if (!str)
|
||||
error_message("alloc strndup", NULL, NULL, NULL);
|
||||
while (s[i] && i < j)
|
||||
{
|
||||
str[i] = s[i];
|
||||
i++;
|
||||
}
|
||||
str[i] = '\0';
|
||||
return (str);
|
||||
}
|
||||
|
||||
static char **ext_w(char **words_array, const char *str, int size)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (j < size)
|
||||
{
|
||||
while (ft_check_space(str[i]) == 1 && str[i])
|
||||
i++;
|
||||
str = str + i;
|
||||
i = 0;
|
||||
while (ft_check_space(str[i]) == 0 && str[i])
|
||||
i++;
|
||||
words_array[j++] = ft_strndup(str, i);
|
||||
str = str + i;
|
||||
i = 0;
|
||||
}
|
||||
words_array[j] = 0;
|
||||
return (words_array);
|
||||
}
|
||||
|
||||
char **ft_split(char const *s)
|
||||
{
|
||||
int size;
|
||||
char **words_array;
|
||||
|
||||
size = count_words(s);
|
||||
words_array = (char **)malloc(sizeof(char *) * (size + 1));
|
||||
if (!words_array)
|
||||
error_message("alloc word cut", NULL, NULL, NULL);
|
||||
if (size == 0)
|
||||
{
|
||||
words_array[0] = NULL;
|
||||
return (words_array);
|
||||
}
|
||||
words_array = ext_w(words_array, s, size);
|
||||
return (words_array);
|
||||
}
|
||||
29
libft/ft_strchr.c
Executable file
29
libft/ft_strchr.c
Executable file
|
|
@ -0,0 +1,29 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strchr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/07 16:32:19 by rparodi #+# #+# */
|
||||
/* Updated: 2024/04/17 19:44:38 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
char *ft_strchr(const char *s, int c)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
i = 0;
|
||||
if (c == 0)
|
||||
return ((char *)s + ft_len(s));
|
||||
while (s[i] != '\0')
|
||||
{
|
||||
if (s[i] == (char)c)
|
||||
return ((char *)s + i);
|
||||
i++;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
26
libft/ft_strdup.c
Executable file
26
libft/ft_strdup.c
Executable file
|
|
@ -0,0 +1,26 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strdup.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/07 16:53:59 by rparodi #+# #+# */
|
||||
/* Updated: 2024/04/17 19:45:29 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
char *ft_strdup(const char *s)
|
||||
{
|
||||
size_t len;
|
||||
char *to_return;
|
||||
|
||||
len = ft_len(s) + 1;
|
||||
to_return = (char *)malloc(sizeof(char) * len);
|
||||
if (!to_return)
|
||||
return (NULL);
|
||||
ft_strlcpy(to_return, s, len);
|
||||
return (to_return);
|
||||
}
|
||||
31
libft/ft_strlcpy.c
Normal file
31
libft/ft_strlcpy.c
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strlcpy.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/07 16:55:25 by rparodi #+# #+# */
|
||||
/* Updated: 2024/04/17 19:45:05 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
size_t ft_strlcpy(char *dst, const char *src, size_t size)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (src[i] && i + 1 < size)
|
||||
{
|
||||
dst[i] = src[i];
|
||||
i++;
|
||||
}
|
||||
if (size > 0)
|
||||
{
|
||||
dst[i] = '\0';
|
||||
i++;
|
||||
}
|
||||
return (ft_len(src));
|
||||
}
|
||||
23
libft/ft_strlen.c
Normal file
23
libft/ft_strlen.c
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strlen.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/07 16:56:24 by rparodi #+# #+# */
|
||||
/* Updated: 2024/04/17 19:42:58 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
size_t ft_len(const char *s)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (s[i] != '\0')
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
31
libft/ft_strncmp.c
Normal file
31
libft/ft_strncmp.c
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strncmp.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/07 16:56:56 by rparodi #+# #+# */
|
||||
/* Updated: 2023/11/09 13:16:39 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
int ft_strncmp(const char *s1, const char *s2, size_t n)
|
||||
{
|
||||
size_t i;
|
||||
int diff;
|
||||
|
||||
i = 0;
|
||||
while ((s1[i] || s2[i]) && i < n)
|
||||
{
|
||||
if (s1[i] != s2[i])
|
||||
{
|
||||
diff = (unsigned char)s1[i] - (unsigned char)s2[i];
|
||||
return (diff);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
32
libft/ft_substr.c
Executable file
32
libft/ft_substr.c
Executable file
|
|
@ -0,0 +1,32 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_substr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/09 13:54:42 by rparodi #+# #+# */
|
||||
/* Updated: 2024/04/17 19:45:46 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
char *ft_substr(char const *s, unsigned int start, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
size_t j;
|
||||
char *str;
|
||||
|
||||
i = start;
|
||||
if (start >= ft_len(s))
|
||||
return (ft_strdup(""));
|
||||
if (len + start > (ft_len(s)))
|
||||
len = ft_len(s) - start;
|
||||
j = 0;
|
||||
str = (char *)malloc(len + 1);
|
||||
if (!str || !s)
|
||||
return (free(str), ft_strdup(""));
|
||||
ft_strlcpy(str, s + start, len + 1);
|
||||
return (str);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue