esig_asc.c

来自「speech signal process tools」· C语言 代码 · 共 3,198 行 · 第 1/5 页

C
3,198
字号
		FILE		*file){    int     ch;    ch = getc(file);    switch (ch)    {    case '\\':    case '?':    case '\'':    case '"':	*val = ch;	break;    case 'a':	*val = '\a';	break;    case 'b':	*val = '\b';	break;    case 'f':	*val = '\f';	break;    case 'n':	*val = '\n';	break;    case 'r':	*val = '\r';	break;    case 't':	*val = '\t';	break;    case 'v':	*val = '\v';	break;    case 'x':	{	    const unsigned long	chkval = ULONG_MAX >> 4;	    unsigned long	xval;	    ch = getc(file);	    if (!isxdigit(ch))		return FALSE;	    xval = 0;	    do {		if (isdigit(ch))		    ch -= '0';		else		    switch (ch)		    {		    case 'A':		    case 'a':			ch = 10;			break;		    case 'B':		    case 'b':			ch = 11;			break;		    case 'C':		    case 'c':			ch = 12;			break;		    case 'D':		    case 'd':			ch = 13;			break;		    case 'E':		    case 'e':			ch = 14;			break;		    case 'F':		    case 'f':			ch = 15;			break;		    }		if (xval > chkval) /* Will overflow. */		    return FALSE;		xval = 16*xval + ch;		ch = getc(file);	    } while (isxdigit(ch));	    if (ch != EOF)		ungetc(ch, file);	    *val = xval;	}	break;    case '0':    case '1':    case '2':    case '3':    case '4':    case '5':    case '6':    case '7':	ungetc(ch, file);	fscanf(file, "%3lo", val);	break;    default:	return FALSE;    }    return TRUE;}/* * Scan file, skipping whitespace and bracketed comments, searching for * a newline character.  If successful, return TRUE, leaving the file * positioned just past the newline.  If unsuccessful, return FALSE. */static intReadAsciiNewline(FILE *file){    int     ch;    ch = getc(file);    for (;;)    {	if (isspace(ch) && ch != '\n')	    ch = getc(file);	else if (ch == '[')	    ch = SkipComment(file);	else break;    }    if (ch != '\n')	return FALSE;    return TRUE;}/* * Scan a sequence of characters, starting with ch and continuing with * characters read from file, skipping whitespace and bracketed comments. * Return the last character examined (ch or the last character read), * which will not be a whitespace character or part of a bracketed * comment (but may be EOF). */static intSkipSpace(int ch, FILE *file){    for (;;)    {	if (isspace(ch))	    ch = getc(file);	else if (ch == '[')	    ch = SkipComment(file);	else break;    }    return ch;}/* * Skip over a bracketed comment in file.  The opening bracket is * assumed to have already been read; the function reads the rest * of the construct, then reads and returns the character after the * closing bracket (possibly EOF).  The return value is EOF if no * closing bracket is found. */static intSkipComment(FILE *file){    int     ch;    do {	ch = getc(file);    } while ((isgraph(ch) && ch != '[' && ch != ']')	   || isspace(ch));    if (ch == ']')    {	ch = getc(file);	return ch;    }    else	return EOF;}/* * Scan file, skipping whitespace and bracketed comments. * Return the last character read, which will not be a whitespace * character or part of a bracketed comment (but may be EOF). */static intGetNonSpace(FILE *file){    int     ch;    ch = getc(file);    ch = SkipSpace(ch, file);    return ch;}/* * Scan file, skipping whitespace and bracketed comments, and leave * the file positioned just before the first character that is not * whitespace or part of a bracketed comment (or at EOF if no * such character is found).  Return TRUE if at least one whitespace * character or bracketed comment is found; otherwise return FALSE. */static intReadSpace(FILE *file){    int     ch;    int     okay;    ch = getc(file);    if (!isspace(ch) && ch != '[')	okay = FALSE;    else    {	ch = SkipSpace(ch, file);	okay = TRUE;    }    if (ch != EOF)	ungetc(ch, file);    return okay;}/* * Scan file, skipping whitespace and bracketed comments, and leave * the file positioned just before the first character that is not * whitespace or part of a bracketed comment, or at EOF if no * such character is found.  Return TRUE if such a character is found, * FALSE if EOF is reached. */static intReadOptSpace(FILE *file){    int     ch;    ch = GetNonSpace(file);    if (ch == EOF)	return FALSE;    ungetc(ch, file);    return TRUE;}/* * Append a character ch to a character array of length *len * contained in a malloc'ed storage block with a size of * *alloc_len bytes, at the location indicated by the pointer *str. * If the block isn't large enough, the function reallocates the block * and updates *str and *alloc_len accordingly.  The function also * increments *len by 1.  If reallocation is necessary, the function * allocates more storage than is required so that a number of subsequent * calls of AddChar may be made without the need to reallocate. * The arguments str, alloc_len, and len are addresses of variables, * rather than value, so that the function can assign to them. * For example str is the address of a variable containing a pointer * to the beginning of the storage area.  To start things off, * initialize *str to NULL, *alloc_len to 0, and *len to 0. * The function returns TRUE upon success and FALSE in case of * allocation failure. */static intAddChar(int	ch,	char	**str,	long	*alloc_len,	long	*len){    if (*len >= *alloc_len)    {	*alloc_len = *len + STR_ALLOC_SIZE;	*str = (char *)	    ((*str == NULL)	     ? malloc(*alloc_len)	     : realloc(*str, *alloc_len));	if (*str == NULL)	    return FALSE;    }    (*str)[(*len)++] = ch;    return TRUE;}/* * *** FUNCTIONS FOR OUTPUT *//* * Write "data" member of field to file in Ascii format. * If annotate != NULL, add annotations in "[]" for readability. * Return TRUE on success, FALSE on failure. */static intWriteAsciiData(FieldSpec *field,	       FILE      *file,	       Annot     *annotate){    long    length;		/* number of data elements */    if (file == NULL || field == NULL || field->type == NO_TYPE)	return FALSE;    length = FieldLength(field);    if (length == 0)	return TRUE;    if (field->data == NULL)	return FALSE;    if (annotate == NULL)	return (AsciiWrite(field->data, field->type,			   length, file, annotate) == length);    else    {	if (length > 1 || field->type == ARRAY)	    fprintf(file, "\n%*.0s", annotate->indent, "");	return AsciiWriteSub(field->data, field->type, length, file,			     field->rank, field->dim, annotate);    }}/* * */static intAsciiWriteSub(void  *data,	      int   type,	      long  length,	      FILE  *file,	      int   rank,	      long  *dim,	      Annot *annotate){    int		i;    long	dim0, j;    long	step, stride;    int		ind;    if (rank == 0)    {	AsciiWrite(data, type, 1, file, annotate);    }    else if (length == dim[0])    {	if (type == ARRAY)	    step = 1;	else	{	    step = (annotate->width - annotate->indent - 3*rank - 2)		/ ApproxWidth(type);	    if (step < 1)		step = 1;	}	stride = step*InternTypeSize(type);	ind = annotate->indent;	for (j = 0; length > 0; j += step, length -= step)	{	    if (j > 0)	    {		fprintf(file, "\n%*.0s", ind, "");		data = (char *) data + stride;	    }	    annotate->indent =		ind + fprintf(file, "[%ld]", j) + 3*(rank - 1) + 2;	    for (i = 1; i < rank; i++)		fprintf(file, "[0]");	    fprintf(file, "  ");	    AsciiWrite(data, type,		       (length < step) ? length : step, file, annotate);	}	annotate->indent = ind;    }    else    {	dim0 = dim[0];	dim += 1;	rank -= 1;	length /= dim0;	stride = length*InternTypeSize(type);	ind = annotate->indent;	for (j = 0; j < dim0; j++)	{	    if (j > 0)		fprintf(file, "\n%*.0s", ind, "");	    annotate->indent = ind + fprintf(file, "[%ld]", j);	    AsciiWriteSub(j*stride + (char *) data, type, length, file,			  rank, dim, annotate);	}	annotate->indent = ind;    }    /*! error checks needed */    return TRUE;}/* * Write to file in Ascii format a sequence of "length" values of data * type "type", found starting at the location indicated by "datap". * Return the number of values successfully written, which will be less * than "length" in case of error. */static intAsciiWrite(void	    *datap,	   int	    type,	   long	    length,	   FILE	    *file,	   Annot    *annotate){    long    j;    /* AsciiWrite is called from WriteAsciiSamples, WriteAsciiData,     * WriteAsciiArray, and AsciiWrite itself.  In all cases it has     * been checked that file != NULL, data != NULL, and length != 0.     */    j = 0;    switch (type)    {    case ARRAY:	{	    Array   *data = (Array *) datap;	    WriteAsciiArray(&data[0], file, annotate);	    for (j = 1; j < length; j++)	    {		putc(' ', file);		WriteAsciiArray(&data[j], file, annotate);	    }	}	break;    case DOUBLE:	{	    double  *data = (double *) datap;	    if (annotate == NULL)	    {		fprintf(file, DBL_FMT, data[0]);		for (j = 1; j < length; j++)		    fprintf(file, " " DBL_FMT, data[j]);	    }	    else	    {		fprintf(file, DBL_WFMT, data[0]);		for (j = 1; j < length; j++)		    fprintf(file, " " DBL_WFMT, data[j]);	    }	}	break;    case FLOAT:	{	    float   *data = (float *) datap;	    if (annotate == NULL)	    {		fprintf(file, FLT_FMT, data[0]);		for (j = 1; j < length; j++)		    fprintf(file, " " FLT_FMT, data[j]);	    }	    else	    {		fprintf(file, FLT_WFMT, data[0]);		for (j = 1; j < length; j++)		    fprintf(file, " " FLT_WFMT, data[j]);	    }	}	break;    case LONG:	{	    long    *data = (long *) datap;	    if (annotate == NULL)	    {		fprintf(file, LNG_FMT, data[0]);		for (j = 1; j < length; j++)		    fprintf(file, " " LNG_FMT, data[j]);	    }	    else	    {		fprintf(file, LNG_WFMT, data[0]);		for (j = 1; j < length; j++)		    fprintf(file, " " LNG_WFMT, data[j]);	    }	}	break;    case ULONG:	{	    Ulong   *data = (Ulong *) datap;	    if (annotate == NULL)	    {		fprintf(file, ULN_FMT, data[0]);		for (j = 1; j < length; j++)		    fprintf(file, " " ULN_FMT, data[j]);	    }	    else	    {		fprintf(file, ULN_WFMT, data[0]);		for (j = 1; j < length; j++)		    fprintf(file, " " ULN_WFMT, data[j]);	    }	}	break;    case SHORT:	{	    short   *data = (short *) datap;	    if (annotate == NULL)	    {		fprintf(file, SHR_FMT, data[0]);		for (j = 1; j < length; j++)		    fprintf(file, " " SHR_FMT, data[j]);	    }	    else	    {		fprintf(file, SHR_WFMT, data[0]);		for (j = 1; j < length; j++)		    fprintf(file, " " SHR_WFMT, data[j]);	    }	}	break;    case USHORT:	{	    Ushort  *data = (Ushort *) datap;	    if (annotate == NULL)	    {		fprintf(file, USH_FMT, (unsigned) data[0]);		for (j = 1; j < length; j++)		    fprintf(file, " " USH_FMT, (unsigned) data[j]);	    }	    else	    {		fprintf(file, USH_WFMT, (unsigned) data[0]);		for (j = 1; j < length; j++)		    fprintf(file, " " USH_WFMT, (unsigned) data[j]);	    }	}	break;    case SCHAR:	{	    Schar   *data = (Schar *) datap;	    if (annotate == NULL)	    {		fprintf(file, SCH_FMT, data[0]);		for (j = 1; j < length; j++)		    fprintf(file, " " SCH_FMT, data[j]);	    }	    else	    {		fprintf(file, SCH_WFMT, data[0]);		for (j = 1; j < length; j++)		    fprintf(file, " " SCH_WFMT, data[j]);	    }	}	break;    case UCHAR:	{	    Uchar   *data = (Uchar *) datap;	    if (annotate == NULL)	    {		fprintf(file, UCH_FMT, (unsigned) data[0]);		for (j = 1; j < length; j++)		    fprintf(file, " " UCH_FMT, (unsigned) data[j]);	    }	    else	    {		fprintf(file, UCH_WFMT, (unsigned) data[0]);		for (j = 1; j < length; j++)		    fprintf(file, " " UCH_WFMT, (unsigned) data[j]);	    }	}	break;    case BOOL:	{	    Bool    *data = (Bool *) datap;	    for (j = 0; j < length; j++)		putc((data[j]) ? '1' : '0', file);	}	break;    case DOUBLE_COMPLEX:	{	    DoubleComplex  *data = (DoubleComplex *) datap;	    if (annotate == NULL)	    {		fprintf(file, DBLCX_FMT,			data[0].real, data[0].imag);		for (j = 1; j < length; j++)		    fprintf(file, " " DBLCX_FMT,			    data[j].real, data[j].imag);	    }	    else	    {		fprintf(file, DBLCX_WFMT,			data[0].real, data[0].imag);		for (j = 1; j < length; j++)		    fprintf(file, " " DBLCX_WFMT,			    data[j].real, data[j].imag);	    }	}	break;

⌨️ 快捷键说明

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