📄 sds_tree.c
字号:
d = (sds_desc *)n->data; printf("%s", d->name); if (d->nelems > 1) printf("[%d]",d->nelems); if (d->type < NPRIMITIVES) printf(" (%s)",type_name[d->type]); else printf(" (Structure)"); /* Must check ntree here for defn only */ if (values && d->type < NPRIMITIVES) { switch (d->type) { char *tim; case SDS_SHORT: printf(" value: %d", *(short *)d->offset); break; case SDS_LONG: printf(" value: %d", *(int *)d->offset); break; case SDS_CHAR: printf(" value: %d", (int)*(char *)d->offset & 0xff); break; case SDS_POINTER: printf(" value: %x", *(int *)d->offset); break; case SDS_UNIX_TIME: tim = ctime((time_t *)d->offset); tim[strlen(tim) - 1] = (char)0; printf(" value: %s", tim); break; case SDS_STRING: printf(" value: %s", (char *)d->offset); break; case SDS_FSTRING: printf(" value: %c", *(char *)d->offset); break; case SDS_FLOAT: printf(" value: %e", *(float *)d->offset); break; case SDS_DOUBLE: printf(" value: %e", *(double *)d->offset); break; default: break; } } if (depth == SDS_OBJECT_LEVEL) printf(" (Object)"); printf("\n");}int sds_tree(sds, object, itree)sds_handle sds,object;int itree;{ struct direc *dptr; sds_tnode *n; if (!itree) { itree = sds_new_tree(sds_new_desc(1,sds_obind2name(sds,0),SDS_SDS)); n = sds_get_top(itree); } else { n = sds_get_top(itree); } if (!itree) { sds_push_error(SDS_NO_SPC,SDS_WARNING,"Could not create new tree"); return 0; } if ((dptr = sds_direc(sds)) == 0) { sds_push_error(SDS_NO_SUCH_SDS,SDS_WARNING,"Could not find dataset"); return 0; } if (object < 1L || object >= dptr[0].nelems) { sds_push_error(SDS_NO_SUCH_OBJ,SDS_WARNING,"Object not in dataset"); return 0; } n = maketree(sds_tlist(sds), dptr[object].elemcod, itree, n, sds_heap(sds), dptr[object].nelems, sds_obind2name(sds,object), SDS_SUN3ARC); sds_start_tree(itree, n); sds_makeaddr(itree, (void *)sds_obind2ptr(sds,object), (void *)0); return itree;}sds_tnode *maketree(tlist,code,itree,tnode,nheap,number,name, arc)struct type_list *tlist;sds_code code;int itree;sds_tnode *tnode;char *nheap;sds_code number;char *name;int arc;{ sds_code index; sds_desc *d,*bitd,*c = (sds_desc *)tnode->data; char *tname = 0; sds_tnode *dtnode = 0, *ctnode = tnode->down; char nativealign = 0; char sourcealign = 0; long nativesize = 0L; long sourcesize = 0L; int indindex = -1; int bitsused = 0; index = code & ~SDS_CODE_MASK; d = sds_new_desc(number,name,code); /* Find the 'current' tnode */ if (!ctnode) ctnode = sds_down_tnode(tnode,d); else { while(ctnode->next) ctnode = ctnode->next; ctnode = sds_next_tnode(ctnode,d); } /* This is a compound data type..... */ if ((code & SDS_INDLIST)) { if ((code = tlist[index].elemcod) & SDS_LENLIST) tname = nheap + (tlist[index++].nelems & 0xffff); if ((code = tlist[index].elemcod) & SDS_SIZE_ALIGN) indindex = index++; } else /* .....or simple array */ { d->type = code; d->size = sds_psize(code); d->ssize = sds_psize(code); d->nelems = number; d->align = (char)sds_palign(code); d->salign = sds_arc_palign(arc, d->type); return ctnode; } while (!((code = tlist[index].elemcod) & SDS_RETLIST)) { number = tlist[index].nelems; if ((code & SDS_INDLIST)) { maketree(tlist, code, itree, ctnode, nheap, number, tname, arc); dtnode = ctnode->down; while(dtnode->next) dtnode = dtnode->next; d = (sds_desc *)dtnode->data; } else if ((code & SDS_OBJECT_DEF)) { sds_tnode * ttnode = sds_findname(itree,tname); if (ttnode == (sds_tnode *)0) { sds_push_error(SDS_NO_SUCH_OBJ,SDS_FATAL, "Unresolved reference defining object"); return 0; } d = sds_new_desc(number,"",code); memcpy((char *)d, (char *)ttnode->data, sizeof(sds_desc)); d->nelems = number; if (!dtnode) { c = (sds_desc *)ctnode->data; dtnode = sds_down_tnode(ctnode,d); } else { while (dtnode->next) dtnode = dtnode->next; c = (sds_desc *)dtnode->data; dtnode = sds_next_tnode(dtnode,d); } sds_dup_down(itree, ttnode, dtnode); } else { d = sds_new_desc(number,tname,code); if(d->type == SDS_BITFIELD) { bitd = d; if (!bitsused) { d->align = (char)sds_palign(SDS_INT); d->size = sds_psize(SDS_INT); d->salign = sds_arc_palign(arc, SDS_INT); d->ssize = d->size; } else { d->align = (char)0; d->size = 0L; d->salign =(char)0; d->ssize = 0L; } bitsused += d->nelems; if (bitsused == BITS(int)) { d->type = SDS_END_BITFIELDS; bitsused = 0; } } else { if (bitsused) { bitsused = 0; bitd->type = SDS_END_BITFIELDS; } d->align = (char)sds_palign(tlist[index].elemcod); d->size = sds_psize(tlist[index].elemcod); d->salign = sds_arc_palign(arc, d->type); d->ssize = d->size; } if (!dtnode) { c = (sds_desc *)ctnode->data; dtnode = sds_down_tnode(ctnode,d); } else { while (dtnode->next) dtnode = dtnode->next; c = (sds_desc *)dtnode->data; dtnode = sds_next_tnode(dtnode,d); } } sourcealign = (d->salign > sourcealign)?d->salign:sourcealign; nativealign = (d->align > nativealign)?d->align:nativealign; sourcesize += align_delta(sourcesize,d->salign); nativesize += align_delta(nativesize,d->align); if (d->type != SDS_BITFIELD) { nativesize += d->size * d->nelems; sourcesize += d->ssize * d->nelems; } else { nativesize += d->size; sourcesize += d->ssize; } tname = strchr(tname,(char)0) + 1; index++; } d = (sds_desc *)dtnode->top->data; d->salign = sourcealign; sourcesize += align_delta(sourcesize,sourcealign); d->ssize = sourcesize; d->align = nativealign; nativesize += align_delta(nativesize,nativealign); d->size = nativesize; if (indindex != -1) { tlist[indindex].nelems = d->size; tlist[indindex].elemcod = SDS_SIZE_ALIGN | ((long)d->align & 0xff); } return ctnode;}sds_tnode *sds_findname(itree,name)int itree;char *name;{ return sds_find(itree,0,name,0); }sds_tnode *sds_likename(itree,n,name)int itree;sds_tnode *n;char *name;{ return sds_find(itree,n,name,1); }sds_tnode *sds_find(itree,n,name, like)int itree;sds_tnode *n;char *name;int like;{ char *sdsname = 0; char *oname = 0; sds_desc *d; char *endpoint,*startpoint = name; int failure = 0; int startfromtop = 0; if (!n) /* start from top */ { n = sds_get_top(itree); startfromtop = 1; } else n = n->next; if (!n) /* still 0 - invalid tree or end of list */ return 0; if (startfromtop) { endpoint = strchr(name,'|'); if (endpoint) { int length = endpoint - startpoint; failure = 1; sdsname = malloc(length); strncpy(sdsname,startpoint,length); sdsname[length] = (char)0; startpoint = endpoint + 1; while (n != 0) { d = (sds_desc *)n->data; if ((like && strstr(d->name, sdsname)) || (!like && !strcmp(sdsname,d->name))) { failure = 0; n = n->down; break; } } free(sdsname); if (failure) return 0; } else n = n->down; } oname = nextname(&startpoint); while (n) { if ((n = look_on_row(n,oname,like)) != 0) { if ((oname = nextname(&startpoint)) != 0) n = n->down; else return n; } else return 0; } return 0;}char *nextname(start)char **start;{ static char * buffer = 0; char *end; int length; if (*start == (char)0) { if (buffer) free(buffer); buffer = 0; } else { if ((end = strchr(*start,'.')) != 0) length = end - *start; else length = strlen(*start) + 1; if (buffer) free(buffer); buffer = malloc(length+1); strncpy(buffer,*start,length); buffer[length] = (char)0; *start = end?end + 1:0; } return buffer;}sds_tnode *look_on_row(n, name, like)sds_tnode *n;char *name;int like;{ sds_desc *d; do { d = (sds_desc *)n->data; if ((like && strstr(d->name, name)) || (!like && !strcmp(name,d->name))) return n; n = n->next; } while (n != 0); return 0;}struct type_list * maketlist(itree)int itree;{ sds_tnode *n; sds_desc *d; sds_code code; int i = 0; tree_pos * pos = sds_get_pos(itree); int nobs = 0, nlist = 0, index = 0; struct type_list *tlist; struct direc *dptr; sds_start_tree(itree, 0); /* count dptr and tlist size */ while ((n = sds_trav_tree(itree)) != 0) { d = (sds_desc *)n->data; if (d->type & SDS_INDLIST) { nlist += 3; /* alignment, size and return space */ if (pos->depth == SDS_OBJECT_LEVEL) { nobs++; nlist--; } } if (!((pos->depth == SDS_OBJECT_LEVEL) && !(d->type & SDS_INDLIST))) nlist++; } printf("%d compound objects, tlist %x\n",nobs,nlist); tlist = (struct type_list *)malloc(nlist * sizeof(struct type_list)); dptr = (struct direc *)malloc((nobs + 1) * sizeof(struct direc)); tlist[nlist-1].elemcod = SDS_ENDLIST; tlist[nlist-1].nelems = 0L; makestruct(sds_get_top(itree)->down, &index, tlist, dptr, SDS_OBJECT_LEVEL); while ((code = tlist[i].elemcod) != SDS_ENDLIST) { printf("%x: %x code %x\n",i,tlist[i].nelems, code); i++; } printf("%x: %x code %x\n",i,tlist[i].nelems, code); for (i= 1; i<nobs;i++) printf("Object %d code %x size %d\n",i,dptr[i].elemcod, dptr[i].elemsz); return tlist;}sds_codemakestruct(n,index,tlist, dptr, depth)sds_tnode *n;int *index,depth;struct type_list *tlist;struct direc *dptr;{ sds_desc *d; int nindex = *index; int start = *index; int iso = (depth == SDS_OBJECT_LEVEL)?1:0; if (!iso) nindex += (countstruct(n) + 2); do { d = (sds_desc *)n->data; if (!iso) { tlist[*index].elemcod = d->type; tlist[*index].nelems = d->nelems; } if (d->type & SDS_INDLIST || d->type == SDS_SDS) { tlist[nindex].elemcod = SDS_LENLIST; tlist[nindex].nelems = 0L; nindex++; tlist[nindex].elemcod = SDS_SIZE_ALIGN | (long)(d->align & 0xff); tlist[nindex].nelems = d->size; nindex++; if (iso) { printf("Object %d\n",iso); dptr[iso].elemcod = makestruct(n->down,&nindex,tlist,dptr, depth + 1); dptr[iso].elemsz = d->size; dptr[iso].align_type = d->align; dptr[iso].nelems = d->nelems; iso++; } else tlist[*index].elemcod = makestruct(n->down,&nindex,tlist,dptr,depth + 1); } if (!iso) (*index)++; } while ((n = n->next) != 0); if (!iso) { tlist[*index].elemcod = SDS_RETLIST; tlist[*index].nelems = 0L; (*index)++; } return (start - 2) | SDS_INDLIST;}intcountstruct(n)sds_tnode *n;{ int i = 0; while((n = n->next) != 0) i++; return i;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -