-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.c
More file actions
77 lines (70 loc) · 2.13 KB
/
context.c
File metadata and controls
77 lines (70 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* context.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: miissa <miissa@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/02 08:06:22 by miissa #+# #+# */
/* Updated: 2026/01/08 10:36:41 by miissa ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int is_flag(const char *arr)
{
return (arr && arr[0] == '-' && arr[1] == '-');
}
void ps_ctx_init(t_ctx *ctx, int print_ops)
{
if (ctx)
{
ctx->print_ops = print_ops;
ctx->bench = 0;
ctx->disorder = 0.0;
ctx->selected = START_ADAPTIVE;
ctx->used = START_ADAPTIVE;
ps_stats_init(&ctx->stats);
}
}
static int set_strategy(t_ctx *ctx, int *set, t_strategy strategy)
{
if (*set)
return (0);
ctx->selected = strategy;
*set = 1;
return (1);
}
static int handle_flag(const char *arr, t_ctx *ctx, int *set)
{
if (ft_strncmp(arr, "--bench", 8) == 0)
{
ctx->bench = 1;
return (1);
}
if (ft_strncmp(arr, "--simple", 9) == 0)
return (set_strategy(ctx, set, START_SIMPLE));
if (ft_strncmp(arr, "--medium", 9) == 0)
return (set_strategy(ctx, set, START_MEDIUM));
if (ft_strncmp(arr, "--complex", 10) == 0)
return (set_strategy(ctx, set, START_COMPLEX));
if (ft_strncmp(arr, "--adaptive", 11) == 0)
return (set_strategy(ctx, set, START_ADAPTIVE));
return (0);
}
int ps_parse_flags(int argc, char **argv, t_ctx *ctx, int *out_start)
{
int i;
int set;
int r;
i = 1;
set = 0;
while (i < argc && is_flag(argv[i]))
{
r = handle_flag(argv[i], ctx, &set);
if (r <= 0)
return (0);
i++;
}
*out_start = i;
return (1);
}