esig_asc.c

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

C
3,198
字号
    case FLOAT_COMPLEX:	{	    FloatComplex   *data = (FloatComplex *) datap;	    if (annotate == NULL)	    {		fprintf(file, FLTCX_FMT,			data[0].real, data[0].imag);		for (j = 1; j < length; j++)		    fprintf(file, " " FLTCX_FMT,			    data[j].real, data[j].imag);	    }	    else	    {		fprintf(file, FLTCX_WFMT,			data[0].real, data[0].imag);		for (j = 1; j < length; j++)		    fprintf(file, " " FLTCX_WFMT,			    data[j].real, data[j].imag);	    }	}	break;    case LONG_COMPLEX:	{	    LongComplex    *data = (LongComplex *) datap;	    if (annotate == NULL)	    {		fprintf(file, LNGCX_FMT,			data[0].real, data[0].imag);		for (j = 1; j < length; j++)		    fprintf(file, " " LNGCX_FMT,			    data[j].real, data[j].imag);	    }	    else	    {		fprintf(file, LNGCX_WFMT,			data[0].real, data[0].imag);		for (j = 1; j < length; j++)		    fprintf(file, " " LNGCX_WFMT,			    data[j].real, data[j].imag);	    }	}	break;    case SHORT_COMPLEX:	{	    ShortComplex   *data = (ShortComplex *) datap;	    if (annotate == NULL)	    {		fprintf(file, SHRCX_FMT,			data[0].real, data[0].imag);		for (j = 1; j < length; j++)		    fprintf(file, " " SHRCX_FMT,			    data[j].real, data[j].imag);	    }	    else	    {		fprintf(file, SHRCX_WFMT,			data[0].real, data[0].imag);		for (j = 1; j < length; j++)		    fprintf(file, " " SHRCX_WFMT,			    data[j].real, data[j].imag);	    }	}	break;    case SCHAR_COMPLEX:	{	    ScharComplex   *data = (ScharComplex *) datap;	    if (annotate == NULL)	    {		fprintf(file, SCHCX_FMT,			data[0].real, data[0].imag);		for (j = 1; j < length; j++)		    fprintf(file, " " SCHCX_FMT,			    data[j].real, data[j].imag);	    }	    else	    {		fprintf(file, SCHCX_WFMT,			data[0].real, data[0].imag);		for (j = 1; j < length; j++)		    fprintf(file, " " SCHCX_WFMT,			    data[j].real, data[j].imag);	    }	}	break;    case CHAR:	j = AsciiWriteChar((char *) datap, length, file);	break;    case WCHAR:	j = AsciiWriteWchar((Wchar *) datap, length, file);	break;    default:	return 0;		/* Unsupported type. */    }    return j;}/* * Write to file the Ascii representation of one item of type ARRAY, * including the surrounding parentheses, data-type name, dimensions, * separating colon, and data values.  Return TRUE for success, FALSE * for failure. */static intWriteAsciiArray(Array *array, FILE *file, Annot *annotate){    short   type;    short   rank;    long    *dim;    void    *data;    int     i;    long    length;    int	    ind;    if (file == NULL || array == NULL)	return FALSE;    type = array->type;    rank = array->rank;    dim = array->dim;    data = array->data;    if (!ValidType(type))	return FALSE;    fprintf(file, "(");    fprintf(file, "%s ", TypeName(type));    if (rank > 0 && dim == NULL)	return FALSE;    for (i = 0; i < rank; i++)	fprintf(file, "%ld ", dim[i]);    fprintf(file, ": ");    length = LongProd(rank, dim);    if (length > 0)    {	if (data == NULL)	    return FALSE;	if (annotate == NULL)	{	    if (AsciiWrite(data, type, length, file, annotate) != length)		return FALSE;	}	else	{	    ind = annotate->indent;	    annotate->indent += 2;	    if (length > 1 || type == ARRAY)		fprintf(file, "\n%*.0s", annotate->indent, "");	    if (!AsciiWriteSub(data, type,			       length, file, rank, dim, annotate))	    {		annotate->indent = ind;		return FALSE;	    }	    annotate->indent = ind;	}    }    fprintf(file, ")");    return TRUE;}/* * Write to file in Ascii format the field specification indicated by * field, including any subfields.  The name consists of (depth - 1) * components found in the string array "names", followed by the string * field->name, with separating dots (".") supplied by the function. * If annotate != NULL, add annotations in "[]" for readability. * The top-level call (in WriteAsciiFieldList) has depth = 1. * Subfields are written by recursive calls within this function, * with depth > 1. */static intWriteAsciiFieldSpec(FieldSpec *field,		    int       depth,		    FILE      *file,		    Annot     *annotate){    static char	*names[MAX_FLD_DEPTH]; /* field name components */    int		i;		/* loop index */    int		rank;		/* number of dimensions */    long	*dim;		/* dimensions */    int		num_misc;	/* number of "misc" attributes */    char	**axis_names;	/* axis_names attribute */    char	occ;		/* occurrence-class code */    FieldList	subfields;	/* subfield list */    if (file == NULL || field == NULL)	return FALSE;    if (depth > MAX_FLD_DEPTH)	return FALSE;		/* Subfield structure too deep. */    /*     * Write full name, ":".     */    names[depth-1] = field->name;    fprintf(file, "%s", names[0]);    for (i = 1; i < depth; i++)	fprintf(file, ".%s", names[i]);    fprintf(file, ": ");    /*     * Write data type.     */    if (!ValidType(field->type))	return FALSE;		/* Bad data type. */    fprintf(file, "%s ", TypeName(field->type));    /*     * Write dimensions.     */    rank = field->rank;    dim = field->dim;    if (rank > 0 && dim == NULL)	return FALSE;		/* Inconsistent rank and dimensions. */    for (i = 0; i < rank; i++)	fprintf(file, "%ld ", dim[i]);    /*     * Write misc attributes, if any non-default.     */    num_misc = 0;    /* units */    if (field->units != NULL && *field->units != '\0')    {	fprintf(file, (num_misc == 0) ? "{" : "; ");	num_misc++;	fprintf(file, "units: ");	(void) WriteAsciiString(field->units, file);    }    /* scale */    if (field->scale != 1.0)    {	fprintf(file, (num_misc == 0) ? "{" : "; ");	num_misc++;	fprintf(file, "scale: " DBL_FMT, field->scale);    }    /* offset */    if (field->offset != 0.0)    {	fprintf(file, (num_misc == 0) ? "{" : "; ");	num_misc++;	fprintf(file, "offset: " DBL_FMT, field->offset);    }    /* axis_names */    if (field->axis_names != NULL)    {	fprintf(file, (num_misc == 0) ? "{" : "; ");	num_misc++;	fprintf(file, "axis_names: ");	axis_names = field->axis_names;	for (i = 0; i < rank; i++)	    fprintf(file, (i == 0) ? "%s" : ", %s",		    (axis_names[i] == NULL) ? "" : axis_names[i]);    }    /* "}" if needed */    if (num_misc > 0)	fprintf(file, "} ");    /*     * Write occurrence class.     */    switch (field->occurrence)    {    case GLOBAL:	occ = 'g';	break;    case REQUIRED:	occ = 'r';	break;    case OPTIONAL:	occ = 'o';	break;    case VIRTUAL:	occ = 'v';	break;    case INCLUDED:	occ = 'i';	break;    default:	return FALSE;		/* Bad occurrence class. */    }    fprintf(file, "<%c>", occ);    /*     * Write data.     */    if (field->type != NO_TYPE	&& field->occurrence != REQUIRED && field->occurrence != OPTIONAL)    {	putc(' ', file);	if (annotate != NULL)	{	    annotate->indent = 2;	}	if (!WriteAsciiData(field, file, annotate))	    return FALSE;	/* Failed writing data. */	if (annotate != NULL)	{	    annotate->indent = 0;	}    }    /*     * Write terminating newline.     */    fprintf(file, "\n");    /*     * Write subfield specs.     */    subfields = field->subfields;    if (subfields != NULL)	for (i = 0; subfields[i] != NULL; i++)	    if (!WriteAsciiFieldSpec(subfields[i],				     depth + 1, file, annotate))		return FALSE;	/* Failed writing subfield spec. */    return TRUE;		/* Success. */}/* * Write to file a string constant representing the string beginning * at "data".  The string is enclosed in double quotes (").  Printing * characters other than double quote and backslash (\) represent * themselves.  Double quote, and backslash are represented by Ansi C * single-character escapes. */static intWriteAsciiString(char *data,		 FILE *file){    int    ch;			/* character to write */    long    j;			/* loop index */    putc('"', file);    for (j = 0; (ch = data[j]) != '\0'; j++)	switch (ch)	{	case '"':	    fputs("\\\"", file);	    break;	case '\\':	    fputs("\\\\", file);	    break;	default:	    if (isprint(ch))	    {		putc(ch, file);	    }	    else return FALSE;	    break;	}    putc('"', file);    return TRUE;}/* * Write to file a string constant representing the array of "size" * characters beginning at "data".  The string is enclosed in double * quotes (").  Printing characters other than double quote and * backslash (\) represent themselves.  Double quote, backslash, and * non-printing characters are represented by Ansi C single-character * escapes or two-digit hex escapes. */static intAsciiWriteChar(char	*data,	       long	size,	       FILE	*file){    int    ch;			/* character to write */    long    j;			/* loop index */    putc('"', file);    for (j = 0; j < size; j++)    {	ch = (Uchar) data[j];	switch (ch)	{	case '"':	    fputs("\\\"", file);	    break;	case '\\':	    fputs("\\\\", file);	    break;	case '\0':	    fputs("\\0", file);	    break;	case '\a':	    fputs("\\a", file);	    break;	case '\b':	    fputs("\\b", file);	    break;	case '\f':	    fputs("\\f", file);	    break;	case '\n':	    fputs("\\n", file);	    break;	case '\r':	    fputs("\\r", file);	    break;	case '\t':	    fputs("\\t", file);	    break;	case '\v':	    fputs("\\v", file);	    break;	default:	    if (isprint(ch))	    {		putc(ch, file);	    }	    else	    {		/*		 * Non-printing characters without special single-		 * character escapes are written in hex.  A two-digit		 * hex escape like \x2f followed by a character like f		 * that happens to be a hex digit is interpreted as a		 * single three-digit hex escape, rather than two		 * characters, if written solid ("\x2ff").  Making a		 * break (e.g. "\x2f" "f") resolves the ambiguity.		 */		fprintf(file,			(j < size - 1 && isxdigit(data[j+1]))			? "\\x%02x\" \""			: "\\x%02x",			ch);	    }	    break;	}    }    putc('"', file);    return size;}/* * Write to file a string constant representing the array of "size" * wide characters beginning at "data".  The string is enclosed in * double quotes (").  Printing characters other than double quote and * backslash (\) represent themselves.  Double quote, backslash, and * non-printing characters are represented by Ansi C single-character * escapes or four-digit hex escapes. */static intAsciiWriteWchar(Wchar	*data,		long	size,		FILE	*file){    long    ch;			/* wide character to write */    long    j;			/* loop index */    putc('"', file);    for (j = 0; j < size; j++)    {	ch = data[j];	switch (ch)	{	case '"':	    fputs("\\\"", file);	    break;	case '\\':	    fputs("\\\\", file);	    break;	case '\0':	    fputs("\\0", file);	    break;	case '\a':	    fputs("\\a", file);	    break;	case '\b':	    fputs("\\b", file);	    break;	case '\f':	    fputs("\\f", file);	    break;	case '\n':	    fputs("\\n", file);	    break;	case '\r':	    fputs("\\r", file);	    break;	case '\t':	    fputs("\\t", file);	    break;	case '\v':	    fputs("\\v", file);	    break;	default:	    if (((Ulong) ch < 0x7f) && isprint(ch))	    {		putc(ch, file);	    }	    else	    {		/*		 * Non-printing characters without special single-		 * character escapes are written as four-digit hex		 * escapes.  (Cf. two digits for CHAR.)		 */		fprintf(file,			(j < size - 1 && (((Ulong) data[j+1] < 0x7f)					  && isxdigit(data[j+1])))			? "\\x%04x\" \""			: "\\x%04x",			ch);	    }	    break;	}    }    putc('"', file);    return size;}/* * */static longApproxWidth(int type){    switch (type)    {    case ARRAY:	return -1;    case DOUBLE:	return (DBL_W) + 1;    case FLOAT:	return (FLT_W) + 1;    case LONG:	return (ULN_W) + 1;    case ULONG:	return (LNG_W) + 1;    case SHORT:	return (SHR_W) + 1;    case USHORT:	return (USH_W) + 1;    case SCHAR:	return (SCH_W) + 1;    case UCHAR:	return (UCH_W) + 1;    case BOOL:	return 1;    case DOUBLE_COMPLEX:	return 2*(DBLCX_W) + 5;    case FLOAT_COMPLEX:	return 2*(FLTCX_W) + 5;    case LONG_COMPLEX:	return 2*(LNGCX_W) + 5;    case SHORT_COMPLEX:	return 2*(SHRCX_W) + 5;    case SCHAR_COMPLEX:	return 2*(SCHCX_W) + 5;    case CHAR:	return CHR_W;    case WCHAR:	return WCH_W;    default:	{	    DebugMsg(1, "ApproxWidth: Invalid code or NO_TYPE.");	    return 0;	}    }}

⌨️ 快捷键说明

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