Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/pure-cfg/src/lib/common/input.c
ViewVC logotype

Annotation of /branches/pure-cfg/src/lib/common/input.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1669 - (view) (download) (as text)

1 : jhr 1231 /*! \file input.c
2 :     *
3 : glk 1252 * \author John Reppy, Gordon Kindlmann
4 : jhr 1231 */
5 :    
6 :     /*
7 :     * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
8 :     * All rights reserved.
9 :     */
10 :     #include "Diderot/diderot.h"
11 :     #include <teem/hest.h>
12 :    
13 : jhr 1648 bool VerboseFlg = false; //! true if running in verbose mode
14 :     bool TimingFlg = false; //! true if timing computation
15 : jhr 1649 bool NrrdOutputFlg = false; //! true if output is in Nrrd format
16 : jhr 1648
17 : jhr 1243 //! Option type tags
18 : jhr 1236 typedef enum {
19 : glk 1253 optTypeUnknown, // 0
20 :     optTypeFlag, // 1
21 :     optTypeBool, // 2
22 :     optTypeInt, // 3
23 :     optTypeReal, // 4
24 :     optTypeReal2, // 5
25 :     optTypeReal3, // 6
26 :     optTypeReal4, // 7
27 :     optTypeString // 8
28 : jhr 1262 } opt_type_t;
29 : jhr 1236
30 : jhr 1262 /*! The struct containing the definition of *one* option, and storage
31 :     * for its value once parsed
32 :     */
33 : glk 1252 typedef struct {
34 : jhr 1268 char *name; //!< option name
35 :     char *desc; //!< option description (a phrase or sentence)
36 :     opt_type_t ty; //!< option type
37 :     union { //! storage that is passed into hest
38 :     int b; //!< storage for boolean-valued options
39 :     double r[4]; //!< storage for real-valued options
40 :     int64_t i[4]; //!< storage for integer-valued options
41 :     char *s; //!< storage for string-valued options
42 : glk 1252 } hval;
43 : jhr 1268 void *valp; //!< pointer to Diderot global variable being set
44 : jhr 1262 } opt_def_t;
45 : jhr 1236
46 : jhr 1669 /*! container for *all* options, the machinery for parsing them,
47 : jhr 1262 * and means of managing their allocation. There is an opaque typedef
48 :     * around this in src/include/Diderot/options.h.
49 :     *
50 :     * The "airArray *mop" is the primary means of managing the things that
51 :     * are dynamically allocated; anything that is allocated is registered
52 :     * in the mop so that it can be freed from a single place later (look
53 :     * for calls to airMopAdd)
54 :     *
55 : jhr 1263 * Note that "odef" is a dynamically re-allocated array (managed by
56 : jhr 1262 * "odefArr"), and it is an array of opt_def_t pointers, and not an
57 :     * array of opt_def_t's, which is necessary: the values that hest sets
58 :     * are set via a pointer, and those values are stored in the opt_def_t,
59 :     * so the memory location of the opt_def_t cannot change, so these are
60 :     * allocated individually once. If odef was an array of opt_def_t's,
61 :     * then the location of the individual op_def_t would change if odef
62 :     * was reallocated for a different length.
63 :     */
64 :     struct Diderot_struct_options {
65 : jhr 1648 char *progInfo; //!< documentation string for describing whole
66 :     //! program, intended to be one or more sentences
67 : jhr 1268 hestOpt *hopt; //!< dynamically managed hest options
68 :     hestParm *hparm; //!< hest parameters
69 :     opt_def_t **odef; //!< dynamically reallocated via odefArr
70 :     unsigned int odefNum; //!< length of meaningful values in odef[]
71 :     airArray *odefArr; //!< manages odef and odefNum;
72 :     airArray *mop; //!< manages hopt, hparm, and odefArr
73 : glk 1252 };
74 : jhr 1236
75 : jhr 1262 static void *OptDefNew ()
76 :     {
77 :     opt_def_t *odef = AIR_CALLOC(1, opt_def_t);
78 :     odef->name = odef->desc = 0;
79 : glk 1252 odef->ty = optTypeUnknown;
80 : jhr 1262 odef->valp = 0;
81 :     return odef;
82 : jhr 1236 }
83 :    
84 : jhr 1262 /*! free an opt_def_t. This doesn't need to be called directly; it will
85 : jhr 1648 * be used by the airArray to manage "odef" and what it points to.
86 : jhr 1262 */
87 :     static void *optDefNix (void *_odef)
88 :     {
89 :     opt_def_t *odef = (opt_def_t *)_odef;
90 : glk 1252 airFree(odef->name);
91 :     airFree(odef->desc);
92 :     if (optTypeString == odef->ty) {
93 :     airFree(odef->hval.s);
94 : jhr 1236 }
95 : glk 1253 airFree(odef);
96 : jhr 1262 return 0;
97 : glk 1252 }
98 : jhr 1236
99 : jhr 1648 /*! create the container for all option information and register standard
100 :     * command-line options.
101 : jhr 1262 */
102 : jhr 1263 Diderot_Options_t *Diderot_OptNew ()
103 : jhr 1262 {
104 :     static const char *me = "Diderot_Options_New";
105 : jhr 1648 Diderot_Options_t *opts = AIR_CALLOC(1, Diderot_Options_t);
106 :     if (opts == 0) {
107 : glk 1260 fprintf(stderr, "%s: unable to allocate output\n", me);
108 :     exit(1);
109 : jhr 1236 }
110 : jhr 1648 opts->progInfo = 0;
111 :     opts->mop = airMopNew();
112 :     opts->hopt = 0;
113 :     opts->hparm = hestParmNew();
114 :     opts->hparm->noArgsIsNoProblem = AIR_TRUE;
115 :     airMopAdd (opts->mop, opts->hparm, (airMopper)hestParmFree, airMopAlways);
116 :     opts->odef = 0;
117 :     opts->odefNum = 0;
118 : jhr 1262 /*
119 :     ** the airArray odefArr manages the allocation of odef, and as a
120 :     ** convenience also sets odefNum to the number of values reliably
121 :     ** set in odef
122 :     */
123 : jhr 1648 opts->odefArr = airArrayNew (
124 :     (void**)(&opts->odef),
125 :     &opts->odefNum,
126 :     sizeof(opt_def_t *),
127 : jhr 1268 8 /* allocation increment */);
128 : jhr 1262
129 : jhr 1648 if (opts->odefArr == 0) {
130 : glk 1260 fprintf(stderr, "%s: unable to allocate option array\n", me);
131 :     exit(1);
132 : glk 1252 }
133 : jhr 1262 /*
134 :     ** the airArray will set odef[I] = OptDefNew for newly allocated
135 :     ** elements I of odef, and will call optDefNix(odef[I]) for
136 :     ** elements of odef that are about to be freed
137 :     */
138 : jhr 1648 airArrayPointerCB (opts->odefArr, OptDefNew, optDefNix);
139 :     airMopAdd (opts->mop, opts->odefArr, (airMopper)airArrayNuke, airMopAlways);
140 :    
141 :     // register standard command-line options
142 :     Diderot_OptAddFlag (opts, "verbose", "enable runtime-system messages", &VerboseFlg);
143 :     Diderot_OptAddFlag (opts, "timing", "enable execution timing", &TimingFlg);
144 :     Diderot_OptAddFlag (opts, "nrrd", "enable nrrd output", &NrrdOutputFlg);
145 :    
146 :     return opts;
147 : jhr 1236 }
148 :    
149 : jhr 1262 /*! free everything associated with a Diderot_Options_t
150 :     */
151 :     void Diderot_OptFree (Diderot_Options_t *dopts)
152 :     {
153 :     if (dopts != 0) {
154 : glk 1252 airFree(dopts->progInfo);
155 : glk 1260 /* frees everything connected to the mop, and the mop itself */
156 : glk 1252 airMopOkay(dopts->mop);
157 :     free(dopts);
158 :     }
159 :     return;
160 : jhr 1236 }
161 :    
162 : jhr 1262 /*! sets the one piece of information in the Diderot_Options_t
163 :     * which is not really for the options; "progInfo" is to describe the
164 :     * whole program.
165 :     */
166 :     void Diderot_OptProgramInfoSet (
167 :     Diderot_Options_t *dopts,
168 :     const char *progInfo)
169 :     {
170 :     static const char *me = "Diderot_OptProgramInfoSet";
171 : jhr 1236
172 : glk 1252 if (progInfo) {
173 :     if (!( dopts->progInfo = airStrdup(progInfo) )) {
174 : glk 1260 fprintf(stderr, "%s: unable to copy program info", me);
175 :     exit(1);
176 : glk 1252 }
177 : jhr 1236 }
178 :     }
179 :    
180 : jhr 1262 /*! main function for adding options to a Diderot_Options_t.
181 :     */
182 :     static opt_def_t *OptAdd (
183 :     Diderot_Options_t *dopts,
184 :     const char *name,
185 :     const char *desc,
186 :     void *valp,
187 :     opt_type_t ty)
188 :     {
189 :     static const char *me = "OptAdd";
190 : glk 1252 unsigned int nidx;
191 :    
192 : jhr 1262 if ((name == 0) || (desc == 0)) {
193 : glk 1260 fprintf(stderr, "%s: need both name (%p) and desc (%p) non-NULL\n",
194 : jhr 1268 me, (void *)name, (void *)desc);
195 : glk 1260 exit(1);
196 : jhr 1236 }
197 : jhr 1262 /* calling airArrayLenIncr may trigger re-allocation of odef;
198 :     * nidx is the index of the newly reserved element, odef[nidx]
199 :     */
200 : glk 1252 nidx = airArrayLenIncr(dopts->odefArr, 1);
201 : jhr 1262 if (dopts->odef == 0) {
202 : glk 1260 fprintf(stderr, "%s: unable to reallocate option array\n", me);
203 :     exit(1);
204 : glk 1252 }
205 : jhr 1262 /* code here uses "odf" to copy one value of odef[] */
206 : jhr 1263 opt_def_t *odf = dopts->odef[nidx];
207 : glk 1260 odf->name = airStrdup(name);
208 :     odf->desc = airStrdup(desc);
209 : jhr 1262 if ((odf->name == 0) || (odf->desc == 0)) {
210 :     fprintf(stderr, "%s: unable to allocate strings (%p, %p)\n",
211 : jhr 1268 me, (void *)odf->name,(void *) odf->desc);
212 : glk 1260 exit(1);
213 : glk 1252 }
214 : glk 1260 odf->valp = valp;
215 :     odf->ty = ty;
216 : glk 1252
217 : glk 1260 return odf;
218 : glk 1252 }
219 : jhr 1236
220 : jhr 1262 void Diderot_OptAddFlag (
221 :     Diderot_Options_t *dopts,
222 :     const char *name, const char *desc,
223 :     bool *flg)
224 :     {
225 :     opt_def_t *odf = OptAdd(dopts, name, desc, (void*)flg, optTypeFlag);
226 : jhr 1268 hestOptAdd (
227 :     &dopts->hopt, name, NULL, airTypeInt, 0, 0, &odf->hval.b,
228 :     NULL, desc);
229 : jhr 1236 }
230 :    
231 : jhr 1263 /*! \brief add a boolean-valued option to the list of options.
232 :     * \param dopts the list of options
233 :     * \param name the name of the option
234 :     * \param desc a short description string
235 :     * \param v a pointer to the location where the value gets stored
236 :     * \param hasDflt true if there is an initial value
237 :     */
238 : jhr 1262 void Diderot_OptAddBool (
239 :     Diderot_Options_t *dopts,
240 :     const char *name, const char *desc,
241 :     bool *v, bool hasDflt)
242 :     {
243 :     opt_def_t *odf = OptAdd(dopts, name, desc, (void*)v, optTypeBool);
244 : jhr 1268 hestOptAdd (
245 :     &dopts->hopt, name, "bool", airTypeBool, 1, 1, &odf->hval.b,
246 :     hasDflt ? (*v ? "true" : "false") : NULL, desc);
247 : glk 1252 }
248 : jhr 1236
249 : glk 1260 /*
250 :     ** On the use of AIR_STRLEN_HUGE below: default values for
251 :     ** command-line options are communicated to hest by a *string*, but
252 :     ** the actual default value comes to us in Diderot global. So these
253 :     ** functions have to convert the value from the Diderot variable into
254 :     ** a string.
255 :     **
256 :     ** The problem is that we don't know how long string will have to be,
257 :     ** and there are few good functions for dealing with this. snprintf
258 :     ** is good for making sure you don't over-write a buffer of fixed
259 :     ** size, but you still have to allocate the buffer. asprintf allocates
260 :     ** as needed, but GLK didn't want to assume it was available (perhaps
261 :     ** this should be revisited)
262 :     **
263 :     ** So instead, we just use a huge buffer, and assume that it will be
264 :     ** more than big enough. Feel free to fix this.
265 :     */
266 :    
267 : jhr 1262 /*! \brief add an integer option to the list of options.
268 :     * \param dopts the list of options
269 :     * \param name the name of the option
270 :     * \param desc a short description string
271 :     * \param v a pointer to the location where the value gets stored
272 :     * \param hasDflt true if there is an initial value
273 :     */
274 :     void Diderot_OptAddInt (
275 :     Diderot_Options_t *dopts,
276 :     const char *name, const char *desc, Diderot_int_t *v, bool hasDflt)
277 :     {
278 : glk 1252 char buf[AIR_STRLEN_HUGE] = "";
279 : jhr 1236 if (hasDflt) {
280 : jhr 1649 snprintf(buf, sizeof(buf), "%ld", (long)*v);
281 : jhr 1236 }
282 : jhr 1262 opt_def_t *odf = OptAdd(dopts, name, desc, (void*)v, optTypeInt);
283 : jhr 1268 hestOptAdd (
284 :     &dopts->hopt, name, "int", airTypeLongInt, 1, 1, odf->hval.i,
285 :     hasDflt ? buf : NULL, desc);
286 : jhr 1236 }
287 :    
288 : jhr 1262 /*! \brief add a real-valued option to the list of options.
289 :     * \param dopts the list of options
290 :     * \param name the name of the option
291 :     * \param desc a short description string
292 :     * \param v a pointer to the location where the value gets stored
293 :     * \param hasDflt true if there is an initial value
294 :     */
295 :     void Diderot_OptAddReal (
296 :     Diderot_Options_t *dopts,
297 :     const char *name, const char *desc, Diderot_real_t *v, bool hasDflt)
298 :     {
299 : glk 1252 char buf[AIR_STRLEN_HUGE] = "";
300 : jhr 1236 if (hasDflt) {
301 : jhr 1649 snprintf(buf, sizeof(buf), "%lf", (double)*v);
302 : jhr 1236 }
303 : jhr 1262 opt_def_t *odf = OptAdd(dopts, name, desc, (void*)v, optTypeReal);
304 : jhr 1268 hestOptAdd (
305 :     &dopts->hopt, name, "val", airTypeDouble, 1, 1, odf->hval.r,
306 :     hasDflt ? buf : NULL, desc);
307 : jhr 1236 }
308 :    
309 : jhr 1262 /*! \brief add a 2-element vector option to the list of options.
310 :     * \param dopts the list of options
311 :     * \param name the name of the option
312 :     * \param desc a short description string
313 :     * \param v a pointer to the location where the value gets stored
314 :     * \param hasDflt true if there is an initial value
315 :     */
316 :     void Diderot_OptAddReal2 (
317 :     Diderot_Options_t *dopts,
318 :     const char *name, const char *desc, Diderot_vec2_t *v, bool hasDflt)
319 :     {
320 : glk 1252 char buf[AIR_STRLEN_HUGE] = "";
321 : jhr 1236
322 :     if (hasDflt) {
323 : glk 1252 Diderot_union2_t u;
324 :     u.v = *v;
325 : jhr 1649 snprintf(buf, sizeof(buf), "%lf %lf", (double)(u.r[0]), (double)(u.r[1]));
326 : jhr 1236 }
327 : jhr 1262 opt_def_t *odf = OptAdd(dopts, name, desc, (void*)v, optTypeReal2);
328 : jhr 1268 hestOptAdd (
329 :     &dopts->hopt, name, "x y", airTypeDouble, 2, 2, odf->hval.r,
330 :     hasDflt ? buf : NULL, desc);
331 : jhr 1236 }
332 :    
333 : jhr 1262 /*! \brief add a 3-element vector option to the list of options.
334 :     * \param dopts the list of options
335 :     * \param name the name of the option
336 :     * \param desc a short description string
337 :     * \param v a pointer to the location where the value gets stored
338 :     * \param hasDflt true if there is an initial value
339 :     */
340 :     void Diderot_OptAddReal3 (
341 :     Diderot_Options_t *dopts,
342 :     const char *name, const char *desc, Diderot_vec3_t *v, bool hasDflt)
343 :     {
344 : glk 1252 char buf[AIR_STRLEN_HUGE] = "";
345 : jhr 1236
346 :     if (hasDflt) {
347 : glk 1252 Diderot_union3_t u;
348 :     u.v = *v;
349 : jhr 1649 snprintf(buf, sizeof(buf), "%lf %lf %lf",
350 :     (double)(u.r[0]), (double)(u.r[1]), (double)(u.r[2]));
351 : jhr 1236 }
352 : jhr 1262 opt_def_t *odf = OptAdd(dopts, name, desc, (void*)v, optTypeReal3);
353 : jhr 1268 hestOptAdd (
354 :     &dopts->hopt, name, "x y z", airTypeDouble, 3, 3, odf->hval.r,
355 :     hasDflt ? buf : NULL, desc);
356 : jhr 1236 }
357 :    
358 : jhr 1262 /*! \brief add a 4-element vector option to the list of options.
359 :     * \param dopts the list of options
360 :     * \param name the name of the option
361 :     * \param desc a short description string
362 :     * \param v a pointer to the location where the value gets stored
363 :     * \param hasDflt true if there is an initial value
364 :     */
365 :     void Diderot_OptAddReal4 (
366 :     Diderot_Options_t *dopts,
367 :     const char *name, const char *desc,
368 :     Diderot_vec4_t *v, bool hasDflt)
369 :     {
370 : glk 1252 char buf[AIR_STRLEN_HUGE] = "";
371 : jhr 1236
372 :     if (hasDflt) {
373 : glk 1252 Diderot_union4_t u;
374 :     u.v = *v;
375 : jhr 1649 snprintf(buf, sizeof(buf), "%lf %lf %lf %lf",
376 : jhr 1268 (double)(u.r[0]), (double)(u.r[1]),
377 :     (double)(u.r[2]), (double)(u.r[3]));
378 : jhr 1236 }
379 : jhr 1262 opt_def_t *odf = OptAdd(dopts, name, desc, (void*)v, optTypeReal4);
380 : jhr 1268 hestOptAdd (
381 :     &dopts->hopt, name, "x y z w", airTypeDouble, 4, 4, odf->hval.r,
382 :     hasDflt ? buf : NULL, desc);
383 : glk 1252 }
384 : jhr 1236
385 : jhr 1262 /*! \brief add a string-vauled option to the list of options.
386 :     * \param dopts the list of options
387 :     * \param name the name of the option
388 :     * \param desc a short description string
389 :     * \param v a pointer to the location where the value gets stored
390 :     * \param hasDflt true if there is an initial value
391 :     */
392 :     void Diderot_OptAddString (
393 :     Diderot_Options_t *dopts,
394 :     const char *name, const char *desc,
395 :     char **v, bool hasDflt)
396 :     {
397 :     opt_def_t *odf = OptAdd(dopts, name, desc, (void*)v, optTypeString);
398 : jhr 1268 hestOptAdd (
399 :     &dopts->hopt, name, "str", airTypeString, 1, 1, &odf->hval.s,
400 :     hasDflt ? *v : NULL, desc);
401 : jhr 1236 }
402 :    
403 : jhr 1262 /*! given argc/argv, learn values for all options and set them
404 :     */
405 :     void Diderot_OptProcess (Diderot_Options_t *dopts, int argc, const char **argv)
406 :     {
407 :     static const char *me = "Diderot_ProcessOptions";
408 : jhr 1236
409 : jhr 1268 hestParseOrDie (
410 :     dopts->hopt, argc-1, argv+1, dopts->hparm, argv[0],
411 :     dopts->progInfo ? dopts->progInfo : "(no program info set)",
412 :     AIR_TRUE, AIR_TRUE, AIR_TRUE);
413 : glk 1260 airMopAdd(dopts->mop, dopts->hopt, (airMopper)hestOptFree, airMopAlways);
414 :     airMopAdd(dopts->mop, dopts->hopt, (airMopper)hestParseFree, airMopAlways);
415 : jhr 1236
416 : glk 1252 // convert values learned by hest to their Diderot representations
417 :     for (int i = 0; i < dopts->odefNum; i++) {
418 : jhr 1263 opt_def_t *odf = dopts->odef[i];
419 : glk 1260 switch (odf->ty) {
420 : jhr 1268 case optTypeFlag:
421 :     case optTypeBool:
422 : glk 1260 *(bool*)(odf->valp) = odf->hval.b ? true : false;
423 : glk 1252 break;
424 : jhr 1268 case optTypeInt:
425 : glk 1260 *(Diderot_int_t *)(odf->valp) = (Diderot_int_t)(odf->hval.i[0]);
426 : glk 1252 break;
427 : jhr 1268 case optTypeReal:
428 : glk 1260 *(Diderot_real_t *)(odf->valp) = (Diderot_real_t)(odf->hval.r[0]);
429 : glk 1252 break;
430 : jhr 1268 case optTypeReal2:
431 : glk 1260 *(Diderot_vec2_t *)(odf->valp) = vec2((Diderot_real_t)(odf->hval.r[0]),
432 :     (Diderot_real_t)(odf->hval.r[1]));
433 : glk 1252 break;
434 : jhr 1268 case optTypeReal3:
435 : glk 1260 *(Diderot_vec3_t *)(odf->valp) = vec3((Diderot_real_t)(odf->hval.r[0]),
436 :     (Diderot_real_t)(odf->hval.r[1]),
437 :     (Diderot_real_t)(odf->hval.r[2]));
438 : glk 1252 break;
439 : jhr 1268 case optTypeReal4:
440 : glk 1260 *(Diderot_vec4_t *)(odf->valp) = vec4((Diderot_real_t)(odf->hval.r[0]),
441 :     (Diderot_real_t)(odf->hval.r[1]),
442 :     (Diderot_real_t)(odf->hval.r[2]),
443 :     (Diderot_real_t)(odf->hval.r[3]));
444 : glk 1252 break;
445 : jhr 1268 case optTypeString: {
446 :     char *s = airStrdup(odf->hval.s);
447 :     if (s == 0) {
448 :     fprintf(stderr, "%s: unable to allocate string copy\n", me);
449 :     exit(1);
450 :     }
451 :     *(char **)(odf->valp) = s;
452 : jhr 1262 } break;
453 : jhr 1268 default:
454 : glk 1260 fprintf(stderr, "%s: unknown option type %d\n", me, odf->ty);
455 :     exit(1);
456 : jhr 1268 } /* switch */
457 : jhr 1243 }
458 : jhr 1236 }

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0