📄 esignal.c
字号:
/* ----------------------------------------------------------- *//* *//* ___ *//* |_| | |_/ SPEECH *//* | | | | \ RECOGNITION *//* ========= SOFTWARE *//* *//* *//* ----------------------------------------------------------- *//* Copyright: Microsoft Corporation *//* 1995-2000 Redmond, Washington USA *//* http://www.microsoft.com *//* *//* Use of this software is governed by a License Agreement *//* ** See the file License for the Conditions of Use ** *//* ** This banner notice must not be removed ** *//* *//* ----------------------------------------------------------- *//* * Example programs for Esignal public external file format. * Utility; field specifications and lists; general I/O. * * Author: Rod Johnson */#include <esignal.h>/* * LOCAL FUNCTION DECLARATIONS *//* Used by FieldListLength, FieldOrder, and TypeOrder. */static int FieldArrayLength(FieldSpec **fields);/* Used by FindField */static FieldSpec *GetField(FieldList list, char *name);static char *FirstComponent(char *name);/* Used by FieldOrder */static FieldSpec **SubFieldOrder(FieldList list, char *prefix);/* Used by ReadPreamble */static int GetLine(char *buf, int len, FILE *file);static int GetLong(long *val, int len, FILE *file);/* Used by GetFieldOrdering */static int LongVal(void *src, int type, long *dest);/* * GLOBAL VARIABLES */static short Types[] ={ NO_TYPE, ARRAY, DOUBLE, FLOAT, LONG, ULONG, SHORT, USHORT, SCHAR, UCHAR, BOOL, DOUBLE_COMPLEX, FLOAT_COMPLEX, LONG_COMPLEX, SHORT_COMPLEX, SCHAR_COMPLEX, CHAR, WCHAR};int DebugMsgLevel;void (*DebugMsgFunc)(char *msg);/* * PUBLIC FUNCTION DEFINITIONS * *** Miscellaneous * *** Debug output * *** Types * *** Field specs and field lists * *** General input/output *//* * *** MISCELLANEOUS * - StrDup * - LongProd *//* * Replacement for non-ANSI function strdup. */char *StrDup(char *str){ char *cpy; if (str == NULL) return NULL; cpy = (char *) malloc(1 + strlen(str)); if (cpy == NULL) return NULL; return strcpy(cpy, str);}/* * Product of array of longs (e.g. array length from dimensions). */longLongProd(int n, long *arr){ int i; long prod; prod = 1; for (i = 0; i < n; i++) prod *= arr[i]; return prod;}/* * *** DEBUG OUTPUT * - DebugPrint */voidDebugPrint(char *msg){ fprintf(stderr, "%s\n", msg);}/* * *** TYPES * - ValidType * - TypeName * - TypeCode * - InternTypeSize * - ExternTypeSize * - *//* * Does type code denote either NO_TYPE or supported data * type? */intValidType(int type /* numeric data-type code */ ){ switch (type) { /* All non-default cases fall through. */ case NO_TYPE: case ARRAY: case DOUBLE: case FLOAT: case LONG: case ULONG: case SHORT: case USHORT: case SCHAR: case UCHAR: case BOOL: case DOUBLE_COMPLEX: case FLOAT_COMPLEX: case LONG_COMPLEX: case SHORT_COMPLEX: case SCHAR_COMPLEX: case CHAR: case WCHAR: return TRUE; default: return FALSE; }}/* * String containing name of data type. */char *TypeName(int type /* numeric data-type code */ ){ switch (type) { case NO_TYPE: return "NO_TYPE"; case ARRAY: return "ARRAY"; case DOUBLE: return "DOUBLE"; case FLOAT: return "FLOAT"; case LONG: return "LONG"; case ULONG: return "ULONG"; case SHORT: return "SHORT"; case USHORT: return "USHORT"; case SCHAR: return "SCHAR"; case UCHAR: return "UCHAR"; case BOOL: return "BOOL"; case DOUBLE_COMPLEX: return "DOUBLE_COMPLEX"; case FLOAT_COMPLEX: return "FLOAT_COMPLEX"; case LONG_COMPLEX: return "LONG_COMPLEX"; case SHORT_COMPLEX: return "SHORT_COMPLEX"; case SCHAR_COMPLEX: return "SCHAR_COMPLEX"; case CHAR: return "CHAR"; case WCHAR: return "WCHAR"; default: /* Invalid code. */ return NULL; }}/* * Return data-type code for type with a given name. */intTypeCode(char *name){ int num_types = sizeof(Types)/sizeof(Types[0]); int i; char *code; if (name == NULL) return 0; for (i = 0; i < num_types; i++) { code = TypeName(Types[i]); if (code != NULL && strcmp(code, name) == 0) return Types[i]; } return 0;}/* * Number of bytes of memory to allocate for an item of a given type. */longInternTypeSize(int type /* numeric data_type code */ ){ switch (type) { case ARRAY: return sizeof(Array); case DOUBLE: return sizeof(double); case FLOAT: return sizeof(float); case LONG: case ULONG: return sizeof(long); case SHORT: case USHORT: return sizeof(short); case SCHAR: return sizeof(Schar); case UCHAR: return sizeof(Uchar); case BOOL: return sizeof(Bool); case DOUBLE_COMPLEX: return sizeof(DoubleComplex); case FLOAT_COMPLEX: return sizeof(FloatComplex); case LONG_COMPLEX: return sizeof(LongComplex); case SHORT_COMPLEX: return sizeof(ShortComplex); case SCHAR_COMPLEX: return sizeof(ScharComplex); case CHAR: return sizeof(char); case WCHAR: return sizeof(Wchar); default: /* Invalid code or NO_TYPE. */ return 0; }}/* * Number of bytes of external storage occupied by an item of a given type * in a file with a given format. */longExternTypeSize(int type, int arch){ switch (arch) { case EDR1: return EdrTypeSize(type, EDR1); break; case EDR2: return EdrTypeSize(type, EDR2); break; case NATIVE: return NativeTypeSize(type); break; case ASCII: return -1; break; default: return -1; break; }}/* * *** FIELD SPECS AND FIELD LISTS * - NewFieldSpec * - FreeFieldSpec * - FreeFieldList * - FreeAxisNames * - FieldLength * - FieldListLength * - FindField * - AddField * - AddSubfield * - FieldOrder * - TypeOrder *//* * Create a new FieldSpec structure. */FieldSpec *NewFieldSpec(int type, /* numeric code for data type */ int rank) /* number of dimensions */{ FieldSpec *field; /* new FieldSpec structure */ /* Check for bad arguments. */ if (!ValidType(type) || rank < 0) return NULL; /* Allocate new struct. */ field = (FieldSpec *) malloc(sizeof(*field)); if (field == NULL) return NULL; /* Fill in default values for members. */ field->type = type; field->rank = rank; if (type == NO_TYPE || rank == 0) field->dim = NULL; else { /* Allocate dimension vector according to rank. */ field->dim = (long *) malloc(rank * sizeof(long)); if (field->dim == NULL) { free(field); return NULL; } } field->occurrence = REQUIRED; field->name = NULL; field->subfields = NULL; field->units = NULL; field->scale = 1.0; field->offset = 0.0; field->axis_names = NULL; field->data = NULL; field->present = TRUE; field->fullname = NULL; return field;}/* * Free storage for a field specification, not including the subfield * list. */voidFreeFieldSpec(FieldSpec *spec){ int i; if (spec == NULL) return; if (spec->name != NULL) free(spec->name); if (spec->dim != NULL) free(spec->dim); if (spec->units != NULL) free(spec->units); if (spec->axis_names != NULL) { for (i = 0; i < spec->rank; i++) if (spec->axis_names[i] != NULL) free(spec->axis_names[i]); free(spec->axis_names); } if (spec->data != NULL) free(spec->data); if (spec->fullname != NULL) free(spec->fullname); free(spec);}/* * Free storage for a field list, including all field specifications, * and (recursively) their subfield lists. */voidFreeFieldList(FieldList list){ int i; if (list == NULL) return; for (i = 0; list[i] != NULL; i++) { FreeFieldList(list[i]->subfields); FreeFieldSpec(list[i]); } free(list);}/* * Free all non-NULL elements of a string array and the array itself. */voidFreeAxisNames(char **axis_names, int rank){ int i; if (axis_names != NULL) { for (i = 0; i < rank; i++) if (axis_names[i] != NULL) free(axis_names[i]); free(axis_names); }}/* * Number of array elements of a field. Zero in case of error. */longFieldLength(FieldSpec *field){ if (field == NULL || (field->rank != 0 && field->dim == NULL) || field->type == NO_TYPE) { return 0; } return LongProd(field->rank, field->dim);}/* * Number of top-level fields on a field list. */intFieldListLength(FieldList list){ /* This depends on field lists being implemented as * NULL-terminated arrays of pointers. */ return FieldArrayLength(list);}/* * Find field specification with given full name on given * field list (including tree of subfields). */FieldSpec *FindField(FieldList list, /* field list */ char *name) /* full name of field */{ char *prefix; /* first component of name */ char *tail; /* rest of name after prefix */ FieldSpec *ancestor; /* parent of named field spec (or parent of parent ...). */ FieldSpec *field; /* named field spec */ /* Check for bad or empty arguments. */ if (list == NULL || *list == NULL || name == NULL) return NULL; /* Parse name. */ tail = strchr(name, DOT); /* Handle simple case immediately or complex case by recursion. */ if (tail == NULL) /* Just one component. */ return GetField(list, name); else /* Multi-component name. */ { tail++; /* Skip over dot. */ prefix = FirstComponent(name); if (prefix == NULL) return NULL; ancestor = GetField(list, prefix); if (ancestor == NULL) /* Search failed. */ field = NULL; else /* Descend into subfields. */ field = FindField(ancestor->subfields, tail); free(prefix); return field; }}/* * Add a field at top level to a field list. */intAddField(FieldList *list, /* variable containing field list */ FieldSpec *field) /* specification of field */{ int n; /* length of list */ FieldList new_list; /* new field list */ /* Check for bad argument. */ if (field == NULL || list == NULL) return FALSE; /* Extend list with 1 new pointer plus terminating NULL. */ if (*list == NULL) { n = 0; new_list = (FieldList) malloc(2 * sizeof(**list)); } else { n = FieldListLength(*list); new_list = (FieldList) realloc(*list, (n+2) * sizeof(**list)); } if (new_list == NULL) /* Allocation failure. */ return FALSE; new_list[n] = field; new_list[n+1] = NULL; *list = new_list; return TRUE;}/* * Add field as subfield of another field. */intAddSubfield(FieldSpec *field, /* field to aquire subfield */ FieldSpec *subfield) /* subfield to be added */{ /* Check for bad arguments. */ if (field == NULL || subfield == NULL) return FALSE; /* Failure. */ /* Add subfield. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -