98 lines
2.7 kB
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* error.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: mleitao <marvin@42.fr> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2026/07/01 04:04:14 by mleitao #+# #+# */
9/* Updated: 2026/07/02 23:51:24 by mleitao ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "error.h"
14#include <stdio.h>
15
16static const char *arg_name(int i)
17{
18 static const char *names[] = {
19 "coders",
20 "burnout",
21 "compile",
22 "debug",
23 "refactor",
24 "compile times",
25 "cooldown",
26 "scheduler"
27 };
28
29 if (i < 0 || i > 7)
30 return ("unknown");
31 return (names[i]);
32}
33
34static const char *reason_str(enum e_err code)
35{
36 if (code == ERR_NOT_DIGIT)
37 return ("is not a valid number");
38 if (code == ERR_OVERFLOW)
39 return ("is out of range");
40 if (code == ERR_ZERO)
41 return ("must be greater than 0");
42 if (code == ERR_SCHED)
43 return ("must be 'fifo' or 'edf'");
44 if (code == ERR_INIT)
45 return ("initialization failed");
46 return ("invalid");
47}
48
49static void print_usage(void)
50{
51 int i;
52
53 i = 0;
54 fprintf(stderr, "\nusage: ./codexion ");
55 while (i < 8)
56 {
57 if (i == 7)
58 {
59 fprintf(stderr, "<%s>\n\n", arg_name(i));
60 break ;
61 }
62 fprintf(stderr, "<%s> ", arg_name(i));
63 i++;
64 }
65 fprintf(stderr,
66 " coders\t| number of coders, >=1\n"
67 " burnout\t| ms until a coder burns out, >=0\n"
68 " compile\t| ms spent compiling, >=0\n"
69 " debug\t\t| ms spent debugging, >=0\n"
70 " refactor\t| ms spent refactoring, >=0\n"
71 " compile times\t|"
72 " compiles per coder before simulation ends, >=0\n"
73 " cooldown\t| ms for a dongle to become available, >=0\n"
74 " scheduler\t| type of scheduler, 'fifo' or 'edf'\n\n");
75}
76
77void report_error(t_err e)
78{
79 if (e.code == OK)
80 return ;
81 if (e.code == ERR_USAGE)
82 print_usage();
83 else if (e.index >= 0)
84 fprintf(stderr, "error: %s %s, got '%s'\n",
85 arg_name(e.index), reason_str(e.code), e.value);
86 else
87 fprintf(stderr, "error: %s\n", reason_str(e.code));
88}
89
90t_err err(enum e_err code, const char *value, int index)
91{
92 t_err e;
93
94 e.code = code;
95 e.value = value;
96 e.index = index;
97 return (e);
98}