| 1 | /*
|
|---|
| 2 | * Unix SMB/CIFS implementation.
|
|---|
| 3 | * Generic Abstract Data Types
|
|---|
| 4 | * Copyright (C) Gerald Carter 2002.
|
|---|
| 5 | *
|
|---|
| 6 | * This program is free software; you can redistribute it and/or modify
|
|---|
| 7 | * it under the terms of the GNU General Public License as published by
|
|---|
| 8 | * the Free Software Foundation; either version 3 of the License, or
|
|---|
| 9 | * (at your option) any later version.
|
|---|
| 10 | *
|
|---|
| 11 | * This program is distributed in the hope that it will be useful,
|
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 14 | * GNU General Public License for more details.
|
|---|
| 15 | *
|
|---|
| 16 | * You should have received a copy of the GNU General Public License
|
|---|
| 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|---|
| 18 | */
|
|---|
| 19 |
|
|---|
| 20 | #include "includes.h"
|
|---|
| 21 | #include "adt_tree.h"
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 | /**************************************************************************
|
|---|
| 25 | *************************************************************************/
|
|---|
| 26 |
|
|---|
| 27 | static bool trim_tree_keypath( char *path, char **base, char **new_path )
|
|---|
| 28 | {
|
|---|
| 29 | char *p;
|
|---|
| 30 |
|
|---|
| 31 | *new_path = *base = NULL;
|
|---|
| 32 |
|
|---|
| 33 | if ( !path )
|
|---|
| 34 | return False;
|
|---|
| 35 |
|
|---|
| 36 | *base = path;
|
|---|
| 37 |
|
|---|
| 38 | p = strchr( path, '/' );
|
|---|
| 39 |
|
|---|
| 40 | if ( p ) {
|
|---|
| 41 | *p = '\0';
|
|---|
| 42 | *new_path = p+1;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | return True;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | /**************************************************************************
|
|---|
| 50 | Initialize the tree's root. The cmp_fn is a callback function used
|
|---|
| 51 | for comparision of two children
|
|---|
| 52 | *************************************************************************/
|
|---|
| 53 |
|
|---|
| 54 | SORTED_TREE* pathtree_init( void *data_p, int (cmp_fn)(void*, void*) )
|
|---|
| 55 | {
|
|---|
| 56 | SORTED_TREE *tree = NULL;
|
|---|
| 57 |
|
|---|
| 58 | if ( !(tree = TALLOC_ZERO_P(NULL, SORTED_TREE)) )
|
|---|
| 59 | return NULL;
|
|---|
| 60 |
|
|---|
| 61 | tree->compare = cmp_fn;
|
|---|
| 62 |
|
|---|
| 63 | if ( !(tree->root = TALLOC_ZERO_P(tree, TREE_NODE)) ) {
|
|---|
| 64 | TALLOC_FREE( tree );
|
|---|
| 65 | return NULL;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | tree->root->data_p = data_p;
|
|---|
| 69 |
|
|---|
| 70 | return tree;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 | /**************************************************************************
|
|---|
| 75 | Find the next child given a key string
|
|---|
| 76 | *************************************************************************/
|
|---|
| 77 |
|
|---|
| 78 | static TREE_NODE* pathtree_birth_child( TREE_NODE *node, char* key )
|
|---|
| 79 | {
|
|---|
| 80 | TREE_NODE *infant = NULL;
|
|---|
| 81 | TREE_NODE **siblings;
|
|---|
| 82 | int i;
|
|---|
| 83 |
|
|---|
| 84 | if ( !(infant = TALLOC_ZERO_P( node, TREE_NODE)) )
|
|---|
| 85 | return NULL;
|
|---|
| 86 |
|
|---|
| 87 | infant->key = talloc_strdup( infant, key );
|
|---|
| 88 | infant->parent = node;
|
|---|
| 89 |
|
|---|
| 90 | siblings = TALLOC_REALLOC_ARRAY( node, node->children, TREE_NODE *, node->num_children+1 );
|
|---|
| 91 |
|
|---|
| 92 | if ( siblings )
|
|---|
| 93 | node->children = siblings;
|
|---|
| 94 |
|
|---|
| 95 | node->num_children++;
|
|---|
| 96 |
|
|---|
| 97 | /* first child */
|
|---|
| 98 |
|
|---|
| 99 | if ( node->num_children == 1 ) {
|
|---|
| 100 | DEBUG(11,("pathtree_birth_child: First child of node [%s]! [%s]\n",
|
|---|
| 101 | node->key ? node->key : "NULL", infant->key ));
|
|---|
| 102 | node->children[0] = infant;
|
|---|
| 103 | }
|
|---|
| 104 | else
|
|---|
| 105 | {
|
|---|
| 106 | /*
|
|---|
| 107 | * multiple siblings .... (at least 2 children)
|
|---|
| 108 | *
|
|---|
|
|---|