📄 atospsg.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 Speech, Inc. Any reproduction, distribution, or publication without the prior written permission of Entropic Speech, Inc. is strictly prohibited. Any public distribution of copies of this work authorized in writing by Entropic Speech, Inc. must bear the notice "Copyright (c) 1987 Entropic Speech, Inc.; All rights reserved" */static char *sccs_id = "@(#)atospsg.c 1.3 6/11/87 ESI";#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_SD: h->hd.sd = (struct sd_header *) calloc(1,SD_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_SD) h->hd.sd->spares[item] = atoh(buf); break; case TAG_src_sf: if(type == FT_SD) getf(h->hd.sd->src_sf); break; case TAG_dcrem: if(type == FT_SD) getf(h->hd.sd->dcrem); 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_prefilter: if(type == FT_SD)h->hd.sd->prefilter = 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 + -