📄 header.c
字号:
{ Header *h; register int i, j; register char *p, *q; int size, used, nentries = N_HEADER_ENTRIES, npad; char *buf; if (sig) { size = STR_ALLOC; if (sig->header) size += sig->header->nbytes - sig->header->npad; if ((buf = malloc(size + 1))) { if (sig->header) { for (i = 0, p = buf, q = sig->header->header, j = sig->header->nbytes - sig->header->npad; i++ < j;) *p++ = *q++; used = j; } else { p = buf; used = 0; } setup_access(sig); for (i = 0; i < nentries; i++) { if (!head_build(&buf, &used, &size, active_header_entries[i])) return (NULL); } if ((j = used & 3)) /* need to pad to INTEGER*4? */ npad = 4 - j; else npad = 0; buf = realloc(buf, used + npad + 1); for (j = 0; j < npad; j++) buf[used + j - 1] = ' '; /* pad with spaces */ used += npad; buf[used] = 0; h = w_new_header(0); h->nbytes = used; h->header = buf; h->npad = npad; head_printf(h, "time", get_date()); return (h); } } return (NULL);}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */intchk_str_len(str, avail, req) char **str; int *avail, req;{ if (req <= *avail) return FALSE; if (str && *str && (*str = realloc(*str, req + STR_ALLOC + 1))) { *avail = req + STR_ALLOC; return FALSE; } printf("chk_str_len: Can't realloc space.\n"); return TRUE;}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *//* * Output the data described by the Selector as a formatted print to str. * Return the number of characters actually written. Ordinary C types will * be followed by newline which is included in the count. NOTE: str must be * large enough to accommodate all types of output! */arg_to_string(str, size, argp) char **str; int *size; Selector *argp;{ int i; if (str && *str && argp && argp->dest) { for (i = 0; var_types[i].proc; i++) { if (!strcmp(argp->format, var_types[i].key)) return (var_types[i].proc(str, size, argp)); } } return (0);}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *//* * This is the same as arg_to_string(), except that the format of the output * is placed (space separated) between the keyword and the value. This makes * possible printing and retrieving arbitrary header data. */arg_to_stringx(str, size, argp) char **str; int *size; Selector *argp;{ int i; Selector sl; char name[100]; if (str && *str && argp && argp->dest) { for (i = 0; var_types[i].proc; i++) { if (!strcmp(argp->format, var_types[i].key)) { sprintf(name, "%s %s", argp->name, argp->format); sl.name = name; sl.format = argp->format; sl.dest = argp->dest; return (var_types[i].proc(str, size, &sl)); } } } return (0);}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */shortpr(str, size, argp) char **str; int *size; Selector *argp;{ char *p; strcpy(*str, argp->name); p = *str + strlen(*str); sprintf(p, " %d\n", *(short *) argp->dest); return (strlen(*str));}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */intpr(str, size, argp) char **str; int *size; Selector *argp;{ char *p; strcpy(*str, argp->name); p = *str + strlen(*str); sprintf(p, " %d\n", *(int *) argp->dest); return (strlen(*str));}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */longpr(str, size, argp) char **str; int *size; Selector *argp;{ char *p; strcpy(*str, argp->name); p = *str + strlen(*str); sprintf(p, " %d\n", *(int *) argp->dest); return (strlen(*str));}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */floatpr(str, size, argp) char **str; int *size; Selector *argp;{ char *p; strcpy(*str, argp->name); p = *str + strlen(*str); sprintf(p, " %f\n", *(float *) argp->dest); return (strlen(*str));}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */doublepr(str, size, argp) char **str; int *size; Selector *argp;{ char *p; strcpy(*str, argp->name); p = *str + strlen(*str); sprintf(p, " %f\n", *(double *) argp->dest); return (strlen(*str));}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */charpr(str, size, argp) char **str; int *size; Selector *argp;{ char *p; strcpy(*str, argp->name); p = *str + strlen(*str); sprintf(p, " %c\n", *(char *) argp->dest); return (strlen(*str));}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */strpr(str, size, argp) char **str; int *size; Selector *argp;{ if (chk_str_len(str, size, strlen(argp->name) + 1 + strlen(argp->dest) + 1)) return 0; strcpy(*str, argp->name); strcat(*str, " "); strcat(*str, argp->dest); strcat(*str, "\n"); return (strlen(*str));}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */cstrpr(str, size, argp) char **str; int *size; Selector *argp;{ char *p; if (chk_str_len(str, size, strlen(argp->name) + 1 + strlen(argp->dest) + 1)) return 0; strcpy(*str, argp->name); p = *str + strlen(*str); sprintf(p, " %s\n", *((char **) argp->dest)); /* append newline and * null */ return (strlen(*str) + 1); /* include the null in length */}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */astrpr(str, size, argp) char **str; int *size; Selector *argp;{ char *p; if (chk_str_len(str, size, strlen(argp->name) + 1 + strlen(argp->dest) + 1)) return 0; strcpy(*str, argp->name); p = *str + strlen(*str); sprintf(p, " %s\n", *((char **) argp->dest)); /* append a newline */ return (strlen(*str)); /* null NOT included in length */}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */qstrpr(str, size, argp) /* Cf. qstr type in read_all_types in parse.c */ char **str; int *size; Selector *argp;{ char *p, *q; int len = 0; int need_quotes = 0; if ((!*argp->dest) || strchr(argp->dest, ' ') || (argp->dest[0] == '"')) need_quotes = 1; if (need_quotes) len = 2; /* 2 delimiting quotes */ for (q = argp->dest; *q; q++) switch (*q) { case '"': case '\\': len += 2; /* escaped */ break; default: len += 1; break; } if (chk_str_len(str, size, strlen(argp->name) + 1 + len + 1)) return 0; strcpy(*str, argp->name); if (need_quotes) strcat(*str, " \""); else strcat(*str, " "); for (p = *str + strlen(*str), q = argp->dest; *q; q++) switch (*q) { case '"': case '\\': *p++ = '\\'; /* Falls through. */ default: *p++ = *q; break; } if (need_quotes) strcpy(p, "\"\n"); else strcpy(p, "\n"); return (strlen(*str)); /* null NOT included in length */}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */shexpr(str, size, argp) char **str; int *size; Selector *argp;{ char *p; strcpy(*str, argp->name); p = *str + strlen(*str); sprintf(p, " %x\n", *(short *) argp->dest); return (strlen(*str));}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */lhexpr(str, size, argp) char **str; int *size; Selector *argp;{ char *p; strcpy(*str, argp->name); p = *str + strlen(*str); sprintf(p, " %x\n", *(int *) argp->dest); return (strlen(*str));}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */loctpr(str, size, argp) char **str; int *size; Selector *argp;{ char *p; strcpy(*str, argp->name); p = *str + strlen(*str); sprintf(p, " %o\n", *(int *) argp->dest); return (strlen(*str));}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */soctpr(str, size, argp) char **str; int *size; Selector *argp;{ char *p; strcpy(*str, argp->name); p = *str + strlen(*str); sprintf(p, " %o\n", *(short *) argp->dest); return (strlen(*str));}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */listpr(str, size, argp) char **str; int *size; Selector *argp;{ int psize; register char *p; List *l; if ((l = *((List **) argp->dest))) { if (chk_str_len(str, size, strlen(argp->name) + 2)) return 0; strcpy(*str, argp->name); strcat(*str, " ;"); psize = strlen(*str); while (l) { p = *str + psize; if (l->str && *l->str) { psize += 1 + strlen(l->str); if (chk_str_len(str, size, psize)) return 0; sprintf(p, " %s", l->str); } l = l->next; } p = *str + psize; if (chk_str_len(str, size, psize + 3)) return 0; sprintf(p, " ;\n"); return (psize + 3); } return (0);}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */stringpr(str, size, argp) char **str; int *size; Selector *argp;{ int psize, dsize; register int i, n; register char *p, *q, *s; if (argp->dest && *((char **) argp->dest)) { s = *((char **) argp->dest); sscanf(s, "%d", &dsize); sprintf(*str, "%s %d ", argp->name, dsize); /* * Skip the byte count (possibly preceeded by whitespace) and the * following (single) space or tab. */ q = s; while ((*q < '0') || (*q > '9')) q++; while ((*q >= '0') && (*q <= '9')) q++; q++; /* q now points to start of data source */ p = *str + (psize = strlen(*str)); /* point to start of data * dest. */ if (chk_str_len(str, size, psize + dsize)) return 0; for (i = dsize; i-- > 0;) *p++ = *q++; *p = 0; return (dsize + psize); } return (0);}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */head_build(buf, used, size, arg) char **buf; /* buffer into which the header is being * built */ Selector *arg; /* pointer to data destined for header */ int *size, /* dimension(-1) of *buf */ *used; /* number of bytes of buff that have been * used */{ int n; static char *scr = NULL; /* A scratch character buffer */ static int scrsize; if (!scr) { if (!(scr = malloc(STR_ALLOC + 1))) { printf("Can't allocate scratch space in w_write_header()\n"); return FALSE; } scrsize = STR_ALLOC; } n = arg_to_string(&scr, &scrsize, arg); if ((n + *used) > *size) { /* bump header block size */ if (!(*buf = realloc(*buf, *size + n + STR_ALLOC + 1))) { printf("Can't allocate enough buffer space in head_build()\n"); return (FALSE); } *size += STR_ALLOC + n; } strncpy((char *) ((*buf) + (*used)), scr, n); *used += n; return (TRUE);}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *//* * Put a keyword-argument pair in header h. Selname points to a string * containing a defined keyword; argp points to a value of the corresponding * type. */head_printf(h, selname, argp) Header *h; char *selname; char *argp;{ Selector *s; static char *str = NULL; static int strsize; int n; if (!strcmp("identifiers", selname)) return (head_ident(h, argp)); if (!str) { if (!(str = malloc(STR_ALLOC + 1))) { printf("Can't allocate scratch space in head_printf()\n"); return 0; } strsize = STR_ALLOC; } s = &head_a0; while (s) { if (!strcmp(selname, s->name)) { if (*(s->format) == '#') s->dest = (char *) (&argp); else s->dest = argp; if ((n = arg_to_string(&str, &strsize, s))) head_append(h, str, n); s->dest = NULL; return (n); } s = s->next; } return (0);}#undef STR_ALLOC/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */head_ident(h, str) Header *h; char *str;{ int n, i; static char delim[] = ";*#!|%@$^&()_:+/?=", ident[] = "identifiers"; char *cp; if (h && str && (n = strlen(str))) { i = 0; while (strrchr(str, delim[i])) { if (!delim[i]) { printf("Couldn't find a unique delimiter from the set %s in head_list()\n", delim); return (0); } else i++; } if ((cp = malloc(n + 6 + strlen(ident) + 1))) { sprintf(cp, "%s %c %s %c\n", ident, delim[i], str, delim[i]); head_append(h, cp, (n = strlen(cp))); free(cp); return (n); } else printf("Can't allocate a buffer in head_ident()\n"); } else printf("Bad arguments to head_ident()\n"); return (0);}/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *//* * Append "size" bytes drawn from "arg" to header "h." The total header * length (h->nbytes) will be forced to be modulo-4.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -