-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparams_parser.c
137 lines (132 loc) · 4.64 KB
/
params_parser.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
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
#include "include/params_parser.h"
#define NUM_OPTS 10
#define NUM_OPTS_INC 5
// ===========================================================================
// opt [PRIVATE FUNCTION]
// Process a single option
// ===========================================================================
int opt(int argc, char **argv, int *index, const char **pwv, int numPwv, optToken **o){
if(strlen(argv[*index]) <= 1){
//lexycal error
fprintf(stderr, "parse_params::opt - lexycal error within %d param. No name specified.\n", *index);
return PROG_ERROR;
}else if(!isalpha(argv[*index][1])){
//lexycal error
fprintf(stderr, "parse_params::opt - lexycal error within %d param. Name must begin with an aplha char.\n", *index);
return PROG_ERROR;
}
/*ceate a buffer*/
*o = malloc(sizeof(optToken));
if(!(*o)){
fprintf(stderr, "parse_params::opt: error while allocating memory for the token structure.\n");
return PROG_ERROR;
}
/*get the name*/
char *name = malloc(sizeof(char) * strlen(argv[*index])); //we will not copy the initial "-"
if(!name){
fprintf(stderr, "parse_params::opt: error while allocating memory for the name.\n");
return PROG_ERROR;
}
strcpy(name, argv[*index] + 1);
/*must be checked whether this option requires an associated value. */
int i;
int found = 0;
for(i = 0; i < numPwv; i++){
if(strcmp(name, pwv[i]) == 0){
found = 1;
break;
}
}
(*o)->name = name;
(*o)->value = NULL;
if(found){
(*index)++; //move the index ahead
if((*index) >= argc){
//syntax error
fprintf(stderr, "parse_params::opt - error, param \"%s\" requires a value to be passed.\n", name);
return PROG_ERROR;
}
if(argv[*index][0] == '-'){
//syntax error
fprintf(stderr, "parse_params::opt - error, param \"%s\" requires a value to be passed.\n", name);
return PROG_ERROR;
}
char *value = malloc(sizeof(char) * (strlen(argv[*index]) + 1));
if(!value){
fprintf(stderr, "parse_params::opt - error while allocating memory for the alue.\n");
return PROG_ERROR;
}
strcpy(value, argv[*index]);
(*o)->value = value;
}
return PROG_SUCCESS;
}
// ===========================================================================
// parse_params
// ===========================================================================
int parse_params(int argc, char **argv, const char **pwv, int numPwvEntries, optToken ***list, int *count){
/*list to hold the parameters*/
int maxNumElements = NUM_OPTS;
*list = malloc(sizeof(optToken *) * NUM_OPTS);
if(!(*list)){
fprintf(stderr, "parse_params: error while allocating memory.\n");
return PROG_ERROR;
}
*count = 0;
int i;
for(i = 0; i < argc; i++){ //for each argument
if(*count >= maxNumElements){ //check if there is enough space in the list
maxNumElements+= NUM_OPTS_INC;
*list = realloc(*list, maxNumElements);
if(!(*list)){
fprintf(stderr, "parse_params: error while allocating memory.\n");
return PROG_ERROR;
}
}
switch (argv[i][0]) { //identify the argument
case '-':{
/*PNAME expected. PVALUEs, if any, are processed in here*/
optToken *o;
if(opt(argc, argv, &i, pwv, numPwvEntries, &o) == PROG_ERROR) return PROG_ERROR;
o->isParam = 1;
(*list)[(*count)++] = o;
break;
}
default:{
/*input string expected*/
optToken *o = malloc(sizeof(optToken));
if(!o){
fprintf(stderr, "parse_params: error while allocating memory.\n");
return PROG_ERROR;
}
char *value = malloc(sizeof(char) * (strlen(argv[i]) + 1));
if(!value){
fprintf(stderr, "parse_params: error while allocating memory.\n");
return PROG_ERROR;
}
strcpy(value, argv[i]);
o->value = value;
o->name = NULL;
o->isParam = 0;
(*list)[(*count)++] = o;
}
}
}
return PROG_SUCCESS;
}
// ===========================================================================
// free_optTokenList
// ===========================================================================
void free_optTokenList(optToken **list, int count){
int i;
for(i = 0; i < count; i++){
if(list[i]->isParam){
free(list[i]->name);
if(list[i]->value != NULL) free(list[i]->value);
}else{
free(list[i]->value);
}
free(list[i]);
}
free(list);
}