]> git.sur5r.net Git - openldap/blob - libraries/libavl/testavl.c
ignore SIGPIPE
[openldap] / libraries / libavl / testavl.c
1 /* testavl.c - Test Tim Howes AVL code */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #include <ac/string.h>
9 #include <sys/types.h>
10
11 #include "avl.h"
12
13 static void ravl_print LDAP_P(( Avlnode *root, int depth ));
14 static void myprint LDAP_P(( Avlnode *root ));
15
16 int
17 main( int argc, char **argv )
18 {
19         Avlnode *tree = NULLAVL;
20         char    command[ 10 ];
21         char    name[ 80 ];
22         char    *p;
23
24         printf( "> " );
25         while ( fgets( command, sizeof( command ), stdin ) != NULL ) {
26                 switch( *command ) {
27                 case 'n':       /* new tree */
28                         ( void ) avl_free( tree, free );
29                         tree = NULLAVL;
30                         break;
31                 case 'p':       /* print */
32                         ( void ) myprint( tree );
33                         break;
34                 case 't':       /* traverse with first, next */
35                         printf( "***\n" );
36                         for ( p = (char * ) avl_getfirst( tree );
37                             p != NULL; p = (char *) avl_getnext( /* tree, p */ ) )
38                                 printf( "%s\n", p );
39                         printf( "***\n" );
40                         break;
41                 case 'f':       /* find */
42                         printf( "data? " );
43                         if ( fgets( name, sizeof( name ), stdin ) == NULL )
44                                 exit( 0 );
45                         name[ strlen( name ) - 1 ] = '\0';
46                         if ( (p = (char *) avl_find( tree, name, strcmp ))
47                             == NULL )
48                                 printf( "Not found.\n\n" );
49                         else
50                                 printf( "%s\n\n", p );
51                         break;
52                 case 'i':       /* insert */
53                         printf( "data? " );
54                         if ( fgets( name, sizeof( name ), stdin ) == NULL )
55                                 exit( 0 );
56                         name[ strlen( name ) - 1 ] = '\0';
57                         if ( avl_insert( &tree, strdup( name ), strcmp, 
58                             avl_dup_error ) != 0 )
59                                 printf( "\nNot inserted!\n" );
60                         break;
61                 case 'd':       /* delete */
62                         printf( "data? " );
63                         if ( fgets( name, sizeof( name ), stdin ) == NULL )
64                                 exit( 0 );
65                         name[ strlen( name ) - 1 ] = '\0';
66                         if ( avl_delete( &tree, name, strcmp ) == NULL )
67                                 printf( "\nNot found!\n" );
68                         break;
69                 case 'q':       /* quit */
70                         exit( 0 );
71                         break;
72                 case '\n':
73                         break;
74                 default:
75                         printf("Commands: insert, delete, print, new, quit\n");
76                 }
77
78                 printf( "> " );
79         }
80         /* NOTREACHED */
81 }
82
83 static void ravl_print( Avlnode *root, int depth )
84 {
85         int     i;
86
87         if ( root == 0 )
88                 return;
89
90         ravl_print( root->avl_right, depth+1 );
91
92         for ( i = 0; i < depth; i++ )
93                 printf( "   " );
94         printf( "%s %d\n", root->avl_data, root->avl_bf );
95
96         ravl_print( root->avl_left, depth+1 );
97 }
98
99 static void myprint( Avlnode *root )
100 {
101         printf( "********\n" );
102
103         if ( root == 0 )
104                 printf( "\tNULL\n" );
105         else
106                 ravl_print( root, 0 );
107
108         printf( "********\n" );
109 }