Example #1
0
static int
NativeWrite(void    *data,
            int     type,
            long    length,
            FILE    *file)
{
   if (file == NULL || data == NULL || length <= 0)
      return 0;

   if (type == ARRAY)
      {
         Array  *array = (Array *) data;
         long   n;

         for (n = 0;
              n < length && WriteNativeArray(&array[n], file);
              n++)
            { }

         return n;
      }
   else
      {
         long   size;           /* size of element (bytes) */

         size = InternTypeSize(type);
         return fwrite(data, size, length, file);
      }
}
Example #2
0
static int
ReadNativeData(FieldSpec *field,
               FILE *file)
{
   long    size;                /* size of element (bytes) */
   long    length;              /* number of elements */

   if (file  == NULL || field == NULL || field->type == NO_TYPE)
      {
         DebugMsg(1, "ReadNativeData: NULL argument or type NO_TYPE.");
         return FALSE;
      }

   size = InternTypeSize(field->type);
   length = FieldLength(field);

   if (field->data == NULL && length != 0)
      {
         field->data = malloc(length * size);
         if (field->data == NULL)
            {
               DebugMsg(1, "ReadNativeData: allocation failure.");
               return FALSE;
            }
      }

   return (NativeRead(field->data, field->type, length, file) == length);
}
Example #3
0
long
NativeTypeSize(int type   /* numeric data-type code */ )
{
   if (type == ARRAY)
      return -1;  /* Variable-length external representation. */
   else
      return InternTypeSize(type);
}
Example #4
0
static int
ReadNativeArray(Array *array,
                FILE *file)
{
   short   type, rank;
   long    *dim;
   long    length;
   long    size;
   void    *data;

   if (array == NULL || file == NULL)
      return FALSE;

   /* Read type, rank. */

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

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

   if (rank == 0)
      dim = NULL;
   else
      {
         dim = (long *) malloc(rank * sizeof(long));
         if (dim == NULL)
            return FALSE;       /* Allocation failure. */

         if (fread(dim, sizeof(long), rank, file) != rank)
            return FALSE;       /* Couldn't get dimensions. */
      }

   length = LongProd(rank, dim);
   size = InternTypeSize(type);

   if (length == 0)
      data = NULL;
   else
      {
         data = malloc(length*size);

         if (data == NULL)
            return FALSE;       /* Allocation failure. */

         if (NativeRead(data, type, length, file) != length)
            return FALSE;
      }

   array->type = type;
   array->rank = rank;
   array->dim = dim;
   array->data = data;

   return TRUE;
}
Example #5
0
void *
AllocSamples(long	nrec,
	     FieldSpec	**fields)
{
    if (fields == NULL || fields[0] == NULL || fields[1] != NULL
	|| fields[0]->occurrence != REQUIRED)
	return NULL;

    return malloc(nrec * FieldLength(fields[0])
		  * InternTypeSize(fields[0]->type));
}
Example #6
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;
}