-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_line_opts.c
93 lines (76 loc) · 1.64 KB
/
cmd_line_opts.c
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
#include "cmd_line_opts.h"
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
int co_get_int(int argc, char** argv, const char* text, int* res) {
int opt_len = strlen(text);
int found = 0;
int ii;
for(ii = 0; ii < argc; ++ii) {
if( argv[ii][0] == '-' ) {
if(!strncmp(text, argv[ii], opt_len)) {
*res = atoi(argv[ii+1]);
found = 1;
break;
}
else {
++ii;
}
}
}
return found;
}
int co_get_bool(int argc, char** argv, const char* text, int* res) {
int opt_len = strlen(text);
int found = 0;
int ii;
for(ii = 0; ii < argc; ++ii) {
if( argv[ii][0] == '-' ) {
if(!strncmp(text, argv[ii], opt_len)) {
*res = 1;
found = 1;
break;
}
else {
++ii;
}
}
}
return found;
}
int co_get_float(int argc, char** argv, const char* text, float* res) {
int opt_len = strlen(text);
int found = 0;
int ii;
for(ii = 0; ii < argc; ++ii) {
if( argv[ii][0] == '-' ) {
if(!strncmp(text, argv[ii], opt_len)) {
*res = atof(argv[ii+1]);
found = 1;
break;
}
else {
++ii;
}
}
}
return found;
}
int co_get_string(int argc, char** argv, const char* text, char* * res) {
int opt_len = strlen(text);
int found = 0;
int ii;
for(ii = 0; ii < argc; ++ii) {
if( argv[ii][0] == '-' ) {
if(!strncmp(text, argv[ii], opt_len)) {
*res = argv[ii+1];
found = 1;
break;
}
else {
++ii;
}
}
}
return found;
}