-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.c
More file actions
116 lines (97 loc) · 2.72 KB
/
commands.c
File metadata and controls
116 lines (97 loc) · 2.72 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
/* Sergeev Artemiy, 33601/2 (3057/2), 07.10.2013 */
#include <string.h>
#include "commands.h"
#define COMMAND(a, b, c) {a, b, c},
static command_t s_CommandsTable[] =
{
#include "commands.txt"
};
#undef COMMAND
#define OPTION(a, b, c) {a, b, c},
static option_t s_OptionsTable[] =
{
#include "options.txt"
};
#undef OPTION
/*** Commands ***/
error_code_t commandHelp( alg_parameters_t parameters )
{
return ERROR_SUCCESS;
}
error_code_t commandEncode( alg_parameters_t parameters )
{
return fanoEncode(parameters);
}
error_code_t commandDecode( alg_parameters_t parameters )
{
return fanoDecode(parameters);
}
/*** Options ***/
int optionT( int argc, char *argv[], alg_parameters_t *parameters )
{
parameters->codingFileName = argv[0];
return 1;
}
/*** Parsing functions ***/
static error_code_t s_parseOption( char *name, option_t **option )
{
unsigned int i;
size_t currentLength,
tableSize = sizeof(s_OptionsTable) / sizeof(s_OptionsTable[0]);
for (i = 0; i < tableSize; i++)
{
currentLength = strlen(s_OptionsTable[i].name);
if (strncmp(s_OptionsTable[i].name, name, currentLength) == 0)
{
*option = &s_OptionsTable[i];
return ERROR_SUCCESS;
}
}
*option = NULL;
return ERROR_OPT_NOT_FOUND;
}
static error_code_t s_parseCommand( char *name, command_t **command )
{
unsigned int i;
size_t currentLength,
tableSize = sizeof(s_CommandsTable) / sizeof(s_CommandsTable[0]);
for (i = 0; i < tableSize; i++)
{
currentLength = strlen(s_CommandsTable[i].name);
if (strncmp(s_CommandsTable[i].name, name, currentLength) == 0)
{
*command = &s_CommandsTable[i];
return ERROR_SUCCESS;
}
}
*command = NULL;
return ERROR_COMMAND_NOT_FOUND;
}
error_code_t parseArguments( int argc, char *argv[] )
{
command_t *command;
option_t *option;
unsigned int argumentIndex = 1;
alg_parameters_t parameters = {DEFAULT_CODES_FILE, NULL, NULL};
if (argc < 3 || argc > 6)
return ERROR_COMMAND_INVALID_PARAMETERS_COUNT;
/* Try to determine command */
if (s_parseCommand(argv[argumentIndex++], &command) != ERROR_SUCCESS)
return ERROR_COMMAND_NOT_FOUND;
/* Try to determine and proceed option */
if (s_parseOption(argv[argumentIndex], &option) == ERROR_SUCCESS)
{
++argumentIndex;
if (option->optionFunc)
argumentIndex += option->optionFunc(argc - argumentIndex, argv + argumentIndex, ¶meters);
}
/* Try to proceed parameters */
if (argc - argumentIndex == 2)
{
parameters.inputFileName = argv[argumentIndex++];
parameters.outputFileName = argv[argumentIndex];
}
else
return ERROR_COMMAND_INVALID_PARAMETERS_COUNT;
return command->commandFunc(parameters);
}