aboutsummaryrefslogtreecommitdiff
path: root/src/checker
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-09-10 10:54:12 +0200
committerCharles Cabergs <me@cacharle.xyz>2021-09-10 10:54:12 +0200
commit7a6533373ab9b0dd075f995c98dcf332a7876fac (patch)
treea9fc5df9ab01c11a24e780dab47039504f122ef3 /src/checker
parentc0124777afcbbb0d5ca7d841360434f25235fb0f (diff)
downloadpush_swap-7a6533373ab9b0dd075f995c98dcf332a7876fac.tar.gz
push_swap-7a6533373ab9b0dd075f995c98dcf332a7876fac.tar.bz2
push_swap-7a6533373ab9b0dd075f995c98dcf332a7876fac.zip
Updated to norm v3
Diffstat (limited to 'src/checker')
-rw-r--r--src/checker/check.c37
-rw-r--r--src/checker/checker.h8
-rw-r--r--src/checker/main.c13
3 files changed, 33 insertions, 25 deletions
diff --git a/src/checker/check.c b/src/checker/check.c
index bbe509f..1459dc6 100644
--- a/src/checker/check.c
+++ b/src/checker/check.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/18 10:16:08 by cacharle #+# #+# */
-/* Updated: 2020/01/19 09:02:08 by cacharle ### ########.fr */
+/* Updated: 2021/09/10 10:26:47 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,11 +16,17 @@ t_status check(t_stack *a, t_stack *b)
{
t_status read_status;
- while ((read_status = read_action(a, b)) != STATUS_EOF)
+ read_status = read_action(a, b);
+ while (read_status != STATUS_EOF)
+ {
+ read_status = read_action(a, b);
if (read_status == STATUS_ERROR)
return (STATUS_ERROR);
- return (stack_sorted(a) && stack_empty(b) ?
- STATUS_SUCCESS : STATUS_FAILURE);
+ }
+ if (stack_sorted(a) && stack_empty(b))
+ return (STATUS_SUCCESS);
+ else
+ return (STATUS_FAILURE);
}
t_status read_action(t_stack *a, t_stack *b)
@@ -41,10 +47,13 @@ t_status read_action(t_stack *a, t_stack *b)
buf[i++] = c;
buf[i] = '\0';
}
- return (ret == 0 ? STATUS_EOF : STATUS_ERROR);
+ if (ret == 0)
+ return (STATUS_EOF);
+ else
+ return (STATUS_ERROR);
}
-static t_action g_actions[] = {
+static const t_action g_actions[] = {
{"sa", FLAG_ARG_A, {.arg_1 = &stack_swap}},
{"sb", FLAG_ARG_B, {.arg_1 = &stack_swap}},
{"ss", FLAG_ARG_A_B, {.arg_2 = &stack_swap_2}},
@@ -58,7 +67,7 @@ static t_action g_actions[] = {
{"rrr", FLAG_ARG_A_B, {.arg_2 = &stack_reverse_rotate_2}},
};
-#define ACTIONS_SIZE (sizeof(g_actions) / sizeof(t_action))
+static const int g_actions_size = sizeof(g_actions) / sizeof(t_action);
t_status exec_action(char action_id[ACTION_ID_BUF_SIZE],
t_stack *a, t_stack *b)
@@ -68,24 +77,24 @@ t_status exec_action(char action_id[ACTION_ID_BUF_SIZE],
i = 0;
while (ft_strcmp(action_id, g_actions[i].id) != 0)
i++;
- if (i == ACTIONS_SIZE)
+ if (i == g_actions_size)
return (STATUS_ERROR);
if (g_actions[i].args == FLAG_ARG_A)
- g_actions[i].func.arg_1(a);
+ g_actions[i].u_func.arg_1(a);
else if (g_actions[i].args == FLAG_ARG_B)
- g_actions[i].func.arg_1(b);
+ g_actions[i].u_func.arg_1(b);
else if (g_actions[i].args == FLAG_ARG_A_B)
- g_actions[i].func.arg_2(a, b);
+ g_actions[i].u_func.arg_2(a, b);
else if (g_actions[i].args == FLAG_ARG_B_A)
- g_actions[i].func.arg_2(b, a);
+ g_actions[i].u_func.arg_2(b, a);
else
return (STATUS_ERROR);
return (STATUS_SUCCESS);
}
-t_bool stack_sorted(t_stack *stack)
+t_bool stack_sorted(t_stack *stack)
{
- int i;
+ int i;
if (stack_length(stack) < 2)
return (TRUE);
diff --git a/src/checker/checker.h b/src/checker/checker.h
index f6d50ef..d049e21 100644
--- a/src/checker/checker.h
+++ b/src/checker/checker.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/18 10:16:12 by cacharle #+# #+# */
-/* Updated: 2020/01/22 10:42:16 by cacharle ### ########.fr */
+/* Updated: 2021/09/10 10:26:37 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,7 +19,7 @@
# define ACTION_ID_BUF_SIZE 4
-typedef enum e_flag_arg
+typedef enum e_flag_arg
{
FLAG_ARG_A,
FLAG_ARG_B,
@@ -30,7 +30,7 @@ typedef enum e_flag_arg
typedef void (*t_func_arg_1)(t_stack *);
typedef void (*t_func_arg_2)(t_stack *, t_stack *);
-typedef struct s_action
+typedef struct s_action
{
const char *id;
t_flag_arg args;
@@ -38,7 +38,7 @@ typedef struct s_action
{
t_func_arg_1 arg_1;
t_func_arg_2 arg_2;
- } func;
+ } u_func;
} t_action;
t_status check(t_stack *a, t_stack *b);
diff --git a/src/checker/main.c b/src/checker/main.c
index a0a08b2..ca38105 100644
--- a/src/checker/main.c
+++ b/src/checker/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/18 10:16:14 by cacharle #+# #+# */
-/* Updated: 2020/01/19 09:08:41 by cacharle ### ########.fr */
+/* Updated: 2021/09/10 10:33:05 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -20,15 +20,14 @@ int main(int argc, char **argv)
if (argc == 1)
return (0);
- if ((a = stack_new(argc - 1)) == NULL)
+ a = stack_new(argc - 1);
+ if (a == NULL)
return (1);
if (parse(argc, argv, a) != STATUS_SUCCESS)
return (1);
- if ((b = stack_new(stack_length(a))) == NULL)
- {
- stack_destroy(a);
- return (1);
- }
+ b = stack_new(stack_length(a));
+ if (b == NULL)
+ return (stack_destroy(a));
s = check(a, b);
if (s == STATUS_SUCCESS)
ft_putendl("OK");