-
Notifications
You must be signed in to change notification settings - Fork 23
/
parameters.cpp
194 lines (166 loc) · 4.44 KB
/
parameters.cpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* parameters.cpp
*
* Definition and reading of the parameters. Include "parameters.h" to
* use these values.
*
* Created on: 11 Nov 2011
* Author: dan
*/
// Disable some Visual Studio warnings
#define _CRT_SECURE_NO_WARNINGS
#include "parameters.h"
#include "math.h"
#include "log.h"
#include "util.h"
#include <stdlib.h>
#include <string.h>
#include "numerics.h"
// to get around Windows' alternative definition of nonstandard strcasecmp()
#if defined(_WIN32) || defined(_WIN64)
#define strcasecmp _stricmp
#endif
// See parameters.h for an explanation
PARAMETERS_TABLE(DEFINE_PARAMETERS)
char HelpString[] = "\n\
\n\
Masked KlustaKwik\n\
\n\
Uses the CEM algorithm with masks to do automatic clustering.\n\n\
";
integer num_used_arguments = 0;
void params_error()
{
fprintf(stderr, "Usage: MaskedKlustaKwik FileBase ElecNo [Arguments]\n\n");
fprintf(stderr, "Arguments (with default values): \n\n");
print_params(stderr);
exit(1);
}
void SetupParams(integer argc, char **argv) {
char fname[STRLEN];
init_params(argc, argv);
// PARAMETER DEFINITIONS GO HERE
// This line reads the PARAMETERS_TABLE from parameters.h and reads each
// of the named parameters from the command line file.
PARAMETERS_TABLE(INPUT_PARAMETERS)
if (argc<3)
{
fprintf(stderr, "Not enough command line arguments.\n\n");
params_error();
}
strcpy(FileBase, argv[1]);
ElecNo = atoi(argv[2]);
if(2*num_used_arguments!=argc-3)
{
fprintf(stderr, "%d %d\n", (int)num_used_arguments, (int)argc);
fprintf(stderr, "Unrecognised command line arguments.\n\n");
params_error();
}
//if(UseDistributional)
//{
// UseMaskedMStep = false;
// UseMaskedEStep = false;
//}
if (Screen) print_params(stdout);
// open log file, if required
if (Log) {
sprintf(fname, "%s.klg.%d", FileBase, (int)ElecNo);
logfp = fopen_safe(fname, "w");
print_params(logfp);
}
}
typedef struct entry_t {
integer t;
char *name;
void *addr;
struct entry_t *next;
} entry;
entry *top, *bottom;
integer argc;
char **argv;
extern char HelpString[];
char help = 0;
/* returns 1 if the parameter was found and changed, else zero. */
integer change_param(char *name, char *value)
{
entry *e;
integer changed = 0;
for(e=bottom; e; e = e->next) if (!strcasecmp(name, e->name)) {
switch (e->t) {
case FLOAT:
*((scalar *) e->addr) = atof(value); break;
case INT:
*((integer *) e->addr) = atoi(value); break;
case BOOLEAN:
if (*value == '0')
*((char *) e->addr) = 0;
else
*((char *) e->addr) = 1;
break;
case STRING:
strncpy((char *)e->addr, value, STRLEN); break;
}
changed = 1;
break;
}
num_used_arguments += changed;
return changed;
}
void init_params(integer ac, char **av)
{
argc = ac;
argv = av;
}
void search_command_line(char *name)
{
integer i;
for(i=0; i<argc-1; i++)
if (argv[i][0] == '-' && !strcasecmp(argv[i]+1, name))
change_param(argv[i] + 1, argv[i+1]);
if (argv[argc-1][0] == '-' && !strcasecmp(argv[argc-1]+1, name))
change_param(argv[argc-1] + 1, "");
}
void add_param(integer t, char *name, void *addr)
{
entry *e;
if (top == NULL) {
bottom = top = e = (entry *) malloc(sizeof(entry));
} else {
e = (entry *) malloc(sizeof(entry));
top->next = e;
top = e;
}
if (e == NULL) {printf("parameter manager out of memory!\n"); exit(1);}
top->t = t;
top->name = name;
top->addr = addr;
top->next = NULL;
search_command_line(name);
}
void print_params(FILE *fp)
{
entry *e;
add_param(BOOLEAN, "help", &help);
if (help)
{
fprintf(fp, HelpString);
PARAMETERS_TABLE(PRINT_PARAMETERS);
exit(0);
}
else
{
for(e=bottom; e; e = e->next) {
fprintf(fp, "%s\t", e->name);
switch (e->t) {
case FLOAT:
fprintf(fp, SCALARFMT "\n", *(scalar *)(e->addr)); break;
case INT:
fprintf(fp, "%d\n", (int)(*(integer *)(e->addr))); break;
case BOOLEAN:
fprintf(fp, "%d\n", (int)(*(char *)(e->addr))); break;
case STRING:
fprintf(fp, "%s\n", (char *)(e->addr)); break;
}
}
}
}