📄 esig_edr.c
字号:
if (nopt % 8 == 0) { if (EdrWriteUchar(&flags, 1, file) != 1) { DebugMsg(2, ("WriteEdrRecord: couldn't write " "\"presence\" flag for OPTIONAL field.")); return FALSE; } flags = 0; } else flags <<= 1; } } if (nopt % 8 != 0) { flags <<= (7 - nopt % 8); if (EdrWriteUchar(&flags, 1, file) != 1) { DebugMsg(1, ("WriteEdrRecord: 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 (!WriteEdrData(fields[i], file, longlong)) { DebugMsg(2, "WriteEdrRecord: couldn't write field data."); return FALSE; } } } return TRUE;}/* * Write nrec one-field records to file in EDR1 or EDR2 format, * depending on the value of longlong. The values are obtained * from 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 written. */longWriteEdrSamples(void *data, long nrec, FieldSpec **fields, FILE *file, int longlong){ int type; long length; if (data == NULL) { DebugMsg(1, "WriteEdrSamples: NULL data pointer."); return 0; } if (nrec < 0) { DebugMsg(1, ("WriteEdrSamples: " "negative number of records specified.")); return 0; } if (fields == NULL || fields[0] == NULL || fields[1] != NULL || fields[0]->occurrence != REQUIRED) { DebugMsg(1, "WriteEdrSamples: bad \"fields\" array."); return 0; } if (file == NULL) { DebugMsg(1, "WriteEdrSamples: 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 EdrWrite(data, type, nrec*length, file, longlong) /length;}/* * LOCAL FUNCTION DEFINITIONS *//* * *** FUNCTIONS FOR INPUT *//* * Read "data" member of field from file in EDR format; * allocate if NULL. Return TRUE on success, FALSE on failure. */static intReadEdrData(FieldSpec *field, FILE *file, int longlong){ long size; /* size of element (bytes) */ long length; /* number of elements */ if (file == NULL || field == NULL || field->type == NO_TYPE) { DebugMsg(1, "ReadEdrData: NULL argument or type NO_TYPE."); return FALSE; } size = InternTypeSize(field->type); length = FieldLength(field); if (field->data == NULL && length != 0) { field->data = malloc(length * size); if (field->data == NULL) { DebugMsg(1, "ReadEdrData: allocation failure."); return FALSE; } } return (EdrRead(field->data, field->type, length, file, longlong) == length);}/* * Read field specification in EDR format from file and return * pointer to it; return NULL on failure. */static FieldSpec *ReadEdrFieldSpec(FILE *file, int longlong){ char *name; /* field name */ short type; /* data type code */ short rank; /* number of dimensions */ int i; /* loop index */ FieldSpec *field; /* field spec being read */ long num_ax_names; /* number of axis names */ /* Read name, type, rank. */ if (!ReadEdrString(&name, file, longlong)) return NULL; /* Failure getting field name. */ if (EdrReadShort(&type, 1, file) != 1) return NULL; /* Couldn't get type. */ if (EdrReadShort(&rank, 1, file) != 1) return NULL; /* Couldn't get rank. */ /* Allocate structure. */ field = NewFieldSpec(type, rank); if (field == NULL) return NULL; /* Couldn't create field spec. */ field->name = name; /* Read dimensions. */ if (rank != 0 && field->dim == NULL) return NULL; /* Inconsistent rank and dimensions. */ if (EdrReadLong(field->dim, rank, file, longlong) != rank) return NULL; /* Couldn't get dimensions. */ /* Read units, scale, offset, axis_names. */ if (!ReadEdrString(&field->units, file, longlong)) return NULL; if (EdrReadDouble(&field->scale, 1, file) != 1) return NULL; /* Couldn't get scale. */ if (EdrReadDouble(&field->offset, 1, file) != 1) return NULL; /* Couldn't get offset. */ if (EdrReadLong(&num_ax_names, 1, file, longlong) != 1) return NULL; /* Couldn't get number of axis names. */ if (num_ax_names > rank) return NULL; /* Bad value for num_ax_names. */ if (num_ax_names != 0) { field->axis_names = (char **) malloc(rank * sizeof(char *)); if (field->axis_names == NULL) return NULL; /* Allocation failure. */ for (i = 0; i < num_ax_names; i++) ReadEdrString(&field->axis_names[i], file, longlong); for ( ; i < rank; i++) field->axis_names[i] = NULL; } /* Read occurrence class. */ if (EdrReadShort(&field->occurrence, 1, file) != 1) return NULL; /* Couldn't get occurrence code. */ /* Read data if required. */ if (type != NO_TYPE && field->occurrence != REQUIRED && field->occurrence != OPTIONAL) { if (!ReadEdrData(field, file, longlong)) return NULL; /* Failure reading data. */ } /* Read subfield list. */ if (!ReadEdrFieldList(&field->subfields, file, longlong)) { FreeFieldSpec(field); return NULL; /* Failure getting subfields. */ } return field; /* Success. */}/* * Read character string in EDR format from file and output * pointer to it. Return TRUE on success, FALSE on failure. */static intReadEdrString(char **string, FILE *file, int longlong){ long length; char *str; long i; int ch; if (EdrReadLong(&length, 1, file, longlong) != 1) return FALSE; /* Couldn't get length. */ str = (char *) malloc(length + 1); if (str == NULL) return FALSE; for (i = 0; i < length && (ch = getc(file)) != EOF; i++) str[i] = ch; if (i < length) { free(str); return FALSE; } str[length] = '\0'; if (string != NULL) *string = str; return TRUE;}/* * Read items of data from a file into a block of storage indicated * by a pointer "data". The number of items is given by "length", * and their data type is indicated by the integer code "type". * Return the number of successfully read items. (A value other * than "length" indicates an error). */static intEdrRead(void *data, int type, long length, FILE *file, int longlong){ if (file == NULL || data == NULL || length <= 0) return 0; switch (type) { case ARRAY: return EdrReadArray((Array *) data, length, file, longlong); case DOUBLE: return EdrReadDouble((double *) data, length, file); case FLOAT: return EdrReadFloat((float *) data, length, file); case LONG: return EdrReadLong((long *) data, length, file, longlong); case ULONG: return EdrReadUlong((Ulong *) data, length, file, longlong); case SHORT: return EdrReadShort((short *) data, length, file); case USHORT: return EdrReadUshort((Ushort *) data, length, file); case SCHAR: return EdrReadSchar((Schar *) data, length, file); case UCHAR: return EdrReadUchar((Uchar *) data, length, file); case BOOL: return EdrReadBool((Bool *) data, length, file); case DOUBLE_COMPLEX: return EdrReadDoubleComplex((DoubleComplex *) data, length, file); case FLOAT_COMPLEX: return EdrReadFloatComplex((FloatComplex *) data, length, file); case LONG_COMPLEX: return EdrReadLongComplex((LongComplex *) data, length, file, longlong); case SHORT_COMPLEX: return EdrReadShortComplex((ShortComplex *) data, length, file); case SCHAR_COMPLEX: return EdrReadScharComplex((ScharComplex *) data, length, file); case CHAR: return EdrReadChar((char *) data, length, file); case WCHAR: return EdrReadWchar((Wchar *) data, length, file); default: /* Invalid code or NO_TYPE. */ return 0; }}/* * Read from file the EDR1 or EDR2 representation of one item of type * ARRAY. Store the type, rank, dimensions, and data in the members of * the array structure, allocating the necessary space for dimensions * and data. Return TRUE for success, FALSE for failure. */static intReadEdrArray(Array *array, FILE *file, int longlong){ short type; /* data type code */ short rank; /* number of dimensions */ long *dim; /* dimensions */ void *data; long length; long size; if (array == NULL || file == NULL) return FALSE; if (EdrReadShort(&type, 1, file) != 1) return FALSE; /* Couldn't get type. */ if (EdrReadShort(&rank, 1, file) != 1) return FALSE; /* Couldn't get rank. */ if (rank == 0) dim = NULL; else { dim = (long *) malloc(rank * sizeof(long)); if (dim == NULL) return FALSE; if (EdrReadLong(dim, rank, file, longlong) != rank) { free(dim); return FALSE; } } length = LongProd(rank, dim); size = InternTypeSize(type); if (length == 0) data = NULL; else { data = malloc(length*size); if (data == NULL || EdrRead(data, type, length, file, longlong) != length) { if (dim != NULL) free(dim); return FALSE; } } array->type = type; array->rank = rank; array->dim = dim; array->data = data; return TRUE;}/* * Each of the following functions with names of the form * * EdrRead<type> * * takes three arguments: * <type> *data, * long length, * FILE *file, * and in some cases a fourth: * int longlong * The function reads "length" items of data of the indicated data type * in EDR portable binary format from the stream "file" into a block of * storage indicated by "data". The fourth argument, "longlong", is * present when the EDR1 and EDR2 representations of the data type differ; * a value of 1 indicates EDR1, and 2 indicates EDR2. The int return * value is the number of items successfully read, which will be less * than "length" in case of error. *//* Read items of type ARRAY */static intEdrReadArray(Array *data, long length, FILE *file, int longlong){ long n; for (n = 0; n < length && ReadEdrArray(&data[n], file, longlong); n++) { } return n;}/* Read items of type DOUBLE */static intEdrReadDouble(double *data, long length, FILE *file){ long n; /* number of items read */ unsigned long hi; /* 28 high_order fraction bits (and hidden bit) */ unsigned long lo; /* 24 low_order fraction bits */ int ex; /* exponent */ int sn; /* sign */ int ch; /* input character */ double item; /* one input data item */ for (n = 0; n < length; n++) { if ((ch = getc(file)) == EOF) break; sn = ((ch & 0x80) != 0); /* sign */ ex = ch & 0x7f; /* 7 bits of exponent */ if ((ch = getc(file)) == EOF) break; ex = (ex << 4) | (ch >> 4); /* rest of exponent */ hi = ch & 0x0f; /* first 4 fraction bits */ if ((ch = getc(file)) == EOF) break; hi = (hi << 8) | ch; if ((ch = getc(file)) == EOF) break; hi = (hi << 8) | ch;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -