示例#1
0
/* generate C to put netCDF record from in-memory data */
static void
gen_load_c(
    void *rec_start
    )
{
    int  idim, ival;
    char *val_string;
    char *charvalp = NULL;
    short *shortvalp = NULL;
    int *intvalp = NULL;
    float *floatvalp = NULL;
    double *doublevalp = NULL;
    unsigned char *ubytevalp = NULL;
    unsigned short *ushortvalp = NULL;
    unsigned int *uintvalp = NULL;
    long long *int64valp = NULL;
    unsigned long long *uint64valp = NULL;
    char stmnt[C_MAX_STMNT];
    size_t stmnt_len;
    char s2[C_MAX_STMNT];

    if (!vars[varnum].has_data)
	return;

    s2[0] = '\0';
    cline("");
    sprintf(stmnt, "   {\t\t\t/* store %s */", vars[varnum].name);
    cline(stmnt);

    if (vars[varnum].ndims > 0) {
	if (vars[varnum].dims[0] == rec_dim) {
	    sprintf(stmnt, "    static MPI_Offset %s_start[RANK_%s];",
		    vars[varnum].lname, vars[varnum].lname);
	    cline(stmnt);

	    sprintf(stmnt, "    static MPI_Offset %s_count[RANK_%s];",
		    vars[varnum].lname, vars[varnum].lname);
	    cline(stmnt);
	}

	/* load variable with data values using static initialization */
	sprintf(stmnt, "    static %s %s[] = {",
		ncctype(vars[varnum].type),
		vars[varnum].lname);

	stmnt_len = strlen(stmnt);
	switch (vars[varnum].type) {
	  case NC_CHAR:
	    val_string = cstrstr((char *) rec_start, var_len);
	    sprintf(s2, "%s", val_string);
	    strncat(stmnt, s2, C_MAX_STMNT - strlen(stmnt) );
	    free(val_string);
	    break;
	  default:
	    switch (vars[varnum].type) {
	      case NC_BYTE:
		charvalp = (char *) rec_start;
		break;
	      case NC_SHORT:
		shortvalp = (short *) rec_start;
		break;
	      case NC_INT:
		intvalp = (int *) rec_start;
		break;
	      case NC_FLOAT:
		floatvalp = (float *) rec_start;
		break;
	      case NC_DOUBLE:
		doublevalp = (double *) rec_start;
		break;
	      case NC_UBYTE:
		ubytevalp = (unsigned char *) rec_start;
		break;
	      case NC_USHORT:
		ushortvalp = (unsigned short *) rec_start;
		break;
	      case NC_UINT:
		uintvalp = (unsigned int *) rec_start;
		break;
	      case NC_INT64:
		int64valp = (long long *) rec_start;
		break;
	      case NC_UINT64:
		uint64valp = (unsigned long long *) rec_start;
		break;
	      default:
		derror("Unhandled type %d\n", vars[varnum].type);
		return;
	    }
            for (ival = 0; ival < var_len-1; ival++) {
		switch (vars[varnum].type) {
		  case NC_BYTE:
			sprintf(s2, "%d, ", *charvalp++);
		    break;
		  case NC_SHORT:
			sprintf(s2, "%d, ", *shortvalp++);
		    break;
		  case NC_INT:
			sprintf(s2, "%ld, ", (long)*intvalp++);
		    break;
		  case NC_FLOAT:
			sprintf(s2, "%.8g, ", *floatvalp++);
		    break;
		  case NC_DOUBLE:
			sprintf(s2, "%#.16g", *doublevalp++);
			tztrim(s2);
			strcat(s2, ", ");
		    break;
		  case NC_UBYTE:
			sprintf(s2, "%hhu, ", (unsigned char)*ubytevalp++);
		    break;
		  case NC_USHORT:
			sprintf(s2, "%hu, ", (unsigned short)*ushortvalp++);
		    break;
		  case NC_UINT:
			sprintf(s2, "%u, ", (unsigned int)*uintvalp++);
		    break;
		  case NC_INT64:
			sprintf(s2, "%lld, ", (long long)*int64valp++);
		    break;
		  case NC_UINT64:
			sprintf(s2, "%llu, ", (unsigned long long)*uint64valp++);
		    break;
		  default:
		    derror("Unhandled type %d\n", vars[varnum].type);
		    return;

		}
		stmnt_len += strlen(s2);
		if (stmnt_len < C_MAX_STMNT)
		  strcat(stmnt, s2);
		else {
		    cline(stmnt);
		    strcpy(stmnt,s2);
		    stmnt_len = strlen(stmnt);
		}
	    }
	    for (;ival < var_len; ival++) {
		switch (vars[varnum].type) {
		  case NC_BYTE:
			sprintf(s2, "%d", *charvalp);
		    break;
		  case NC_SHORT:
			sprintf(s2, "%d", *shortvalp);
		    break;
		  case NC_INT:
			sprintf(s2, "%ld", (long)*intvalp);
		    break;
		  case NC_FLOAT:
			sprintf(s2, "%.8g", *floatvalp);
		    break;
		  case NC_DOUBLE:
			sprintf(s2, "%#.16g", *doublevalp++);
			tztrim(s2);
		    break;
		  case NC_UBYTE:
			sprintf(s2, "%hhu, ", (unsigned char)*ubytevalp++);
		    break;
		  case NC_USHORT:
			sprintf(s2, "%hu, ", (unsigned short)*ushortvalp++);
		    break;
		  case NC_UINT:
			sprintf(s2, "%u, ", (unsigned int)*uintvalp++);
		    break;
		  case NC_INT64:
			sprintf(s2, "%lld, ", (long long)*int64valp++);
		    break;
		  case NC_UINT64:
			sprintf(s2, "%llu, ", (unsigned long long)*uint64valp++);
		    break;
		  default:
		    derror("Unhandled type %d\n", vars[varnum].type);
		    break;
		}
		stmnt_len += strlen(s2);
		if (stmnt_len < C_MAX_STMNT)
		  strcat(stmnt, s2);
		else {
		    cline(stmnt);
		    strcpy(stmnt,s2);
		    stmnt_len = strlen(stmnt);
		}
	    }
	    break;
	}
	strcat(stmnt,"};");
	cline(stmnt);

	if (vars[varnum].dims[0] == rec_dim) {
	    sprintf(stmnt,
		    "    %s_len = %lu;			/* number of records of %s data */",
		    dims[rec_dim].lname,
		    (unsigned long)vars[varnum].nrecs, /* number of recs for this variable */
		    vars[varnum].name);
	    cline(stmnt);

	    for (idim = 0; idim < vars[varnum].ndims; idim++) {
		sprintf(stmnt, "    %s_start[%d] = 0;",
			vars[varnum].lname,
			idim);
		cline(stmnt);
	    }

	    for (idim = 0; idim < vars[varnum].ndims; idim++) {
		sprintf(stmnt, "    %s_count[%d] = %s_len;",
			vars[varnum].lname,
			idim,
			dims[vars[varnum].dims[idim]].lname);
		cline(stmnt);
	    }
	}

	if (vars[varnum].dims[0] == rec_dim) {
	    sprintf(stmnt,
		    "    stat = ncmpi_put_vara_%s_all(ncid, %s_id, %s_start, %s_count, %s);",
		    ncstype(vars[varnum].type),
		    vars[varnum].lname,
		    vars[varnum].lname,
		    vars[varnum].lname,
		    vars[varnum].lname);
	     cline(stmnt);
	} else {		/* non-record variables */
	    cline("  ncmpi_begin_indep_data(ncid);");
	    sprintf(stmnt,
		    "    stat = ncmpi_put_var_%s(ncid, %s_id, %s);",
		    ncstype(vars[varnum].type),
		    vars[varnum].lname,
		    vars[varnum].lname);
	    cline(stmnt);
	    cline("  ncmpi_end_indep_data(ncid);");
	}
    } else {			/* scalar variables */
	/* load variable with data values using static initialization */
	sprintf(stmnt, "    static %s %s = ",
		ncctype(vars[varnum].type),
		vars[varnum].lname);

	switch (vars[varnum].type) {
	  case NC_CHAR:
	    val_string = cstrstr((char *) rec_start, var_len);
	    val_string[strlen(val_string)-1] = '\0';
	    sprintf(s2, "'%s'", &val_string[1]);
	    free(val_string);
	    break;
	  case NC_BYTE:
	    charvalp = (char *) rec_start;
	    sprintf(s2, "%d", *charvalp);
	    break;
	  case NC_SHORT:
	    shortvalp = (short *) rec_start;
	    sprintf(s2, "%d", *shortvalp);
	    break;
	  case NC_INT:
	    intvalp = (int *) rec_start;
	    sprintf(s2, "%ld", (long)*intvalp);
	    break;
	  case NC_FLOAT:
	    floatvalp = (float *) rec_start;
	    sprintf(s2, "%.8g", *floatvalp);
	    break;
	  case NC_DOUBLE:
	    doublevalp = (double *) rec_start;
	    sprintf(s2, "%#.16g", *doublevalp++);
	    tztrim(s2);
	    break;
	  case NC_UBYTE:
	    ubytevalp = (unsigned char *) rec_start;
	    sprintf(s2, "%hhu", (unsigned char)*ubytevalp);
	    break;
	  case NC_USHORT:
	    ushortvalp = (unsigned short *) rec_start;
	    sprintf(s2, "%hu", (unsigned short)*ushortvalp);
	    break;
	  case NC_UINT:
	    uintvalp = (unsigned int *) rec_start;
	    sprintf(s2, "%u", (unsigned int)*uintvalp);
	    break;
	  case NC_INT64:
	    int64valp = (long long *) rec_start;
	    sprintf(s2, "%lld", (long long)*int64valp);
	    break;
	  case NC_UINT64:
	    uint64valp = (unsigned long long *) rec_start;
	    sprintf(s2, "%llu", (unsigned long long)*uint64valp);
	    break;
	  default:
	    derror("Unhandled type %d\n", vars[varnum].type);
	    break;
	}
	strncat(stmnt, s2, C_MAX_STMNT - strlen(stmnt) );
	strcat(stmnt,";");
	cline(stmnt);
	cline("  ncmpi_begin_indep_data(ncid);");
	sprintf(stmnt,
		"    stat = ncmpi_put_var_%s(ncid, %s_id, &%s);",
		ncstype(vars[varnum].type),
		vars[varnum].lname,
		vars[varnum].lname);
	cline(stmnt);
	cline("  ncmpi_end_indep_data(ncid);");
    }
    cline("    check_err(stat,__LINE__,__FILE__);");
    cline("   }");
}
示例#2
0
/*
 * Create Fortran data statement to initialize numeric variable with
 * values.
 */
static void
f_var_init(
    int varnum,			/* which variable */
    void *rec_start		/* start of data */
    )
{
    char *val_string;
    char *charvalp;
    short *shortvalp;
    int *intvalp;
    float *floatvalp;
    double *doublevalp;
    unsigned char *ubytevalp;
    unsigned short *ushortvalp;
    unsigned int *uintvalp;
    long long *int64valp;
    unsigned long long *uint64valp;
    char stmnt[FORT_MAX_STMNT];
    size_t stmnt_len;
    char s2[FORT_MAX_STMNT];
    int ival;

    /* load variable with data values  */
    sprintf(stmnt, "data %s /",vars[varnum].lname);
    stmnt_len = strlen(stmnt);
    switch (vars[varnum].type) {
    case NC_BYTE:
	charvalp = (char *) rec_start;
	for (ival = 0; ival < var_len-1; ival++) {
	    val_string = fstring(NC_BYTE,(void *)charvalp++,0);
	    sprintf(s2, "%s, ", val_string);
	    fstrcat(stmnt, s2, &stmnt_len);
	    free(val_string);
	}
	val_string = fstring(NC_BYTE,(void *)charvalp++,0);
	fstrcat(stmnt, val_string, &stmnt_len);
	free(val_string);
	break;
    case NC_SHORT:
	shortvalp = (short *) rec_start;
	for (ival = 0; ival < var_len-1; ival++) {
	    sprintf(s2, "%d, ", *shortvalp++);
	    fstrcat(stmnt, s2, &stmnt_len);
	}
	sprintf(s2, "%d", *shortvalp);
	fstrcat(stmnt, s2, &stmnt_len);
	break;
    case NC_INT:
	intvalp = (int *) rec_start;
	for (ival = 0; ival < var_len-1; ival++) {
	    sprintf(s2, "%ld, ", (long)*intvalp++);
	    fstrcat(stmnt, s2, &stmnt_len);
	}
	sprintf(s2, "%ld", (long)*intvalp);
	fstrcat(stmnt, s2, &stmnt_len);
	break;
    case NC_FLOAT:
	floatvalp = (float *) rec_start;
	for (ival = 0; ival < var_len-1; ival++) {
	    sprintf(s2, "%.8g, ", *floatvalp++);
	    fstrcat(stmnt, s2, &stmnt_len);
	}
	sprintf(s2, "%.8g", *floatvalp);
	fstrcat(stmnt, s2, &stmnt_len);
	break;
    case NC_DOUBLE:
	doublevalp = (double *) rec_start;
	for (ival = 0; ival < var_len-1; ival++) {
	    sprintf(s2, "%#.16g", *doublevalp++);
	    tztrim(s2);
	    expe2d(s2);	/* change 'e' to 'd' in exponent */
	    fstrcat(s2, ", ", &stmnt_len);
	    fstrcat(stmnt, s2, &stmnt_len);
	}
	sprintf(s2, "%#.16g", *doublevalp++);
	tztrim(s2);
	expe2d(s2);
	fstrcat(stmnt, s2, &stmnt_len);
	break;
    case NC_UBYTE:
	ubytevalp = (unsigned char *) rec_start;
	for (ival = 0; ival < var_len-1; ival++) {
	    sprintf(s2, "%hhu, ", (unsigned char)*ubytevalp++);
	    fstrcat(stmnt, s2, &stmnt_len);
	}
	sprintf(s2, "%hhu", (unsigned char)*ubytevalp);
	fstrcat(stmnt, s2, &stmnt_len);
	break;
    case NC_USHORT:
	ushortvalp = (unsigned short *) rec_start;
	for (ival = 0; ival < var_len-1; ival++) {
	    sprintf(s2, "%hu, ", (unsigned short)*ushortvalp++);
	    fstrcat(stmnt, s2, &stmnt_len);
	}
	sprintf(s2, "%hu", (unsigned short)*ushortvalp);
	fstrcat(stmnt, s2, &stmnt_len);
	break;
    case NC_UINT:
	uintvalp = (unsigned int *) rec_start;
	for (ival = 0; ival < var_len-1; ival++) {
	    sprintf(s2, "%u, ", (unsigned int)*uintvalp++);
	    fstrcat(stmnt, s2, &stmnt_len);
	}
	sprintf(s2, "%u", (unsigned int)*uintvalp);
	fstrcat(stmnt, s2, &stmnt_len);
	break;
    case NC_INT64:
	int64valp = (long long *) rec_start;
	for (ival = 0; ival < var_len-1; ival++) {
	    sprintf(s2, "%lld, ", (long long)*int64valp++);
	    fstrcat(stmnt, s2, &stmnt_len);
	}
	sprintf(s2, "%lld", (long long)*int64valp);
	fstrcat(stmnt, s2, &stmnt_len);
	break;
    case NC_UINT64:
	uint64valp = (unsigned long long *) rec_start;
	for (ival = 0; ival < var_len-1; ival++) {
	    sprintf(s2, "%llu, ", (unsigned long long)*uint64valp++);
	    fstrcat(stmnt, s2, &stmnt_len);
	}
	sprintf(s2, "%llu", (unsigned long long)*uint64valp);
	fstrcat(stmnt, s2, &stmnt_len);
	break;
    default:
	derror("fstrstr: bad type");
	break;
    }
    fstrcat(stmnt, "/", &stmnt_len);

    /* For record variables, store data statement for later use;
      otherwise, just print it. */
    if (vars[varnum].ndims > 0 && vars[varnum].dims[0] == rec_dim) {
	char *dup_stmnt = (char*) emalloc(strlen(stmnt)+1);
	strcpy(dup_stmnt, stmnt); /* ULTRIX missing strdup */
	vars[varnum].data_stmnt = dup_stmnt;
    } else {
	fline(stmnt);
    }
}
示例#3
0
文件: datamap.cpp 项目: crazii/mameui
static void read_control(datamap *map, HWND control, windows_options *opts, datamap_entry *entry, const char *option_name)
{
	BOOL bool_value = 0;
	int int_value = 0;
	float float_value = 0;
	const char *string_value;
	int selected_index = 0;
	int trackbar_pos = 0;
	std::string error;
	// use default read value behavior
	switch(get_control_type(control))
	{
		case CT_BUTTON:
			assert(entry->type == DM_BOOL);
			bool_value = Button_GetCheck(control);
			opts->set_value(option_name, bool_value, OPTION_PRIORITY_CMDLINE,error);
			break;

		case CT_COMBOBOX:
			selected_index = ComboBox_GetCurSel(control);
			if (selected_index >= 0)
			{
				switch(entry->type)
				{
					case DM_INT:
						int_value = (int) ComboBox_GetItemData(control, selected_index);
						opts->set_value(option_name, int_value, OPTION_PRIORITY_CMDLINE,error);
						break;

					case DM_STRING:
						string_value = (const char *) ComboBox_GetItemData(control, selected_index);
						opts->set_value(option_name, string_value ? string_value : "", OPTION_PRIORITY_CMDLINE,error);
						break;

					default:
						break;
				}
			}
			break;

		case CT_TRACKBAR:
			trackbar_pos = SendMessage(control, TBM_GETPOS, 0, 0);
			float_value = trackbar_value_from_position(entry, trackbar_pos);
			switch(entry->type)
			{
				case DM_INT:
					int_value = (int) float_value;
					if (int_value != opts->int_value(option_name)) {
						opts->set_value(option_name, int_value, OPTION_PRIORITY_CMDLINE,error);
					}
					break;

				case DM_FLOAT:
					// Use tztrim(float_value) or we get trailing zero's that break options_equal().
					if (float_value != opts->float_value(option_name)) {
						opts->set_value(option_name, tztrim(float_value), OPTION_PRIORITY_CMDLINE,error);
					}
					break;

				default:
					break;
			}
			break;

		case CT_EDIT:
			// NYI
			break;

		case CT_STATIC:
		case CT_LISTVIEW:
		case CT_UNKNOWN:
			// non applicable
			break;
	}
}
示例#4
0
/*
 * Print list of attribute values, for numeric attributes.  Attribute values
 * must be printed with explicit type tags, because CDL doesn't have explicit
 * syntax to declare an attribute type.
 */
static void
pr_att_vals(
     nc_type type,
     size_t len,
     const double *vals
     )
{
    int iel;
    signed char sc;
    short ss;
    int ii;
    char gps[30];
    float ff;
    double dd;

    if (len == 0)
	return;
    for (iel = 0; iel < len-1; iel++) {
	switch (type) {
	case NC_BYTE:
	    sc = (signed char) vals[iel] & 0377;
	    Printf ("%db, ", sc);
	    break;
	case NC_SHORT:
	    ss = vals[iel];
	    Printf ("%ds, ", ss);
	    break;
	case NC_INT:
	    ii = (int) vals[iel];
	    Printf ("%d, ", ii);
	    break;
	case NC_FLOAT:
	    ff = vals[iel];
	    (void) sprintf(gps, float_att_fmt, ff);
	    tztrim(gps);	/* trim trailing 0's after '.' */
	    Printf ("%s, ", gps);
	    break;
	case NC_DOUBLE:
	    dd = vals[iel];
	    (void) sprintf(gps, double_att_fmt, dd);
	    tztrim(gps);
	    Printf ("%s, ", gps);
	    break;
	default:
	    error("pr_att_vals: bad type");
	}
    }
    switch (type) {
    case NC_BYTE:
	sc = (signed char) vals[iel] & 0377;
	Printf ("%db", sc);
	break;
    case NC_SHORT:
	ss = vals[iel];
	Printf ("%ds", ss);
	break;
    case NC_INT:
	ii = (int) vals[iel];
	Printf ("%d", ii);
	break;
    case NC_FLOAT:
	ff = vals[iel];
	(void) sprintf(gps, float_att_fmt, ff);
	tztrim(gps);
	Printf ("%s", gps);
	break;
    case NC_DOUBLE:
	dd = vals[iel];
	(void) sprintf(gps, double_att_fmt, dd);
	tztrim(gps);
	Printf ("%s", gps);
	break;
    default:
	error("pr_att_vals: bad type");
    }
}
示例#5
0
文件: hdiff_misc.c 项目: schwehr/hdf4
/*
* Print list of attribute values.  Attribute values must be printed with
* explicit type tags, because their types are not declared.
*/
void
pr_att_vals(nc_type type, int len, void *vals)
{
 int iel;
 union {
  char *cp;
  int16 *sp;
  int32 *lp;
  float32 *fp;
  float64 *dp;
 } gp;
 char *sp;
 unsigned char uc;
 char gps[30];  /* for ascii of a float or double precision */
 char *f_fmt = "%#.8g";
 char *d_fmt = "%#.16g";
 
 switch (type) {
 case DFNT_INT8:
  gp.cp = (char *) vals;
  for (iel = 0; iel < len; iel++)
   if (isprint(uc = *gp.cp++ & 0377))
    Printf ("'%c'%s", uc, iel<len-1 ? ", " : "");
   else
    Printf ("'\\%o'%s", uc, iel<len-1 ? ", " : "");
   break;
 case DFNT_CHAR:
  gp.cp = (char *) vals;
  Printf ("\"");
  /* adjust len so trailing nulls don't get printed */
  sp = gp.cp + len - 1;
  while (*sp-- == '\0' && len > 0)
   len--;
  for (iel = 0; iel < len; iel++)
   switch (uc = *gp.cp++ & 0377) {
 case '\b':
  Printf ("\\b");
  break;
 case '\f':
  Printf ("\\f");
  break;
 case '\n':  /* generate linebreaks after new-lines */
  Printf ("\\n\",\n    \"");
  break;
 case '\r':
  Printf ("\\r");
  break;
 case '\t':
  Printf ("\\t");
  break;
 case '\v':
  Printf ("\\v");
  break;
 case '\\':
  Printf ("\\\\");
  break;
 case '\'':
  Printf ("\\'");
  break;
 case '\"':
  Printf ("\\\"");
  break;
 default:
  Printf ("%c",uc);
  break;
  }
  Printf ("\"");
  break;
 case DFNT_INT16:
  gp.sp = (int16 *) vals;
  for (iel = 0; iel < len; iel++)
   Printf("%ds%s",*gp.sp++,iel<len-1 ? ", " : "");
  break;
 case DFNT_INT32:
  gp.lp = (int32 *) vals;
  for (iel = 0; iel < len; iel++)
   Printf("%d%s",*gp.lp++,iel<len-1 ? ", " : "");
  break;
 case DFNT_FLOAT:
  gp.fp = (float32 *) vals;
  for (iel = 0; iel < len; iel++) {
   int ll;
   (void) sprintf(gps, f_fmt, * gp.fp++);
   /* append a trailing "f" for floating-point attributes */
   ll = strlen(gps);
   gps[ll + 1] = '\0';
   gps[ll] = 'f';
   tztrim(gps); /* trim trailing 0's after '.' */
   Printf ("%s%s", gps, iel<len-1 ? ", " : "");
  }
  break;
 case DFNT_DOUBLE:
  gp.dp = (float64 *) vals;
  for (iel = 0; iel < len; iel++) {
   (void) sprintf(gps, d_fmt, *gp.dp++);
   tztrim(gps); /* trim trailing 0's after '.' */
   Printf ("%s%s", gps, iel<len-1 ? ", " : "");
  }
  break;
 default:
  fprintf(stderr,"pr_att_vals: bad type - %d", type);
 }
}