1 #include "arguments.h"
2 #include "etc.h"
3
DeleteArgument(int * argc,char ** argv,int i)4 static void DeleteArgument(int * argc, char ** argv, int i)
5 {
6 (*argc)--;
7 for (; i < (*argc); i++)
8 argv[i] = argv[i + 1];
9 }
10
CheckArguments(int * argc,char ** argv,char * s)11 static int CheckArguments(int * argc, char ** argv, char * s)
12 {
13 int i, r = 0;
14
15 for (i = 1; i < *argc; i++) {
16 if (!StringCmp(s, argv[i])) {
17 DeleteArgument(argc, argv, i);
18 r = 1;
19 }
20 }
21 return (r);
22 }
23
GetStringFromArguments(int * argc,char ** argv,char * s)24 static char * GetStringFromArguments(int * argc, char ** argv, char * s)
25 {
26 int i;
27 char * p = (char *)NULL;
28
29 for (i = 1; i < *argc; i++) {
30 if (!StringCmp(s, argv[i])) {
31 if (i == *argc - 1) {
32 DeleteArgument(argc, argv, i);
33 } else {
34 DeleteArgument(argc, argv, i);
35 p = argv[i];
36 DeleteArgument(argc, argv, i);
37 }
38 }
39 }
40 return (p);
41 }
42
Arguments_Read(int * argc,char ** argv,Argument arguments[])43 int Arguments_Read(int * argc, char ** argv, Argument arguments[])
44 {
45 int i;
46 char * p;
47 void (*funcp)();
48 int n = 0;
49
50 for (i = 0; arguments[i].name != NULL; i++) {
51 if (arguments[i].flag == ARGUMENTS_NONE) {
52 /* None */
53 } else if (arguments[i].flag == ARGUMENTS_FUNCTION) {
54 if (CheckArguments(argc, argv, arguments[i].name)) {
55 funcp = (void (*)())(arguments[i].value);
56 (*funcp)();
57 n++;
58 }
59 } else if (arguments[i].flag == ARGUMENTS_FLAG) {
60 if ((p = GetStringFromArguments(argc,argv,arguments[i].name)) != NULL) {
61 if ( (!StringCmp(p, "off")) || (!StringCmp(p, "OFF")) ) {
62 *((short int *)(arguments[i].value)) = ARGUMENTS_FALSE;
63 n++;
64 } else if ( (!StringCmp(p, "on" )) || (!StringCmp(p, "ON" )) ) {
65 *((short int *)(arguments[i].value)) = ARGUMENTS_TRUE;
66 n++;
67 }
68 }
69 } else if (arguments[i].flag == ARGUMENTS_FLAG_ON) {
70 if (CheckArguments(argc, argv, arguments[i].name)) {
71 *((short int *)(arguments[i].value)) = ARGUMENTS_TRUE;
72 n++;
73 }
74 } else if (arguments[i].flag == ARGUMENTS_FLAG_OFF) {
75 if (CheckArguments(argc, argv, arguments[i].name)) {
76 *((short int *)(arguments[i].value)) = ARGUMENTS_FALSE;
77 n++;
78 }
79 } else if (arguments[i].flag == ARGUMENTS_INTEGER) {
80 if ((p=GetStringFromArguments(argc, argv, arguments[i].name)) != NULL) {
81 *((int *)(arguments[i].value)) = atoi(p);
82 n++;
83 }
84 } else if (arguments[i].flag == ARGUMENTS_STRING) {
85 if ((p=GetStringFromArguments(argc, argv, arguments[i].name)) != NULL) {
86 *((char **)(arguments[i].value)) = p;
87 n++;
88 }
89 }
90 }
91
92 while (*argc > 1) {
93 fprintf(stderr, "ERROR : Unknown Argument : %s\n", argv[1]);
94 DeleteArgument(argc, argv, 1);
95 }
96
97 return (n);
98 }
99
100 /*****************************************************************************/
101 /* End of Program. */
102 /*****************************************************************************/
103