main(int argc, char **argv) { extern int optind; /* for use of getopt() */ extern char *optarg; /* for use of getopt() */ int ch; /* command-line option letter */ int outarch = NATIVE; char **field_names = NULL; int num_fields = 0; int alloc_fields = 0; int rflag = NO; /* -r option specified? */ char *rrange; /* arguments of -r option */ long start_rec; /* starting record number */ long end_rec; /* ending record number */ long num_recs; /* number of records to read (0 means all up to end of file) */ long num_read; /* number of records actually read */ int Aflag = NO; /* annotate Ascii output? */ Annot *annotate = NULL; int annwidth = 70; char *iname; /* input file name */ FILE *ifile; /* input stream */ struct header *ihd; /* input file header */ struct fea_data *irec; /* input record */ char *oname; /* output file name */ FILE *ofile; /* output stream */ FieldList list; /* output field list */ int outord = TYPE_ORDER; FieldSpec **ofields; /* output fields in field or type order */ double rec_freq; double start_time_offset; char *fdata; double *edata; int type; int len, i; long dim[1]; FieldSpec *field; FieldList source; /* field list of source file */ while ((ch = getopt(argc, argv, "a:f:r:x:A:F")) != EOF) switch (ch) { case 'a': outarch = ((!strcmp(optarg, "EDR1")) ? EDR1 : (!strcmp(optarg, "EDR2")) ? EDR2 : (!strcmp(optarg, "NATIVE")) ? NATIVE : (!strcmp(optarg, "ASCII")) ? ASCII : UNKNOWN); break; case 'f': if (num_fields >= alloc_fields) { size_t size; alloc_fields = num_fields + 1 + num_fields/2; size = (alloc_fields + 1) * sizeof(char *); field_names = (char **) ((field_names == NULL) ? malloc(size) : realloc(field_names, size)); } field_names[num_fields++] = optarg; field_names[num_fields] = NULL; break; case 'r': rflag = YES; rrange = optarg; break; case 'x': debug_level = atoi(optarg); break; case 'A': Aflag = YES; annwidth = atoi(optarg); break; case 'F': outord = FIELD_ORDER; break; default: SYNTAX; break; } if (argc - optind > 2) { fprintf(stderr, "%s: too many file names specified.\n", ProgName); SYNTAX; } if (argc - optind < 2) { fprintf(stderr, "%s: too few file names specified.\n", ProgName); SYNTAX; } iname = eopen(ProgName, argv[optind++], "r", FT_FEA, NONE, &ihd, &ifile); oname = argv[optind++]; start_rec = 1; end_rec = LONG_MAX; num_recs = 0; if (rflag) { lrange_switch(rrange, &start_rec, &end_rec, 0); if (end_rec != LONG_MAX) num_recs = end_rec - start_rec + 1; } REQUIRE(start_rec >= 1, "can't start before beginning of file"); REQUIRE(end_rec >= start_rec, "empty range of records specified"); if (debug_level) fprintf(stderr, "start_rec: %ld. end_rec: %ld. num_recs: %ld.\n", start_rec, end_rec, num_recs); DebugMsgLevel = debug_level; DebugMsgFunc = DebugPrint; irec = allo_fea_rec(ihd); REQUIRE(irec != NULL, "can't allocate memory for input record"); list = fea_to_FieldList(ihd, irec, field_names, FALSE); REQUIRE(list != NULL, "failure converting input header to field list"); switch (outord) { case TYPE_ORDER: if (debug_level) fprintf(stderr, "making type-ordered field array.\n"); ofields = TypeOrder(list); break; case FIELD_ORDER: if (debug_level) fprintf(stderr, "making field-ordered field array.\n"); ofields = FieldOrder(list); break; default: REQUIRE(0, "output order neither TYPE_ORDER nor FIELD_ORDER"); break; } if (debug_level) fprintf(stderr, "setting field ordering.\n"); REQUIRE(SetFieldOrdering(&list, outord), "can't set field ordering of output"); rec_freq = get_genhd_val("record_freq", ihd, 1.0); type = genhd_type("record_freq", &len, ihd); if (type != HD_UNDEF) { field = AddGlobalField(&list, "recordFreq", 0, NULL, EDOUBLE, NULL); *(double *) field->data = rec_freq; } type = genhd_type("start_time", &len, ihd); if (type != HD_UNDEF && rec_freq != 0) { start_time_offset = (start_rec - 1) / rec_freq; fdata = (char *) get_genhd("start_time", ihd); edata = (double *) type_convert((long) len, fdata, type, (char *) NULL, DOUBLE, (void (*)()) NULL); if (start_time_offset != 0) { for (i = 0; i < len; i++) edata[i] += start_time_offset; } dim[0] = len; AddGlobalField(&list, "startTime", 1, dim, EDOUBLE, edata); } source = fea_to_FieldList(ihd, NULL, NULL, TRUE); (void) AddSource(&list, 0, iname, source); (void) AddCommandLine(&list, GetCommandLine(argc, argv)); if (debug_level) fprintf(stderr, "annwidth %d.\n", annwidth); if (Aflag) { annotate = (Annot *) malloc(sizeof(Annot)); annotate->position = 0; annotate->indent = 0; annotate->width = annwidth; annotate->recnum = 0; } if (debug_level) fprintf(stderr, "writing Esignal header.\n"); REQUIRE(OpenOut(oname, list, outarch, &ofile, annotate), "write header failed"); if (ofile == stdout) oname = "<stdout>"; num_read = start_rec - 1; if (debug_level) fprintf(stderr, "skipping %ld records.\n", num_read); /* skiprec(ifile, num_read, size_rec(ihd)); */ fea_skiprec(ifile, num_read, ihd); while (num_read++ < end_rec && get_fea_rec(irec, ihd, ifile) != EOF) { if (debug_level > 2) fprintf(stderr, "Record number %ld read.\n", num_read); WriteRecord(ofields, outarch, ofile, annotate); } if (--num_read < end_rec && num_recs != 0) fprintf(stderr, "fea2esig: only %ld records read.\n", num_read - (start_rec - 1)); exit(0); /*NOTREACHED*/ }
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; }