int WriteFieldList(FieldList list, int arch, FILE *file, Annot *annotate) { switch (arch) { case EDR1: return WriteEdrFieldList(list, file, EDR1); break; case EDR2: return WriteEdrFieldList(list, file, EDR2); break; case NATIVE: return WriteNativeFieldList(list, file); break; case ASCII: return WriteAsciiFieldList(list, file, annotate); break; default: return FALSE; break; } }
static int WriteNativeFieldSpec(FieldSpec *field, FILE *file) { int rank; /* number of dimensions */ int i; /* loop index */ long num_ax_names; /* number of axis names */ if (file == NULL || field == NULL) return FALSE; if (!WriteNativeString(field->name, file)) return FALSE; if (fwrite(&field->type, sizeof(short), 1, file) != 1) return FALSE; if (fwrite(&field->rank, sizeof(short), 1, file) != 1) return FALSE; rank = field->rank; if (rank != 0 && field->dim == NULL) return FALSE; /* Inconsistent rank and dimensions. */ if (fwrite(field->dim, sizeof(long), rank, file) != rank) return FALSE; if (!WriteNativeString(field->units, file)) return FALSE; if (fwrite(&field->scale, sizeof(double), 1, file) != 1) return FALSE; if (fwrite(&field->offset, sizeof(double), 1, file) != 1) return FALSE; num_ax_names = (field->axis_names == NULL) ? 0 : rank; if (fwrite(&num_ax_names, sizeof(long), 1, file) != 1) return FALSE; for (i = 0; i < num_ax_names; i++) if (!WriteNativeString(field->axis_names[i], file)) return FALSE; if (fwrite(&field->occurrence, sizeof(short), 1, file) != 1) return FALSE; if (field->type != NO_TYPE && field->occurrence != REQUIRED && field->occurrence != OPTIONAL) { if (!WriteNativeData(field, file)) return FALSE; } if (!WriteNativeFieldList(field->subfields, file)) return FALSE; return TRUE; }
int WriteHeader(FieldList list, int arch, FILE *file, Annot *annotate) { FILE *temp; /* temporary file */ int okay; /* success or failure? */ long rec_size; /* record size */ long fld_size; /* field-list size */ static char buf[BUFSIZ]; /* buffer for file copy */ char *architecture; /* architecture name */ int n; /* byte count for file copy */ long tot; /* byte count total for file copy */ /* Make temp file. */ temp = tmpfile(); if (temp == NULL) return FALSE; /* Failure to make temp file. */ /* Write field list to temp file; get record size. */ switch (arch) { case NATIVE: okay = WriteNativeFieldList(list, temp); architecture = EsignalArch; if (okay) rec_size = NativeRecordSize(list); break; case EDR1: /* * On machines whose native architecture is EDR1, could call * WriteNativeFieldList here. */ okay = WriteEdrFieldList(list, temp, EDR1); architecture = "EDR1"; if (okay) rec_size = EdrRecordSize(list, EDR1); break; case EDR2: /* * On machines whose native architecture is EDR2, could call * WriteNativeFieldList here. */ okay = WriteEdrFieldList(list, temp, EDR2); architecture = "EDR2"; if (okay) rec_size = EdrRecordSize(list, EDR2); break; case ASCII: okay = WriteAsciiFieldList(list, temp, annotate); architecture = "ASCII"; if (okay) rec_size = -1; break; default: DebugMsg(1, "WriteHeader: unrecognized architecture code."); okay = FALSE; /* Unsupported architecture. */ break; } if (!okay) { fclose(temp); return FALSE; /* Failure to write field list. */ } /* Get field-list size */ fld_size = ftell(temp); /* Write preamble. */ if (!WritePreamble(architecture, fld_size, rec_size, file)) return FALSE; /* Failure to write preamble. */ /* Copy field list from temp file. */ rewind(temp); tot = 0; while ((n = fread(buf, 1, BUFSIZ, temp)) > 0) { fwrite(buf, 1, n, file); tot += n; } if (tot != fld_size) return FALSE; /* I/O error or wrong fld_size. */ return TRUE; /* success */ }