📄 sp_utils.c
字号:
#ifndef lint static char rcsid[] = "$Header: /home/beldar/stan/sphere/RCS/sp_utils.c,v 1.4 1993/03/25 00:20:51 stan Exp stan $";#endif/* LINTLIBRARY *//** File: sp_utils.c **/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include <sp/sphere.h>extern int farray_fields;extern struct field_t *farray[];/***************************************************************//* Reads an existing header in from file pointer "fp". *//* The file pointer is assumed to be positioned at the *//* beginning of a speech file with a header in NIST SPHERE *//* format. *//* On success, "fp" is positioned at the end of the header *//* (ready to read samples) and a pointer to a header *//* structure is returned. *//* On failure, argument "error" will point to a string *//* describing the problem. *//* If "parse_flag" is false (zero), the fields in the header *//* will not be parsed and inserted into the header *//* structure; the structure will contain zero fields. *//* This is useful for operations on files when the contents *//* of the header are not important, for example when *//* stripping the header. *//***************************************************************/FUNCTION struct header_t *sp_open_header(fp,parse_flag,error)register FILE *fp;char **error;int parse_flag;{register struct header_t *h;int header_size, i;struct field_t **fv;if (fp == FPNULL) return HDRNULL; /* check sanity of arguments */if (spx_read_header(fp,&header_size,parse_flag,error) < 0) return HDRNULL;if ((! parse_flag) || (farray_fields == 0)) fv = FVNULL;else { fv = spx_get_field_vector(farray_fields); if (fv == FVNULL) { for (i=0; i<farray_fields; i++) (void) spx_deallocate_field(farray[i]); return HDRNULL; } (void) spx_copy_field_vector(farray, fv, farray_fields);}h = spx_allocate_header(farray_fields,fv);if (h == HDRNULL) for (i=0; i<farray_fields; i++) (void) spx_deallocate_field(farray[i]);return h;}/*******************************************************************//* Deletes all fields from the header pointed to by h. *//*******************************************************************/FUNCTION int sp_clear_fields(h)register struct header_t *h;{register int i, j, errors = 0;if (h == HDRNULL) return -1; /* check sanity of arguments */for (i=0, j = h->fc; i<j; i++) { if (spx_deallocate_field(h->fv[i]) < 0) errors++; h->fv[i] = FNULL;}if (h->fv != FVNULL) mtrf_free((char *) h->fv);h->fv = FVNULL;h->fc = 0;return errors ? -1 : 0;}/***********************************************************************//* Reclaims the space allocated for the header structure pointed to *//* by h. First reclaims all space allocated for the header's fields, *//* if any exist. *//***********************************************************************/FUNCTION int sp_close_header(h)register struct header_t *h;{ (void) sp_clear_fields(h); mtrf_free((char *) h); return 0;}/**************************************************************************//* make an exact copy of the header pointed to by h, and then return *//* the new header. *//**************************************************************************/FUNCTION struct header_t *sp_dup_header(h)struct header_t *h;{ struct header_t *duph; struct field_t *nf, **fv; int i, n; if (h == HDRNULL) return(0); duph = sp_create_header(); if ( duph == HDRNULL ) { fprintf(stderr,"Error: Unable to dup header, can't allocate mem.\n"); return(HDRNULL); } /* just loop through all the names, adding each field */ for (i=0; i < h->fc ; i++){ nf = spx_allocate_field_str(h->fv[i]->type,h->fv[i]->name, h->fv[i]->data,h->fv[i]->datalen); if (nf == FNULL) return(HDRNULL); fv = spx_get_field_vector(duph->fc + 1); if (fv == FVNULL) return(HDRNULL); if (duph->fc > 0) { (void) spx_copy_field_vector(duph->fv, fv, duph->fc); mtrf_free((char *) duph->fv); } fv[duph->fc++] = nf; duph->fv = fv; } return(duph);}int sp_dup(spin, spout)SP_FILE *spin, *spout;{ char *proc_name="sp_dup"; struct header_t *h; int i, n; long lint; double real; if (spin->open_mode == SP_mode_read) h = spin->read_spifr->header; else if (spin->open_mode == SP_mode_write) h = spin->write_spifr->header; else return_err(proc_name,100,100,"Unable to dup header opened for update"); /* just loop through all the names, adding each field */ for (i=0; i < h->fc ; i++){ switch (h->fv[i]->type){ case T_STRING: if (sp_h_set_field(spout,h->fv[i]->name,h->fv[i]->type,h->fv[i]->data) != 0){ sp_print_return_status(stdout); return_err(proc_name,200,200,rsprintf("Unable to copy STRING field '%s'",h->fv[i]->name)); } break; case T_INTEGER: lint=atol(h->fv[i]->data); if (sp_h_set_field(spout,h->fv[i]->name,h->fv[i]->type,&lint) != 0){ sp_print_return_status(stdout); return_err(proc_name,200,200,rsprintf("Unable to copy INTEGER field '%s'",h->fv[i]->name)); } break; case T_REAL: real=atof(h->fv[i]->data); if (sp_h_set_field(spout,h->fv[i]->name,h->fv[i]->type,&real) != 0){ sp_print_return_status(stdout); return_err(proc_name,200,200,rsprintf("Unable to copy REAL field '%s'",h->fv[i]->name)); } break; } } if (sp_set_default_operations(spout) != 0) return_err(proc_name,300,300,"Unable to set default operations duplicated file"); return_success(proc_name,0,0,"ok");}/*********************************************************************//* Returns the number of fields stored in the specified header. *//*********************************************************************/FUNCTION int sp_get_nfields(h)struct header_t *h;{if (h == HDRNULL) return -1; /* check sanity of arguments */return h->fc;}/*********************************************************************//* Fills in an array of character pointers with addresses of the *//* fields in the specified header. No more than n pointers in the *//* array will be set. *//* Returns the number of pointers set. *//*********************************************************************/FUNCTION int sp_get_fieldnames(h,n,v)struct header_t *h;int n;char *v[];{register struct field_t **fv;int i, fc;if (h == HDRNULL) return -1; /* check sanity of arguments */if (v == (char **) NULL) return -1;fc = h->fc;fv = h->fv;for (i=0; i < fc && i < n; i++) v[i] = fv[i]->name;return i;}/***********************************************************************//* Returns the type and size (in bytes) of the specified header field. *//* Types are T_INTEGER, T_REAL, T_STRING (defined in header.h). *//* The size of a T_INTEGER field is sizeof(long). *//* The size of a T_REAL field is sizeof(double). *//* The size of a string is variable and does not includes a *//* null-terminator byte (null bytes are allowed in a string). *//***********************************************************************/FUNCTION int sp_get_field(h,name,type,size)struct header_t *h;char *name;int *type, *size;{register int i, fc;register struct field_t **fv;if (h == HDRNULL) return -1; /* check sanity of arguments */if (name == CNULL) return -1;fc = h->fc;fv = h->fv;for (i=0; i < fc ; i++, fv++) if (strcmp(name,(*fv)->name) == 0) { switch ((*fv)->type) { case T_INTEGER: *size = sizeof(long); break; case T_REAL: *size = sizeof(double); break; case T_STRING: *size = (*fv)->datalen; break; default: return -1; } *type = (*fv)->type; return 0; }return -1;}/*********************************************************************//* Returns the type of the specified header field. *//* Types are T_INTEGER, T_REAL, T_STRING (defined in header.h). *//*********************************************************************/FUNCTION int sp_get_type(h,name)struct header_t *h;char *name;{register int i, fc;register struct field_t **fv;if (h == HDRNULL) return -1; /* check sanity of arguments */if (name == CNULL) return -1;fc = h->fc;fv = h->fv;for (i=0; i < fc ; i++, fv++) if (strcmp(name,(*fv)->name) == 0) switch ((*fv)->type) { case T_INTEGER: case T_REAL: case T_STRING: return (*fv)->type; default: return -1; }return -1;}/*********************************************************************//* Returns the size (in bytes) of the specified header field. *//* The size of a T_INTEGER field is sizeof(long). *//* The size of a T_REAL field is sizeof(double). *//* The size of a string is variable and does not includes a *//* null-terminator byte (null bytes are allowed in a string). *//*********************************************************************/FUNCTION int sp_get_size(h,name)struct header_t *h;char *name;{register int i, fc;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -