void FreeFieldList(FieldList list) { int i; if (list == NULL) return; for (i = 0; list[i] != NULL; i++) { FreeFieldList(list[i]->subfields); FreeFieldSpec(list[i]); } free(list); }
static FieldSpec * ReadNativeFieldSpec(FILE *file) { 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 (!ReadNativeString(&name, file)) return NULL; /* Failure getting field name. */ if (fread(&type, sizeof(short), 1, file) != 1) return NULL; /* Couldn't get type. */ if (fread(&rank, sizeof(short), 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 (fread(field->dim, sizeof(long), rank, file) != rank) return NULL; /* Couldn't get dimensions. */ /* Read units, scale, offset, axis_names. */ if (!ReadNativeString(&field->units, file)) return NULL; if (fread(&field->scale, sizeof(double), 1, file) != 1) return NULL; /* Couldn't get scale. */ if (fread(&field->offset, sizeof(double), 1, file) != 1) return NULL; /* Couldn't get offset. */ if (fread(&num_ax_names, sizeof(long), 1, file) != 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++) ReadNativeString(&field->axis_names[i], file); for ( ; i < rank; i++) field->axis_names[i] = NULL; } /* Read occurrence class. */ if (fread(&field->occurrence, sizeof(short), 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 (!ReadNativeData(field, file)) return NULL; /* Failure reading data. */ } /* Read subfield list. */ if (!ReadNativeFieldList(&field->subfields, file)) { FreeFieldSpec(field); return NULL; /* Failure getting subfields. */ } return field; /* Success. */ }