예제 #1
0
파일: header.c 프로젝트: boundles/MergeWav
/* 
 * Read header from file. Return a pointer to the allocated (possibly
 * empty) header or NULL in case of error.  
 */
spfheader_t *spf_header_read(FILE *f)
{
  spfheader_t *hp;
  int lino = 1;
  char c, line[5120], *p;
  char *name, *value;
  int n = 0;

  /* create empty header */
  if ((hp = spf_header_init(NULL)) == NULL) {
    fprintf(stderr, "spf_header_read(): cannot allocate memory\n");
    return(NULL);
  }
  
  c = getc(f);
  ungetc(c, f);

  if (c == '<') { /* variable length header */

    if (fgets(line, 5120, f) == NULL) {
      fprintf(stderr, "spf_header_read(): cannot read line %d\n", lino);
      spf_header_free(hp);
      return(NULL);
    }

    if (strcmp(line, "<header>\n") != 0) {
      fprintf(stderr, "spf_header_read(): expecting <header> tag at line %d\n", lino);
      spf_header_free(hp);
      return(NULL);
    }

    while (1) {

      lino++;
      if (fgets(line, 5120, f) == NULL) {
	fprintf(stderr, "spf_header_read(): cannot read line %d (maybe a missing </header> tag)\n", lino);
	spf_header_free(hp);
	return(NULL);
      }
      
      if (strcmp(line, "</header>\n") == 0)
	break;
      
      /* find out field name and value in line */
      p = line; 
      name = value = NULL;
      while (*p && strchr(" \t\n", *p)) /* skip heading blanks */
	p++;
      
      if (! *p) /* it's an empty line ==> skip it! */
	continue;
      
      /* find out name */
      name = p;
      p++;
      while (*p && strchr(" \t=", *p) == NULL)
	p++;
      
      if (! *p) {
	fprintf(stderr, "spf_header_read(): no separator in variable header at line %d\n", lino);
	spf_header_free(hp);
	return(NULL);
      }
    
      *p = 0;
      p++;
      while (*p && strchr(" \t=", *p))
	p++;
      
      if (! *p) {
	fprintf(stderr, "spf_header_read(): no value for attribute %s in variable header at line %d\n", name, lino);
	spf_header_free(hp);
	return(NULL);
      }
      
      value = p;
      p++;
      while (*p && *p != ';')
	p++;
      
      if (! *p) {
	fprintf(stderr, "spf_header_read(): no end delimiter for attribute %s in variable header at line %d\n", name, lino);
	spf_header_free(hp);
	return(NULL);
      }
      
      *p = 0;
      n++;
      
      /* reallocate memory */
      if ((hp->field = (spfield_t *)realloc(hp->field, n * sizeof(spfield_t))) == NULL) {
	fprintf(stderr, "spf_header_read(): cannot allocate memory\n");
	spf_header_free(hp);
	return(NULL);
      }
      hp->nfields = n;
      if ((hp->field[n-1].name = strdup(name)) == NULL || (hp->field[n-1].value = strdup(value)) == NULL) {
	fprintf(stderr, "spf_header_read(): cannot allocate memory\n");
	spf_header_free(hp);
	return(NULL);
      }
    }

  }

  return(hp);
}
예제 #2
0
파일: spf.c 프로젝트: anygo/array4j
/*
 * Open output feature stream. Return a pointer to the stream or NULL
 * in case of error.
 */
spfstream_t *spf_output_stream_open(const char *fn, unsigned short idim, long iflag,
                                    long cflag, float frate, const spfield_t *vh, size_t nbytes)
{
    spfstream_t *s;
    unsigned short dim, idx[9];
    long flag;
    float rate;

    if ((s = (spfstream_t *)malloc(sizeof(spfstream_t))) == NULL) {
        fprintf(stderr, "spf_output_stream_open(): cannot allocate memory\n");
        return(NULL);
    }

    s->name = NULL;
    s->f = NULL;
    s->iomode = SPRO_STREAM_WRITE_MODE;
    s->Fs = frate;

    s->idim = idim;
    s->iflag = iflag;
    s->cflag = cflag;
    s->escale = 0.0;
    s->winlen = 0;

    s->header = NULL;
    s->buf = NULL;
    s->start = 0;
    s->idx = 0;

    /* determine output flag and dim */
    s->oflag = iflag | cflag;
    dim = s->idim;

    if (s->oflag != s->iflag) {
        spf_indexes(idx, s->idim, s->iflag);
        s->odim = spf_tot_dim(idx[1] + 1, s->oflag);
        if (s->odim > dim)
            dim = s->odim;
    }
    else
        s->odim = s->idim;

    /* allocate buffer */
    if ((s->buf = spf_buf_alloc(dim, nbytes)) == NULL) {
        fprintf(stderr, "spf_output_stream_open(): cannot create output buffer for %s\n", (fn) ? (fn) : "stdout");
        spf_stream_close(s);
        return(NULL);
    }
    s->buf->dim = s->idim; /* set the actual size */

    /* create stream header */
    if ((s->header = spf_header_init(vh)) == NULL) {
        fprintf(stderr, "spf_output_stream_open(): cannot initialize variable length header\n");
        spf_stream_close(s);
        return(NULL);
    }

    /* set stream filename */
    if (fn && strcmp(fn, "-") != 0) {
        if ((s->name = strdup(fn)) == NULL) {
            fprintf(stderr, "spf_output_stream_open(): cannot set stream name %s\n", fn);
            spf_stream_close(s);
            return(NULL);
        }
        if ((s->f = fopen(fn, "w")) == NULL) {
            fprintf(stderr, "spf_output_stream_open(): cannot open input file %s\n", fn);
            spf_stream_close(s);
            return(NULL);
        }
    }
    else
        s->f = stdout;

    /* write header to stream */
    if (spf_header_write(s->header, s->f)) {
        fprintf(stderr, "spf_output_stream_open(): cannot write header to %s\n", (fn) ? (fn) : "stdout");
        spf_stream_close(s);
        return(NULL);
    }

    dim = s->odim;
    flag = s->oflag;
    rate = s->Fs;

#ifdef WORDS_BIGENDIAN
    sp_swap(&dim, SIZEOF_SHORT);
    sp_swap(&flag, SIZEOF_LONG);
    sp_swap(&rate, sizeof(float));
#endif

    if (fwrite(&dim, SIZEOF_SHORT, 1, s->f) != 1 ||
            fwrite(&flag, SIZEOF_LONG, 1, s->f) != 1 ||
            fwrite(&rate, sizeof(float), 1, s->f) != 1) {
        fprintf(stderr, "spf_output_stream_open() -- cannot write fixed header to %s\n", (fn) ? (fn) : "stdout");
        return(NULL);
    }

    return(s);
}