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

SCM Repository

[diderot] Annotation of /trunk/src/lib/common/input.c
ViewVC logotype

Annotation of /trunk/src/lib/common/input.c

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : jhr 1232 /*! \file input.c
2 :     *
3 : jhr 1301 * \author John Reppy, Gordon Kindlmann
4 : jhr 1232 */
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 1671 bool VerboseFlg = false; //! true if running in verbose mode
14 :     bool TimingFlg = false; //! true if timing computation
15 :     bool NrrdOutputFlg = false; //! true if output is in Nrrd format
16 :    
17 : jhr 1301 //! Option type tags
18 :     typedef enum {
19 :     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 :     } opt_type_t;
29 :    
30 :     /*! The struct containing the definition of *one* option, and storage
31 :     * for its value once parsed
32 :     */
33 :     typedef struct {
34 :     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 :     } hval;
43 :     void *valp; //!< pointer to Diderot global variable being set
44 :     } opt_def_t;
45 :    
46 : jhr 1671 /*! container for *all* options, the machinery for parsing them,
47 : jhr 1301 * 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 :     * Note that "odef" is a dynamically re-allocated array (managed by
56 :     * "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 1671 char *progInfo; //!< documentation string for describing whole
66 :     //! program, intended to be one or more sentences
67 : jhr 1301 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 :     };
74 :    
75 :     static void *OptDefNew ()
76 : jhr 1232 {
77 : jhr 1301 opt_def_t *odef = AIR_CALLOC(1, opt_def_t);
78 :     odef->name = odef->desc = 0;
79 :     odef->ty = optTypeUnknown;
80 :     odef->valp = 0;
81 :     return odef;
82 : jhr 1232 }
83 :    
84 : jhr 1301 /*! free an opt_def_t. This doesn't need to be called directly; it will
85 : jhr 1671 * be used by the airArray to manage "odef" and what it points to.
86 : jhr 1301 */
87 :     static void *optDefNix (void *_odef)
88 : jhr 1232 {
89 : jhr 1301 opt_def_t *odef = (opt_def_t *)_odef;
90 :     airFree(odef->name);
91 :     airFree(odef->desc);
92 :     if (optTypeString == odef->ty) {
93 :     airFree(odef->hval.s);
94 :     }
95 :     airFree(odef);
96 :     return 0;
97 :     }
98 : jhr 1232
99 : jhr 1671 /*! create the container for all option information and register standard
100 :     * command-line options.
101 : jhr 1301 */
102 :     Diderot_Options_t *Diderot_OptNew ()
103 :     {
104 :     static const char *me = "Diderot_Options_New";
105 : jhr 1671 Diderot_Options_t *opts = AIR_CALLOC(1, Diderot_Options_t);
106 :     if (opts == 0) {
107 : jhr 1301 fprintf(stderr, "%s: unable to allocate output\n", me);
108 :     exit(1);
109 :     }
110 : jhr 1671 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 1301 /*
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 1671 opts->odefArr = airArrayNew (
124 :     (void**)(&opts->odef),
125 :     &opts->odefNum,
126 :     sizeof(opt_def_t *),
127 : jhr 1301 8 /* allocation increment */);
128 : jhr 1232
129 : jhr 1671 if (opts->odefArr == 0) {
130 : jhr 1301 fprintf(stderr, "%s: unable to allocate option array\n", me);
131 :     exit(1);
132 : jhr 1232 }
133 : jhr 1301 /*
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 1671 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 1301 }
148 : jhr 1232
149 : jhr 1301 /*! free everything associated with a Diderot_Options_t
150 :     */
151 :     void Diderot_OptFree (Diderot_Options_t *dopts)
152 :     {
153 :     if (dopts != 0) {
154 :     airFree(dopts->progInfo);
155 :     /* frees everything connected to the mop, and the mop itself */
156 :     airMopOkay(dopts->mop);
157 :     free(dopts);
158 :     }
159 :     return;
160 : jhr 1232 }
161 :    
162 : jhr 1301 /*! 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 : jhr 1232 {
170 : jhr 1301 static const char *me = "Diderot_OptProgramInfoSet";
171 : jhr 1232
172 : jhr 1301 if (progInfo) {
173 :     if (!( dopts->progInfo = airStrdup(progInfo) )) {
174 :     fprintf(stderr, "%s: unable to copy program info", me);
175 :     exit(1);
176 :     }
177 :     }
178 :     }
179 : jhr 1232
180 : jhr 1301 /*! 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 :     unsigned int nidx;
191 :    
192 :     if ((name == 0) || (desc == 0)) {
193 :     fprintf(stderr, "%s: need both name (%p) and desc (%p) non-NULL\n",
194 :     me, (void *)name, (void *)desc);
195 :     exit(1);
196 : jhr 1232 }
197 : jhr 1301 /* calling airArrayLenIncr may trigger re-allocation of odef;
198 :     * nidx is the index of the newly reserved element, odef[nidx]
199 :     */
200 :     nidx = airArrayLenIncr(dopts->odefArr, 1);
201 :     if (dopts->odef == 0) {
202 :     fprintf(stderr, "%s: unable to reallocate option array\n", me);
203 :     exit(1);
204 :     }
205 :     /* code here uses "odf" to copy one value of odef[] */
206 :     opt_def_t *odf = dopts->odef[nidx];
207 :     odf->name = airStrdup(name);
208 :     odf->desc = airStrdup(desc);
209 :     if ((odf->name == 0) || (odf->desc == 0)) {
210 :     fprintf(stderr, "%s: unable to allocate strings (%p, %p)\n",
211 :     me, (void *)odf->name,(void *) odf->desc);
212 :     exit(1);
213 :     }
214 :     odf->valp = valp;
215 :     odf->ty = ty;
216 :    
217 :     return odf;
218 :     }
219 : jhr 1232
220 : jhr 1301 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 :     hestOptAdd (
227 :     &dopts->hopt, name, NULL, airTypeInt, 0, 0, &odf->hval.b,
228 :     NULL, desc);
229 : jhr 1232 }
230 :    
231 : jhr 1301 /*! \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 :     void Diderot_OptAddBool (
239 :     Diderot_Options_t *dopts,
240 :     const char *name, const char *desc,
241 :     bool *v, bool hasDflt)
242 : jhr 1232 {
243 : jhr 1301 opt_def_t *odf = OptAdd(dopts, name, desc, (void*)v, optTypeBool);
244 :     hestOptAdd (
245 :     &dopts->hopt, name, "bool", airTypeBool, 1, 1, &odf->hval.b,
246 :     hasDflt ? (*v ? "true" : "false") : NULL, desc);
247 :     }
248 : jhr 1232
249 : jhr 1301 /*
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 : jhr 1232
267 : jhr 1301 /*! \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 :     char buf[AIR_STRLEN_HUGE] = "";
279 :     if (hasDflt) {
280 : jhr 1671 snprintf(buf, sizeof(buf), "%ld", (long)*v);
281 : jhr 1232 }
282 : jhr 1301 opt_def_t *odf = OptAdd(dopts, name, desc, (void*)v, optTypeInt);
283 :     hestOptAdd (
284 :     &dopts->hopt, name, "int", airTypeLongInt, 1, 1, odf->hval.i,
285 :     hasDflt ? buf : NULL, desc);
286 :     }
287 : jhr 1232
288 : jhr 1301 /*! \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 :     char buf[AIR_STRLEN_HUGE] = "";
300 :     if (hasDflt) {
301 : jhr 1671 snprintf(buf, sizeof(buf), "%lf", (double)*v);
302 : jhr 1301 }
303 :     opt_def_t *odf = OptAdd(dopts, name, desc, (void*)v, optTypeReal);
304 :     hestOptAdd (
305 :     &dopts->hopt, name, "val", airTypeDouble, 1, 1, odf->hval.r,
306 :     hasDflt ? buf : NULL, desc);
307 : jhr 1232 }
308 : jhr 1301
309 :     /*! \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 :     char buf[AIR_STRLEN_HUGE] = "";
321 :    
322 :     if (hasDflt) {
323 :     Diderot_union2_t u;
324 :     u.v = *v;
325 : jhr 1671 snprintf(buf, sizeof(buf), "%lf %lf", (double)(u.r[0]), (double)(u.r[1]));
326 : jhr 1301 }
327 :     opt_def_t *odf = OptAdd(dopts, name, desc, (void*)v, optTypeReal2);
328 :     hestOptAdd (
329 :     &dopts->hopt, name, "x y", airTypeDouble, 2, 2, odf->hval.r,
330 :     hasDflt ? buf : NULL, desc);
331 :     }
332 :    
333 :     /*! \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 :     char buf[AIR_STRLEN_HUGE] = "";
345 :    
346 :     if (hasDflt) {
347 :     Diderot_union3_t u;
348 :     u.v = *v;
349 : jhr 1671 snprintf(buf, sizeof(buf), "%lf %lf %lf",
350 :     (double)(u.r[0]), (double)(u.r[1]), (double)(u.r[2]));
351 : jhr 1301 }
352 :     opt_def_t *odf = OptAdd(dopts, name, desc, (void*)v, optTypeReal3);
353 :     hestOptAdd (
354 :     &dopts->hopt, name, "x y z", airTypeDouble, 3, 3, odf->hval.r,
355 :     hasDflt ? buf : NULL, desc);
356 :     }
357 :    
358 :     /*! \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 :     char buf[AIR_STRLEN_HUGE] = "";
371 :    
372 :     if (hasDflt) {
373 :     Diderot_union4_t u;
374 :     u.v = *v;
375 : jhr 1671 snprintf(buf, sizeof(buf), "%lf %lf %lf %lf",
376 : jhr 1301 (double)(u.r[0]), (double)(u.r[1]),
377 :     (double)(u.r[2]), (double)(u.r[3]));
378 :     }
379 :     opt_def_t *odf = OptAdd(dopts, name, desc, (void*)v, optTypeReal4);
380 :     hestOptAdd (
381 :     &dopts->hopt, name, "x y z w", airTypeDouble, 4, 4, odf->hval.r,
382 :     hasDflt ? buf : NULL, desc);
383 :     }
384 :    
385 :     /*! \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 :     hestOptAdd (
399 :     &dopts->hopt, name, "str", airTypeString, 1, 1, &odf->hval.s,
400 :     hasDflt ? *v : NULL, desc);
401 :     }
402 :    
403 :     /*! 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 :    
409 :     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 :     airMopAdd(dopts->mop, dopts->hopt, (airMopper)hestOptFree, airMopAlways);
414 :     airMopAdd(dopts->mop, dopts->hopt, (airMopper)hestParseFree, airMopAlways);
415 :    
416 :     // convert values learned by hest to their Diderot representations
417 :     for (int i = 0; i < dopts->odefNum; i++) {
418 :     opt_def_t *odf = dopts->odef[i];
419 :     switch (odf->ty) {
420 :     case optTypeFlag:
421 :     case optTypeBool:
422 :     *(bool*)(odf->valp) = odf->hval.b ? true : false;
423 :     break;
424 :     case optTypeInt:
425 :     *(Diderot_int_t *)(odf->valp) = (Diderot_int_t)(odf->hval.i[0]);
426 :     break;
427 :     case optTypeReal:
428 :     *(Diderot_real_t *)(odf->valp) = (Diderot_real_t)(odf->hval.r[0]);
429 :     break;
430 :     case optTypeReal2:
431 :     *(Diderot_vec2_t *)(odf->valp) = vec2((Diderot_real_t)(odf->hval.r[0]),
432 :     (Diderot_real_t)(odf->hval.r[1]));
433 :     break;
434 :     case optTypeReal3:
435 :     *(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 :     break;
439 :     case optTypeReal4:
440 :     *(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 :     break;
445 :     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 :     } break;
453 :     default:
454 :     fprintf(stderr, "%s: unknown option type %d\n", me, odf->ty);
455 :     exit(1);
456 :     } /* switch */
457 :     }
458 :     }

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