This commit is contained in:
kinou-p 2022-01-18 06:57:33 +01:00
parent 772733d41e
commit eeb366d97e
19 changed files with 1106 additions and 34 deletions

View File

@ -6,34 +6,49 @@
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2021/11/13 13:06:47 by apommier #+# #+# #
# Updated: 2022/01/17 14:08:51 by apommier ### ########.fr #
# Updated: 2022/01/18 05:51:26 by apommier ### ########.fr #
# #
# **************************************************************************** #
NAME = push_swap
SRCS = push_swap.c \
process.c\
SRCS = Utils/push_swap.c \
Utils/process.c\
main.c\
sorting.c\
t_slist.c\
set_a.c\
optimise_move.c\
optimise_move2.c\
indexing.c
Utils/sorting.c\
Utils/t_slist.c\
Utils/set_a.c\
Utils/sort_little.c\
Utils/optimise_move.c\
Utils/optimise_move2.c\
Utils/indexing.c
OBJS = ${SRCS:.c=.o}
BONUS_C = checker_folder/main.c\
checker_folder/checker_utils.c\
Utils/push_swap.c \
Utils/process.c\
Utils/sorting.c\
Utils/t_slist.c\
Utils/set_a.c\
Utils/sort_little.c\
Utils/optimise_move.c\
Utils/optimise_move2.c\
Utils/indexing.c
BONUS_O = ${BONUS_C:.c=.o}
CFLAGS = -Wall -Wextra -Werror
RM = rm -rf
LIBFT = ./libft
2:${OBJS}
gcc -g ${CFLAGS} ${OBJS} ${LIBFT}/libft.a
mv a.out push_swap
${NAME}:${OBJS}
make bonus -C ${LIBFT}
gcc -g ${CFLAGS} ${OBJS} ${LIBFT}/libft.a
gcc -g ${OBJS} ${LIBFT}/libft.a
mv a.out push_swap
bonus:${BONUS_C}
make bonus -C ${LIBFT}
gcc -g ${CFLAGS} ${LIBFT}/libft.a
mv a.out checker
all: ${NAME}
clean:

37
Utils/indexing.c Normal file
View File

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* indexing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/05 03:23:29 by apommier #+# #+# */
/* Updated: 2022/01/05 03:23:29 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void lst_indexing(t_list *list)
{
int index;
t_list *swap;
t_list *save;
swap = list;
save = list;
index = 0;
while (save)
{
index = 1;
while (swap)
{
if (*(int *)save->nbr > *(int *)swap->nbr)
index++;
swap = swap->next;
}
save->index = index;
swap = list;
save = save->next;
}
}

124
Utils/optimise_move.c Normal file
View File

@ -0,0 +1,124 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* optimise_move.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/15 23:50:43 by apommier #+# #+# */
/* Updated: 2022/01/17 14:09:26 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include"push_swap.h"
int ft_strcmp(const char *s1, t_slist *str)
{
int i;
char *s2;
if (!str)
return (1);
s2 = (char *)str->action;
i = 0;
if (s2)
{
while (s1[i] || s2[i])
{
if (s1[i] != s2[i])
{
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
}
i++;
}
return (0);
}
return (1);
}
void cut_lst(t_slist *lst)
{
t_slist *save;
save = 0;
if (!lst)
return ;
if (lst->next)
{
save = lst->next->next;
lst->next->next = 0;
if (lst->next)
free(lst->next);
lst->next = save;
}
}
t_slist *transform_list(t_slist *start, t_slist *start2, int r1, int r2)
{
t_slist *type;
type = start;
if (r2 < r1)
{
while (r2--)
{
cut_lst(start2);
start->action = "rr";
start = start->next;
}
}
else
{
while (r1--)
{
cut_lst(start2);
start->action = "rr";
start = start->next;
}
}
return (type);
}
t_slist *sequence(t_slist *lst, t_slist *start1, t_slist *start2, int r1)
{
char *type;
int r2;
type = 0;
r2 = 0;
if (!ft_strcmp("ra", lst) || !ft_strcmp("rb", lst))
{
type = lst->action;
while (!ft_strcmp(type, lst) && r2++ >= 0)
lst = lst->next;
lst = transform_list(start1, start2, r1, r2);
}
return (lst);
}
void optimise_move(t_slist **action, t_slist *start2, int r1)
{
t_slist *start1;
t_slist *lst;
char *type;
type = 0;
lst = *action;
while (lst)
{
r1 = 0;
if (!ft_strcmp("ra", lst) || !ft_strcmp("rb", lst))
{
start1 = lst;
type = lst->action;
while (!ft_strcmp(type, lst) && r1++ >= 0)
{
start2 = lst;
lst = lst->next;
}
lst = sequence(lst, start1, start2, r1);
}
else if (lst)
lst = lst->next;
}
}

83
Utils/optimise_move2.c Normal file
View File

@ -0,0 +1,83 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* optimise_move2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/17 07:44:45 by apommier #+# #+# */
/* Updated: 2022/01/17 14:16:41 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_slist *transform_list2(t_slist *start, t_slist *start2, int r1, int r2)
{
t_slist *type;
type = start;
if (r2 < r1)
{
while (r2--)
{
cut_lst(start2);
start->action = "rrr";
start = start->next;
}
}
else
{
while (r1--)
{
cut_lst(start2);
start->action = "rrr";
start = start->next;
}
}
return (type);
}
t_slist *sequence2(t_slist *lst, t_slist *start1, t_slist *start2, int r1)
{
char *type;
int r2;
type = 0;
r2 = 0;
if (!ft_strcmp("rra", lst) || !ft_strcmp("rrb", lst))
{
type = lst->action;
while (!ft_strcmp(type, lst) && r2++ >= 0)
lst = lst->next;
lst = transform_list2(start1, start2, r1, r2);
}
return (lst);
}
void optimise_move2(t_slist **action, t_slist *start2, int r1)
{
t_slist *start1;
t_slist *lst;
char *type;
type = 0;
lst = *action;
while (lst)
{
r1 = 0;
if (!ft_strcmp("rra", lst) || !ft_strcmp("rrb", lst))
{
start1 = lst;
type = lst->action;
while (!ft_strcmp(type, lst) && r1++ >= 0)
{
start2 = lst;
lst = lst->next;
}
lst = sequence2(lst, start1, start2, r1);
}
else if (lst)
lst = lst->next;
}
}

80
Utils/process.c Normal file
View File

@ -0,0 +1,80 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* process.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/17 11:52:37 by apommier #+# #+# */
/* Updated: 2022/01/17 11:52:37 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void ft_sa_sb(t_list **list, t_slist **process, char type, int set)
{
t_list *swap;
swap = (*list)->next->next;
(*list)->next->next = *list;
*list = (*list)->next;
(*list)->next->next = swap;
if (set)
{
if (type == 'a')
s_lstadd_back(process, new_slist("sa"));
else if (type == 'b')
s_lstadd_back(process, new_slist("sb"));
}
}
void ft_ra_rb(t_list **list, t_slist **process, char type)
{
t_list *swap;
swap = *list;
(ft_lstlast(*list))->next = *list;
*list = (*list)->next;
swap->next = 0;
if (type == 'a')
s_lstadd_back(process, new_slist("ra"));
else if (type == 'b')
s_lstadd_back(process, new_slist("rb"));
}
t_list *ft_pa(t_list **list_a, t_list **list_b, t_slist **process)
{
t_list *swap;
swap = *list_b;
*list_b = (*list_b)->next;
ft_lstadd_front(list_a, swap);
s_lstadd_back(process, new_slist("pa"));
return (*list_b);
}
void ft_pb(t_list **list_a, t_list **list_b, t_slist **process)
{
t_list *swap;
swap = *list_a;
*list_a = (*list_a)->next;
ft_lstadd_front(list_b, swap);
s_lstadd_back(process, new_slist("pb"));
}
void ft_rra_rrb(t_list **list, t_slist **process, char type)
{
t_list *swap;
if(!(*list)->next)
return ;
swap = ft_lstlast(*list);
ft_lstbeforelast(*list)->next = 0;
ft_lstadd_front(list, swap);
if (type == 'a')
s_lstadd_back(process, new_slist("rra"));
else if (type == 'b')
s_lstadd_back(process, new_slist("rrb"));
}

142
Utils/push_swap.c Normal file
View File

@ -0,0 +1,142 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/25 23:10:37 by apommier #+# #+# */
/* Updated: 2022/01/18 06:08:53 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int is_nbr(int nbrarg, char **list)
{
int i;
int j;
i = 0;
j = 0;
while (--nbrarg)
{
j++;
i = 0;
while (list[j][i])
{
while (list[j][i] == ' ')
i++;
if (list[j][i] == '-' || (list[j][i] == '+'))
i++;
if (list[j][i] && (list[j][i] < '0' || list[j][i] > '9'))
return (0);
while (list[j][i] && list[j][i] >= '0' && list[j][i] <= '9')
i++;
if (list[j][i] && list[j][i] != ' ')
return (0);
}
}
return (1);
}
int is_double(int nbrarg, char **list)
{
int i;
int j;
int len;
i = 1;
j = 1;
nbrarg--;
while (nbrarg - j)
{
i = 1;
while (list[j + i])
{
if (ft_strlen(list[j]) > ft_strlen(list[j + i]))
len = ft_strlen(list[j]);
else
len = ft_strlen(list[j + i]);
if (ft_strncmp(list[j], list[j + i], len) == 0)
return (0);
i++;
}
j++;
}
return (1);
}
int push_swap(int nbrarg, char **list)
{
t_list *start;
int lst_size;
start = 0;
if (!is_nbr(nbrarg, list) || !is_double(nbrarg, list))
{
ft_putstr_fd("Error\n", 2);
return (0);
}
start = set_list(--nbrarg, list);
lst_size = ft_lstsize(start);
if (lst_size == 1)
return (1);
lst_indexing(start);
if (lst_size == 3 || lst_size == 5)
{
sort_little(start);
return (1);
}
start = find_loop_index(start);
ft_lstclear(&start, &free);
return (1);
}
t_list *set_list(int nbrarg, char **srcs)
{
int *tmp;
int i;
int j;
t_list *swap;
j = 1;
tmp = 0;
i = 0;
swap = 0;
while (nbrarg--)
{
i = 0;
while (srcs[j][i])
{
tmp = ft_calloc(sizeof(int), 1);
*tmp = ft_atoi(&srcs[j][i]);
ft_lstadd_back(&swap, ft_lstnew(tmp));
while (srcs[j][i] == ' ')
i++;
while (srcs[j][i] && srcs[j][i] != ' ')
i++;
}
j++;
}
return (swap);
}
t_list *best_b(t_list **b, t_list *start_b, t_list *a, int best_move)
{
int move;
t_list *best_b;
move = 0;
while (*b)
{
move = find_nbr(ft_lstsize(start_b), a, start_b, (*b)->index);
if (move < best_move)
{
best_move = move;
best_b = *b;
}
*b = (*b)->next;
}
return (best_b);
}

136
Utils/set_a.c Normal file
View File

@ -0,0 +1,136 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* set_a.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/07 19:14:03 by apommier #+# #+# */
/* Updated: 2022/01/17 15:07:09 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_list *find_loop_index(t_list *list)
{
t_list *start;
t_list *save;
int loop_size;
int loop_size2;
loop_size2 = 0;
loop_size = 0;
start = list;
while (list)
{
loop_size = find_loop(start, list, 0);
if (loop_size > loop_size2)
{
loop_size2 = loop_size;
save = list;
}
list = list->next;
}
find_loop(start, save, 1);
list = swap_to_b(start, save, 0, loop_size2);
return (list);
}
int find_loop(t_list *start, t_list *list, int setswap)
{
int countdown;
int loop_size;
t_list *swap;
loop_size = 0;
swap = list->next;
countdown = ft_lstsize(start) - 1;
while (countdown)
{
if (swap)
{
if (swap->index > list->index)
{
list = swap;
loop_size++;
}
else if (setswap)
swap->swap = 1;
countdown--;
swap = swap->next;
}
else
swap = start;
}
return (loop_size);
}
t_list *swap_to_b(t_list *list, t_list *best, t_list *b, int loop)
{
int lst_size;
t_slist *action;
action = 0;
lst_size = ft_lstsize(list);
while (lst_size)
{
list = is_swap(list, &action, &loop, best);
if (list->swap)
ft_pb(&list, &b, &action);
else
ft_ra_rb(&list, &action, 'a');
lst_size--;
}
list = swap_to_a(list, b, b, action);
list = turn_list(list, action);
optimise_move(&action, 0, 0);
optimise_move2(&action, 0, 0);
printf_slist(action);
s_lstclear(&action);
return (list);
}
t_list *turn_list(t_list *a, t_slist *action)
{
t_list *start;
int place;
start = a;
place = 0;
while (a->index > 1)
{
a = a->next;
place++;
}
a = start;
if (place > ft_lstsize(a) / 2 && place > 2)
{
while (ft_lstsize(start) - (place++))
ft_rra_rrb(&start, &action, 'a');
}
else
{
while (place--)
ft_ra_rb(&start, &action, 'a');
}
return (start);
}
t_list *is_swap(t_list *list, t_slist **action, int *loop, t_list *best)
{
int swap;
swap = 0;
ft_sa_sb(&list, action, 'a', 0);
swap = find_loop(list, best, 0);
if (swap > *loop + 1)
{
*loop = swap;
s_lstadd_back(action, new_slist("sa"));
find_loop(list, best, 1);
}
else
ft_sa_sb(&list, action, 'a', 0);
return (list);
}

114
Utils/sort_little.c Normal file
View File

@ -0,0 +1,114 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_little.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/18 03:31:31 by apommier #+# #+# */
/* Updated: 2022/01/18 06:46:07 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
//t_list *end_sort3(t_list *list, t_slist **action);
//{
//}
void printf_llist(t_list *start)
{
int i = 0;
printf("----printf_list----\n");
while(start)
{
i++;
printf("lst%d --- nbr: %d index: %d\n", i,*(int*)start->nbr, start->index);
start = start->next;
}
printf("----end_print----");
return;
}
void sort_little(t_list *list)
{
t_slist *action;
action = 0;
if (is_sorted(list))
{
ft_lstclear(&list, &free);
return ;
}
if (ft_lstsize(list) == 3)
list = sort3(list, &action);
else
list = sort5(list, &action, 0);
list = turn_list(list, action);
printf_slist(action);
s_lstclear(&action);
}
t_list *sort3(t_list *a, t_slist **action)
{
if (is_sorted(a))
return (a);
if (a->index < a->next->index && a->index < a->next->next->index)
{
ft_sa_sb(&a, action, 'a', 1);
ft_ra_rb(&a, action, 'a');
}
else if (a->index > a->next->index && a->index > a->next->next->index)
{
if (a->next->index > a->next->next->index)
{
ft_sa_sb(&a, action, 'a', 1);
ft_rra_rrb(&a, action, 'a');
}
else
ft_ra_rb(&a, action, 'a');
}
else if (a->index > a->next->index && a->index < a->next->next->index)
ft_sa_sb(&a, action, 'a', 1);
else if (a->index < a->next->index && a->next->next->index)
{
ft_rra_rrb(&a, action, 'a');
}
return (a);
}
t_list *sort5(t_list *list, t_slist **action, t_list *b)
{
ft_pb(&list, &b, action);
ft_pb(&list, &b, action);
list = sort3(list, action);
//printf_llist(list);
if (b->index < b->next->index)
ft_sa_sb(&b, action, 'b', 1);
if (b->index == 5)
ft_pa(&list, &b, action);
while (b)
{
list = swap(list, &b, action, b);
}
//printf_llist(list);
return (list);
}
int is_sorted(t_list *list)
{
while (list->next)
{
if (list->index > list->next->index)
return (0);
else
list = list->next;
}
return (1);
}

128
Utils/sorting.c Normal file
View File

@ -0,0 +1,128 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sorting.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/07 19:14:03 by apommier #+# #+# */
/* Updated: 2022/01/07 19:14:03 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_list *swap(t_list *a, t_list **b, t_slist **action, t_list *to_swap)
{
int place_for_b;
int place;
t_list *start_b;
start_b = *b;
place = 0;
while ((*b)->index != to_swap->index)
{
place++;
*b = (*b)->next;
}
start_b = make_list(place, start_b, *action, 'b');
place_for_b = find_place_for_b(a, to_swap->index, ft_lstsize(start_b));
a = make_list(place_for_b, a, *action, 'a');
*b = ft_pa(&a, &start_b, action);
return (a);
}
t_list *make_list(int place, t_list *list, t_slist *action, char type)
{
int lstsize;
lstsize = ft_lstsize(list);
if (place > lstsize / 2)
{
while (lstsize - place)
{
ft_rra_rrb(&list, &action, type);
place++;
}
}
else
{
while (place)
{
ft_ra_rb(&list, &action, type);
place--;
}
}
return (list);
}
t_list *swap_to_a(t_list *a, t_list *b, t_list *start_b, t_slist *action)
{
int lst_size;
int size_b;
int move;
int best_move;
t_list *to_swap;
to_swap = 0;
size_b = ft_lstsize(b);
move = size_b + ft_lstsize(a);
best_move = move;
lst_size = size_b;
while (lst_size)
{
best_move = ft_lstsize(a) + ft_lstsize(b);
b = start_b;
to_swap = best_b(&b, start_b, a, best_move);
a = swap(a, &start_b, &action, to_swap);
lst_size--;
}
return (a);
}
int find_place_for_b(t_list *list, int index, int size_b)
{
int count;
int save_index;
t_list *start;
start = list;
save_index = ft_lstsize(list) + size_b;
count = 0;
while (list)
{
if (list->index > index && list->index < save_index)
save_index = list->index;
list = list->next;
}
while (start->index != save_index)
{
count++;
start = start->next;
}
return (count);
}
int find_nbr(int size_b, t_list *a, t_list *b, int index_b)
{
int move;
int place;
int size_a;
size_a = ft_lstsize(a);
place = 1;
move = 0;
while (b->index != index_b)
{
place++;
b = b->next;
}
move = find_place_for_b(a, b->index, size_b);
if (move > size_a / 2)
move = size_a - move;
if (place > size_b / 2)
move += size_b - place;
else
move += place;
return (move);
}

70
Utils/t_slist.c Normal file
View File

@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* t_slist.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/09 18:46:50 by apommier #+# #+# */
/* Updated: 2022/01/09 18:46:50 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_slist *new_slist(void *content)
{
t_slist *new;
new = (t_slist *)malloc(sizeof(t_slist));
if (!new)
return (0);
new->action = content;
new->next = 0;
return (new);
}
void s_lstclear(t_slist **lst)
{
t_slist *chr;
chr = *lst;
while (*lst)
{
chr = (*lst)->next;
free(*lst);
*lst = chr;
}
lst = 0;
}
void s_lstadd_back(t_slist **alst, t_slist *new)
{
if (*alst == 0)
*alst = new;
else
s_lstlast(*alst)->next = new;
new->next = 0;
}
t_slist *s_lstlast(t_slist *lst)
{
if (!lst)
return (0);
while (lst->next)
lst = lst->next;
return (lst);
}
void printf_slist(t_slist *start)
{
int i;
i = 0;
while (start)
{
i++;
ft_putendl_fd((char *)start->action, 1);
start = start->next;
}
}

View File

@ -31,7 +31,9 @@ SRCS = ft_memset.c \
ft_putchar_fd.c \
ft_putstr_fd.c \
ft_putendl_fd.c \
ft_putnbr_fd.c
ft_putnbr_fd.c \
get_next_line.c \
get_next_line_utils.c
OBJS = ${SRCS:.c=.o}
BONUS_C = ft_lstnew.c ft_lstadd_front.c ft_lstsize.c ft_lstlast.c ft_lstadd_back.c \
ft_lstdelone.c ft_lstclear.c ft_lstiter.c ft_lstmap.c ft_lstbeforelast.c

View File

@ -6,17 +6,17 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 00:09:17 by apommier #+# #+# */
/* Updated: 2022/01/17 11:24:37 by apommier ### ########.fr */
/* Updated: 2022/01/18 06:50:22 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *nptr)
long ft_atoi(const char *nptr)
{
int i;
int nbr;
int minus;
long nbr;
long minus;
minus = 1;
nbr = 0;

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 00:09:57 by apommier #+# #+# */
/* Updated: 2022/01/17 11:27:24 by apommier ### ########.fr */
/* Updated: 2022/01/17 21:18:04 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,8 +19,6 @@ void *ft_calloc(size_t nmemb, size_t size)
i = 0;
new = malloc(size * nmemb);
if (!new)
return (0);
if (new)
{
while (size * nmemb - i)

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/12 13:57:59 by apommier #+# #+# */
/* Updated: 2022/01/17 11:34:28 by apommier ### ########.fr */
/* Updated: 2022/01/17 21:18:59 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,9 +14,11 @@
char *ft_strchr(const char *s, int c)
{
unsigned char *str;
char *str;
str = (unsigned char *)s;
if (!s)
return ("no s");
str = (char *)s;
while ((*str != c) && (*str != 0))
str++;
if (*str == c)

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 00:13:19 by apommier #+# #+# */
/* Updated: 2022/01/17 11:38:56 by apommier ### ########.fr */
/* Updated: 2022/01/17 21:12:05 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,6 +17,8 @@ size_t ft_strlen(const char *s)
size_t i;
i = 0;
if (!s)
return (0);
while (s[i] != 0)
i++;
return (i);

100
libft/get_next_line.c Normal file
View File

@ -0,0 +1,100 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kinou <alexpomms@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/23 07:55:17 by kinou #+# #+# */
/* Updated: 2021/11/01 10:21:35 by kinou ### ########.fr */
/* */
/* ************************************************************************** */
#include "../Utils/push_swap.h"
char *ft_free(char *save, int *end)
{
if (!*end)
{
free(save);
free(end);
return (0);
}
free(end);
return (save);
}
char *set_line(char *line, char *save)
{
int i;
i = 0;
line = ft_strjoin(save, 0);
while (line[i] && line[i] != '\n')
i++;
if (line[i] == '\n')
{
while (line[i++])
line[i] = '\0';
}
return (line);
}
char *set_save(char *save)
{
char *delete;
int i;
i = 0;
delete = save;
while (save[i] && save[i] != '\n')
i++;
if (save[i] != '\n')
i = 0;
save = ft_strjoin((save + i + 1), 0);
free(delete);
return (save);
}
char *next_line(char *save, int *end, int fd)
{
char *delete;
char *dest;
while (!ft_strchr(save, '\n') && *end > 0)
{
dest = ft_calloc(1, 1 + 1);
*end = read(fd, dest, 1);
delete = save;
save = ft_strjoin(save, dest);
free(delete);
free(dest);
}
return (save);
}
char *get_next_line(int fd)
{
static char *save = NULL;
int *end;
char *line;
line = 0;
if (fd < 0)
return (0);
end = malloc(sizeof(int *));
*end = 1;
if (save == NULL)
save = ft_calloc(1, 1);
save = next_line(save, end, fd);
line = set_line(line, save);
if (ft_strlen(line) > 0)
{
save = set_save(save);
save = ft_free(save, end);
return (line);
}
free(line);
save = ft_free(save, end);
return (0);
}

View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/14 06:05:54 by apommier #+# #+# */
/* Updated: 2022/01/17 21:42:00 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../Utils/push_swap.h"
char *ft_strjoin(char *save, char *s2)
{
char *dest;
int i;
int j;
i = 0;
j = 0;
if (!save && !s2)
return (0);
dest = malloc(ft_strlen(save) + ft_strlen(s2) + 1);
while (save && save[i])
{
dest[j] = save[i];
j++;
i++;
}
i = 0;
while (s2 && s2[i])
{
dest[j] = s2[i];
j++;
i++;
}
dest[j] = 0;
return (dest);
}

View File

@ -6,15 +6,16 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/11 03:21:27 by apommier #+# #+# */
/* Updated: 2022/01/17 15:47:21 by apommier ### ########.fr */
/* Updated: 2022/01/18 01:32:40 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <unistd.h>
# include <stdlib.h>
# include <unistd.h>
# include "get_next_line.h"
typedef struct t_slist
{
@ -45,7 +46,7 @@ int ft_strncmp(const char *s1, const char *s2, size_t n);
size_t ft_strlcpy(char *dst, const char *src, size_t size);
size_t ft_strlcat(char *dst, const char *src, size_t size);
char *ft_strnstr(const char *big, const char *little, size_t len);
int ft_atoi(const char *nptr);
long ft_atoi(const char *nptr);
void *ft_calloc(size_t nmenb, size_t size);
char *ft_strdup(const char *s);

7
main.c
View File

@ -6,19 +6,16 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/25 23:15:43 by apommier #+# #+# */
/* Updated: 2022/01/17 11:46:44 by apommier ### ########.fr */
/* Updated: 2022/01/17 21:27:14 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include "Utils/push_swap.h"
int main(int argc, char *argv[])
{
if (argc == 1)
{
ft_putstr_fd("Error\n", 2);
return (1);
}
push_swap(argc, argv);
return (1);
}