-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_input.c
executable file
·80 lines (71 loc) · 1.51 KB
/
parse_input.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
#include <string.h>
#include "header.h"
int parse_params (int argc, char **argv, int numtags, cl_tag* t)
{
int ii, ij, status, count;
/* Check validity of tags */
for (ii=0; ii<numtags; ii++)
{
if (strcmp(t[ii].name,"")==0)
return TAGERR_NAME;
if (t[ii].type > TAGTYPE_STRING)
return TAGERR_TYPE;
if (t[ii].data == NULL)
return TAGERR_DATA;
}
/* Loop through command to find tags */
count = 0;
for (ii=1; ii<argc; ii++)
{
/* Loop through each provided tag */
for (ij=0; ij<numtags; ij++)
{
/* Check parameter against tag */
if (strcmp(t[ij].name, argv[ii])==0)
{
status = parse_params_action(argv, ii, t[ij]);
if (status<0) return status;
ii += status;
ij = numtags;
count++;
}
}
}
return count;
}
/* Given a match of tag t and argv[ii], do something */
int parse_params_action (char **argv, int ii, cl_tag t)
{
int skip;
skip = 0;
switch (t.type)
{
case TAGTYPE_BOOL:
*((int*)t.data) = 1;
break;
case TAGTYPE_INT:
if (argv[ii+1]==NULL) return PARSE_ERROR;
*((int*)t.data) = atoi(argv[ii+1]);
skip = 1;
break;
case TAGTYPE_FLOAT:
if (argv[ii+1]==NULL) return PARSE_ERROR;
*((float*)t.data) = atof(argv[ii+1]);
skip = 1;
break;
case TAGTYPE_STRING:
if (argv[ii+1]==NULL) return PARSE_ERROR;
strcpy((char*)t.data, argv[ii+1]);
skip = 1;
break;
}
return skip;
}
/* fgets leaves a trailing newline, this removes it */
void trim (char* str)
{
int len;
len = strlen(str)-1;
if (str[len] == '\n')
str[len] = 0;
}