📄 atosps.c
字号:
/* converts a file produced by spstoa back to a binary sps data file * based on atosds, version 1.1. * Converted to SPS by Alan Parker * * This material contains proprietary software of Entropic Processing, Inc. * Any reproduction, distribution, or publication without the the prior * written permission of Entropic Processing, Inc. is strictly prohibited. * Any public distribution of copies of this work authorized in writing by * Entropic Processing, Inc. must bear the notice * * "Copyright 1986 Entropic Processing, Inc." */#ifdef SCCSstatic char *sccs_id = "@(#)atosps.c 1.3 5/6/86 EPI";#endif#include "tagdef.h"#include <stdio.h>#include <sps/sps.h>#define geth(a) a =atoh(buf+3)#define getf(a) a =atof(buf+3)char *Program = "atosps";char *in_file = "<stdin>";char *nextval(), *cvt_typtxt(), *index(), *savestring();double atof();struct zfunc *make_z();main (argc, argv)char **argv;{ FILE * istrm = stdin, *ostrm = stdout; register struct header *h = new_header (0); register int i; char buf[50]; if (argc > 1 && strcmp (argv[1], "-") != 0) { TRYOPEN (Program, argv[1], "r", istrm); in_file = argv[1]; } if (!fgets (buf, 10, istrm)) { fprintf (stderr, "%s: %s is empty\n", Program, in_file); exit (1); } else if (strcmp (buf, "{\n") != 0) { fprintf (stderr, "%s: %s has the wrong format\n", Program, in_file); } if (argc > 2 && strcmp (argv[2], "-") != 0) { TRYOPEN (Program, argv[2], "w", ostrm); } readhead (istrm, h, -1); write_header (h, ostrm); for (i = 0; i < h->common.ndrec; i++) { readrec (istrm, ostrm, h); } exit (0);}/* When readhead is called, the opening "{" line has already been read */readhead (in, h, lev)FILE * in;register struct header *h;int lev;{ char buf[BUFSIZ]; int i, len, tag; int nxthead = 0, nxtsrc = 0; short type=0, item=0; while (1) { if (!fgets (buf, BUFSIZ, in)) { fprintf (stderr, "%s: unexpected EOF in %s\n", Program, in_file); exit (1); } len = strlen (buf); if (buf[0] == '}') return; else if (buf[0] == '{') { h->variable.srchead[nxthead] = new_header (0); readhead (in, h->variable.srchead[nxthead], lev-1); nxthead += 1; } else { if (sscanf (buf, "%x", &tag) != 1) synerr ("bad header tag", lev); switch (tag) { case TAG_type: if(h->common.type != 0) { fprintf(stderr,"Type already set;bad file!\n"); exit(1); } geth(h->common.type); type = h->common.type; switch (h->common.type) { case FT_ANA: h->hd.ana = (struct ana_header *) calloc(1,ANA_SIZE); break; case FT_SD: h->hd.sd = (struct sd_header *) calloc(1,SD_SIZE); break; case FT_PIT: h->hd.pit = (struct pit_header *) calloc(1,PIT_SIZE); break; default: fprintf(stderr,"Bad file type code %d.\n", h->common.type); exit (1); } case TAG_check: geth(h->common.check); break; case TAG_date: buf[len-1] = '\0'; (void) strcpy(h->common.date,buf+3,DATESIZE); break; case TAG_hdvers: buf[len-1] = '\0'; (void) strcpy(h->common.hdvers,buf+3,VERSIONSIZE); break; case TAG_prog: buf[len-1] = '\0'; (void) strcpy(h->common.prog,buf+3,PROGSIZE); break; case TAG_vers: buf[len-1] = '\0'; (void) strcpy(h->common.vers,buf+3,VERSIONSIZE); break; case TAG_progdate: buf[len-1] = '\0'; (void) strcpy(h->common.progdate,buf+3,DATESIZE); break; case TAG_ndrec: geth(h->common.ndrec); break; case TAG_tag: geth(h->common.tag); break; case TAG_ndouble: geth(h->common.ndouble); break; case TAG_nfloat: geth(h->common.nfloat); break; case TAG_nlong: geth(h->common.nlong); break; case TAG_nshort: geth(h->common.nshort); break; case TAG_nchar: geth(h->common.nchar); break; case TAG_spare: geth(item); if (!fgets (buf, BUFSIZ, in)) { fprintf (stderr, "%s: unexpected EOF in %s\n", Program, in_file); exit (1); } if(type == 0) h->common.spares[item] = atoh(buf); if(type == FT_ANA) h->hd.ana->spares[item] = atoh(buf); if(type == FT_SD) h->hd.sd->spares[item] = atoh(buf); if(type == FT_PIT) h->hd.pit->spares[item] = atoh(buf); break; case TAG_start: if(type == FT_ANA) geth(h->hd.ana->start); if(type == FT_PIT) geth(h->hd.pit->start); break; case TAG_nan: if(type == FT_ANA) geth(h->hd.ana->nan); if(type == FT_PIT) geth(h->hd.pit->nan); break; case TAG_frmlen: geth(h->hd.ana->frmlen); break; case TAG_order_vcd: geth(h->hd.ana->order_vcd); break; case TAG_order_unvcd: geth(h->hd.ana->order_unvcd); break; case TAG_p_offset: geth(h->hd.ana->p_offset); break; case TAG_piq_method: geth(h->hd.ana->piq_method); break; case TAG_rcq_method: geth(h->hd.ana->rcq_method); break; case TAG_pwq_method: geth(h->hd.ana->pwq_method); break; case TAG_lpq_method: geth(h->hd.ana->lpq_method); break; case TAG_win_type: if(type == FT_ANA)geth(h->hd.ana->win_type); if(type == FT_PIT)geth(h->hd.pit->win_type); break; case TAG_coh_method: if(type == FT_ANA)geth(h->hd.ana->coh_method); if(type == FT_PIT)geth(h->hd.pit->coh_method); break; case TAG_src_sf: if(type == FT_ANA) getf(h->hd.ana->src_sf); if(type == FT_PIT) getf(h->hd.pit->src_sf); if(type == FT_SD) getf(h->hd.sd->src_sf); break; case TAG_a_method: geth(h->hd.ana->a_method); break; case TAG_dcrem: if(type == FT_ANA) getf(h->hd.ana->dcrem); if(type == FT_PIT) geth(h->hd.pit->dcrem); if(type == FT_SD) getf(h->hd.sd->dcrem); break; case TAG_psynch: geth(h->hd.ana->psynch); break; case TAG_maxpulses: geth(h->hd.ana->maxpulses); break; case TAG_maxlpc: geth(h->hd.ana->maxlpc); break; case TAG_maxraw: geth(h->hd.ana->maxraw); break; case TAG_matsiz: geth(h->hd.ana->matsiz); break; case TAG_rms_fullscale: getf(h->hd.ana->rms_fullscale); break; case TAG_sinc_flg: geth(h->hd.ana->sinc_flg); break; case TAG_stepsz: geth(h->hd.pit->stepsz); break; case TAG_u_pflt_order: geth(h->hd.pit->u_pflt_order); break; case TAG_v_pflt_order: geth(h->hd.pit->v_pflt_order); break; case TAG_p_method: geth(h->hd.pit->p_method); break; case TAG_pt_wsize: geth(h->hd.pit->pt_wsize); break; case TAG_uv_thresh: getf(h->hd.pit->uv_thresh); break; case TAG_min_pp: geth(h->hd.pit->min_pp); break; case TAG_max_pp: geth(h->hd.pit->max_pp); break; case TAG_wflt_order: geth(h->hd.pit->wflt_order); break; case TAG_coh_window_frac: getf(h->hd.pit->coh_window_frac); break; case TAG_pb_offset: geth(h->hd.pit->pb_offset); break; case TAG_harmonic_mult: getf(h->hd.pit->harmonic_mult); break; case TAG_tm_uv_thresh: getf(h->hd.pit->tm_uv_thresh); break; case TAG_am_uv_thresh: getf(h->hd.pit->am_uv_thresh); break; case TAG_hp_thresh: getf(h->hd.pit->hp_thresh); break; case TAG_pulse_search: geth(h->hd.pit->pulse_search); break; case TAG_gain_halt: getf(h->hd.pit->gain_halt); break; case TAG_min_coh_len: geth(h->hd.pit->min_coh_len); break; case TAG_max_coh_len: geth(h->hd.pit->max_coh_len); break; case TAG_equip: geth(h->hd.sd->equip); break; case TAG_max_value: getf(h->hd.sd->max_value); break; case TAG_sf: getf(h->hd.sd->sf); break; case TAG_synt_method: geth(h->hd.sd->synt_method); break; case TAG_scale: getf(h->hd.sd->scale); break; case TAG_q_method: geth(h->hd.sd->q_method); break; case TAG_v_excit_method: geth(h->hd.sd->v_excit_method); break; case TAG_uv_excit_method: geth(h->hd.sd->uv_excit_method); break; case TAG_spare1: geth(h->hd.sd->spare1); break; case TAG_nchan: geth(h->hd.sd->nchan); break; case TAG_synt_interp: geth(h->hd.sd->synt_interp); break; case TAG_synt_pwr: geth(h->hd.sd->synt_pwr); break; case TAG_synt_rc: geth(h->hd.sd->synt_rc); break; case TAG_synt_order: geth(h->hd.sd->synt_order); break; case TAG_source: buf[len - 1] = 0; h->variable.source[nxtsrc++] = savestring (buf + 3); break; case TAG_pre_emp: h->hd.ana->pre_emp = make_z (buf + 3); break; case TAG_prefilter: if(type == FT_SD)h->hd.sd->prefilter = make_z (buf + 3); if(type == FT_PIT)h->hd.pit->prefilter = make_z (buf + 3); break; case TAG_lpf: h->hd.pit->lpf = make_z (buf +3); break; case TAG_de_emp: h->hd.sd->de_emp = make_z (buf + 3); break; case TAG_typtxt: buf[len - 1] = '\0'; h->variable.typtxt = savestring (cvt_typtxt (buf + 3)); break; case TAG_comment: buf[len - 1] = '\0'; h->variable.comment = savestring (cvt_typtxt (buf + 3)); break; case TAG_refer: buf[len - 1] = '\0'; h->variable.refer = savestring (buf + 3); break; default: fprintf (stderr, "%s: bad tag code (%x) in %s\n", Program, tag, in_file); exit (1); } } }}atoh (str)char *str;{ long tmp = 0; (void) sscanf (str, "%lx", &tmp); return tmp;}readrec (in, out, h)FILE *in, *out;struct header *h;{ double dbuf[1024]; float fbuf[1024]; long lbuf[1024]; short sbuf[1024]; char cbuf[1024]; long tag; register int i; static recnum = 0; recnum += 1; if (BOOL (h->common.tag)) { if (fscanf (in, "%lx", &tag) != 1) synerr ("bad record tag", recnum); fwrite ((char *) & tag, sizeof tag, 1, out); } if (h->common.ndouble) { for (i = 0; i < h->common.ndouble; i++) { if (fscanf (in, "%lf", &dbuf[i]) != 1) synerr ("bad double datum", recnum); } (void)fwrite((char *)dbuf,sizeof (double),h->common.ndouble,out); } if (h->common.nfloat) { for (i = 0; i < h->common.nfloat; i++) { if (fscanf (in, "%f", &fbuf[i]) != 1) synerr ("bad float datum", recnum); } (void)fwrite((char *)fbuf,sizeof (float),h->common.nfloat,out); } if (h->common.nlong) { for (i = 0; i < h->common.nlong; i++) { if (fscanf (in, "%ld", &lbuf[i]) != 1) synerr ("bad long datum", recnum); } (void)fwrite((char *)lbuf,sizeof (long),h->common.nlong,out); } if (h->common.nshort) { for (i = 0; i < h->common.nshort; i++) { if (fscanf (in, "%ld", &lbuf[i]) != 1) synerr ("bad short datum", recnum); } for (i = 0; i < h->common.nshort; i++) sbuf[i] = lbuf[i]; (void)fwrite((char *)sbuf,sizeof (short),h->common.nshort,out); } if (h->common.nchar) { for (i = 0; i < h->common.nchar; i++) { if (fscanf (in, "%x", &cbuf[i]) != 1) synerr ("bad char datum", recnum); } (void)fwrite((char *)cbuf,sizeof (char),h->common.nchar,out); }}struct zfunc *make_z (str)register char *str;{ register int i; int nz, np; float num[200], den[200]; nz = atoi (str); str = nextval (str); np = atoi (str); for (i = 0; i < nz; i++) { str = nextval (str); num[i] = atof (str); } for (i = 0; i < np; i++) { str = nextval (str); den[i] = atof (str); } return new_zfunc (nz, np, num, den);}char *nextval (s)char *s;{ if ((s = index (s, ' ')) != NULL) { while (*s == ' ') s++; if (*s != 0) return s; } fprintf ("%s: zfunc syntax error in %s\n", Program, in_file); exit (1);}char *cvt_typtxt (s)char *s;{ register char *p = s; while (*p) { if (*p == '\r') *p = '\n'; p++; } return s;}synerr (txt, n)char *txt;int n;{ if (n < 0) fprintf (stderr, "%s: syntax error in %s: %s, header level %d\n", Program, in_file, txt, -n); else fprintf (stderr, "%s: syntax error in %s: %s, record %d\n", Program, in_file, txt, n); exit (1);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -