Ejemplo n.º 1
0
static FieldSpec *
AddCommandLine(FieldList    *list,
	       char	    *line)
{
    FieldSpec	*field;


    if (line == NULL)
	return NULL;

    field = FindField(*list, "commandLine");

    if (field == NULL)
    {
	field = NewFieldSpec(ECHAR, 1);
	if (field == NULL)
	{
	    DebugMsg(1, "AddCommandLine: Couldn't create field spec.");
	    return NULL;
	}

	field->dim[0] = strlen(line);
	field->name = savestring("commandLine");
	field->occurrence = GLOBAL;

	if (!AddField(list, field))
	{
	    DebugMsg(1, "AddCommandLine: Couldn't add field spec.");
	    return NULL;
	}
    }
    else			/* field != NULL */
    {
	if (field->occurrence != GLOBAL)
	{
	    DebugMsg(1, "AddCommandLine: non-GLOBAL field \'commandLine\".");
	    return NULL;
	}

	field->type = ECHAR;
	field->rank = 1;
	field->dim = (long *) ((field->dim == NULL)
				? malloc(sizeof(long))
				: realloc(field->dim, sizeof(long)));

	if (field->dim == NULL)
	{
	    DebugMsg(1, "AddCommandLine: couldn't (re)allocate dimension.");
	    return NULL;
	}

	field->dim[0] = 1 + strlen(line);
    }

    field->data = line;

    return field;
}
Ejemplo n.º 2
0
int
SetFieldOrdering(FieldList  *list,
		 int	    order)
{
    FieldSpec	*field;

    if (*list == NULL)
    {
	DebugMsg(1, "SetFieldOrdering: NULL field list.");
	return FALSE;
    }

    field = FindField(*list, "fieldOrder");

    if (field == NULL)
    {
	if (order == TYPE_ORDER)
	    return TRUE;

	field = NewFieldSpec(SHORT, 0);
	if (field == NULL)
	{
	    DebugMsg(1, "SetFieldOrdering: Couldn't create field spec.");
	    return FALSE;
	}

	field->name = StrDup("fieldOrder");
	field->occurrence = GLOBAL;
	if (!AddField(list, field))
	{
	    DebugMsg(1, "SetFieldOrdering: Couldn't add field spec.");
	    return FALSE;
	}
    }
    else    /* field != NULL */
    {
	if (field->occurrence != GLOBAL)
	{
	    DebugMsg(1, "SetFieldOrdering: non-GLOBAL field \"fieldOrder\".");
	    return FALSE;
	}
	field->type = SHORT;
	field->rank = 0;
    }

    field->data = ((field->data == NULL)
		   ? malloc(sizeof(short))
		   : realloc(field->data, sizeof(short)));
    if (field->data == NULL)
    {
	DebugMsg(1, "SetFieldOrdering: couldn't (re)allocate data.");
	return FALSE;
    }

    *(short *) field->data = order;
    return TRUE;
}
Ejemplo n.º 3
0
static FieldSpec *
ReadNativeFieldSpec(FILE *file)
{
   char *name;          /* field name */
   short        type;           /* data type code */
   short        rank;           /* number of dimensions */
   int          i;              /* loop index */
   FieldSpec    *field;         /* field spec being read */
   long num_ax_names;   /* number of axis names */

   /* Read name, type, rank. */

   if (!ReadNativeString(&name, file))
      return NULL;              /* Failure getting field name. */

   if (fread(&type, sizeof(short), 1, file) != 1)
      return NULL;              /* Couldn't get type. */

   if (fread(&rank, sizeof(short), 1, file) != 1)
      return NULL;              /* Couldn't get rank. */

   /* Allocate structure. */

   field = NewFieldSpec(type, rank);
   if (field == NULL)
      return NULL;              /* Couldn't create field spec. */

   field->name = name;

   /* Read dimensions. */

   if (rank != 0 && field->dim == NULL)
      return NULL;              /* Inconsistent rank and dimensions. */

   if (fread(field->dim, sizeof(long), rank, file) != rank)
      return NULL;              /* Couldn't get dimensions. */

   /* Read units, scale, offset, axis_names. */

   if (!ReadNativeString(&field->units, file))
      return NULL;

   if (fread(&field->scale, sizeof(double), 1, file) != 1)
      return NULL;              /* Couldn't get scale. */

   if (fread(&field->offset, sizeof(double), 1, file) != 1)
      return NULL;              /* Couldn't get offset. */

   if (fread(&num_ax_names, sizeof(long), 1, file) != 1)
      return NULL;              /* Couldn't get number of axis names. */

   if (num_ax_names > rank)
      return NULL;              /* Bad value for num_ax_names. */

   if (num_ax_names != 0)
      {
         field->axis_names = (char **) malloc(rank * sizeof(char *));
         if (field->axis_names == NULL)
            return NULL;        /* Allocation failure. */

         for (i = 0; i < num_ax_names; i++)
            ReadNativeString(&field->axis_names[i], file);

         for ( ; i < rank; i++)
            field->axis_names[i] = NULL;
      }

   /* Read occurrence class. */

   if (fread(&field->occurrence, sizeof(short), 1, file) != 1)
      return NULL;              /* Couldn't get occurrence code. */

   /* Read data if required. */

   if (type != NO_TYPE
       && field->occurrence != REQUIRED && field->occurrence != OPTIONAL)
      {
         if (!ReadNativeData(field, file))
            return NULL;        /* Failure reading data. */
      }

   /* Read subfield list. */

   if (!ReadNativeFieldList(&field->subfields, file))
      {
         FreeFieldSpec(field);
         return NULL;           /* Failure getting subfields. */
      }

   return field;                /* Success. */
}
Ejemplo n.º 4
0
static FieldSpec *
AddSource(FieldList *list,
	  int	    srcnum,
	  char	    *name,
	  FieldList source)
{
    FieldSpec	*field;
    char	srcname[30];
    int		type;
    int		rank;

    if (name == NULL && source == NULL)
	return NULL;

    sprintf(srcname, "source_%d", srcnum);

    if (name == NULL)
    {
	type = NO_TYPE;
	rank = 0;
    }
    else
    {
	type = ECHAR;
	rank = 1;
    }

    field = FindField(*list, srcname);

    if (field == NULL)
    {
	field = NewFieldSpec(type, rank);
	if (field == NULL)
	{
	    DebugMsg(1, "AddSource: Couldn't create field spec.");
	    return NULL;
	}

	field->name = savestring(srcname);
	field->occurrence = VIRTUAL;

	if (!AddField(list, field))
	{
	    DebugMsg(1, "AddSource: Couldn't add field spec.");
	    return NULL;
	}
    }
    else			/* field != NULL */
    {
	if (field->occurrence != VIRTUAL)
	{
	    DebugMsg(1, "AddSource: non-VIRTUAL source_<n> field.");
	    return NULL;
	}

	field->type = type;
	field->rank = rank;

	if (field->dim != NULL)
	{
	    free(field->dim);
	    field->dim = NULL;
	}

	if (name != NULL)
	    field->dim = (long *) malloc(sizeof(long));
    }

    field->subfields = source;

    if (name != NULL)
    {
	field->dim[0] = strlen(name);
	field->data = name;
    }

    return field;
}
Ejemplo n.º 5
0
static FieldSpec *
AddGlobalField(FieldList    *list,
	       char	    *name,
	       int	    rank,
	       long	    *dim,
	       int	    type,
	       void	    *ptr)
{
    FieldSpec	*field;
    int		i;
    long	len;
    size_t	size;
    

    if (name == NULL)
	return NULL;

    if (rank > 0 && dim == NULL)
    {
	DebugMsg(1, "AddGlobalField: NULL dimensions but rank > 0.");
	return NULL;
    }

    field = FindField(*list, name);

    if (field == NULL)
    {
	field = NewFieldSpec(type, rank);

	if (field == NULL)
	{
	    DebugMsg(1, "AddGlobalField: Couldn't create field spec.");
	    return NULL;
	}

	field->name = savestring(name);
	field->occurrence = GLOBAL;

	if (!AddField(list, field))
	{
	    DebugMsg(1, "AddGlobalField: Couldn't add field spec.");
	    return NULL;
	}
    }
    else			/* field != NULL */
    {
	if (field->occurrence != GLOBAL)
	{
	    DebugMsg(1, "AddGlobalField: non-GLOBAL field with same name.");
	    return NULL;
	}

	field->type = type;

	if (field->rank != rank)
	{
	    if (rank == 0)
	    {
		free(field->dim);
		field->dim = NULL;
	    }
	    else
	    {
		field->dim =
		    (long *) ((field->dim == NULL)
			      ? malloc(rank * sizeof(long))
			      : realloc(field->dim, rank * sizeof(long)));

		if (field->dim == NULL)
		{
		    DebugMsg(1,
			     "AddGlobalField: couldn't allocate dimensions.");
		    return NULL;
		}
	    }

	    field->rank = rank;
	}
    }

    len = 1;
    for (i = 0; i < rank; i++)
    {
	field->dim[i] = dim[i];
	len *= dim[i];
    }

    size = len * InternTypeSize(type);

    if (size == 0)
	field->data = NULL;
    else if (ptr != NULL)
	field->data = ptr;
    else
    {
	field->data = malloc(size);

	if (field->data == NULL)
	{
	    DebugMsg(1, "AddGlobalField: couldn't allocate data storage.");
	    return NULL;
	}
    }

    return field;
}
Ejemplo n.º 6
0
static FieldList
fea_to_FieldList(struct header	    *hdr,
		 struct fea_data    *rec,
		 char		    **field_names,
		 int		    copy_sources)
{
    struct fea_header	*fea;
    FieldList		list;
    int			i, j;
    FieldSpec		*field, *subfield;
    char		*name;
    int			spstype;
    long		*dim;
    int			rank;
    int			type;
    char		**codes;
    char		**gnames;
    int			gnum;
    struct varsize	*var;
    int			nsrc;
    FieldList		source;
    char		*line;
    long		len;


    if (hdr == NULL)
	return NULL;

    if (hdr->common.type != FT_FEA)
	return NULL;

    fea = hdr->hd.fea;

    list = NULL;

    if (hdr->common.tag && FindStr(ESPS_TAG, field_names))
    {
	field = NewFieldSpec(ELONG, 0);
	field->name = savestring(ESPS_TAG);
	field->occurrence = REQUIRED;
	if (rec != NULL)
	    field->data = &rec->tag;
	AddField(&list, field);
    }

    if (debug_level)
	fprintf(stderr, "fea_to_FieldList: field count %d.\n",
		fea->field_count);

    for (i = 0; i < (int) fea->field_count; i++)
    {
	long	size;

	name = fea->names[i];

	if (FindStr(name, field_names))
	{
	    spstype = fea->types[i];
	    size = fea->sizes[i];
	    rank = fea->ranks[i];
	    dim = fea->dimens[i];

	    if (debug_level >= 2)
		fprintf(stderr,
			"fea_to_FieldList: field[%d]: \"%s\".\n",
			i, name);

	    type = EspsTypeToElib(spstype);
	    field = NewFieldSpec(type, rank);
	    if (rank == 1)
		field->dim[0] = size;
	    else
		for (j = 0; j < rank; j++)
		    field->dim[j] = dim[j];
	    field->name = savestring(name);
	    field->occurrence = REQUIRED;
	    if (spstype == CODED)
	    {
		codes = fea->enums[i];
		subfield = NewFieldSpec(ECHAR, 2);
		subfield->name = savestring("enumStrings");
		subfield->occurrence = GLOBAL;
		StrArrToRect(codes, &subfield->dim, &subfield->data);
		AddSubfield(field, subfield);
	    }
	    if (rec != NULL)
		field->data = get_fea_ptr(rec, name, hdr);
	    AddField(&list, field);
	}
    }

    gnames = genhd_list(&gnum, hdr);

    for (i = 0; i < gnum; i++)
    {
	int	size;

	name = gnames[i];
	spstype = genhd_type(name, &size, hdr);

	if (debug_level >= 2)
	    fprintf(stderr,
		    "fea_to_FieldList: global field[%d]: \"%s\".\n",
		    i, name);

	type = EspsTypeToElib(spstype);
	field = NewFieldSpec(type, 1);
	field->dim[0] = size;
	field->name = savestring(name);
	field->occurrence = GLOBAL;
	if (spstype == CODED)
	{
	    codes = genhd_codes(name, hdr);
	    subfield = NewFieldSpec(ECHAR, 2);
	    subfield->name = savestring("enumStrings");
	    subfield->occurrence = GLOBAL;
	    StrArrToRect(codes, &subfield->dim, &subfield->data);
	    AddSubfield(field, subfield);
	}
	field->data = get_genhd(name, hdr);
	AddField(&list, field);
    }

    if (fea->fea_type != NONE)
    {
	field = AddGlobalField(&list, "FeaSubtype",
			       0, NULL, ESHORT, NULL);
	*(short *) field->data = fea->fea_type;
	subfield = NewFieldSpec(ECHAR, 2);
	subfield->name = savestring("enumStrings");
	subfield->occurrence = GLOBAL;
	StrArrToRect(fea_file_type, &subfield->dim, &subfield->data);
	AddSubfield(field, subfield);
    }

    var = &hdr->variable;

    if (copy_sources)
    {
	nsrc = MAX(var->nnames, var->nheads);
	for (i = 0; i < nsrc; i++)
	{
	    source = (i >= var->nheads) ? NULL
		: fea_to_FieldList(var->srchead[i], NULL, NULL, TRUE);
	    name = (i >= var->nnames) ? NULL
		: var->source[i];
	    (void) AddSource(&list, i, name, source);
	}
    }

    if (var->comment != NULL)
    {
	line = savestring(var->comment);
	len = strlen(line);
	if (line[len-1] == '\n')
	    line[len-1] = '\0';
	(void) AddCommandLine(&list, line);
    }

    return list;
}