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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1259, Tue May 24 19:36:35 2011 UTC revision 1260, Thu May 26 12:29:07 2011 UTC
# Line 125  Line 125 
125      optTypeString         // 8      optTypeString         // 8
126  } optType_t;  } optType_t;
127    
128    /*
129    ** optDef_t
130    **
131    ** the struct containing the definition of *one* option, and storage
132    ** for its value once parsed
133    */
134  typedef struct {  typedef struct {
135      char   *name;         // option name      char   *name;         // option name
136      char   *desc;         // option description (a phrase or sentence)      char   *desc;         // option description (a phrase or sentence)
# Line 138  Line 144 
144      void *valp;           // pointer to Diderot global variable being set      void *valp;           // pointer to Diderot global variable being set
145  } optDef_t;  } optDef_t;
146    
147    /*
148    ** struct Diderot_Options_s
149    **
150    ** container for *all* options, and the machinery for parsing them,
151    ** and means of managing their allocation.  There is an opaque typedef
152    ** around this in src/include/Diderot/options.h.
153    **
154    ** The "airArray *mop" is the primary means of managing the things that
155    ** are dynamically allocated; anything that is allocated is registered
156    ** in the mop so that it can be freed from a single place later (look
157    ** for calls to airMopAdd)
158    **
159    **  Note that "odef" is a dynamically re-allocated array (managed by
160    ** "odefArr"), and it is an array of optDef_t pointers, and not an
161    ** array of optDef_t's, which is necessary: the values that hest sets
162    ** are set via a pointer, and those values are stored in the optDef_t,
163    ** so the memory location of the optDef_t cannot change, so these are
164    ** allocated individually once.  If odef was an array of optDef_t's,
165    ** then the location of the individual optDef_t would change if odef
166    ** was reallocated for a different lenth.
167    */
168  struct Diderot_Options_s {  struct Diderot_Options_s {
169      char *progInfo;       // documentation string for describing whole program,      char *progInfo;       // documentation string for describing whole program,
170                            // intended to be one or more sentences                            // intended to be one or more sentences
# Line 149  Line 176 
176      airArray *mop;        // manages hopt, hparm, and odefArr      airArray *mop;        // manages hopt, hparm, and odefArr
177  };  };
178    
179    /*
180    ** optDefNew
181    **
182    ** allocate and initialize one optDef_t.  This doesn't need to be
183    ** called directly; it will be used by the airArray to manage "odef"
184    ** and what it points to.
185    */
186  static void *optDefNew () {  static void *optDefNew () {
187      optDef_t *odef = AIR_CALLOC(1, optDef_t);      optDef_t *odef = AIR_CALLOC(1, optDef_t);
188      odef->name = odef->desc = NULL;      odef->name = odef->desc = NULL;
# Line 158  Line 192 
192      return (void*)odef;      return (void*)odef;
193  }  }
194    
195    /*
196    ** optDefNix
197    **
198    ** free one optDef_t. This doesn't need to be called directly; it will
199    ** be used by the airArray to manage "odef" and what it points to.
200    */
201  static void *optDefNix (void *_odef) {  static void *optDefNix (void *_odef) {
202      optDef_t *odef = (optDef_t *)_odef;      optDef_t *odef = (optDef_t *)_odef;
203      airFree(odef->name);      airFree(odef->name);
# Line 169  Line 209 
209      return NULL;      return NULL;
210  }  }
211    
212    /*
213    ******** Diderot_Options_New
214    **
215    ** create the container for all option information
216    */
217  struct Diderot_Options_s *Diderot_Options_New (void) {  struct Diderot_Options_s *Diderot_Options_New (void) {
218      static const char me[]="Diderot_Options_New";      static const char me[]="Diderot_Options_New";
219      struct Diderot_Options_s *dopts = AIR_CALLOC(1, struct Diderot_Options_s);      struct Diderot_Options_s *dopts = AIR_CALLOC(1, struct Diderot_Options_s);
# Line 184  Line 229 
229      airMopAdd(dopts->mop, dopts->hparm, (airMopper)hestParmFree, airMopAlways);      airMopAdd(dopts->mop, dopts->hparm, (airMopper)hestParmFree, airMopAlways);
230      dopts->odef = NULL;      dopts->odef = NULL;
231      dopts->odefNum = 0;      dopts->odefNum = 0;
232        /*
233        ** the airArray odefArr manages the allocation of odef, and as a
234        ** convenience also sets odefNum to the number of values reliably
235        ** set in odef
236        */
237      dopts->odefArr = airArrayNew((void**)(&dopts->odef), &dopts->odefNum,      dopts->odefArr = airArrayNew((void**)(&dopts->odef), &dopts->odefNum,
238                                   sizeof(optDef_t*), 8 /* allocation increment */);                                   sizeof(optDef_t*), 8 /* allocation increment */);
239      if (!(dopts->odefArr)) {      if (!(dopts->odefArr)) {
240          fprintf (stderr, "%s: unable to allocate option array\n", me);          fprintf (stderr, "%s: unable to allocate option array\n", me);
241          exit (1);          exit (1);
242      }      }
243        /*
244        ** the airArray will set odef[I] = optDefNew for newly allocated
245        ** elements I of odef, and will call optDefNix(odef[I]) for
246        ** elements of odef that are about to be freed
247        */
248      airArrayPointerCB(dopts->odefArr, optDefNew, optDefNix);      airArrayPointerCB(dopts->odefArr, optDefNew, optDefNix);
249      airMopAdd(dopts->mop, dopts->odefArr, (airMopper)airArrayNuke, airMopAlways);      airMopAdd(dopts->mop, dopts->odefArr, (airMopper)airArrayNuke, airMopAlways);
250    
251      return dopts;      return dopts;
252  }  }
253    
254    /*
255    ******** Diderot_Options_Nix
256    **
257    ** free everything associated with a struct Diderot_Options_s
258    */
259  void Diderot_Options_Nix (struct Diderot_Options_s *dopts) {  void Diderot_Options_Nix (struct Diderot_Options_s *dopts) {
260      if (dopts) {      if (dopts) {
261          airFree(dopts->progInfo);          airFree(dopts->progInfo);
262            /* frees everything connected to the mop, and the mop itself */
263          airMopOkay(dopts->mop);          airMopOkay(dopts->mop);
264          free(dopts);          free(dopts);
265      }      }
266      return;      return;
267  }  }
268    
269    /*
270    ******** Diderot_OptProgramInfoSet
271    **
272    ** sets the one piece of information in the struct Diderot_Options_s
273    ** which is not really for the options; "progInfo" is to describe the
274    ** whole program.
275    */
276  void Diderot_OptProgramInfoSet (struct Diderot_Options_s *dopts,  void Diderot_OptProgramInfoSet (struct Diderot_Options_s *dopts,
277                                  const char *progInfo) {                                  const char *progInfo) {
278      static const char me[]="Diderot_OptProgramInfoSet";      static const char me[]="Diderot_OptProgramInfoSet";
# Line 217  Line 285 
285      }      }
286  }  }
287    
288    /*
289    ** optAdd
290    **
291    ** main function for adding options to a struct Diderot_Options_s.
292    */
293  static optDef_t *optAdd (struct Diderot_Options_s *dopts,  static optDef_t *optAdd (struct Diderot_Options_s *dopts,
294                           const char *name, const char *desc,                           const char *name, const char *desc,
295                           void *valp, optType_t ty) {                           void *valp, optType_t ty) {
# Line 228  Line 301 
301                   me, name, desc);                   me, name, desc);
302          exit (1);          exit (1);
303      }      }
304        /* calling airArrayLenIncr may trigger re-allocation of odef;
305           nidx is the index of the newly reserved element, odef[nidx] */
306      nidx = airArrayLenIncr(dopts->odefArr, 1);      nidx = airArrayLenIncr(dopts->odefArr, 1);
307      if (!dopts->odef) {      if (!dopts->odef) {
308          fprintf (stderr, "%s: unable to reallocate option array\n", me);          fprintf (stderr, "%s: unable to reallocate option array\n", me);
309          exit (1);          exit (1);
310      }      }
311      optDef_t *odef = dopts->odef[nidx];      /* code here uses "odf" to copy one value of odef[] */
312      odef->name = airStrdup(name);      optDef_t *odf = dopts->odef[nidx];
313      odef->desc = airStrdup(desc);      odf->name = airStrdup(name);
314      if (!(odef->name && odef->desc)) {      odf->desc = airStrdup(desc);
315        if (!(odf->name && odf->desc)) {
316          fprintf (stderr, "%s: unable to allocate strings (%p,%p)\n",          fprintf (stderr, "%s: unable to allocate strings (%p,%p)\n",
317                   me, odef->name, odef->desc);                  me, odf->name, odf->desc);
318          exit (1);          exit (1);
319      }      }
320      odef->valp = valp;      odf->valp = valp;
321      odef->ty = ty;      odf->ty = ty;
322    
323      return odef;      return odf;
324  }  }
325    
326  void Diderot_OptAddFlag (struct Diderot_Options_s *dopts,  void Diderot_OptAddFlag (struct Diderot_Options_s *dopts,
327                           const char *name, const char *desc,                           const char *name, const char *desc,
328                           bool *flg) {                           bool *flg) {
329      optDef_t *odef = optAdd (dopts, name, desc, (void*)flg, optTypeFlag);      optDef_t *odf = optAdd(dopts, name, desc, (void*)flg, optTypeFlag);
330      hestOptAdd (&dopts->hopt, name, NULL, airTypeInt, 0, 0, &odef->hval.b,      hestOptAdd(&dopts->hopt, name, NULL, airTypeInt, 0, 0, &odf->hval.b,
331                  NULL, desc);                  NULL, desc);
332  }  }
333    
334  void Diderot_OptAddBool (struct Diderot_Options_s *dopts,  void Diderot_OptAddBool (struct Diderot_Options_s *dopts,
335                           const char *name, const char *desc,                           const char *name, const char *desc,
336                           bool *v, bool hasDflt) {                           bool *v, bool hasDflt) {
337      optDef_t *odef = optAdd (dopts, name, desc, (void*)v, optTypeBool);      optDef_t *odf = optAdd(dopts, name, desc, (void*)v, optTypeBool);
338      hestOptAdd (&dopts->hopt, name, "bool", airTypeBool, 1, 1, &odef->hval.b,      hestOptAdd(&dopts->hopt, name, "bool", airTypeBool, 1, 1, &odf->hval.b,
339                  hasDflt ? (*v ? "true" : "false") : NULL, desc);                  hasDflt ? (*v ? "true" : "false") : NULL, desc);
340  }  }
341    
342    /*
343    ** On the use of AIR_STRLEN_HUGE below: default values for
344    ** command-line options are communicated to hest by a *string*, but
345    ** the actual default value comes to us in Diderot global.  So these
346    ** functions have to convert the value from the Diderot variable into
347    ** a string.
348    **
349    ** The problem is that we don't know how long string will have to be,
350    ** and there are few good functions for dealing with this.  snprintf
351    ** is good for making sure you don't over-write a buffer of fixed
352    ** size, but you still have to allocate the buffer.  asprintf allocates
353    ** as needed, but GLK didn't want to assume it was available (perhaps
354    ** this should be revisited)
355    **
356    ** So instead, we just use a huge buffer, and assume that it will be
357    ** more than big enough.  Feel free to fix this.
358    */
359    
360  void Diderot_OptAddInt (struct Diderot_Options_s *dopts,  void Diderot_OptAddInt (struct Diderot_Options_s *dopts,
361                          const char *name, const char *desc,                          const char *name, const char *desc,
362                          Diderot_int_t *v, bool hasDflt) {                          Diderot_int_t *v, bool hasDflt) {
# Line 270  Line 364 
364      if (hasDflt) {      if (hasDflt) {
365          sprintf (buf, "%ld", (long)*v);          sprintf (buf, "%ld", (long)*v);
366      }      }
367      optDef_t *odef = optAdd (dopts, name, desc, (void*)v, optTypeInt);      optDef_t *odf = optAdd(dopts, name, desc, (void*)v, optTypeInt);
368      hestOptAdd (&dopts->hopt, name, "int", airTypeLongInt, 1, 1, odef->hval.i,      hestOptAdd(&dopts->hopt, name, "int", airTypeLongInt, 1, 1, odf->hval.i,
369                  hasDflt ? buf : NULL, desc);                  hasDflt ? buf : NULL, desc);
370  }  }
371    
# Line 282  Line 376 
376      if (hasDflt) {      if (hasDflt) {
377          sprintf (buf, "%lf", (double)*v);          sprintf (buf, "%lf", (double)*v);
378      }      }
379      optDef_t *odef = optAdd (dopts, name, desc, (void*)v, optTypeReal);      optDef_t *odf = optAdd(dopts, name, desc, (void*)v, optTypeReal);
380      hestOptAdd (&dopts->hopt, name, "val", airTypeDouble, 1, 1, odef->hval.r,      hestOptAdd(&dopts->hopt, name, "val", airTypeDouble, 1, 1, odf->hval.r,
381                  hasDflt ? buf : NULL, desc);                  hasDflt ? buf : NULL, desc);
382  }  }
383    
# Line 297  Line 391 
391          u.v = *v;          u.v = *v;
392          sprintf (buf, "%lf %lf", (double)(u.r[0]), (double)(u.r[1]));          sprintf (buf, "%lf %lf", (double)(u.r[0]), (double)(u.r[1]));
393      }      }
394      optDef_t *odef = optAdd (dopts, name, desc, (void*)v, optTypeReal2);      optDef_t *odf = optAdd(dopts, name, desc, (void*)v, optTypeReal2);
395      hestOptAdd (&dopts->hopt, name, "x y", airTypeDouble, 2, 2, odef->hval.r,      hestOptAdd(&dopts->hopt, name, "x y", airTypeDouble, 2, 2, odf->hval.r,
396                  hasDflt ? buf : NULL, desc);                  hasDflt ? buf : NULL, desc);
397  }  }
398    
# Line 313  Line 407 
407          sprintf (buf, "%lf %lf %lf",          sprintf (buf, "%lf %lf %lf",
408                   (double)(u.r[0]), (double)(u.r[1]), (double)(u.r[2]));                   (double)(u.r[0]), (double)(u.r[1]), (double)(u.r[2]));
409      }      }
410      optDef_t *odef = optAdd (dopts, name, desc, (void*)v, optTypeReal3);      optDef_t *odf = optAdd(dopts, name, desc, (void*)v, optTypeReal3);
411      hestOptAdd (&dopts->hopt, name, "x y z", airTypeDouble, 3, 3, odef->hval.r,      hestOptAdd(&dopts->hopt, name, "x y z", airTypeDouble, 3, 3, odf->hval.r,
412                  hasDflt ? buf : NULL, desc);                  hasDflt ? buf : NULL, desc);
413  }  }
414    
# Line 330  Line 424 
424                   (double)(u.r[0]), (double)(u.r[1]),                   (double)(u.r[0]), (double)(u.r[1]),
425                   (double)(u.r[2]), (double)(u.r[3]));                   (double)(u.r[2]), (double)(u.r[3]));
426      }      }
427      optDef_t *odef = optAdd (dopts, name, desc, (void*)v, optTypeReal4);      optDef_t *odf = optAdd(dopts, name, desc, (void*)v, optTypeReal4);
428      hestOptAdd (&dopts->hopt, name, "x y z w", airTypeDouble, 4, 4, odef->hval.r,      hestOptAdd(&dopts->hopt, name, "x y z w", airTypeDouble, 4, 4, odf->hval.r,
429                  hasDflt ? buf : NULL, desc);                  hasDflt ? buf : NULL, desc);
430  }  }
431    
432  void Diderot_OptAddString (struct Diderot_Options_s *dopts,  void Diderot_OptAddString (struct Diderot_Options_s *dopts,
433                             const char *name, const char *desc,                             const char *name, const char *desc,
434                             char **v, bool hasDflt) {                             char **v, bool hasDflt) {
435      optDef_t *odef = optAdd (dopts, name, desc, (void*)v, optTypeString);      optDef_t *odf = optAdd(dopts, name, desc, (void*)v, optTypeString);
436      hestOptAdd (&dopts->hopt, name, "str", airTypeString, 1, 1, &odef->hval.s,      hestOptAdd(&dopts->hopt, name, "str", airTypeString, 1, 1, &odf->hval.s,
437                  hasDflt ? *v : NULL, desc);                  hasDflt ? *v : NULL, desc);
438  }  }
439    
440    /*
441    ******** Diderot_ProcessOptions
442    **
443    ** given argc/argv, learn values for all options and set them
444    */
445  void Diderot_ProcessOptions (struct Diderot_Options_s *dopts,  void Diderot_ProcessOptions (struct Diderot_Options_s *dopts,
446                               int argc, const char **argv) {                               int argc, const char **argv) {
447      static const char me[]="Diderot_ProcessOptions";      static const char me[]="Diderot_ProcessOptions";
# Line 355  Line 454 
454    
455      // convert values learned by hest to their Diderot representations      // convert values learned by hest to their Diderot representations
456      for (int i = 0;  i < dopts->odefNum;  i++) {      for (int i = 0;  i < dopts->odefNum;  i++) {
457          optDef_t *odef = dopts->odef[i];          optDef_t *odf = dopts->odef[i];
458          switch (odef->ty) {          switch (odf->ty) {
459          case optTypeFlag:          case optTypeFlag:
460          case optTypeBool:          case optTypeBool:
461              *(bool*)(odef->valp) = odef->hval.b ? true : false;              *(bool*)(odf->valp) = odf->hval.b ? true : false;
462              break;              break;
463          case optTypeInt:          case optTypeInt:
464              *(Diderot_int_t *)(odef->valp) = (Diderot_int_t)(odef->hval.i[0]);              *(Diderot_int_t *)(odf->valp) = (Diderot_int_t)(odf->hval.i[0]);
465              break;              break;
466          case optTypeReal:          case optTypeReal:
467              *(Diderot_real_t *)(odef->valp) = (Diderot_real_t)(odef->hval.r[0]);              *(Diderot_real_t *)(odf->valp) = (Diderot_real_t)(odf->hval.r[0]);
468              break;              break;
469          case optTypeReal2:          case optTypeReal2:
470              *(Diderot_vec2_t *)(odef->valp) = vec2((Diderot_real_t)(odef->hval.r[0]),              *(Diderot_vec2_t *)(odf->valp) = vec2((Diderot_real_t)(odf->hval.r[0]),
471                                                     (Diderot_real_t)(odef->hval.r[1]));                                                    (Diderot_real_t)(odf->hval.r[1]));
472              break;              break;
473          case optTypeReal3:          case optTypeReal3:
474              *(Diderot_vec3_t *)(odef->valp) = vec3((Diderot_real_t)(odef->hval.r[0]),              *(Diderot_vec3_t *)(odf->valp) = vec3((Diderot_real_t)(odf->hval.r[0]),
475                                                     (Diderot_real_t)(odef->hval.r[1]),                                                    (Diderot_real_t)(odf->hval.r[1]),
476                                                     (Diderot_real_t)(odef->hval.r[2]));                                                    (Diderot_real_t)(odf->hval.r[2]));
477              break;              break;
478          case optTypeReal4:          case optTypeReal4:
479              *(Diderot_vec4_t *)(odef->valp) = vec4((Diderot_real_t)(odef->hval.r[0]),              *(Diderot_vec4_t *)(odf->valp) = vec4((Diderot_real_t)(odf->hval.r[0]),
480                                                     (Diderot_real_t)(odef->hval.r[1]),                                                    (Diderot_real_t)(odf->hval.r[1]),
481                                                     (Diderot_real_t)(odef->hval.r[2]),                                                    (Diderot_real_t)(odf->hval.r[2]),
482                                                     (Diderot_real_t)(odef->hval.r[3]));                                                    (Diderot_real_t)(odf->hval.r[3]));
483              break;              break;
484          case optTypeString:          case optTypeString:
485              if (!( *(char **)(odef->valp) = airStrdup(odef->hval.s) )) {              if (!( *(char **)(odf->valp) = airStrdup(odf->hval.s) )) {
486                  fprintf(stderr, "%s: unable to allocate string copy\n", me);                  fprintf(stderr, "%s: unable to allocate string copy\n", me);
487                  exit (1);                  exit (1);
488              }              }
489              break;              break;
490          default:          default:
491              fprintf (stderr, "%s: unknown option type %d\n", me, odef->ty);              fprintf(stderr, "%s: unknown option type %d\n", me, odf->ty);
492              exit (1);              exit (1);
493          }          }
494      }      }

Legend:
Removed from v.1259  
changed lines
  Added in v.1260

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