home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume27 / avl-subs / part01 / t_trtest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-06  |  2.2 KB  |  140 lines

  1. /* t_trtest - test the tree functions
  2.  * vix 24jul87 [documented, added savestr for net distribution]
  3.  * vix 22jan93 [revisited; uses RCS, ANSI, POSIX; has bug fixes]
  4.  */
  5.  
  6. #ifndef LINT
  7. static char RCSid[] = "$Id:";
  8. #endif
  9.  
  10. #define MAIN
  11.  
  12. #include <stdio.h>
  13. #include <strings.h>
  14. #include <stdlib.h>
  15. #include "vixie.h"
  16. #include "tree.h"
  17.  
  18.  
  19. static void    trtest        __P( (tree **, char *, int) );
  20. static void    tree_trav1    __P( (tree *, int) );
  21. static int    compar        __P( (char *, char *) );
  22. static void    duar        __P( (char *) );
  23. static char    *savestr    __P( (char *) );
  24.  
  25.  
  26. int
  27. main()
  28. {
  29.     tree    *t;
  30.     char    line[100];
  31.  
  32.     tree_init(&t);
  33.     while (printf("key (or .):  "), gets(line), line[0] != '.')
  34.     {
  35.         if (strncmp(line, "~r ", 3)) {
  36.             trtest(&t, line, 1);
  37.         }
  38.         else {
  39.             FILE *f;
  40.  
  41.             if (!(f = fopen(&line[3], "r")))
  42.                 perror(&line[3]);
  43.             else {
  44.                 while (fgets(line, 100, f)) {
  45.                     line[strlen(line)-1] = '\0';
  46.                     printf("(%s)\n", line);
  47.                     trtest(&t, line, 0);
  48.                 }
  49.                 fclose(f);
  50.             }
  51.         }
  52.     }
  53.     return 0;
  54. }
  55.  
  56. static void
  57. trtest(tt, line, inter)
  58.     tree    **tt;
  59.     char    *line;
  60.     int    inter;
  61. {
  62.     char    opts[100], *pc, *n;
  63.     int    opt, status;
  64.  
  65.     pc = tree_srch(tt, compar, line);
  66.     printf("tree_srch=%x\n", (unsigned)pc);
  67.     if (pc)
  68.     {
  69.         printf("     <%s>\n", pc);
  70.  
  71.         if (inter) {
  72.             printf("delete? "); gets(opts); opt = (opts[0]=='y');
  73.         }
  74.         else
  75.             opt = 1;
  76.  
  77.         if (opt) {
  78.             status = tree_delete(tt, compar, line, duar);
  79.             printf("delete=%d\n", status);
  80.         }
  81.     }
  82.     else
  83.     {
  84.         if (inter) {
  85.             printf("add? "); gets(opts); opt = (opts[0]=='y');
  86.         }
  87.         else
  88.             opt = 1;
  89.  
  90.         if (opt) {
  91.             char    *savestr();
  92.  
  93.             n = savestr(line);
  94.             tree_add(tt, compar, n, duar);
  95.         }
  96.     }
  97.     tree_trav1(*tt, 0);
  98. }
  99.  
  100. static void
  101. tree_trav1(t, l)
  102.     tree    *t;
  103.     int    l;
  104. {
  105.     int    i;
  106.  
  107.     if (!t) return;
  108.     tree_trav1(t->tree_l, l+1);
  109.     for (i=0;  i<l;  i++) printf("  ");
  110.     printf("%08lx (%s)\n", (unsigned)t->tree_p, (char*)t->tree_p);
  111.     tree_trav1(t->tree_r, l+1);
  112. }    
  113.     
  114. static void
  115. duar(pc)
  116.     char *pc;
  117. {
  118.     printf("duar called, pc=%08X: <%s>\n", (unsigned)pc, pc?pc:"");
  119.     free(pc);
  120. }
  121.  
  122. static int
  123. compar(l, r)
  124.     char *l, *r;
  125. {
  126.     printf("compar(%s,%s)=%d\n", l, r, strcmp(l, r));
  127.     return strcmp(l, r);
  128. }
  129.  
  130. static char *
  131. savestr(str)
  132.     char    *str;
  133. {
  134.     char    *save;
  135.  
  136.     save = malloc(strlen(str) + 1);
  137.     strcpy(save, str);
  138.     return save;
  139. }
  140.