esig_nat.c

来自「speech signal process tools」· C语言 代码 · 共 963 行 · 第 1/2 页

C
963
字号
    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);}/* * Read items of data from a file into a block of storage indicated * by a pointer "data".  The number of items is given by "length", * and their data type is indicated by the integer code "type". * Return the number of successfully read items.  (A value other * than "length" indicates an error). */static intNativeRead(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 && ReadNativeArray(&array[n], file);	     n++)	{ }	return n;    }    else    {	long	size;		/* size of element (bytes) */	size = InternTypeSize(type);	return fread(data, size, length, file);    }}/* * Read from file the native-mode representation of one item of type * ARRAY.  Store the type, rank, dimensions, and data in the members of * the array structure, allocating the necessary space for dimensions * and data.  Return TRUE for success, FALSE for failure. */static intReadNativeArray(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;}/* * Read field specification in native binary format from file * and return pointer to it; return NULL on failure. */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. */}/* * Read character string in native binary format from file and * assign a pointer to it via output variable.  Return TRUE on * success, FALSE on failure. */static intReadNativeString(char **string, FILE *file){    long    length;    char    *str;    if (fread(&length, sizeof(long), 1, file) != 1)	return FALSE;		/* Couldn't get length. */    if (length == 0)	str = NULL;    else    {	str = (char *) malloc(length + 1);	if (str ==  NULL)	    return FALSE;	if (fread(str, 1, length, file) != length)	{	    free(str);	    return FALSE;	}	str[length] = '\0';    }    if (string != NULL)	*string = str;    return TRUE;}/* * *** FUNCTIONS FOR OUTPUT *//* * Write "data" member of field to file in native binary format. * Return TRUE on success, FALSE on failure. */static intWriteNativeData(FieldSpec *field,		FILE      *file){    long    length;		/* number of elements */    if (file  == NULL || field == NULL || field->type == NO_TYPE)	return FALSE;    length = FieldLength(field);    if (length != 0 && field->data == NULL)	return FALSE;    return (NativeWrite(field->data, field->type, length, file) == length);}/* * Write items of data to a file in native binary format from * the block of storage indicated by the pointer "data". * The number of items is given by "length", and their data type * is indicated by the integer code "type". * Return the number of successfully written items.  (A value other * than "length" indicates an error). */static intNativeWrite(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);    }}/* * Write to "file" the native binary representation of one item of type * ARRAY from the location indicated by "array". * Return TRUE for success, FALSE for failure. * * */static intWriteNativeArray(Array *array,		 FILE  *file){    short   type, rank;    long    *dim;    void    *data;    long    length;    if (array == NULL || file == NULL)	return FALSE;    type = array->type;    rank = array->rank;    dim = array->dim;    data = array->data;    if (fwrite(&type, sizeof(short), 1, file) != 1)	return FALSE;    if (fwrite(&rank, sizeof(short), 1, file) != 1)	return FALSE;    if (rank > 0)    {	if (dim == NULL)	    return FALSE;	if (fwrite(dim, sizeof(long), rank, file) != rank)	    return FALSE;    }    length = LongProd(rank, dim);    if (length > 0)    {	if (data == NULL)	    return FALSE;	if (NativeWrite(data, type, length, file) != length)	    return FALSE;    }    return TRUE;}/* * Write field specification to file in native binary format. * Return TRUE on success, FALSE on failure. */static intWriteNativeFieldSpec(FieldSpec	*field,		     FILE	*file){    int     rank;		/* number of dimensions */    int     i;			/* loop index */    long    num_ax_names;	/* number of axis names */    if (file == NULL || field == NULL)	return FALSE;    if (!WriteNativeString(field->name, file))	return FALSE;    if (fwrite(&field->type, sizeof(short), 1, file) != 1)	return FALSE;    if (fwrite(&field->rank, sizeof(short), 1, file) != 1)	return FALSE;    rank = field->rank;    if (rank != 0 && field->dim == NULL)	return FALSE;		/* Inconsistent rank and dimensions. */    if (fwrite(field->dim, sizeof(long), rank, file) != rank)	return FALSE;    if (!WriteNativeString(field->units, file))	return FALSE;    if (fwrite(&field->scale, sizeof(double), 1, file) != 1)	return FALSE;    if (fwrite(&field->offset, sizeof(double), 1, file) != 1)	return FALSE;    num_ax_names = (field->axis_names == NULL) ? 0 : rank;    if (fwrite(&num_ax_names, sizeof(long), 1, file) != 1)	return FALSE;    for (i = 0; i < num_ax_names; i++)	if (!WriteNativeString(field->axis_names[i], file))	    return FALSE;    if (fwrite(&field->occurrence, sizeof(short), 1, file) != 1)	return FALSE;    if (field->type != NO_TYPE	&& field->occurrence != REQUIRED && field->occurrence != OPTIONAL)    {	if (!WriteNativeData(field, file))	    return FALSE;    }    if (!WriteNativeFieldList(field->subfields, file))	return FALSE;    return TRUE;}/* * Write character string in native binary format to a file. *  Return TRUE on success, FALSE on failure. */static intWriteNativeString(char *string,		  FILE *file){    long    length;    if (file == NULL)	return FALSE;    if (string == NULL)	string = "";    length = strlen(string);    if (fwrite(&length, sizeof(long), 1, file) != 1)	return FALSE;		/* Couldn't write length. */    if (fwrite(string, 1, length, file) != length)	return FALSE;    return TRUE;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?