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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1236 - (download) (as text) (annotate)
Tue May 17 22:09:19 2011 UTC (10 years, 4 months ago) by jhr
File size: 9043 byte(s)
  Added hest wrappers
/*! \file input.c
 *
 * \author John Reppy
 */

/*
 * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 */
#include "Diderot/diderot.h"
#include <teem/hest.h>

/* FIXME: eventally we should change the naming conventions in the header files to be generic */
#if defined(DIDEROT_SINGLE_PRECISION)
#define vec2(a,b)		vec2f(a,b)
#define vec3(a,b,c)		vec3f(a,b,c)
#define vec4(a,b,c,d)		vec4f(a,b,c,d)
#else
#define vec2(a,b)		vec2d(a,b)
#define vec3(a,b,c)		vec3d(a,b,c)
#define vec4(a,b,c,d)		vec4d(a,b,c,d)
#endif

Status_t Diderot_InputString (const char *name, const char **v, bool hasDflt)
{
    return DIDEROT_FAIL;
}

Status_t Diderot_InputReal (const char *name, Diderot_real_t *v, bool hasDflt)
{
    char	buf[256];
    double	f;

    while (true) {
	if (hasDflt)
	    printf("Enter value for %s (default %lf): ", name, (double)*v);
	else
	    printf("Enter value for %s: ", name);
	fflush (stdout);

	char *p = fgets(buf, sizeof(buf), stdin);
	if (p == NULL)
	    return DIDEROT_FAIL;	// EOF
	int n = sscanf(buf, "%lf\n", &f);
	if (n == 1) {
	    *v = (Diderot_real_t)f;
	    return DIDEROT_OK;;
	}
	else if (hasDflt)
	    return DIDEROT_OK;;
    }

}

Status_t Diderot_InputVec2 (const char *name, Diderot_vec2_t *v, bool hasDflt)
{
    char	buf[256];
    double	f1, f2;

    while (true) {
	if (hasDflt) {
	    Diderot_union2_t u;
	    u.v = *v;
	    printf("Enter value for %s (default %f %f): ",
		name, (double)(u.r[0]), (double)(u.r[1]));
	}
	else
	    printf("Enter value for %s: ", name);
	fflush (stdout);

	char *p = fgets(buf, sizeof(buf), stdin);
	if (p == NULL)
	    return DIDEROT_FAIL;	// EOF
	int n = sscanf(buf, "%lf %lf\n", &f1, &f2);
	if (n == 2) {
	    *v = vec2((Diderot_real_t)f1, (Diderot_real_t)f2);
	    return DIDEROT_OK;;
	}
	else if (hasDflt)
	    return DIDEROT_OK;;
    }

}

Status_t Diderot_InputVec3 (const char *name, Diderot_vec3_t *v, bool hasDflt)
{
    char	buf[256];
    double	f1, f2, f3;

    while (true) {
	if (hasDflt) {
	    Diderot_union3_t u;
	    u.v = *v;
	    printf("Enter value for %s (default %f %f %f): ",
		name, (double)(u.r[0]), (double)(u.r[1]), (double)(u.r[2]));
	}
	else
	    printf("Enter value for %s: ", name);
	fflush (stdout);

	char *p = fgets(buf, sizeof(buf), stdin);
	if (p == NULL)
	    return DIDEROT_FAIL;	// EOF
	int n = sscanf(buf, "%lf %lf %lf\n", &f1, &f2, &f3);
	if (n == 3) {
	    *v = vec3((Diderot_real_t)f1, (Diderot_real_t)f2, (Diderot_real_t)f3);
	    return DIDEROT_OK;;
	}
	else if (hasDflt)
	    return DIDEROT_OK;;
    }

}

typedef enum {
    OPT_FLAG,
    OPT_BOOL,
    OPT_INT,
    OPT_INT2,
    OPT_INT3,
    OPT_INT4,
    OPT_REAL,
    OPT_REAL2,
    OPT_REAL3,
    OPT_REAL4,
    OPT_STRING
} OptionTy_t;

typedef union {
    char	*name;	// option name
    OptionTy_t	ty;	// option type
    struct {		// storage that is passed into hest
	int	b;	    // storage for boolean-valued options
	double	r[4];	    // storage for real-valued options
	int64_t	i[4];	    // storage for integer-valued options
	char	*s;	    // storage for string-valued options
    } hestVal;
    void	*val;	// pointer to global that is being initialized.
} OptionDesc_t;

static hestOpt *Options = 0;
static OptionDesc_t *OptDescs;
static int	MaxNumOptions;
static int	NumOptions;

void Diderot_InitOptions ()
{
    MaxNumOptions = 8;  // initial size of OptDescs array
    NumOptions = 0;
    OptDescs = (OptionDesc_t *)malloc(MaxNumOptions * sizeof(OptionDesc_t));
    if (OptDescs == 0) {
	fprintf (stderr, "Diderot_InitOptions: unable to allocate memory\n");
	exit (1);
    }
}

static OptionDesc_t *OptAdd (const char *name, OptionTy_t ty)
{
    if (NumOptions == MaxNumOptions) {
      // grow the OptDescs array
	int newN = 2 * MaxNumOptions;
	OptionDesc_t *newDescs = (OptionDesc_t *)malloc(newN * sizeof(OptionDesc_t));
	if (newDescs == 0) {
	    fprintf (stderr, "AddOpt: unable to allocate memory\n");
	    exit (1);
	}
	memcpy (newDescs, OptDescs, MaxNumOptions * sizeof(OptionDesc_t));
	free (OptDescs);
	MaxNumOptions = newN;
    }

    OptionDesc_t *desc = &(OptDescs[NumOptions++]);
    desc->name = (char *)malloc(strlen(name)+1);
    if (desc->name == 0) {
	fprintf (stderr, "AddOpt: unable to allocate memory\n");
	exit (1);
    }
    strcpy (desc->name, name);
    desc->ty = ty;

    return desc;
}

void Diderot_OptAddFlag (char *name, bool *flg)
{
    OptionDesc_t *desc = OptAdd (name, OPT_FLAG);
    desc->val = flg;
    hestOptAdd (&Options, name, 0, airTypeBool, 0, 0, &(desc->hestVal.b), 0, 0);
}

void Diderot_OptAddBool (char *name, bool *v, bool hasDflt)
{
    char buf[32];
    char *dflt = 0;

    if (hasDflt) {
	sprintf (buf, "%s", *v ? "true" : "false");
    }
    OptionDesc_t *desc = OptAdd (name, OPT_BOOL);
    desc->val = v;
    hestOptAdd (&Options, name, 0, airTypeBool, 1, 1, &(desc->hestVal.b), dflt, 0);

}

void Diderot_OptAddInt (char *name, Diderot_int_t *v, bool hasDflt)
{
    char buf[32];
    char *dflt = 0;

    if (hasDflt) {
	sprintf (buf, "%ld", (long)*v);
    }
    OptionDesc_t *desc = OptAdd (name, OPT_INT);
    desc->val = v;
    hestOptAdd (&Options, name, 0, airTypeLongInt, 1, 1, desc->hestVal.i, dflt, 0);

}

void Diderot_OptAddReal (char *name, Diderot_real_t *v, bool hasDflt)
{
    char buf[32];
    char *dflt = 0;

    if (hasDflt) {
	sprintf (buf, "%lf", (double)*v);
    }
    OptionDesc_t *desc = OptAdd (name, OPT_REAL);
    desc->val = v;
    hestOptAdd (&Options, name, 0, airTypeDouble, 1, 1, desc->hestVal.r, dflt, 0);

}

void Diderot_OptAddReal2 (char *name, Diderot_vec2_t *v, bool hasDflt)
{
    char buf[32];
    char *dflt = 0;

    if (hasDflt) {
	Diderot_union2_t u;
	u.v = *v;
	sprintf (buf, "%lf %lf", (double)(u.r[0]), (double)(u.r[1]));
    }
    OptionDesc_t *desc = OptAdd (name, OPT_REAL2);
    desc->val = v;
    hestOptAdd (&Options, name, 0, airTypeDouble, 2, 2, desc->hestVal.r, dflt, 0);

}

void Diderot_OptAddReal3 (char *name, Diderot_vec3_t *v, bool hasDflt)
{
    char buf[64];
    char *dflt = 0;

    if (hasDflt) {
	Diderot_union3_t u;
	u.v = *v;
	sprintf (buf, "%lf %lf %lf",
	    (double)(u.r[0]), (double)(u.r[1]), (double)(u.r[2]));
    }
    OptionDesc_t *desc = OptAdd (name, OPT_REAL3);
    desc->val = v;
    hestOptAdd (&Options, name, 0, airTypeDouble, 3, 3, desc->hestVal.r, dflt, 0);

}

void Diderot_OptAddReal4 (char *name, Diderot_vec4_t *v, bool hasDflt)
{
    char buf[128];
    char *dflt = 0;

    if (hasDflt) {
	Diderot_union3_t u;
	u.v = *v;
	sprintf (buf, "%lf %lf %lf %lf",
	    (double)(u.r[0]), (double)(u.r[1]),
	    (double)(u.r[2]), (double)(u.r[3]));
    }
    OptionDesc_t *desc = OptAdd (name, OPT_REAL4);
    desc->val = v;
    hestOptAdd (&Options, name, 0, airTypeDouble, 4, 4, desc->hestVal.i, dflt, 0);

}

void Diderot_OptAddString (char *name, char **v, bool hasDflt)
{
    char *dflt = 0;

    if (hasDflt) {
	dflt = *v;
    }
    OptionDesc_t *desc = OptAdd (name, OPT_STRING);
    desc->val = v;
    hestOptAdd (&Options, name, 0, airTypeString, 1, 1, desc->hestVal.s, dflt, 0);

}

void Diderot_ProcessOptions (int argc, const char **argv)
{
    airArray *mop = airMopNew();
    hestParm *hparm = hestParmNew();
    hparm->noArgsIsNoProblem = AIR_TRUE;
    hestParseOrDie (
	Options, argc-1, argv+1, hparm,
	argv[0], 0, AIR_TRUE, AIR_TRUE, AIR_TRUE);
    airMopAdd (mop, hparm, (airMopper)hestParmFree, airMopAlways);
    airMopAdd (mop, Options, (airMopper)hestOptFree, airMopAlways);
    airMopAdd (mop, Options, (airMopper)hestParseFree, airMopAlways);

  // convert options to Diderot representation
    for (int i = 0;  i < NumOptions;  i++) {
	OptionDesc_t *desc = &(OptDescs[i]);
	switch (desc->ty) {
	  case OPT_FLAG:
	    break;
	  case OPT_BOOL:
	    *(bool*)(desc->val) = desc->hestVal.b ? true : false;
	    break;
	  case OPT_INT:
	    *(Diderot_int_t *)(desc->val) = (Diderot_int_t)(desc->hestVal.i[0]);
	    break;
//	  case OPT_INT2:
//	  case OPT_INT3:
//	  case OPT_INT4:
	  case OPT_REAL:
	    *(Diderot_real_t *)(desc->val) = (Diderot_real_t)(desc->hestVal.r[0]);
	    break;
	  case OPT_REAL2:
	    *(Diderot_vec2_t *)(desc->val) = vec2(
		(Diderot_real_t)(desc->hestVal.r[0]),
		(Diderot_real_t)(desc->hestVal.r[1]));
	  case OPT_REAL3:
	    *(Diderot_vec3_t *)(desc->val) = vec3(
		(Diderot_real_t)(desc->hestVal.r[0]),
		(Diderot_real_t)(desc->hestVal.r[1]),
		(Diderot_real_t)(desc->hestVal.r[2]));
	  case OPT_REAL4:
	    *(Diderot_vec4_t *)(desc->val) = vec4(
		(Diderot_real_t)(desc->hestVal.r[0]),
		(Diderot_real_t)(desc->hestVal.r[1]),
		(Diderot_real_t)(desc->hestVal.r[2]),
		(Diderot_real_t)(desc->hestVal.r[3]));
	    break;
	  case OPT_STRING: {
		char *s = (char *)malloc(strlen(desc->hestVal.s) + 1);
		if (s == 0) {
		    fprintf (stderr, "Diderot_ProcessOptions: unable to allocate memory\n");
		    exit (1);
		}
		strcpy (s, desc->hestVal.s);
		*(char **)(desc->val) = s;
	    }
	    break;
	  default:
	    fprintf (stderr, "Diderot_ProcessOptions: bogus option type\n");
	    exit (1);
	}
    }

    airMopOkay(mop);

}

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