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

📄 esig_asc.c

📁 隐马尔科夫模型工具箱
💻 C
📖 第 1 页 / 共 5 页
字号:
      return FALSE;   name[i] = '\0';   if (ch != EOF)      ungetc(ch, file);   code = TypeCode(name);   if (code == 0)      return FALSE;   *type = code;   return TRUE;}/* * Read a possibly empty sequence of unsigned longs (typically * array dimensions) from file.  If it's nonempty, allocate an * array to hold the values.  Assign to *rank the number of values * read.  Assign to *dim a pointer to the allocated array, if any, * or NULL if the sequence was empty.  (But if rank or dim is NULL, * omit the corresponding assignment.)  Return TRUE for success, * FALSE for error. */static intReadAsciiDims(short *rank, long **dim, FILE *file){   long    d;   long    *dd;   int     r;   /*! Check for overflow.  fscanf doesn't, and Sun's strtol doesn't. */   if (!ReadSpace(file) || fscanf(file, "%lu", &d) != 1)      {         r = 0;         dd = NULL;      }   else      {         dd = (long *) malloc(sizeof(*dd));         if (dd == NULL)            return FALSE;         dd[0] = d;         for (r = 1;              ReadSpace(file) && fscanf(file, "%lu", &d) == 1;              r++)            {               dd = (long *) realloc(dd, (r + 1) * sizeof(*dd));               if (dd == NULL)                  return FALSE;               dd[r] = d;            }      }   if (rank != NULL)      *rank = r;   if (dim != NULL)      *dim = dd;   return TRUE;}/* * Read Ascii representations of the miscellaneous attributes * (units, scale, offset, axis_names) from file, and output the * values found by assignment to the variables *units, *scale, *offset, * and *axis_names (if defined).  If no explicit representation * for an attribute is found, omit the corresponding assignment. * (The caller is assumed to have assigned default values to the * variables before calling ReadAsciiMisc).  If any of the arguments * units, scale, offset, and axis_names is NULL, omit the corresponding * assignment.  Return TRUE upon success and FALSE in case of error. */static intReadAsciiMisc(int rank, char **units, double *scale,              double *offset, char ***axis_names, FILE *file){   /*    * Length of longest attribute name ("axis_names"), including    * terminal null character.    */#define MAX_ATTR        11   char    attr[MAX_ATTR];   int     ch;   int     i;   char    *un;   double  sc;   double  offsetr;   char    **ax;   char    got_units = FALSE;   char    got_scale = FALSE;   char    got_offset = FALSE;   char    got_axis_names = FALSE;   ch = GetNonSpace(file);   if (ch != '{')      {         ungetc(ch, file);         return TRUE;      }   do {      ch = GetNonSpace(file);      for (i = 0; i < MAX_ATTR && (islower(ch) || ch == '_'); i++)         {            attr[i] = ch;            ch = getc(file);         }      if (i == MAX_ATTR)         return FALSE;      attr[i] = '\0';      ch = SkipSpace(ch, file);      if (ch != ':')         return FALSE;      if (strcmp(attr, "units") == 0)         {            if (!got_units++                && ReadAsciiString(&un, file))               {                  if (units != NULL)                     *units = un;               }            else               return FALSE;         }      else if (strcmp(attr, "scale") == 0)         {            if (!got_scale++                && ReadAsciiDouble(&sc, file))               {                  if (scale != NULL)                     *scale = sc;               }            else               return FALSE;         }      else if (strcmp(attr, "offset") == 0)         {            if (!got_offset++                && ReadAsciiDouble(&offsetr, file))               {                  if (offset != NULL)                     *offset = offsetr;               }            else               return FALSE;         }      else if (strcmp(attr, "axis_names") == 0)         {            if (!got_axis_names++                && ReadAsciiAxisNames(&ax, rank, file))               {                  if (axis_names != NULL)                     *axis_names = ax;               }            else               return FALSE;         }      else         return FALSE;      ch = GetNonSpace(file);   } while (ch == ';');   if (ch != '}')      return FALSE;   return TRUE;#undef MAX_ATTR}/* * Read from file a string constant consisting of Ascii printing characters * and including surrounding double quote characters("").  Interpret escape * sequences \\, \?, \', and \"  by removing the backslash.  Allocate space * to hold the string value, including terminating null character.  Store * a pointer to the string in the variable whose address is given by * string (unless string is NULL).  Return TRUE on success, FALSE on * failure. */static intReadAsciiString(char **string, FILE *file){   int     ch;   char    *str = NULL;   long    alloc_len = 0;   long    len = 0;   ch = GetNonSpace(file);   if (ch != '"')      return FALSE;   for (ch = getc(file); isprint(ch) && ch != '"'; ch = getc(file))      {         if (ch == '\\')            {               ch = getc(file);               switch (ch)                  {                  case '\\':    /* fall through */                  case '?':     /* fall through */                  case '\'':    /* fall through */                  case '"':                     if (!AddChar(ch, &str, &alloc_len, &len))                        return FALSE;                     break;                  default:                     free(str);                     return FALSE;                  }            }         else if (!AddChar(ch, &str, &alloc_len, &len))            return FALSE;      }   if (ch != '"' || !AddChar('\0', &str, &alloc_len, &len))      {         free(str);         return FALSE;      }   if (string != NULL)      *string = str;   return TRUE;}/* * Read a floating-point constant (with optional leading whitespace) * from file and assign the value to the variable whose address * is given by x. */static intReadAsciiDouble(double *x, FILE *file){   return (ReadOptSpace(file) && fscanf(file, "%lf", x) == 1);}/* * Read from file a comma-separated list with at most "rank" members, * each of which is either a C identifier or empty.  (This construct * occurs in the Ascii form of a specification for the miscellaneous * field attribute "axis_names".)  Allocate a pointer array of length * rank (or NULL if rank is 0) and assign to its elements: * - a string pointer for each identifier on the list, * - a NULL for each empty member of the list * - a NULL for each array position beyond the end of the list. * Store a pointer to the array (or NULL) in the variable whose * address is given by "axis_names" (unless that argument is NULL). * Return TRUE for success, FALSE for failure. */static intReadAsciiAxisNames(char ***axis_names, int rank, FILE *file){   char    **ax;   int     ch;   char    *str;   long    len;   long    alloc_len;   int     i;   if (rank == 0)      {         ax = NULL;         ch = GetNonSpace(file);         if (isalpha(ch) || ch == '_' || ch == ',')            return FALSE;      }   else      {         ax = (char **) malloc(rank * sizeof(*ax));         if (ax == NULL)            return FALSE;         i = 0;         do {            ch = GetNonSpace(file);            if (isalpha(ch) || ch == '_')               {                  str = NULL;                  len = 0;                  alloc_len = 0;                  if (!AddChar(ch, &str, &alloc_len, &len))                     {                        FreeAxisNames(ax, rank);                        return FALSE;                     }                  for (ch = getc(file);                       isalnum(ch) || ch == '_';                       ch = getc(file))                     {                        if (!AddChar(ch, &str, &alloc_len, &len))                           {                              FreeAxisNames(ax, rank);                              return FALSE;                           }                     }                  if (!AddChar('\0', &str, &alloc_len, &len))                     {                        FreeAxisNames(ax, rank);                        return FALSE;                     }                  ax[i] = str;                  ch = SkipSpace(ch, file);               }            else               ax[i] = NULL;            i++;         } while (ch == ',' && i < rank);         if (ch == ',')            {               FreeAxisNames(ax, rank);               return FALSE;            }         else            for ( ; i < rank; i++)               ax[i] = NULL;      }   ungetc(ch, file);   if (axis_names != NULL)      *axis_names = ax;   return TRUE;}/* * Read from file an "occurrence" construct (which occurs in Ascii field * specifications: <g>, <r>, <o>, <v), or <i>.  Assign the corresponding code * (GLOBAL, REQUIRED, OPTIONAL, VIRTUAL, or INCLUDED) to the variable * whose address is occurrence (unless that argument is NULL). * Return TRUE for success, FALSE for failure. */static intReadAsciiOccurrence(short *occurrence, FILE *file){   int     ch;   int     occ;   ch = GetNonSpace(file);   if (ch != '<')      return FALSE;   switch (getc(file))      {      case 'g':         occ = GLOBAL;         break;      case 'r':         occ = REQUIRED;         break;      case 'o':         occ = OPTIONAL;         break;      case 'v':         occ = VIRTUAL;         break;      case 'i':         occ = INCLUDED;         break;      default:         return FALSE;      }   if (getc(file) != '>')      return FALSE;   if (occurrence != NULL)      *occurrence = occ;   return TRUE;}/* * Read from "file" a sequence of length "length" of values of data * type "type" (in Ascii representation); store the results starting * at the location indicated by "datap".  Return the number of values * successfully read, which will be less than "length" in case of * error. */static intAsciiRead(void  *datap,          int   type,          long  length,          FILE  *file){   long    j;   int     ch;   if (length == 0)      return TRUE;   if (!ReadOptSpace(file))      return 0;   j = 0;   switch (type)      {      case ARRAY:         {            Array   *data = (Array *) datap;            do {               ch = getc(file);               if (ch != '(')                  return j;               if (!ReadOptSpace(file)                   || !ReadAsciiArray(&data[j], file))                  {                     return j;                  }               ch = GetNonSpace(file);               if (ch != ')')                  return j;            } while (++j < length && ReadSpace(file));         }         break;      case DOUBLE:         {            double  *data = (double *) datap;            do {               if (fscanf(file, "%lf", &data[j]) != 1)                  return j;            } while (++j < length && ReadSpace(file));         }         break;      case FLOAT:         {            float   *data = (float *) datap;            do {               if (fscanf(file, "%f", &data[j]) != 1)                  return j;            } while (++j < length && ReadSpace(file));         }         break;      case LONG:         {            long    *data = (long *) datap;            do {               if (fscanf(file, "%ld", &data[j]) != 1)                  return j;

⌨️ 快捷键说明

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