FILE *tmpfile() { FILE *tmp; char *file_name; tmp = e_temp_file(NULL, &file_name); spsassert(tmp,"Cannot create temporary file!\n"); (void)unlink(file_name); return tmp; }
static int FindStr(char *str, char **arr) { int i; spsassert(str != NULL, "FindStr: NULL string."); if (arr == NULL) return TRUE; /* NULL array implies all fields */ for (i = 0; arr[i] != NULL; i++) if (strcmp(str, arr[i]) == 0) return TRUE; return FALSE; }
static pc_wav_hd * get_hd(char *prefix, int num_read, FILE *file) { char *end_pref; pc_wav_hd *wav_hd; int got_ck_hd; char ckID[4]; unsigned long ckSize; end_pref = prefix + num_read; num_read -= PcWavPrefixSize; /* skip "RIFF", <size>, "WAVE" */ spsassert(num_read >= 0, "get_hd: error reading PC WAVE header"); wav_hd = malloc(sizeof(pc_wav_hd)); if (wav_hd == NULL) { error_msg("get_hd", "couldn't allocate storage for PC WAVE header."); return NULL; } got_ck_hd = get_ck_hd(ckID, &ckSize, end_pref, &num_read, file); while (got_ck_hd && strncmp(ckID, "fmt ", 4) != 0 && strncmp(ckID, "data", 4) != 0) { got_ck_hd = (skip_ck(ckSize, end_pref, &num_read, file) && get_ck_hd(ckID, &ckSize, end_pref, &num_read, file)); } if (!got_ck_hd) { error_msg("get_hd", "end of file within PC WAVE chunk."); return NULL; } if (strncmp(ckID, "fmt ", 4) != 0) { error_msg("get_hd", "no \"fmt\" chunk before data in PC WAVE file."); return NULL; } if (!(get_lsbf_ushort(&wav_hd->FormatTag, end_pref, &num_read, file) && get_lsbf_ushort(&wav_hd->Channels, end_pref, &num_read, file) && get_lsbf_ulong(&wav_hd->SamplesPerSec, end_pref, &num_read, file) && get_lsbf_ulong(&wav_hd->AvgBytesPerSec, end_pref, &num_read, file) && get_lsbf_ushort(&wav_hd->BlockAlign, end_pref, &num_read, file))) { error_msg("get_hd", "failed to read PC WAVE \"fmt\" chunk."); return NULL; } ckSize -= 14; /* 3 ushort & 2 ulong */ switch (wav_hd->FormatTag) { case WAVE_FORMAT_PCM: if (!get_lsbf_ushort(&wav_hd->spec.pcm.BitsPerSample, end_pref, &num_read, file)) { error_msg("get_hd", "failed to read PC WAVE \"fmt\" chunk (PCM part)."); return NULL; } ckSize -= 2; /* 1 ushort */ break; default: error_msg("get_hd", "only WAVE_FORMAT_PCM supported."); return NULL; } if (ckSize < 0) { error_msg("get_hd", "inconsistent chunk size for PC WAVE \"fmt\"."); return NULL; } do { got_ck_hd = (skip_ck(ckSize, end_pref, &num_read, file) && get_ck_hd(ckID, &ckSize, end_pref, &num_read, file)); } while(got_ck_hd && strncmp(ckID, "data", 4) != 0); if (!got_ck_hd) { error_msg("get_hd", "failed to find PC WAVE \"data\" chunk."); return NULL; } if (num_read > 0) { error_msg("get_hd", "read too far ahead."); return NULL; } wav_hd->data_len = ckSize; return wav_hd; }
static struct header * FieldList_to_fea(FieldList list, struct fea_data **rec, char **fnames, int copy_sources) { struct header *hdr; int i, j; FieldSpec *fld; char **codes; FieldList subfields; FieldSpec *subf; char *name; void *ptr; int spstype; long *dim; struct fea_header *fea; struct header *source; if (list == NULL) return NULL; hdr = new_header(FT_FEA); for (i = 0; (fld = list[i]) != NULL; i++) { codes = NULL; if (fld->occurrence != VIRTUAL && (subfields = fld->subfields) != NULL) { for (j = 0; (subf = subfields[j]) != NULL; j++) { if (strcmp(subf->name, "enumStrings") != 0) { if (debug_level) fprintf(stderr, "FieldList_to_fea: subfields " "not supported in ESPS FEA files.\n"); } else if (fld->type != SHORT) { if (debug_level) fprintf(stderr, "FieldList_to_fea: Non-SHORT field " "has subfield enumStrings.\n"); } else if (subf->type != CHAR) { if (debug_level) fprintf(stderr, "FieldList_to_fea: enumStrings not CHAR.\n"); } else if (subf->rank != 2) { if (debug_level) fprintf(stderr, "FieldList_to_fea: enumStrings " "not of rank 2.\n"); } else codes = StrArrayFromRect(subf->dim, subf->data); } } if (FieldIsTag(fld) && FindStr(ESPS_TAG, fnames)) hdr->common.tag = TRUE; else if (FieldIsFeaSubtype(fld)) hdr->hd.fea->fea_type = *(short *) fld->data; else { name = fld->name; switch(fld->occurrence) { case GLOBAL: { int size = (int) FieldLength(fld); ptr = fld->data; if (debug_level >= 2) fprintf(stderr, "FieldList_to_fea: " "global field[%d]: \"%s\".\n", i, name); if (fld->rank > 1) { if (debug_level) fprintf(stderr, "FieldList_to_fea: rank %d globals " "not supported in ESPS FEA files.\n", fld->rank); } if (size == 0) { if (debug_level) fprintf(stderr, "FieldList_to_fea: empty globals " "not supported in ESPS FEA files.\n"); } else if (codes != NULL) (void) add_genhd_e(name, (short *) ptr, size, codes, hdr); else switch (fld->type) { case DOUBLE: (void) add_genhd_d(name, (double *) ptr, size, hdr); break; case FLOAT: (void) add_genhd_f(name, (float *) ptr, size, hdr); break; case LONG: (void) add_genhd_l(name, (long *) ptr, size, hdr); break; case SHORT: (void) add_genhd_s(name, (short *) ptr, size, hdr); break; case SCHAR: case UCHAR: case CHAR: (void) add_genhd_c(name, (char *) ptr, size, hdr); break; default: if (debug_level) fprintf(stderr, "FieldList_to_fea: global type %d " "not supported in ESPS FEA files.\n", fld->type); } } break; case REQUIRED: { long size = FieldLength(fld); if (debug_level >= 2) fprintf(stderr, "FieldList_to_fea: " "required field[%d]: \"%s\".\n", i, name); if (FindStr(name, fnames)) { spstype = (codes != NULL) ? CODED : ElibTypeToEsps(fld->type); if (spstype != UNDEF) { dim = (long *) malloc(fld->rank * sizeof(long)); for (j = 0; j < fld->rank; j++) dim[j] = fld->dim[j]; add_fea_fld(name, size, fld->rank, dim, spstype, codes, hdr); } } } break; case OPTIONAL: if (debug_level) fprintf(stderr, "FieldList_to_fea: optional fields " "not supported in ESPS FEA files.\n"); break; case VIRTUAL: if (copy_sources) { if (strncmp(name, "source_", 7) != 0) { if (debug_level) fprintf(stderr, "Field_List_to_fea: VIRTUAL " "field other than source_<n>.\n"); } else if ((subfields = fld->subfields) != NULL || fld->type == CHAR) { size_t len; char *data; source = FieldList_to_fea(subfields, NULL, NULL, TRUE); len = FieldLength(fld); data = (char *) malloc(len + 1); strncpy(data, fld->data, len); data[len] = '\0'; add_source_file(hdr, data, source); } } break; case INCLUDED: if (debug_level) fprintf(stderr, "FieldList_to_fea: included fields " "not supported in ESPS FEA files.\n"); break; default: spsassert(0, "FieldList_to_fea: " "unrecognized occurrence code.\n"); break; } } } if (rec != NULL) { *rec = allo_fea_rec(hdr); fea = hdr->hd.fea; if (hdr->common.tag) { fld = FindField(list, ESPS_TAG); fld->data = &(*rec)->tag; } for (i = 0; i < (int) fea->field_count; i++) { name = fea->names[i]; fld = FindField(list, name); fld->data = get_fea_ptr(*rec, name, hdr); } } return hdr; }