Wild Life: trees.c Source File

Wild Life 2.30

- Main Page
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Macros
trees.c
Go to the documentation of this file.
1
6 /* Copyright 1991 Digital Equipment Corporation.
7 ** All Rights Reserved.
8 *****************************************************************/
9
10 #include "defs.h"
11
21 long intcmp(long a,long b)
22 {
23 #ifdef CMPDBG
24 printf("intcmp a = %ld b = %ld a - b = %ld\n", a ,b , a - b);
25 #endif
26 return a - b;
27 }
28
29
41 long is_int(char **s, long *len, long *sgn)
42 {
43 char *sint; /* Ptr to first non-zero digit */
44 char *stmp; /* Scratchpad for string ptr */
45 #ifdef CMPDBG
46 printf("is_int *s = %s\n",*s);
47 #endif
48 /*
49 { register char *p= *s;
50 register char c= *p;
51 if(c>'0' && c<='9' && *(p+1)==0) return TRUE;
52 }
53 */
54
55 stmp=(*s);
56 if ((*sgn=(*stmp=='-'))) {
57 stmp++;
58 if (!*stmp)
59 {
60 #ifdef CMPDBG
61 printf("is_int = FALSE\n");
62 #endif
63 return FALSE;
64 }
65 }
66 if (!*stmp)
67 {
68 #ifdef CMPDBG
69 printf("is_int = FALSE\n");
70 #endif
71 return FALSE; /* 6.10 */
72 }
73 while (*stmp=='0') stmp++;
74 sint=stmp;
75 while (*stmp) {
76 if (*stmp<'0' || *stmp>'9')
77 {
78 #ifdef CMPDBG
79 printf("is_int = FALSE\n");
80 #endif
81 return FALSE;
82 }
83 stmp++;
84 }
85 *len=stmp-sint;
86 *s=sint;
87 #ifdef CMPDBG
88 printf("is_int = TRUE *len = %ld *sgn = %ld *s = %s\n",*len,*sgn,*s);
89 #endif
90 return TRUE;
91 }
92
106 long featcmp(char *str1,char *str2)
107 {
108 long len1,len2,sgn1,sgn2;
109 char *s1,*s2;
110
111 if(str1==str2)
112 return 0;
113
114 /* if (*str1==0 && *str2==0) return 0; "" bug is unaffected -- PVR 23.2.94 */
115
116 if(*(str1+1)==0 && *(str2+1)==0)
117 return *str1 - *str2;
118
119
120 s1=str1; /* Local copies of the pointers */
121 s2=str2;
122
123 if (is_int(&s1,&len1,&sgn1)) {
124 if (is_int(&s2,&len2,&sgn2)) {
125 if (sgn1!=sgn2) return (sgn2-sgn1); /* Check signs first */
126 if (len1!=len2) return (len1-len2); /* Then check lengths */
127 return strcmp(s1,s2); /* Use strcmp only if same sign and length */
128 }
129 else
130 return -1;
131 }
132 else {
133 if (is_int(&s2,&len2,&sgn2))
134 return 1;
135 else
136 return strcmp(s1,s2);
137 }
138 }
139
150 char *heap_ncopy_string(char *s,int n)
151 {
152 char *p;
153
154 if (s==one || s==two) return s;
155
156 p=(char *)heap_alloc(n+1);
157 strncpy(p,s,n);
158 p[n]='\0';
159
160 return p;
161 }
162
172 char *heap_copy_string(char *s)
173 { return heap_ncopy_string(s,strlen(s)); }
174
184 char *stack_copy_string(char *s)
185 {
186 char *p;
187
188 if (s==one || s==two) return s;
189
190 p=(char *)stack_alloc(strlen(s)+1);
191 strcpy(p,s);
192
193 return p;
194 }
195
224 ptr_node general_insert(long comp,char *keystr,ptr_node *tree,GENERIC info,long heapflag,long copystr,long bkflag)
225 {
226 long cmp;
227 ptr_node result;
228 long to_do=TRUE;
229
230
231 do {
232 if (*tree==NULL) {
233 if (bkflag==1) push_ptr_value(int_ptr,(GENERIC *)tree);
234 else if (bkflag==2) push_ptr_value_global(int_ptr,(GENERIC *)tree);
235 *tree = (heapflag==HEAP) ? HEAP_ALLOC(node): STACK_ALLOC(node);
236 result= *tree;
237 (*tree)->key = copystr ? heap_copy_string(keystr) : keystr;
238 (*tree)->left=NULL;
239 (*tree)->right=NULL;
240 (*tree)->data=info;
241 to_do=FALSE;
242 }
243 else {
244 if (comp == INTCMP)
245 cmp = intcmp((long)keystr,(long) (*tree)->key);
246 else if (comp == FEATCMP)
247 cmp = featcmp(keystr,(*tree)->key);
248 else if (comp == STRCMP)
249 cmp = strcmp(keystr,(*tree)->key);
250 else
251 Errorline("Bad comp in general_insert.\n");
252
253 if (cmp<0)
254 tree=(&((*tree)->left));
255 else
256 if (cmp==0) {
257 if (bkflag)
258 Errorline("attempt to overwrite an existing feature; ignored.\n");
259 else
260 (*tree)->data=info;
261 result= *tree;
262 to_do=FALSE;
263 }
264 else
265 tree=(&((*tree)->right));
266 }
267 } while (to_do);
268
269 return result;
270 }
271
284 void heap_insert_copystr(char *keystr,ptr_node *tree,GENERIC info)
285 {
286 (void)general_insert(FEATCMP,keystr,tree,info,HEAP,TRUE,0L);
287 }
288
301 void stack_insert_copystr(char *keystr,ptr_node *tree,GENERIC info)
302 {
303
304 (void)general_insert(FEATCMP,keystr,tree,info,STACK,TRUE,0L);
305 }
306
320 ptr_node heap_insert(long comp,char *keystr,ptr_node *tree,GENERIC info)
321 {
322
323 return general_insert(comp,keystr,tree,info,HEAP,FALSE,0L);
324 }
325
337 ptr_node stack_insert(long comp,char *keystr,ptr_node *tree,GENERIC info)
338 {
339
340 return general_insert(comp,keystr,tree,info,STACK,FALSE,0L);
341 }
342
357 ptr_node bk_stack_insert(long comp,char *keystr,ptr_node *tree,GENERIC info)
358 {
359
360 return general_insert(comp,keystr,tree,info,STACK,FALSE,1L);
361 }
362
377 ptr_node bk2_stack_insert(long comp,char *keystr,ptr_node *tree,GENERIC info)
378 {
379
380 return general_insert(comp,keystr,tree,info,STACK,FALSE,2L);
381 }
382
394 ptr_node find(long comp,char *keystr,ptr_node tree)
395 {
396 ptr_node result;
397 long cmp;
398 long to_do=TRUE;
399
400 /*
401 if(comp==strcmp)
402 printf("%s ",keystr);
403 */
404
405 do {
406 if (tree==NULL) {
407 result=NULL;
408 to_do=FALSE;
409 }
410 else {
411 if (comp == INTCMP)
412 cmp = intcmp((long)keystr,(long) (tree)->key);
413 else if (comp == FEATCMP)
414 cmp = featcmp(keystr,(tree)->key);
415 else if (comp == STRCMP)
416 cmp = strcmp(keystr,(tree)->key);
417 else
418 Errorline("Bad comp in general_insert.\n");
419
420 if (cmp<0)
421 tree=tree->left;
422 else
423 if (cmp==0) {
424 result=tree;
425 to_do=FALSE;
426 }
427 else
428 tree=tree->right;
429 }
430 } while (to_do);
431
432
433 /* RM: Jan 27 1993
434 if(comp==strcmp)
435 printf("Find: '%s' -> %x\n",keystr,result);
436 */
437
438 return result;
439 }
440
452 ptr_node find_data(GENERIC p,ptr_node t)
453 {
454 ptr_node r=NULL;
455
456 if(t)
457 if(t->data==p)
458 r=t;
459 else {
460 r=find_data(p,t->left);
461 if(r==NULL)
462 r=find_data(p,t->right);
463 }
464
465 return r;
466 }
467
522 void delete_attr(char *s,ptr_node *n)
523 {
524 long cmp;
525 ptr_node new,r;
526
527 if (*n) {
528 cmp=featcmp(s,(*n)->key);
529 if (cmp<0)
530 delete_attr(s,&((*n)->left));
531 else if (cmp>0)
532 delete_attr(s,&((*n)->right));
533 else if ((*n)->left) {
534 if ((*n)->right) {
535 r=(*n)->right;
536 new=heap_insert(FEATCMP,r->key,&((*n)->left),r->data);
537 new->left=r->left;
538 new->right=r->right;
539 *n = (*n) -> left;
540 }
541 else
542 *n = (*n)->left;
543 }
544 else
545 *n = (*n)->right;
546 }
547 }
bk_stack_insert
ptr_node bk_stack_insert(long comp, char *keystr, ptr_node *tree, GENERIC info)
bk_stack_insert
Definition: trees.c:357
push_ptr_value
void push_ptr_value(type_ptr t, GENERIC *p)
push_ptr_value
Definition: login.c:383
HEAP
#define HEAP
Flag to indicate heap allocation.
Definition: def_const.h:324
FEATCMP
#define FEATCMP
indicates to use featcmp for comparison (in trees.c)
Definition: def_const.h:979
bk2_stack_insert
ptr_node bk2_stack_insert(long comp, char *keystr, ptr_node *tree, GENERIC info)
bk2_stack_insert
Definition: trees.c:377
INTCMP
#define INTCMP
indicates to use intcmp for comparison (in trees.c)
Definition: def_const.h:971
stack_copy_string
char * stack_copy_string(char *s)
stack_copy_string
Definition: trees.c:184
two
char * two
Definition: def_glob.h:892
find
ptr_node find(long comp, char *keystr, ptr_node tree)
find
Definition: trees.c:394
push_ptr_value_global
void push_ptr_value_global(type_ptr t, GENERIC *p)
push_ptr_value_global
Definition: login.c:523
find_data
ptr_node find_data(GENERIC p, ptr_node t)
find_data
Definition: trees.c:452
defs.h
includes
heap_copy_string
char * heap_copy_string(char *s)
heap_copy_string
Definition: trees.c:172
heap_insert_copystr
void heap_insert_copystr(char *keystr, ptr_node *tree, GENERIC info)
heap_insert_copystr
Definition: trees.c:284
wl_node::data
GENERIC data
Definition: def_struct.h:201
NULL
#define NULL
Definition: def_const.h:533
heap_ncopy_string
char * heap_ncopy_string(char *s, int n)
heap_ncopy_string
Definition: trees.c:150
wl_node::left
ptr_node left
Definition: def_struct.h:199
Errorline
void Errorline(char *format,...)
Errorline.
Definition: error.c:465
GENERIC
unsigned long * GENERIC
unsigned long *GENERIC
Definition: def_struct.h:35
stack_insert
ptr_node stack_insert(long comp, char *keystr, ptr_node *tree, GENERIC info)
stack_insert
Definition: trees.c:337
wl_node::key
char * key
Definition: def_struct.h:198
TRUE
#define TRUE
Standard boolean.
Definition: def_const.h:268
STRCMP
#define STRCMP
indicates to use strcmp for comparison (c function)
Definition: def_const.h:963
FALSE
#define FALSE
Standard boolean.
Definition: def_const.h:275
heap_insert
ptr_node heap_insert(long comp, char *keystr, ptr_node *tree, GENERIC info)
heap_insert
Definition: trees.c:320
intcmp
long intcmp(long a, long b)
intcmp
Definition: trees.c:21
one
char * one
Definition: def_glob.h:891
STACK_ALLOC
#define STACK_ALLOC(A)
Definition: def_macro.h:21
general_insert
ptr_node general_insert(long comp, char *keystr, ptr_node *tree, GENERIC info, long heapflag, long copystr, long bkflag)
ptr_node general_insert
Definition: trees.c:224
featcmp
long featcmp(char *str1, char *str2)
featcmp
Definition: trees.c:106
wl_node
Definition: def_struct.h:197
is_int
long is_int(char **s, long *len, long *sgn)
is_int
Definition: trees.c:41
delete_attr
void delete_attr(char *s, ptr_node *n)
delete_attr
Definition: trees.c:522
stack_insert_copystr
void stack_insert_copystr(char *keystr, ptr_node *tree, GENERIC info)
stack_insert_copystr
Definition: trees.c:301
HEAP_ALLOC
#define HEAP_ALLOC(A)
Definition: def_macro.h:20
heap_alloc
GENERIC heap_alloc(long s)
heap_alloc
Definition: memory.c:1616
stack_alloc
GENERIC stack_alloc(long s)
stack_alloc
Definition: memory.c:1642
STACK
#define STACK
Flag to indicate stack allocation.
Definition: def_const.h:331
wl_node::right
ptr_node right
Definition: def_struct.h:200
int_ptr
#define int_ptr
values of type_ptr
Definition: def_const.h:397