📄 esig2fea.c
字号:
/* * This material contains unpublished, proprietary software of * Entropic Research Laboratory, Inc. Any reproduction, distribution, * or publication of this work must be authorized in writing by Entropic * Research Laboratory, Inc., and must bear the notice: * * "Copyright (c) 1995 Entropic Research Laboratory, Inc. * All rights reserved" * * The copyright notice above does not evidence any actual or intended * publication of this source code. * * Written by: Rod Johnson * Checked by: * Revised by: * * Write Esignal file data to FEA file. * */static char *sccs_id = "@(#)esig2fea.c 1.4 2/5/97 ERL";#include <esps/esps.h>#include <esps/fea.h>/* NAME CONFLICT: The ESPS data-type codes named * DOUBLE, FLOAT, LONG, SHORT, and CHAR * are different from the Esignal data-type codes with the same names. * While the ESPS definitions are in effect, declare variables so that * the ESPS codes can be referred to as * FDOUBLE, FFLOAT, FLONG, FSHORT, and FCHAR * instead. (Think of "fea-double", "fea-float", etc.) * After the inclusion of <esignal.h> below, the Esignal definitions of * DOUBLE, FLOAT, LONG, SHORT, and CHAR * take effect. */const static int FDOUBLE = DOUBLE;#undef DOUBLEconst static int FFLOAT = FLOAT;#undef FLOATconst static int FLONG = LONG;#undef LONGconst static int FSHORT = SHORT;#undef SHORTconst static int FCHAR = CHAR;#undef CHAR#undef BOOL#include <esignal.h>#define SCCS_VERSION "1.4"#define SCCS_DATE "2/5/97"#define ESPS_TAG "Tag"#define ESPS_FEA_SUBTYPE "FeaSubtype"#define REQUIRE(test, text) {if (!(test)) {(void) fprintf(stderr, \"%s: %s - exiting\n", ProgName, text); exit(1);}}#define SYNTAX \USAGE("esig2fea [-f field]... [-r range] [-x debug_level] [-F] [-T subtype]\n\tinput.esig output.fea");void lrange_switch();char *savestring();char *get_cmd_line();static struct header *FieldList_to_fea(FieldList list, struct fea_data **rec, char **fnames, int copy_sources);static int FindStr(char *str, char **arr);static char **StrArrayFromRect(long *dim, void *data);static int FieldIsTag(FieldSpec *fld);static int FieldIsFeaSubtype(FieldSpec *fld);static long SkipRecs(FILE *file, long skip, long size, FieldSpec **fields, int arch);static int ElibTypeToEsps(int type);int debug_level = 0;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 */ static char *ProgName = "esig2fea"; /* name of this program */ static char *Version = SCCS_VERSION; /* program SCCS version */ static char *Date = SCCS_DATE; /* program SCCS date */ 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 */ char *iname; /* input file name */ FILE *ifile; /* input stream */ FieldList list; /* input field list */ int inord; /* input: field order or type order? */ FieldSpec **ifields; /* input fields in field or type order */ char *subtype = NULL; /* FEA subtype name */ int subtype_code = NONE; /* numeric subtype code */ FieldSpec *fld; /* spec of various special fields */ char *oname; /* output file name */ FILE *ofile; /* output stream */ struct header *ohd; /* output file header */ struct fea_data *orec; /* output record */ int outord = TYPE_ORDER; char *version; /* version from input preamble */ int arch; /* architecture from input preamble */ long pre_size; /* preamble size */ long hdr_size; /* header size (bytes) from preamble */ long rec_size; /* record size from preamble */ double rec_freq; double start_time_offset; double *data; long len, i; struct header *source; /* embedded source-file header */ while ((ch = getopt(argc, argv, "f:r:x:FT:")) != EOF) switch (ch) { 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 'F': outord = FIELD_ORDER; break; case 'T': subtype = optarg; 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; } DebugMsgLevel = debug_level; DebugMsgFunc = DebugPrint; iname = argv[optind++]; list = OpenIn(iname, &version, &arch, &pre_size, &hdr_size, &rec_size, &ifile); REQUIRE(list != NULL, "read header failed"); if (ifile == stdin) iname = "<stdin>"; oname = argv[optind++]; start_rec = 0; end_rec = LONG_MAX; num_recs = 0; /* 0 means continue to end of file */ 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 >= 0, "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); REQUIRE(GetFieldOrdering(list, &inord), "cant get field ordering of input"); switch (inord) { case TYPE_ORDER: if (debug_level) fprintf(stderr, "making type-ordered field array.\n"); ifields = TypeOrder(list); break; case FIELD_ORDER: if (debug_level) fprintf(stderr, "making field-ordered field array.\n"); ifields = FieldOrder(list); break; default: REQUIRE(0, "input order neither TYPE_ORDER nor FIELD_ORDER"); break; } ohd = FieldList_to_fea(list, &orec, field_names, FALSE); REQUIRE(ohd != NULL, "failure converting input field list to header & record struct"); if (subtype != NULL) { subtype_code = lin_search(fea_file_type, subtype); if (subtype_code == -1) fprintf(stderr, "%s: unknown FEA file subtype \"%s\" ignored.\n", ProgName, subtype); else ohd->hd.fea->fea_type = subtype_code; } if (outord == FIELD_ORDER) ohd->hd.fea->field_order = YES; fld = FindField(list, "recordFreq"); if (fld != NULL && fld->occurrence == GLOBAL && fld->data != NULL) { (void) type_convert(1L, (char *) fld->data, ElibTypeToEsps(fld->type), (char *) &rec_freq, FDOUBLE, (void (*)()) NULL); *add_genhd_d("record_freq", NULL, 1, ohd) = rec_freq; } else rec_freq = 1.0; fld = FindField(list, "startTime"); if (fld != NULL && fld->occurrence == GLOBAL && fld->data != NULL && rec_freq != 0) { start_time_offset = start_rec / rec_freq; len = FieldLength(fld); data = (double *) type_convert(len, (char *) fld->data, ElibTypeToEsps(fld->type), (char *) NULL, FDOUBLE, (void (*)()) NULL); if (start_time_offset != 0) { for (i = 0; i < len; i++) data[i] += start_time_offset; } (void) add_genhd_d("start_time", data, len, ohd); } (void) strcpy(ohd->common.prog, ProgName); (void) strcpy(ohd->common.vers, Version); (void) strcpy(ohd->common.progdate, Date); source = FieldList_to_fea(list, NULL, NULL, TRUE); add_source_file(ohd, savestring(iname), source); add_comment(ohd, get_cmd_line(argc, argv)); oname = eopen(ProgName, oname, "w", NONE, NONE, NULL, &ofile); write_header(ohd, ofile); num_read = SkipRecs(ifile, start_rec, RecordSize(list, arch), ifields, arch); if (num_read != start_rec) { fprintf(stderr, "%s: couldn't reach starting record; only %ld skipped.\n", ProgName, num_read); exit(0); } for ( ; num_read <= end_rec && ReadRecord(ifields, arch, ifile); num_read++) { put_fea_rec(orec, ohd, ofile); } if (num_read <= end_rec && num_recs != 0) fprintf(stderr, "esig2fea: only %ld records read.\n", num_read - start_rec); 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");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -