-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
153 lines (138 loc) · 3.78 KB
/
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.h"
int argslist_parse(ArgsList **argslist, char **stdinArgs, int numArgs)
{
int i = 0, j = 0, rc = 0, rc2 = 0;
char *nextArg = NULL;
ArgsList *tmp_argslist = NULL;
ERR_IF(*argslist != NULL);
ERR_IF(argslist == NULL);
// Allocate space and assign values to argslist.
tmp_argslist = malloc(sizeof(ArgsList));
ERR_IF( !tmp_argslist );
// Initialize this to false as default.
tmp_argslist->l = 0;
tmp_argslist->p = -1;
// Preset arrays
memset(&tmp_argslist->s, 0, 10);
memset(&tmp_argslist->g, '\0', 10);
memset(&tmp_argslist->n, '\0', 10);
// Assign values according to schema
for(i=0; i < numArgs; i++)
{
char *currArg = stdinArgs[i];
if( strcmp(currArg, "-l") == 0 )
{
if(tmp_argslist->l == 0)
tmp_argslist->l = 1;
}
else if( strcmp(currArg, "-p") == 0 )
{
if( tmp_argslist->p != -1 )
{
printf("ERROR: Already set port!\n");
rc = -1;
break;
}
i++;
tmp_argslist->p = atoi(stdinArgs[i]);
}
else if( strcmp(currArg, "-d") == 0 )
{
if(tmp_argslist->d != NULL)
{
printf("ERROR: Already set directory!\n");
rc = -1;
break;
}
i++;
tmp_argslist->d = strdup(stdinArgs[i]);
}
else if( strcmp(currArg, "-g") == 0 )
{
i++;
nextArg = stdinArgs[i];
while( (j < 10) && (*nextArg != '\0') )
{
if( *nextArg != ',' )
{
tmp_argslist->g[j] = *nextArg;
j++;
}
*nextArg++;
}
}
else if( (strcmp(currArg, "-n") == 0) || (strcmp(currArg, "-s") == 0) )
{
j = 0;
nextArg = strtok(stdinArgs[++i], ",");
while( (j < 10) && (nextArg != NULL) )
{
if( (strcmp(currArg, "-n") == 0) )
{
tmp_argslist->n[j] = atoi(nextArg);
}
else if( (strcmp(currArg, "-s") == 0) )
{
tmp_argslist->s[j] = nextArg;
}
j++;
nextArg = strtok(NULL, ",");
}
}
continue;
}
// Error handling
if( rc != 0 )
{
argslist_free(tmp_argslist);
}
else
{
if(tmp_argslist->d == NULL)
tmp_argslist->d = "Not Given";
// Pass back
// -Don't need to free tmp_argslist explicitly:
// freeing argslist will take care of it.
*argslist = tmp_argslist;
}
return rc;
}
void argslist_print(ArgsList *argslist)
{
int i, rc = 0, rc2 = 0;
char *logBool = NULL;
ERR_IF(argslist == NULL);
logBool = (argslist->l == 1) ? "true":"false";
printf("Logging: %s\n", logBool);
printf("Port: %d\n", argslist->p);
printf("Directory: %s\n", argslist->d);
printf("StrList:\n");
for( i = 0; i < 10; i++ )
{
if( argslist->s[i] != 0 )
printf("\t%s\n", argslist->s[i]);
}
printf("CharList:\n");
for( i = 0; i < 10; i++ )
{
if( argslist->g[i] != '\0' )
printf("\t%c\n", argslist->g[i]);
}
printf("NumList:\n");
for( i = 0; i < 10; i++ )
{
if( argslist->n[i] != '\0' )
printf("\t%d\n", argslist->n[i]);
}
}
void argslist_free(ArgsList *argslist)
{
// Free all allocated space
if(argslist->d != "Not Given")
free(argslist->d);
argslist->d = NULL;
free(argslist);
}