Refactor dir structure and prog argument parsing
authorLukas Jiriste <ljiriste@student.42prague.com>
Tue, 6 Feb 2024 12:23:16 +0000 (13:23 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Tue, 6 Feb 2024 12:23:16 +0000 (13:23 +0100)
Makefile
src/actions.c [moved from src_checker/actions.c with 100% similarity]
src/arg_parsing.c [new file with mode: 0644]
src/checker.c [moved from src_checker/checker.c with 73% similarity]
src/checker.h [moved from src_checker/checker.h with 78% similarity]
src/push.c [moved from src_push/push.c with 100% similarity]
src/stack_manipulation.c [moved from src_checker/stack_manipulation.c with 100% similarity]

index b5dfb58eecb23f03dd429506563df86c824440aa..d39cbd0ca30628bed40a5772071d43940463034a 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -6,22 +6,23 @@ RM := rm -f
 
 SUBPROJECTS := Libft
 
-INCDIR := src_checker
+INCDIR := src
 SUBINCDIR := $(addsuffix /inc, $(SUBPROJECTS));
 INCLUDE := $(addprefix -I, $(INCDIR) $(SUBINCDIR))
 
-CHECKSRCDIR := src_checker
+CHECKSRCDIR := src
+
+PUSHSRCDIR = src
 
 CHECKSOURCES :=        checker.c                               \
                                actions.c                               \
                                stack_manipulation.c    \
+                               arg_parsing.c                   \
 
 CHECKSOURCES := $(addprefix $(CHECKSRCDIR)/, $(CHECKSOURCES))
 
 CHECKOBJECTS := $(CHECKSOURCES:.c=.o)
 
-PUSHSRCDIR = src_push
-
 PUSHSOURCES := push.c                                  \
 
 PUSHSOURCES := $(addprefix $(PUSHSRCDIR)/, $(PUSHSOURCES))
similarity index 100%
rename from src_checker/actions.c
rename to src/actions.c
diff --git a/src/arg_parsing.c b/src/arg_parsing.c
new file mode 100644 (file)
index 0000000..6d11fae
--- /dev/null
@@ -0,0 +1,71 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   arg_parsing.c                                      :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/01/31 09:58:54 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/02/06 12:34:29 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "checker.h"
+#include <stdlib.h>
+
+static int     is_int(const char *str)
+{
+       char    *test;
+
+       test = ft_itoa(ft_atoi(str));
+       if (ft_strcmp(str, test))
+       {
+               free(test);
+               return (0);
+       }
+       free(test);
+       return (1);
+}
+
+static int     contains(int new, t_stack *s)
+{
+       const size_t    start_ind = s->ind;
+       int                             cur;
+
+       if (s->stack.size == 0)
+               return (0);
+       cur = stack_top(s);
+       if (cur == new)
+               return (1);
+       stack_rotate(s, 1);
+       while (s->ind != start_ind)
+       {
+               cur = stack_top(s);
+               if (cur == new)
+                       return (1);
+               stack_rotate(s, 1);
+       }
+       return (0);
+}
+
+int    parse(int argc, char **argv, t_stack *s)
+{
+       size_t  i;
+       int             new;
+
+       if (argc == 1)
+               return (1);
+       i = 1;
+       while (i < (size_t)argc)
+       {
+               if (!is_int(argv[i]))
+                       return (1);
+               new = ft_atoi(argv[i]);
+               if (contains(new, s))
+                       return (1);
+               stack_push(s, new);
+               stack_rotate(s, 1);
+               ++i;
+       }
+       return (0);
+}
similarity index 73%
rename from src_checker/checker.c
rename to src/checker.c
index a2c6994ca34da3fc5db2a911647f873e829ad16d..f54db04f249e6597f68b688efb2e3bc57015ddb0 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/01/24 10:31:06 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/01/24 14:41:00 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/02/06 13:19:15 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 
 #define COMMANDS_NUM 11
 
-static int     command_parse(const char *input, t_command *command)
+static int     command_exists(const char *input)
 {
-       const size_t    len = ft_strlen(input);
-       const char              *com_str[COMMANDS_NUM] = {"sa", "sb", "ss", "pa", "pb", "ra", "rb", "rr", "rra", "rrb", "rrr"};
        size_t                  i;
+       const char              *com_str[COMMANDS_NUM]
+               = {"sa", "sb", "ss", "pa", "pb", "ra", "rb", "rr", "rra", "rrb", "rrr"};
 
        i = 0;
-       while (1)
+       while (i < COMMANDS_NUM)
        {
                if (!ft_strcmp(input, com_str[i++]))
                        break ;
                if (i == COMMANDS_NUM)
-                       return (1);
+                       return (0);
        }
+       return (1);
+}
+
+static int     command_parse(const char *input, t_command *command)
+{
+       const size_t    len = ft_strlen(input);
+
+       if (!command_exists(input))
+               return (1);
        if (len == 3)
                command->action = action_reverse_rotate;
        else if (input[0] == 's')
@@ -90,9 +99,9 @@ void  free_stack(t_stack *s)
 
 int    is_sorted(t_stack *s)
 {
-       const size_t start_ind = s->ind;
-       int             prev;
-       int             cur;
+       const size_t    start_ind = s->ind;
+       int                             prev;
+       int                             cur;
 
        prev = stack_top(s);
        stack_rotate(s, 1);
@@ -107,63 +116,6 @@ int        is_sorted(t_stack *s)
        return (1);
 }
 
-int    is_int(const char *str)
-{
-       char    *test;
-
-       test = ft_itoa(ft_atoi(str));
-       if (ft_strcmp(str, test))
-       {
-               free(test);
-               return (0);
-       }
-       free(test);
-       return (1);
-}
-
-int    contains(int new, t_stack *s)
-{
-       const size_t start_ind = s->ind;
-       int             cur;
-
-       if (s->stack.size == 0)
-               return (0);
-       cur = stack_top(s);
-       if (cur == new)
-               return (1);
-       stack_rotate(s, 1);
-       while (s->ind != start_ind)
-       {
-               cur = stack_top(s);
-               if (cur == new)
-                       return (1);
-               stack_rotate(s, 1);
-       }
-       return (0);
-}
-
-int    parse(int argc, char **argv, t_stack *s)
-{
-       size_t  i;
-       int             new;
-
-       if (argc == 1)
-               return (1);
-       i = 1;
-       while (i < (size_t)argc)
-       {
-               if (!is_int(argv[i]))
-                       return (1);
-               new = ft_atoi(argv[i]);
-               if (contains(new, s))
-                       return (1);
-               stack_push(s, new);
-               stack_rotate(s, 1);
-               ++i;
-       }
-       return (0);
-}
-
 void   clean_up(t_stacks *s)
 {
        free_stack(&s->a);
similarity index 78%
rename from src_checker/checker.h
rename to src/checker.h
index ab16b7e6f66b424e84ea7f1506758bc8f95be71d..5776dae0cff5a16572e026d8d5967f5fdb4ccbfd 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/01/24 12:13:51 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/01/24 12:21:55 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/01/31 09:59:33 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -34,18 +34,25 @@ typedef enum e_target
        both,
 }      t_target;
 
-typedef void(t_action(t_stacks *s, t_target target));
+typedef void   (t_action(t_stacks *s, t_target target));
 
 typedef struct s_command
 {
-       t_action        *action;
+       t_action        *action;
        t_target        target;
 }                              t_command;
 
+/*
 t_action       action_push;
 t_action       action_swap;
 t_action       action_rotate;
 t_action       action_reverse_rotate;
+*/
+
+void   action_push(t_stacks *s, t_target target);
+void   action_swap(t_stacks *s, t_target target);
+void   action_rotate(t_stacks *s, t_target target);
+void   action_reverse_rotate(t_stacks *s, t_target target);
 
 void   stack_push(t_stack *s, int el);
 void   stack_swap(t_stack *s);
@@ -53,4 +60,5 @@ void  stack_rotate(t_stack *s, int amount);
 void   stack_pop(t_stack *s);
 int            stack_top(t_stack *s);
 
+int    parse(int argc, char **argv, t_stack *s);
 #endif //CHECKER_H
similarity index 100%
rename from src_push/push.c
rename to src/push.c