📄 genhd.c
字号:
/* * This material contains unpublished, proprietary software of * Entropic Research Laboratory, Inc. Any reproduction, distribution, * or publication of this work must be authorized in writing by Entropic * Research Laboratory, Inc., and must bear the notice: * * "Copyright (c) 1986-1990 Entropic Speech, Inc. * "Copyright (c) 1990-1996 Entropic Research Laboratory, Inc. * All rights reserved" * * The copyright notice above does not evidence any actual or intended * publication of this source code. * * Written by: Alan Parker * Checked by: * Revised by: * * Brief description: * This module contains support for the generic header items. This * allows header items that aren't built into header.h to be created * at run-time. * */static char *sccs_id = "@(#)genhd.c 1.29 28 Oct 1999 ESI/ERL";#include <stdio.h>#include <esps/esps.h>#include <esps/unix.h>#define PWD_SIZE 512char *uniq_name();static char *hostname();static char *filename();char *savestring();char *add_genhd_xfile();/* very simple hash function*/static inthash (s)char *s;{ int sum = 0; while (*s) sum += *s++; return (sum % GENTABSIZ);}/* look up an entry in the hash table. Return NULL if not found, else return the address of the node*/static struct gen_hd *lookup (s, tab)char *s;struct gen_hd *tab[];{ struct gen_hd *np; spsassert (tab != NULL && s != NULL,"error in lookup"); for (np = tab[hash (s)]; np != NULL; np = np -> next) if (strcmp (s, np -> name) == 0) return (np); /* found it */ return (NULL); /* not found */}/* add_genhd creates a new header item and returns a pointer to it. The pointer is returned as char *, so you must cast it to the right type.*/char *add_genhd (name, type, size, ptr, codes, hd)char *name;int type;int size;char *ptr;char **codes;struct header *hd;{ struct gen_hd *np, *lookup (); int hashval;/* be sure arguments are reasonable */ spsassert (hd != NULL && name != NULL && size >= 1,"error in add_genhd"); spsassert (type >= DOUBLE && type <= AFILE,"Bad type in add_genhd"); spsassert (type != UNDEF,"Bad type in add_genhd"); if (type == CODED) spsassert (codes != NULL,"Bad codes in add_genhd"); spsassert (type != BYTE,"Type BYTE not implemented for generics yet");/* see if name already defined, if it is, just wipe it out */ np = lookup (name, hd -> variable.gentab); if (np == NULL) {/* allocate a node if this name is not defined */ np = (struct gen_hd *) calloc (1, sizeof (*np)); spsassert (np != NULL,"calloc failed");/* save the name */ np -> name = savestring (name); spsassert (np -> name != NULL,"savestring failed");/* insert the node into the hashtable */ hashval = hash (np -> name); np -> next = hd -> variable.gentab[hashval]; hd -> variable.gentab[hashval] = np;/* increment the counter */ hd -> variable.ngen++; }/* save the size and type */ np -> size = size; np -> type = type;/* if its a CODED type, save the array of possible values */ if (type == CODED) np -> codes = codes; if (ptr == NULL) {/* allocate the data area and return a pointer to it */ np -> d_ptr = calloc ((unsigned) size, (unsigned) typesiz (type)); spsassert (np -> d_ptr != NULL,"calloc failed"); } else {/* else use the user supplied location */ np -> d_ptr = ptr; } return np -> d_ptr;}/* genhd_type returns the type and size of a generic header item. */intgenhd_type (name, size, hd)char *name;int *size;struct header *hd;{ struct gen_hd *np, *lookup ();/* check the arguments */ spsassert (name != NULL && hd != NULL,"Bad args to genhd_type");/* if not defined, just return HD_UNDEF */ if ((np = lookup (name, hd -> variable.gentab)) == NULL) return HD_UNDEF;/* return the size and type */ if (size != NULL) *size = np -> size; return np -> type;}/* get_genhd returns a pointer to the data for a generic header item. The fuction returns char *, so it must be cast into the right type.*/char *get_genhd (name, hd)char *name;struct header *hd;{ struct gen_hd *np, *lookup ();/* check arguments */ spsassert (name != NULL && hd != NULL,"Bad args to get_genhd");/* return NULL if name not defined */ if ((np = lookup (name, hd -> variable.gentab)) == NULL) return NULL;/* return pointer */ return np -> d_ptr;}/* returns the number of generic header items and a pointer to an array of char pointers of the names. The array has one more element than the number of items, the last element is NULL.*/char **genhd_list (size, hd)int *size;struct header *hd;{ struct gen_hd *np; int j = 0, i; char **array;/* check arguments */ spsassert (hd != NULL,"hd is NULL in genhd_list");/* if there are no items, just return NULL in array */ if (hd -> variable.ngen == 0) { *size = 0; return NULL; }/* allocate memory for array */ array = (char **) calloc ((unsigned) (hd -> variable.ngen + 1), sizeof (char *)); spsassert (array != NULL,"calloc failed in genhd_list");/* go through data structure and collect names */ j = 0; for (i = 0; i < GENTABSIZ; i++) { np = hd -> variable.gentab[i]; while (np != NULL) { /* be sure count right */ spsassert (j < hd -> variable.ngen,"internal error in genhd_list"); array[j++] = np -> name; np = np -> next; } } array[j] = NULL; *size = j; return (array);}/* add_genhd_d creates a header item of type double and returns a pointer to the data. The user can supply a pointer through ptr */double *add_genhd_d (name, ptr, size, hd)char *name;double *ptr;int size;struct header *hd;{/* check the arguments */ spsassert (name != NULL && hd != NULL && size>0,"Bad args in add_genhd_d");/* call add_genhd with the right arguments */ return (double *) add_genhd (name, DOUBLE, size, (char *) ptr, (char **) NULL, hd);}/* add_genhd_f creates a header item of type float and returns a pointer to the data. The user can supply a pointer through ptr */float *add_genhd_f (name, ptr, size, hd)char *name;float *ptr;int size;struct header *hd;{/* check the arguments */ spsassert (name != NULL && hd != NULL && size>0,"Bad args in add_genhd_f");/* call add_genhd with the right arguments */ return (float *) add_genhd (name, FLOAT, size, (char *) ptr, (char **) NULL, hd);}/* add_genhd_l creates a header item of type long and returns a pointer to the data. The user can supply a pointer through ptr */long *add_genhd_l (name, ptr, size, hd)char *name;long *ptr;int size;struct header *hd;{/* check the arguments */ spsassert (name != NULL && hd != NULL && size>0,"Bad args in add_genhd_l");/* call add_genhd with the right arguments */ return (long *) add_genhd (name, LONG, size, (char *) ptr, (char **) NULL, hd);}/* add_genhd_s creates a header item of type short and returns a pointer to the data. The user can supply a pointer through ptr */short *add_genhd_s (name, ptr, size, hd)char *name;short *ptr;int size;struct header *hd;{/* check the arguments */ spsassert (name != NULL && hd != NULL && size>0,"Bad args in add_genhd_s");/* call add_genhd with the right arguments */ return (short *) add_genhd (name, SHORT, size, (char *) ptr, (char **) NULL, hd);}/* add_genhd_c creates a header item of type char and returns a pointer to the data. The user can supply a pointer through ptr */char *add_genhd_c (name, ptr, size, hd)char *name;char *ptr;int size;struct header *hd;{/* if ptr != NULL and size == 0, then assume a NULL terminated string */ if (ptr != NULL && size == 0) size = strlen (ptr) + 1;/* check the arguments */ spsassert (name != NULL && hd != NULL && size>0,"Bad args in add_genhd_c");/* call add_genhd with the right arguments */ return (char *) add_genhd (name, CHAR, size, (char *) ptr, (char **) NULL, hd);}/* add_genhd_e creates a header item of type coded and returns a pointer to the data. The user can supply a pointer to the data through ptr. The user must supply a pointer to an array of strings that are the possible values.*/short *add_genhd_e (name, ptr, size, codes, hd)char *name;short *ptr;int size;char **codes;struct header *hd;{/* check the arguments */ spsassert (name != NULL && hd != NULL && size > 0 && codes != NULL, "Bad args in add_genhd_e");/* call add_genhd with the right arguments */ return (short *) add_genhd (name, CODED, size, (char *) ptr, codes, hd);}/* genhd_codes returns the array of possible value strings associated with a generic header item of type coded */char **genhd_codes (name, hd)char *name;struct header *hd;{ struct gen_hd *np, *lookup ();/* check arguments */ spsassert (name != NULL && hd != NULL,"Bad args in genhd_codes");/* return NULL if name not defined or not a CODED */ if ((np = lookup (name, hd -> variable.gentab)) == NULL) return NULL; if (np -> type != CODED) return NULL;/* return pointer to codes */ return np -> codes;}/* returns the list of coded values for name */char **get_genhd_coded (name, hd)char *name;struct header *hd;{ struct gen_hd *np, *lookup (); char **val; int i; short *s_ptr;/* check arguments */ spsassert (name != NULL && hd != NULL,"Bad args in get_genhd_coded");/* return NULL if name not defined or not a CODED */ if ((np = lookup (name, hd -> variable.gentab)) == NULL) return NULL; if (np -> type != CODED) return NULL;/* get values into the array of character pointers val */ val = (char **) malloc ((unsigned) np->size * sizeof (char **)); s_ptr = (short *) np->d_ptr; for (i = 0; i < np->size; i++) if (idx_ok(*s_ptr, np->codes)) val[i] = np->codes[*s_ptr++]; else { val[i] = "invalid code"; s_ptr++; } return val;}/* returns a header pointer for the external ESPS file */struct header *get_genhd_efile(name, hd)char *name;struct header *hd;{ struct gen_hd *np, *lookup (); struct header *ext_hd; FILE * strm; int tmp_flag=0; char *tmp_file = "/tmp/genhdXXXXXX";/* check arguments */ spsassert (name != NULL && hd != NULL,"Bad args in get_genhd_efile");/* return NULL if name not defined or not a EFILE */ if ((np = lookup (name, hd -> variable.gentab)) == NULL) return NULL; if (np -> type != EFILE) return NULL;/* try and open the referenced file and read the header *//* if it has a hostname, try and rcp the file from the remote host */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -