(file) Return to smpl_02.c CVS log (file) (dir) Up to [Development] / JSOC / proj / cookbook

File: [Development] / JSOC / proj / cookbook / smpl_02.c (download)
Revision: 1.3, Thu Nov 3 23:30:47 2011 UTC (11 years, 1 month ago) by rick
Branch: MAIN
CVS Tags: Ver_LATEST, Ver_DRMSLATEST, Ver_9-5, Ver_9-41, Ver_9-4, Ver_9-3, Ver_9-2, Ver_9-1, Ver_9-0, Ver_8-8, Ver_8-7, Ver_8-6, Ver_8-5, Ver_8-4, Ver_8-3, Ver_8-2, Ver_8-12, Ver_8-11, Ver_8-10, Ver_8-1, Ver_8-0, Ver_7-1, Ver_7-0, Ver_6-4, Ver_6-3, Ver_6-2, Ver_6-1, Ver_6-0, NetDRMS_Ver_LATEST, NetDRMS_Ver_9-5, NetDRMS_Ver_9-41, NetDRMS_Ver_9-4, NetDRMS_Ver_9-3, NetDRMS_Ver_9-2, NetDRMS_Ver_9-1, NetDRMS_Ver_9-0, NetDRMS_Ver_8-8, NetDRMS_Ver_8-7, NetDRMS_Ver_8-6, NetDRMS_Ver_8-5, NetDRMS_Ver_8-4, NetDRMS_Ver_8-3, NetDRMS_Ver_8-2, NetDRMS_Ver_8-12, NetDRMS_Ver_8-11, NetDRMS_Ver_8-10, NetDRMS_Ver_8-1, NetDRMS_Ver_8-0, NetDRMS_Ver_7-1, NetDRMS_Ver_7-0, NetDRMS_Ver_6-4, NetDRMS_Ver_6-3, NetDRMS_Ver_6-2, NetDRMS_Ver_6-1, NetDRMS_Ver_6-0, HEAD
Changes since 1.2: +1 -1 lines
strdup return of cmdparams_get_str()

/*
 *  smpl_02.c						$DRMS/proj/cookbook/
 *
 *  Annother simple module that does nothing but echo back the values of
 *    arguments. It illustrates the use of command line parsing and the
 *    variety of argument types; also introduces time string functions
 *
 *  Usage:
 *    smpl_02 [-avVH]
 *
 *  Bugs:
 *    The module is of no particular use, and exists merely for heuristic
 *      and testing purposes.
 *
 *  Revision history is at end of file.
 */

#include <jsoc_main.h>
char *module_name = "CookbookRecipe:02";
char *version_id = "1.0";

ModuleArgs_t module_args[] = {
  {ARG_STRING,	"name",	"Not Specified", "a string"}, 
  {ARG_INT,	"ival",	"1", "a positive integer", "[1,)"},
  {ARG_INTS,	"iptr", "[0]", "array of integers"},
  {ARG_FLOAT,	"fval",	"0.0", "a real number"},
  {ARG_FLOAT,	"fvalns", "Unspecified", "a real number"},
  {ARG_FLOATS,	"fptr", "{2.71828, 3.14159, -1}", "array of real numbers"},
  {ARG_TIME,	"time", "1582.10.5_00:00:00",
      "a time, in standard date_time format"},
  {ARG_NUME, "colour", "", "enumerated choice without a default",
      "red, orange, yellow, green, blue, indigo, violet"},
  {ARG_NUME, "mois", "Brumaire", "enumerated choice with a default",
      "Vendémiaire, Brumaire, Frimaire, Nivôse, Pluviôse, Ventôse, Germinal, \
      Florial, Prairial, Messidor, Thermidor, Fructidor"},
  {ARG_FLAG,	"e",	"", "a flag value"}, 
  {ARG_END}
};

int DoIt (void) {
  CmdParams_t *params = &cmdparams;
  double *fpval;
  int *ipval;
  int i, fpvals, ipvals;
  char *colours[] = {"red", "orange", "yellow", "green", "blue", "violet"};
  char *moiss[] = {"Vendémiaire", "Brumaire", "Frimaire", "Nivôse", "Pluviôse",
      "Ventôse", "Germinal", "Florial", "Prairial", "Messidor", "Thermidor",
      "Fructidor"};
  char key[64], tbuf[64];

  TIME tval = params_get_time (params, "time");
  double fval = params_get_double (params, "fval");
  int ival = params_get_int (params, "ival");
  int colour = params_get_int (params, "colour");
  int mois = params_get_int (params, "mois");
  char *name = strdup (params_get_str (params, "name"));
  int flagset = params_isflagset (params, "e");

  printf ("name = %s\n", name);
  printf ("fval = %g\n", fval);
  printf ("ival = %d\n", ival);
  sprint_time (tbuf, tval, "UT", 3);
  printf ("time = %s\n", tbuf);
  printf ("colour = %d (%s)\n", colour, colours[colour]);
  printf ("mois = %d (%s)\n", mois, moiss[mois]);
  printf ("-e? : %d\n", flagset);

  printf ("fpvals = %d\n", fpvals = params_get_int (params, "fptr_nvals"));
  fpval = (double *)malloc (fpvals * sizeof (double));
  for (i = 0; i < fpvals; i++) {
    sprintf (key, "fptr_%d_value", i);
    fpval[i] = params_get_double (params, key);
    printf ("fpval[%d] = %g\n", i, fpval[i]);
  }
  printf ("ipvals = %d\n", ipvals = params_get_int (params, "iptr_nvals"));
  ipval = (int *)malloc (ipvals * sizeof (int));
  for (i = 0; i < ipvals; i++) {
    sprintf (key, "iptr_%d_value", i);
    ipval[i] = params_get_int (params, key);
    printf ("ipval[%d] = %d\n", i, ipval[i]);
  }

  return (0);
}

/*
 *  Revision History
 *
 *  07.02.27	created by RSB, based on original using SSSC API
 *  09.04.14	minor mods to illustrate additional features of arg parsing
 */

Karen Tian
Powered by
ViewCVS 0.9.4