home *** CD-ROM | disk | FTP | other *** search
- /* ===( tree.c )=== */
-
- /*
- * Contains all tree initialization, manipulation, etc. functions
- */
-
- #include <stdio.h>
- #include <bench.h>
- #include <tree.h>
- #include <dllist.h>
-
- #ifdef ANSI
- static void t_entinit(TREE *, struct tree_entry *, char *);
- int t_idxcmp(struct tree_entry *, struct tree_entry *);
- static void t_subbuild(TREE *, struct tree_entry *);
- static char *t_token(char *, char *, char *);
- #else
- static void t_entinit();
- int t_idxcmp();
- static void t_subbuild();
- static char *t_token();
- #endif
-
- static int t_row;
- static int t_col;
-
- struct tree_entry *t_add(tp, name)
- TREE *tp;
- char *name;
- {
- struct tree_entry t_ent;
- struct tree_entry *t_new = NULL;
-
- t_entinit(tp, &t_ent, name);
-
- if (dll_add(tp->listhd, &t_ent, ADD_NOSORT) != -1)
- t_new = (struct tree_entry *)dll_curr(tp->listhd);
-
- return(t_new);
- }
-
- int t_build(tp)
- TREE *tp;
- {
- struct tree_entry *t_ent;
- t_row = 0;
- t_col = 1;
-
- if ((t_ent = (struct tree_entry *)dll_seek(tp->listhd, 0, SEEK_SET)) != NULL)
- t_subbuild(tp, t_ent);
-
- return(t_row);
- }
-
- struct tree_entry *t_childadd(tp, name, parent)
- TREE *tp;
- char *name;
- char *parent;
- {
- struct tree_entry *t_new = NULL;
- struct tree_entry *t_old;
-
- t_old = t_namefind(tp, parent);
- if (t_old != NULL)
- {
- t_new = t_add(tp, name);
- if (t_new != NULL)
- {
- t_new->t_parent = t_old->t_idx;
- if (t_old->t_child == -1)
- t_old->t_child = t_new->t_idx;
- else
- {
- for(t_old = t_idxfind(tp, t_old->t_child); t_old->t_next != -1; t_old = t_idxfind(tp, t_old->t_next))
- ;
- t_new->t_prev = t_old->t_idx;
- t_old->t_next = t_new->t_idx;
- }
- }
- }
-
- return(t_new);
- }
-
- int t_close(tp)
- TREE *tp;
- {
- int listhd;
-
- listhd = tp->listhd;
- free(tp);
-
- return(dll_close(listhd));
- }
-
- static void t_entinit(tp, t_ent, name)
- TREE *tp;
- struct tree_entry *t_ent;
- char *name;
- {
- strcpy(t_ent->t_name, name);
- t_ent->t_idx = tp->num_members++;
- t_ent->t_parent = -1;
- t_ent->t_child = -1;
- t_ent->t_prev = -1;
- t_ent->t_next = -1;
- t_ent->t_from = -1;
- t_ent->t_to = -1;
- t_ent->t_row = 1;
- t_ent->t_col = 1;
-
- return;
- }
-
- int t_idxcmp(te1, te2)
- register struct tree_entry *te1, *te2;
- {
- return(te2->t_idx - te1->t_idx);
- }
-
- struct tree_entry *t_idxfind(tp, t_idx)
- TREE *tp;
- int t_idx;
- {
- struct tree_entry t_ent;
-
- t_ent.t_idx = t_idx;
- return((struct tree_entry *)dll_find(tp->listhd, &t_ent));
- }
-
- struct tree_entry *t_namefind(tp, name)
- TREE *tp;
- char *name;
- {
- struct tree_entry *t_find;
-
- if (tp->uniq == UNIQUE)
- {
- t_find = (struct tree_entry *)dll_seek(tp->listhd, 0, SEEK_SET);
- for (; t_find != NULL; t_find = (struct tree_entry *)dll_next(tp->listhd))
- if (strcmp(t_find->t_name, name) == 0)
- break;
- }
- else
- {
- char *partial;
- char sep;
-
- t_find = t_rowfind(tp, 1);
- if (t_find != NULL)
- {
- if (strncmp(t_find->t_name, name, strlen(t_find->t_name)) == 0)
- {
- name += strlen(t_find->t_name);
- for(partial = t_token(name, ">\\/", &sep); partial != NULL; partial = t_token(NULL, ">\\/", &sep))
- {
- t_find = t_idxfind(tp, t_find->t_child);
- while (t_find != NULL)
- {
- if (strcmp(t_find->t_name, partial) == 0)
- break;
- if (t_find->t_next == -1)
- t_find = NULL;
- else
- t_find = t_idxfind(tp, t_find->t_next);
- }
- if (t_find == NULL)
- break;
- }
- }
- else
- t_find = NULL;
- }
- }
-
- return(t_find);
- }
-
- TREE *t_open(name, uniq)
- char *name;
- int uniq;
- {
- TREE *tp;
- struct tree_entry t_ent;
-
- tp = (TREE *)alloc(sizeof(TREE));
-
- if (tp != NULL)
- {
- tp->num_members = 0;
- tp->uniq = uniq;
-
- if ((tp->listhd = dll_open(t_idxcmp, sizeof(struct tree_entry))) == -1)
- {
- free(tp);
- tp = NULL;
- }
- else
- {
- t_entinit(tp, &t_ent, name);
-
- if (dll_add(tp->listhd, &t_ent, ADD_NOSORT) == -1)
- {
- dll_close(tp->listhd);
- free(tp);
- tp = NULL;
- }
- }
- }
-
- return(tp);
- }
-
- struct tree_entry *t_rowfind(tp, t_row)
- TREE *tp;
- int t_row;
- {
- struct tree_entry *t_find;
-
- t_find = (struct tree_entry *)dll_seek(tp->listhd, 0, SEEK_SET);
- for ( ; t_find != NULL; t_find = (struct tree_entry *)dll_next(tp->listhd))
- if (t_row == t_find->t_row)
- break;
-
- return(t_find);
- }
-
- static void t_subbuild(tp, t_ent)
- TREE *tp;
- struct tree_entry *t_ent;
- {
- static struct tree_entry *t_find;
-
- t_ent->t_row = ++t_row;
- t_ent->t_col = t_col;
-
- if (t_ent->t_child != -1)
- if ((t_find = t_idxfind(tp, t_ent->t_child)) != NULL)
- {
- t_col += 2;
- t_subbuild(tp, t_find);
- t_col -= 2;
- }
-
- if (t_ent->t_to != -1)
- if ((t_find = t_idxfind(tp, t_ent->t_to)) != NULL)
- {
- t_col += 2;
- t_subbuild(tp, t_find);
- t_col -= 2;
- }
-
- if (t_ent->t_next != -1)
- if ((t_find = t_idxfind(tp, t_ent->t_next)) != NULL)
- t_subbuild(tp, t_find);
-
- return;
- }
-
- struct tree_entry *t_toadd(tp, name, from)
- TREE *tp;
- char *name;
- char *from;
- {
- struct tree_entry *t_new = NULL;
- struct tree_entry *t_old;
-
- t_old = t_namefind(tp, from);
- if (t_old != NULL)
- {
- t_new = t_add(tp, name);
- if (t_new != NULL)
- {
- t_old->t_to = t_new->t_idx;
- t_new->t_from = t_old->t_idx;
- }
- }
- return(t_new);
- }
-
- static char *t_token(string, delim, end)
- char *string, *delim, *end;
- {
- static char *ptr;
- static char *ds;
- char *rptr;
- char *p;
- char *d;
-
- if (string != NULL)
- ptr = string;
-
- ds = delim;
-
- do
- {
- for(p = ptr; *p; p++)
- {
- for(d = ds; *d; d++)
- {
- if (*d == *p)
- {
- *end = *d;
- rptr = ptr;
- ptr = p + 1;
- break;
- }
- }
- if (*d)
- break;
- }
- if (*p)
- *p = 0;
- else
- {
- if (*ptr == 0)
- rptr = NULL;
- else
- {
- rptr = ptr;
- ptr = p;
- *end = *p;
- }
- }
- if (rptr == NULL)
- break;
- }
- while(*rptr == 0);
-
- return(rptr);
- }
-
- int t_write(tp, tfp)
- TREE *tp;
- FILE *tfp;
- {
- struct tree_entry *t_ent;
-
- if (fwrite(tp, sizeof(TREE), 1, tfp) != 1)
- return(-1);
-
- t_ent = (struct tree_entry *)dll_seek(tp->listhd, 0, SEEK_SET);
- for ( ; t_ent != NULL; t_ent = (struct tree_entry *)dll_next(tp->listhd))
- if (fwrite(t_ent, sizeof(struct tree_entry), 1, tfp) != 1)
- return(-1);
- }
-
- TREE *t_read(tfp)
- FILE *tfp;
- {
- struct tree_entry t_ent;
- TREE *tp;
- int i;
-
- tp = (TREE *)alloc(sizeof(TREE));
- if (tp != NULL)
- {
- i = fread(tp, sizeof(TREE), 1, tfp);
- if (i != 1 || tp->num_members <= 0)
- {
- free(tp);
- tp = NULL;
- }
- else
- {
- tp->listhd = dll_open(t_idxcmp, sizeof(struct tree_entry));
- if (tp->listhd == -1)
- {
- free(tp);
- tp = NULL;
- }
- else
- {
- for(i = 0; i < tp->num_members; i++)
- {
- if (fread(&t_ent, sizeof(struct tree_entry), 1, tfp) != 1)
- break;
- if (t_ent.t_idx != i)
- break;
- if (dll_add(tp->listhd, &t_ent, ADD_NOSORT) == -1)
- break;
- }
- if (i < tp->num_members)
- {
- dll_close(tp->listhd);
- free(tp);
- tp = NULL;
- }
- }
- }
- }
- return(tp);
- }
-