📄 genhd.c
字号:
if (hostname(np->d_ptr)) { char *cmd = malloc(80); (void)sprintf(cmd,"rcp %s %s",np->d_ptr,mktemp(tmp_file)); if (system(cmd) != 0) { Fprintf(stderr, "get_genhd_efile: problem getting file %s from host %s.\n", np->d_ptr, hostname(np->d_ptr)); } (void)free(cmd); if ((strm = fopen (tmp_file, "r")) == NULL) return NULL; tmp_flag=1; } else if ((strm = fopen (np -> d_ptr, "r")) == NULL) return NULL; ext_hd = read_header (strm);/* close the file and return */ (void) fclose (strm); if (tmp_flag) (void)unlink(tmp_file); return ext_hd;}/* returns a file stream pointer for the external file */FILE *get_genhd_afile(name, hd)char *name;struct header *hd;{ struct gen_hd *np, *lookup (); 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 AFILE */ if ((np = lookup (name, hd -> variable.gentab)) == NULL) return NULL; if (np -> type != AFILE) return NULL;/* try and open the referenced file and read the header *//* if it has a hostname, for now just punt */ if (hostname(np->d_ptr)) { char *cmd = malloc(80); (void)sprintf(cmd,"rcp %s %s",np->d_ptr,mktemp(tmp_file)); if (system(cmd) != 0) { Fprintf(stderr, "get_genhd_efile: problem getting file %s from host %s.\n", np->d_ptr, hostname(np->d_ptr)); } (void)free(cmd); if ((strm = fopen (tmp_file, "r")) == NULL) return NULL; tmp_flag=1; } else if ((strm = fopen (np -> d_ptr, "r")) == NULL) return NULL;/* close the file and return */ if (tmp_flag) (void)unlink(tmp_file); return strm;}/* returns the name for an external file */char *get_genhd_efile_name (name, hd)char *name;struct header *hd;{ struct gen_hd *np, *lookup ();/* check arguments */ spsassert (name != NULL && hd != NULL,"Bad args in get_genhd_efile_name");/* 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; return np -> d_ptr;}/* returns the name for an external file */char *get_genhd_afile_name (name, hd)char *name;struct header *hd;{ struct gen_hd *np, *lookup ();/* check arguments */ spsassert (name != NULL && hd != NULL,"Bad args in get_genhd_afile_name");/* return NULL if name not defined or not a AFILE */ if ((np = lookup (name, hd -> variable.gentab)) == NULL) return NULL; if (np -> type != AFILE) return NULL; return np -> d_ptr;}intcopy_genhd (dest, src, name)struct header *dest, *src;char *name;{ int i, count = 0, k; char *ptr; struct gen_hd *np, *lookup();/* check arguments */ spsassert (dest && src,"Bad args in copy_genhd");/* check for no items in src */ if (src -> variable.ngen == 0) return 0; if (name == NULL) {/* find entries */ for (i = 0; i < GENTABSIZ; i++) { np = src -> variable.gentab[i]; while (np != NULL) {/* create new item and copy data */ ptr = add_genhd (np -> name, np -> type, (int) np -> size, (char *)NULL, np -> codes, dest); for (k = 0; k < np -> size * typesiz (np -> type); k++) ptr[k] = np -> d_ptr[k]; np = np -> next; count++; } } return count; } else { if ((np = lookup(name, src->variable.gentab)) == NULL) return 0;/* create new item and copy data */ ptr = add_genhd (name, np -> type, (int) np -> size, (char *)NULL, np -> codes, dest); for (k = 0; k < np -> size * typesiz (np -> type); k++) ptr[k] = np -> d_ptr[k]; return 1; }}intcopy_genhd_uniq (dest, src, name)struct header *dest, *src;char *name;{ char **dest_names; /* names of header items from init_feaspec_hd() */ char *one_name[2]; /* NULL-terminated list containing name */ char **src_names; /* names of input header items */ char *uname; /* header item name, possibly altered by uniq_name() */ int type; /* type of header item */ int size; /* size of header item */ int nitems = 0; /* number of src header items */ int i;/* check arguments */ spsassert (dest && src,"Bad header args in copy_genhd_uniq");/* check for no items in src */ if (src -> variable.ngen == 0) return(0);/* check for no items in dest */ if (dest -> variable.ngen == 0) return(copy_genhd(dest, src, name));/* Copy generic header items, renaming to avoid conflicts*/ dest_names = genhd_list(&nitems, dest); if (name != NULL) { one_name[0] = name; one_name[1] = NULL; src_names = one_name; nitems = (genhd_type(name, &size, src) == HD_UNDEF) ? 0 : 1; } else src_names = genhd_list(&nitems, src); for (i = 0; i < nitems; i++) { if (genhd_type(*src_names, &size, dest) == HD_UNDEF) uname = *src_names; else uname = uniq_name(*src_names, dest_names); type = genhd_type(*src_names, &size, src); (void) add_genhd(uname, type, size, get_genhd(*src_names, src), genhd_codes(*src_names, src), dest); src_names++; } return(nitems);}/* this function returns a from a:/foo/bar */staticchar *hostname(s)char *s;{ if (strchr (s,':') == NULL) return NULL; return strtok(savestring(s),":");}/* this function returns /foo/bar from a:/foo/bar */staticchar *filename(s)char *s;{ return strchr(s,':')+1;}/* add_genhd_efile creates a header item of type EFILE and returns a pointer to the file_name */char *add_genhd_efile (item_name, file_name, hd)char *item_name;char *file_name;struct header *hd;{ spsassert(item_name && file_name && hd,"Bad args in add_genhd_efile"); return add_genhd_xfile(item_name, file_name, hd, EFILE);}/* add_genhd_afile creates a header item of type AFILE and returns a pointer to the file_name */char *add_genhd_afile (item_name, file_name, hd)char *item_name;char *file_name;struct header *hd;{ spsassert(item_name && file_name && hd,"Bad args in add_genhd_afile"); return add_genhd_xfile(item_name, file_name, hd, AFILE);}/* add_genhd_xfile creates a header item of type EFILE or AFILE and returns a pointer to the file_name */char *add_genhd_xfile (item_name, file_name, hd, type)char *item_name;char *file_name;struct header *hd;int type;{ char *pwd, *name; int size; name = savestring(file_name); if (hostname (name)) { if (*filename (name) != '/') { Fprintf (stderr, "add_genhd_xfile: when using a host name, a full path must be given.\n"); exit (1); } pwd = name; } else if (*name != '/') { pwd = malloc ((unsigned) PWD_SIZE); spsassert (pwd,"malloc failed in add_genhd_xfile");#if !defined(hpux) & !defined(OS5) & !defined(LINUX) if (getwd (pwd) == NULL) {#else if (getcwd (pwd,PWD_SIZE) == NULL) {#endif Fprintf (stderr, "add_genhd_xfile: cannot get cwd!\n"); (void)strcpy (pwd, " "); } (void) strcat (pwd, "/"); (void) strcat (pwd, name); } else pwd = name; size = strlen (pwd) + 1;/* call add_genhd with the right arguments */ return (char *) add_genhd (item_name, type, size, pwd, (char **) NULL, hd);}longget_genhd_val_l(name, hd, def_val)char *name;struct header *hd;long def_val;{ struct gen_hd *np, *lookup ();/* check arguments */ spsassert (name != NULL && hd != NULL,"Bad args to get_genhd_val");/* return def_val value if name not defined */ if ((np = lookup (name, hd -> variable.gentab)) == NULL) return def_val;/* return value as a long for numeric types */ switch (np -> type) { case BYTE: return *np -> d_ptr; case SHORT: return *(short *)np -> d_ptr; case LONG: return *(long *)np -> d_ptr; case FLOAT: return *(float *)np -> d_ptr; case DOUBLE: return *(double *)np -> d_ptr; default: Fprintf(stderr, "get_genhd_val_l: default value returned for %s because type is %s\n", name, type_codes[np->type]); return def_val; }}doubleget_genhd_val(name, hd, def_val)char *name;struct header *hd;double def_val;{ struct gen_hd *np, *lookup ();/* check arguments */ spsassert (name != NULL && hd != NULL,"Bad args to get_genhd_val");/* return def_val value if name not defined */ if ((np = lookup (name, hd -> variable.gentab)) == NULL) return def_val;/* return value as a double for numeric types */ switch (np -> type) { case BYTE: return *np -> d_ptr; case SHORT: return *(short *)np -> d_ptr; case LONG: return *(long *)np -> d_ptr; case FLOAT: return *(float *)np -> d_ptr; case DOUBLE: return *(double *)np -> d_ptr; default: Fprintf(stderr, "get_genhd_val: default value returned for %s because type is %s\n", name, type_codes[np->type]); return def_val; }}doubleget_genhd_val_array(name, index, hd, def_val)char *name;int index;struct header *hd;double def_val;{ struct gen_hd *np, *lookup ();/* check arguments */ spsassert (name != NULL && hd != NULL,"Bad args to get_genhd_val"); spsassert (index > -1, "index less than zero in get_genhd_val");/* return def_val value if name not defined */ if ((np = lookup (name, hd -> variable.gentab)) == NULL) return def_val;/* return def_val value if index is greater than number of items */ if (index > (np->size-1)) return def_val;/* return value as a double for numeric types */ switch (np -> type) { case BYTE: return *(np -> d_ptr + index); case SHORT: return * ((short *)np -> d_ptr + index); case LONG: return *((long *)np -> d_ptr + index); case FLOAT: return *((float *)np -> d_ptr + index); case DOUBLE: return *((double *)np -> d_ptr + index); default: Fprintf(stderr, "get_genhd_val: default value returned for %s because type is %s\n", name, type_codes[np->type]); return def_val; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -