示例#1
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 */

    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*/
}
示例#2
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*/
}