📄 header.c
字号:
/* Generate a Header structure containing all relevant information in the Signal structure including any previous header. Total header length will always be modulo 4 bytes. */Header *w_write_header( sig ) Signal *sig;{ 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 reallocate 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 outputis placed (space separated) between the keyword and the value. This makespossible 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));}/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/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 */}/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/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,"%ld",&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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -