Add to git.
[c2lib.git] / tree.c
1 /* A tree class.
2  * - By Richard W.M. Jones <rich@annexia.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  * $Id: tree.c,v 1.2 2002/10/08 15:10:28 rich Exp $
19  */
20
21 #include "config.h"
22
23 #include <stdlib.h>
24
25 #ifdef HAVE_STRING_H
26 #include <string.h>
27 #endif
28
29 #include <pool.h>
30 #include <vector.h>
31 #include <pstring.h>
32
33 #include "tree.h"
34
35 tree
36 _tree_new (pool pool, size_t size)
37 {
38   tree t = (tree) new_vector (pool, tree);
39
40   t = prealloc (pool, t, sizeof (*t) + size);
41   t->size = size;
42
43   return t;
44 }
45
46 extern tree
47 copy_tree (pool pool, tree t)
48 {
49   tree nt = _tree_new (pool, t->size);
50   int i;
51
52   /* Copy the node data. */
53   memcpy (nt->data, t->data, t->size);
54
55   /* Copy each subnode, recursively. */
56   for (i = 0; i < tree_size (t); ++i)
57     {
58       tree st, nst;
59
60       tree_get (t, i, st);
61       nst = copy_tree (pool, st);
62       tree_push_back (nt, nst);
63     }
64
65   return nt;
66 }
67
68 void
69 _tree_get_data (tree t, void *ptr)
70 {
71   if (ptr) memcpy (ptr, t->data, t->size);
72 }
73
74 void
75 _tree_set_data (tree t, const void *ptr)
76 {
77   if (ptr) memcpy (t->data, ptr, t->size);
78 }