-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetparam.C
203 lines (173 loc) · 4.4 KB
/
getparam.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
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
195
196
197
198
199
200
201
202
/*************************************************************************/
/* */
/* Copyright (c) 1994 Stanford University */
/* */
/* All rights reserved. */
/* */
/* Permission is given to use, copy, and modify this software for any */
/* non-commercial purpose as long as this copyright notice is not */
/* removed. All other uses, including redistribution in whole or in */
/* part, are forbidden without prior written permission. */
/* */
/* This software is provided with absolutely no warranty and no */
/* support. */
/* */
/*************************************************************************/
/*
* GETPARAM.C:
*/
EXTERN_ENV
#include <iostream>
#define global extern
#include "stdinc.h"
//#include <stdio.h>
local string *defaults = NULL; /* vector of "name=value" strings */
local string inputarray[12];
/*
* INITPARAM: ignore arg vector, remember defaults.
*/
void initparam(string *defv)
{
char buf[128];
long leng;
defaults = defv;
for(int i = 0; i < 12; i++) {
fgets(buf, 128, stdin);
//std::cerr << "read buf: " << buf << "\n";
size_t n = strlen(buf);
if (n > 0 && buf[n - 1] == '\n') {
buf[n - 1] = '\0';
}
leng = strlen(buf) + 1;
int idx = i;
if(idx > 0 && idx < 3)
idx++;
else if (idx == 3)
idx = 1;
if(leng > 1) {
inputarray[idx] = strcpy(static_cast<char*>(malloc(leng)), buf);
} else {
inputarray[idx] = strcpy(static_cast<char*>(malloc(1)), "");
}
memset( (void *) buf, '\0', 128);
}
#if 0
std::cerr << "inputarray:\n";
for(int i = 0; i < 12; i++)
std::cerr << i << " " << inputarray[i] << "\n";
#endif
}
/*
* GETPARAM: export version prompts user for value.
*/
string getparam(string name)
{
long i, leng = 0;
string def;
char buf[128];
//if (defaults == NULL)
//error("getparam: called before initparam\n");
i = scanbind(defaults, name);
//std::cerr << "in getparam, idx " << i << "\n";
if (i < 0)
error("getparam: %s unknown\n", name);
def = extrvalue(defaults[i]);
//if(inputarray[i] != NULL)
leng = strlen(inputarray[i]) + 1;
if (leng > 1) {
return inputarray[i];
} else {
return (def);
}
#if 0
def = extrvalue(defaults[i]);
fgets(buf, 128, stdin);
size_t n = strlen(buf);
if (n > 0 && buf[n - 1] == '\n') {
buf[n - 1] = '\0';
}
leng = strlen(buf) + 1;
if (leng > 1) {
return (strcpy(static_cast<char*>(malloc(leng)), buf));
}
else {
return (def);
}
#endif
}
/*
* GETIPARAM, ..., GETDPARAM: get long, long, bool, or double parameters.
*/
long getiparam(string name)
{
string val;
for (val = ""; *val == '\0';) {
val = getparam(name);
}
return (atoi(val));
}
long getlparam(string name)
{
string val;
for (val = ""; *val == '\0'; )
val = getparam(name);
return (atol(val));
}
bool getbparam(string name)
{
string val;
for (val = ""; *val == '\0'; )
val = getparam(name);
if (strchr("tTyY1", *val) != NULL) {
return (TRUE);
}
if (strchr("fFnN0", *val) != NULL) {
return (FALSE);
}
error("getbparam: %s=%s not bool\n", name, val);
}
double getdparam(string name)
{
string val;
for (val = ""; *val == '\0'; ) {
val = getparam(name);
}
return (atof(val));
}
/*
* SCANBIND: scan binding vector for name, return index.
*/
long scanbind(string bvec[], string name)
{
long i;
for (i = 0; bvec[i] != NULL; i++)
if (matchname(bvec[i], name))
return (i);
return (-1);
}
/*
* MATCHNAME: determine if "name=value" matches "name".
*/
bool matchname(string bind, string name)
{
char *bp, *np;
bp = bind;
np = name;
while (*bp == *np) {
bp++;
np++;
}
return (*bp == '=' && *np == '\0');
}
/*
* EXTRVALUE: extract value from name=value string.
*/
string extrvalue(string arg)
{
char *ap;
ap = (char *) arg;
while (*ap != '\0')
if (*ap++ == '=')
return ((string) ap);
return (NULL);
}