-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflags.c
More file actions
181 lines (127 loc) · 3.59 KB
/
flags.c
File metadata and controls
181 lines (127 loc) · 3.59 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "flags.h"
char *flag_arr[FLAG_COUNT] = {"-n", "-d", "-c", "-r", "--help"};
int init_flag_holder(struct flag_holder *flag_holder, int *pipe_count_var, int *microseconds_delay_var, int *color_var, int *rand_seed_var){
flag_holder->pipe_count_var = pipe_count_var; // NOOOOOO I HAVE TO DO OOP NOOOOOOOOOOOOO
flag_holder->microseconds_delay_var = microseconds_delay_var;
flag_holder->color_var = color_var;
flag_holder->rand_seed_var = rand_seed_var;
return 0;
}
void print_help(void){
printf("\
Usage: pipes [OPTION ARGUMENT]...\n\
Draw a pipe screensaver to the terminal\n\
\n\
-n INT draw INT pipes to the screen\n\
-d INT delay INT microseconds between each new frame\n\
-c INT draw all pipes using INT as the ansi escape color code\n\
-r INT use INT as the random seed\n\
\n\
--help print this text and exit\n\
\n");
}
int handle_flag_n(char *pipe_count, int *pipe_count_var){
if (pipe_count == NULL){
printf("null argument passed to -n\n");
return -1;
}
if (atoi(pipe_count) == 0){
printf("invalid arg %s passed to flag -n\n", pipe_count);
return -1;
} else {
*pipe_count_var = atoi(pipe_count);
return 0;
}
}
int handle_flag_d(char *pipe_delay, int *microseconds_delay_var){
if (pipe_delay == NULL){
printf("null argument passed to -d\n");
return -1;
}
int pipe_delay_conv = atoi(pipe_delay);
if (pipe_delay_conv == 0){
printf("invalid arg %s passed to flag -d\n", pipe_delay);
return -1;
} else {
*microseconds_delay_var = pipe_delay_conv;
return 0;
}
}
int handle_flag_c(char *color_mode, int *color_var){
if (color_mode == NULL){
printf("null argument passed to -c\n");
return -1;
}
int color_mode_conv = atoi(color_mode);
if (color_mode_conv == 0){
printf("invalid arg %s passed to flag -c\n", color_mode);
return -1;
} else if (color_mode_conv < 30 || color_mode_conv > 39){
printf("invalid color mode\n");
return -1;
} else {
*color_var = color_mode_conv;
return 0;
}
return 0;
}
int handle_flag_r(char *rand_seed, int *rand_seed_var){
if (rand_seed == NULL){
printf("null argument passed to -r\n");
return -1;
}
*rand_seed_var = atoi(rand_seed);
return 0;
}
void handle_flag_help(void){
print_help();
exit(EXIT_FAILURE);
}
int find_flag_index(char *wanted_flag, char *flag_list[FLAG_COUNT]){
for (int flag_index = 0; flag_index < FLAG_COUNT; flag_index++){
if (strcmp(flag_list[flag_index], wanted_flag) == 0){
return flag_index;
}
}
return -1;
}
int parse_flags(int argc, char **argv, struct flag_holder *flag_holder){
for (int arg_index = 1; arg_index < argc; arg_index++){
int flag_index = find_flag_index(argv[arg_index], flag_arr);
if (flag_index == -1){
continue;
}
char *next_arg;
if (arg_index + 1 == argc){
next_arg = NULL;
} else {
next_arg = argv[arg_index + 1];
}
int return_code = 0;
switch (flag_index){
case 0:
return_code = handle_flag_n(next_arg, flag_holder->pipe_count_var);
break;
case 1:
return_code = handle_flag_d(next_arg, flag_holder->microseconds_delay_var);
break;
case 2:
return_code = handle_flag_c(next_arg, flag_holder->color_var);
break;
case 3:
return_code = handle_flag_r(next_arg, flag_holder->rand_seed_var);
break;
case 4:
handle_flag_help();
break;
}
if (return_code == -1){
print_help();
return -1;
}
}
return 0;
}