OSDN Git Service

Slight build improvements
[mingw/pexports.git] / str_tree.c
1 #include <string.h>
2 #include <stdlib.h>
3
4 #include "str_tree.h"
5
6 static str_tree *
7 new_leaf(const char *s, void *extra)
8 {
9   str_tree *leaf;
10   leaf = (str_tree *) malloc(sizeof(str_tree));
11   if (!leaf)
12     return NULL;
13   leaf->s = strdup(s);
14   if (!leaf->s)
15     {
16       free(leaf);
17       return NULL;
18     }
19   leaf->extra = extra;
20   leaf->left = leaf->right = NULL;
21   return leaf;
22 }
23
24 str_tree *
25 str_tree_add(str_tree **root, const char *s, void *extra)
26 {
27   if (!*root)
28     return (*root = new_leaf(s, extra));
29   else if (strcmp(s, (*root)->s) < 0)
30     return str_tree_add(&(*root)->left, s, extra);
31   else
32     return str_tree_add(&(*root)->right, s, extra);
33 }
34
35 str_tree *
36 str_tree_find(str_tree *node, const char *s)
37 {
38   if (node == NULL)
39     return NULL;
40   if (strcmp(s, node->s) == 0)
41     return node;
42   else if (strcmp(s, node->s) < 0)
43     return str_tree_find(node->left, s);
44   else
45     return str_tree_find(node->right, s);
46 }