⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 esig_nat.c

📁 隐马尔科夫模型工具箱
💻 C
📖 第 1 页 / 共 2 页
字号:
/* ----------------------------------------------------------- *//*                                                             *//*                          ___                                *//*                       |_| | |_/   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. * native binary I/O. * * Author:  Rod Johnson */#include <esignal.h>/* * LOCAL CONSTANTS *//* *  LOCAL FUNCTION DECLARATIONS *//* Functions for input */static int          ReadNativeData(FieldSpec *field, FILE *file);static int          NativeRead(void *data,                               int type, long length, FILE *file);static int          ReadNativeArray(Array *array, FILE *file);static FieldSpec    *ReadNativeFieldSpec(FILE *file);static int          ReadNativeString(char **string, FILE *file);/* Functions for output */static int          WriteNativeData(FieldSpec *field, FILE *file);static int          NativeWrite(void *data,                                int type, long length, FILE *file);static int          WriteNativeArray(Array *array, FILE *file);static int          WriteNativeFieldSpec(FieldSpec *field, FILE *file);static int          WriteNativeString(char *string, FILE *file);/* * PUBLIC FUNCTION DEFINITIONS * - NativeTypeSize * - NativeRecordSize * - ReadNativeFieldList * - ReadNativeRecord * - ReadNativeSamples * - WriteNativeFieldList * - WriteNativeRecord * - WriteNativeSamples *//* * Size in bytes of native binary external representation of data type. */longNativeTypeSize(int type   /* numeric data-type code */ ){   if (type == ARRAY)      return -1;  /* Variable-length external representation. */   else      return InternTypeSize(type);}/* * Return the size, in bytes, of one record in native binary format, * according to the field specs on a field list. * Return 0 on NULL input; -1 is a code for variable-length records. */longNativeRecordSize(FieldList list){   long size;           /* total array size in bytes */   FieldSpec    **fld_order;   long i;   long fld_size;   fld_order = FieldOrder(list);   if (fld_order == NULL)      return 0;   size = 0;   for (i = 0 ; fld_order[i] != NULL; i++)      {         if (fld_order[i]->occurrence == OPTIONAL)            {               size = -1;               break;            }         fld_size =            FieldLength(fld_order[i])            * NativeTypeSize(fld_order[i]->type);         if (fld_size < 0)            {               size = -1;               break;            }         else            size += fld_size;      }   free(fld_order);   return size;}/* * Read field list in native binary format from file. * Return TRUE on success, FALSE on failure. */intReadNativeFieldList(FieldList *listp, /* output variable */                    FILE      *file) /* input file */{   FieldList    list;   long num_fields;   long i;   FieldSpec    *spec;   if (fread(&num_fields, sizeof(long), 1, file) != 1)      {         DebugMsg(1, "ReadNativeFieldList: Couldn't get number of fields.");         return FALSE;      }   list = NULL;   for (i = 0; i < num_fields; i++)      {         spec = ReadNativeFieldSpec(file);         if (spec == NULL || !AddField(&list, spec))            {               if (list != NULL)                  FreeFieldList(list);               DebugMsg(2, ((spec == NULL)                            ? "ReadNativeFieldList: couldn't read field spec."                            : ("ReadNativeFieldList: "                               "couldn't add field spec to list.")));               return FALSE;            }      }   *listp = list;   return TRUE;}/* * Read one record in native binary format from file into the "data" * members of the field specs on a NULL-terminated linear array * of REQUIRED and OPTIONAL fields, like those produced by TypeOrder * and FieldOrder.  Return TRUE on success, FALSE on failure. */intReadNativeRecord(FieldSpec  **fields,                 FILE       *file){   long    i;   long    nopt;   Uchar   flags;   if (file == NULL || fields == NULL)      {         DebugMsg(1, "ReadNativeRecord: NULL argument.");         return FALSE;      }   /*! If FieldOrder & TypeOrder returned a linear array of OPTIONAL    * fields as well as the array of REQUIRED & OPTIONAL, we could    * avoid scanning all of "fields" checking for OPTIONAL entries    * for every record read. */   nopt = 0;   for (i = 0 ; fields[i] != NULL; i++)      {         if (fields[i]->occurrence == OPTIONAL)            {               if (nopt % 8 == 0)                  {                     if (fread(&flags, 1, 1, file) != 1)                        {                           DebugMsg(1, ("ReadNativeRecord: can't read "                                        "\"presence\" flag for OPTIONAL field."));                           return FALSE;                        }                  }               else                  flags <<= 1;               fields[i]->present = ((flags & 0x80) != 0);               nopt++;            }      }   for (i = 0 ; fields[i] != NULL; i++)      if (fields[i]->occurrence == REQUIRED || fields[i]->present)         {            if (!ReadNativeData(fields[i], file))               {                  DebugMsg(1, "ReadNativeRecord: couldn't read field data.");                  return FALSE;               }         }   return TRUE;}/* * Read nrec one-field records in native binary format from file into * the array indicated by data.  The field is specified by fields, * a NULL-terminated linear array like those produced by TypeOrder * and FieldOrder and containing exactly one REQUIRED field. * Return the number of complete records read. */longReadNativeSamples(void      *data,                  long      nrec,                  FieldSpec **fields,                  FILE      *file){   int          type;   long length;   if (data == NULL)      {         DebugMsg(1, "ReadNativeSamples: NULL data pointer.");         return 0;      }   if (nrec < 0)      {         DebugMsg(1, ("ReadNativeSamples: "                      "negative number of records specified."));         return 0;      }   if (fields == NULL || fields[0] == NULL || fields[1] != NULL       || fields[0]->occurrence != REQUIRED)      {         DebugMsg(1, "ReadNativeSamples: bad \"fields\" array.");         return 0;      }   if (file == NULL)      {         DebugMsg(1, "ReadNativeSamples: NULL file pointer.");         return 0;      }   type = fields[0]->type;   if (type == NO_TYPE)      return 0;   length = FieldLength(fields[0]);   if (nrec*length == 0)      return nrec;   return NativeRead(data, type, nrec*length, file) / length;}/* * Write field list to file in native binary format. * Return TRUE on success, FALSE on failure. */intWriteNativeFieldList(FieldList list,                     FILE      *file){   long    num_fields;          /* number of fields */   long    i;                   /* loop index */   if (file == NULL)      {         DebugMsg(1, "WriteNativeFieldList: NULL file pointer.");         return FALSE;      }   num_fields = FieldListLength(list);   if (fwrite(&num_fields, sizeof(long), 1, file) != 1)      {         DebugMsg(1, "WriteNativeFieldList: couldn't write number of fields.");         return FALSE;      }   for (i = 0; i < num_fields; i++)      if (!WriteNativeFieldSpec(list[i], file))         {            DebugMsg(2, "WriteNativeFieldList: couldn't write field spec.");            return FALSE;         }   return TRUE;         /* Success. */}/* * Write to file one record in native binary format, consisting of the * contenta of the "data" members of the field specs on a NULL-terminated * linear array of REQUIRED and OPTIONAL fields, like those produced by * TypeOrder and FieldOrder. * Return TRUE on success, FALSE on failure. */intWriteNativeRecord(FieldSpec **fields,                  FILE      *file){   long    i;   long    nopt;   Uchar   flags;   if (file == NULL || fields == NULL)      {         DebugMsg(1, "WriteNativeRecord: NULL argument.");         return FALSE;      }   /*! If FieldOrder & TypeOrder returned a linear array of OPTIONAL    * fields as well as the array of REQUIRED & OPTIONAL, we could    * avoid scanning all of "fields" checking for OPTIONAL entries    * for every record written. */   nopt = 0;   flags = 0;   for (i = 0; fields[i] != NULL; i++)      {         if (fields[i]->occurrence == OPTIONAL)            {               flags |= fields[i]->present;               nopt++;               if (nopt % 8 == 0)                  {                     if (fwrite(&flags, 1, 1, file) != 1)                        {                           DebugMsg(1, ("WriteNativeRecord:  couldn't write "                                        "\"presence\" flag for OPTIONAL field."));                           return FALSE;                        }                     flags = 0;                  }               else                  flags <<= 1;            }      }   if (nopt % 8 != 0)      {         flags <<= (7 - nopt % 8);         if (fwrite(&flags, 1, 1, file) != 1)            {               DebugMsg(1, ("WriteNativeRecord: couldn't write "                            "\"presence\" flags for OPTIONAL fields."));               return FALSE;            }      }   for (i = 0; fields[i] != NULL; i++)      {         if (fields[i]->occurrence == REQUIRED || fields[i]->present)            {               if (!WriteNativeData(fields[i], file))                  {                     DebugMsg(1, "WriteNativeRecord: couldn't write field data.");                     return FALSE;                  }            }      }   return TRUE;}/* * Write nrec one-field records in native binary format to file from the * array indicated by data.  The The field is specified by fields, a * NULL-terminated linear array like those produced by TypeOrder * and FieldOrder and containing exactly one REQUIRED field. * Return the number of complete records written. */longWriteNativeSamples(void     *data,                   long     nrec,                   FieldSpec **fields,                   FILE     *file){   int          type;   long length;   if (data == NULL)      {         DebugMsg(1, "WriteNativeSamples: NULL data pointer.");         return 0;      }   if (nrec < 0)      {         DebugMsg(1, ("WriteNativeSamples: "                      "negative number of records specified."));         return 0;      }   if (fields == NULL || fields[0] == NULL || fields[1] != NULL       || fields[0]->occurrence != REQUIRED)      {         DebugMsg(1, "WriteNativeSamples: bad \"fields\" array.");         return 0;      }   if (file == NULL)      {         DebugMsg(1, "WriteNativeSamples: NULL file pointer.");         return 0;      }   type = fields[0]->type;   if (type == NO_TYPE)      return 0;   length = FieldLength(fields[0]);   if (nrec*length == 0)      return nrec;   return NativeWrite(data, type, nrec*length, file) / length;}/* * LOCAL FUNCTION DEFINITIONS *//* * *** FUNCTIONS FOR INPUT *//* * Read "data" member of field from file in native binary format; * allocate if NULL.  Return TRUE on success, FALSE on failure. */static intReadNativeData(FieldSpec *field,               FILE *file){

⌨️ 快捷键说明

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