summaryrefslogtreecommitdiff
path: root/input_handling.c
diff options
context:
space:
mode:
authorDominik Kaiser2024-04-12 18:51:50 +0200
committerDominik Kaiser2024-04-12 18:51:50 +0200
commit9c64c9d253fa22b29005ec53dca6fedcead2ae6c (patch)
treed7e01093e0a5a4ef9b4ef2bc674ec86e3b97e452 /input_handling.c
parent1f1d95faadca635adcb8e46bb0aecc0574654a24 (diff)
downloadpush_swap-9c64c9d253fa22b29005ec53dca6fedcead2ae6c.tar.gz
push_swap-9c64c9d253fa22b29005ec53dca6fedcead2ae6c.zip
Add libft and input handling
Diffstat (limited to 'input_handling.c')
-rw-r--r--input_handling.c96
1 files changed, 96 insertions, 0 deletions
diff --git a/input_handling.c b/input_handling.c
new file mode 100644
index 0000000..93e37f6
--- /dev/null
+++ b/input_handling.c
@@ -0,0 +1,96 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* input_handling.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/04/12 17:31:49 by dkaiser #+# #+# */
+/* Updated: 2024/04/12 18:49:38 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "push_swap.h"
+
+static int is_nbr(char *str)
+{
+ if (*str == '-')
+ str++;
+ while (*str)
+ {
+ if (ft_isdigit(*str))
+ str++;
+ else
+ return (0);
+ }
+ return (1);
+}
+
+static int is_input_only_nbrs(int argc, char *argv[])
+{
+ while (argc-- > 1)
+ {
+ if (!is_nbr(argv[argc]))
+ return (0);
+ }
+ return (1);
+}
+
+static int are_numbers_unique(t_list *stack)
+{
+ t_list *cmp_elem;
+
+ while (stack->next)
+ {
+ cmp_elem = stack->next;
+ while (cmp_elem->next)
+ {
+ if (*(int *)stack->content == *(int *)cmp_elem->content)
+ return (0);
+ cmp_elem = cmp_elem->next;
+ }
+ stack = stack->next;
+ }
+ return (1);
+}
+
+static t_list *get_stack_from_input(int argc, char *argv[])
+{
+ t_list *result;
+ t_list *cur;
+ int *content;
+
+ result = NULL;
+ while (argc-- > 1)
+ {
+ content = malloc(sizeof(int));
+ if (content)
+ {
+ *content = ft_atoi(argv[argc]);
+ cur = ft_lstnew(content);
+ if (cur)
+ {
+ ft_lstadd_front(&result, cur);
+ continue ;
+ }
+ free(content);
+ }
+ ft_lstclear(&result, free);
+ return (NULL);
+ }
+ return (result);
+}
+
+t_list *create_stack(int argc, char *argv[])
+{
+ t_list *result;
+
+ if (!is_input_only_nbrs(argc, argv))
+ return (NULL);
+ result = get_stack_from_input(argc, argv);
+ if (!result)
+ return (NULL);
+ if (!are_numbers_unique(result))
+ ft_lstclear(&result, free);
+ return (result);
+}