-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathget_opt.c
More file actions
executable file
·186 lines (173 loc) · 4.63 KB
/
get_opt.c
File metadata and controls
executable file
·186 lines (173 loc) · 4.63 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
182
183
184
185
186
#include "get_opt.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static const char *optString = "a:n:s:P";
int check_opt(int argc){
return argc > 6;
}
/**
* Function that receives a string which is a sorting method name
* and returns his constant value
* @param char[] method
* @return int
*/
int get_sort_method(char method[]) {
int selected_method;
//printf("%d\n", selected_method);
if(strcmp(method, "selection") == 0)
selected_method = SELECTION;
else if(strcmp(method, "insertion") == 0)
selected_method = INSERTION;
else if(strcmp(method, "shell") == 0)
selected_method = SHELL;
else if(strcmp(method, "quick") == 0)
selected_method = QUICK;
else if(strcmp(method, "heap") == 0)
selected_method = HEAP;
else if(strcmp(method, "merge") == 0)
selected_method = MERGE;
else if(strcmp(method, "gpuquick") == 0)
selected_method = GPUQUICK;
else if(strcmp(method, "gpumerge") == 0)
selected_method = GPUMERGE;
else
selected_method = UNDEFINED;
return selected_method;
}
/**
* Function that receives a string containing
* a number and convert it to an integer
* @param char[] method
* @return int
*/
int get_array_size(char sizeOpt[]) {
int size = atoi(sizeOpt);
if(size < 0)
size = UNDEFINED;
return size;
}
/**
* Function that receives a string which is an array type
* and returns his constant
* @param char[] type
* @return int
*/
int get_array_type(char type[]) {
int selected_type;
if(strcmp(type, "random") == 0)
selected_type = RANDOM_ORDER;
else if(strcmp(type, "ascending") == 0)
selected_type = ASCENDING_ORDER;
else if(strcmp(type, "descending") == 0)
selected_type = DESCENDING_ORDER;
else if(strcmp(type, "almost") == 0)
selected_type = ALMOST_ORDERED;
else
selected_type = UNDEFINED;
return selected_type;
}
/**
* Function that receives the args and calls the program with the
* given parameters
* @param int argc
* @param char** argv
* @param int* method
* @param int* size
* @param int* array_type
* @param int* print_vector
*/
void get_opt(int argc, char **argv, int *method, int *size, int *array_type, int *print_vector) {
int opt = 0;
opt = getopt(argc, argv, optString);
//a:n:s:p
while(opt != -1) {
switch(opt) {
case 'a': // algorithm
*method = get_sort_method(optarg);
break;
case 'n': // number of elements
*size = get_array_size(optarg);
break;
case 's': // (vector) situation
*array_type = get_array_type(optarg);
break;
case 'P': // print
*print_vector = 1;
break;
default:
/* You won't actually get here. */
break;
}
opt = getopt(argc, argv, optString);
}
}
/**
* Function that receives an integer which is the sorting method
* constant and returns a string containing his name
* @param int method
* @return char*
*/
char* get_method_name(int method){
char *name;
switch(method){
case SELECTION:
name = "Selection";
break;
case INSERTION:
name = "Insertion";
break;
case SHELL:
name = "Shell";
break;
case QUICK:
name = "Quick";
break;
case HEAP:
name = "Heap";
break;
case MERGE:
name = "Merge";
break;
case GPUQUICK:
name = "CUDA Quick Sort";
break;
case GPUMERGE:
name = "CUDA Merge Sort";
break;
case UNDEFINED:
default:
name = " - ";
break;
}
return name;
}
/**
* Function that receives an integer which is the array type
* constant and returns a string containing his name
* @param int type
* @return char*
*/
char* get_array_type_name(int type){
char *name;
switch(type){
case RANDOM_ORDER:
name = "Random";
break;
case ASCENDING_ORDER:
name = "Ascending Order";
break;
case DESCENDING_ORDER:
name = "Descending Order";
break;
case ALMOST_ORDERED:
name = "Almost Sorted";
break;
case UNDEFINED:
default:
name = " - ";
break;
}
return name;
}