4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license from the top-level
9 * OPENSOLARIS.LICENSE or <http://opensource.org/licenses/CDDL-1.0>.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each file
14 * and include the License file from the top-level OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Developed at Lawrence Livermore National Laboratory (LLNL-CODE-403049).
24 * Copyright (C) 2013-2014 Lawrence Livermore National Security, LLC.
33 #include <sys/sysmacros.h>
34 #include "zed_strings.h"
38 avl_node_t *iteratorp;
41 struct zed_strings_node {
46 typedef struct zed_strings_node zed_strings_node_t;
49 * Compare zed_strings_node_t nodes [x1] and [x2].
50 * As required for the AVL tree, return -1 for <, 0 for ==, and +1 for >.
53 _zed_strings_node_compare(const void *x1, const void *x2)
62 s1 = ((const zed_strings_node_t *) x1)->string;
64 s2 = ((const zed_strings_node_t *) x2)->string;
78 * Return a new string container, or NULL on error.
81 zed_strings_create(void)
85 zsp = malloc(sizeof (*zsp));
89 memset(zsp, 0, sizeof (*zsp));
90 avl_create(&zsp->tree, _zed_strings_node_compare,
91 sizeof (zed_strings_node_t), offsetof(zed_strings_node_t, node));
93 zsp->iteratorp = NULL;
98 * Destroy the string container [zsp] and all strings within.
101 zed_strings_destroy(zed_strings_t *zsp)
104 zed_strings_node_t *np;
110 while ((np = avl_destroy_nodes(&zsp->tree, &cookie)))
113 avl_destroy(&zsp->tree);
118 * Add a copy of the string [s] to the container [zsp].
119 * Return 0 on success, or -1 on error.
121 * FIXME: Handle dup strings.
124 zed_strings_add(zed_strings_t *zsp, const char *s)
127 zed_strings_node_t *np;
133 len = sizeof (zed_strings_node_t) + strlen(s) + 1;
139 assert((char *) np->string + strlen(s) < (char *) np + len);
140 (void) strcpy(np->string, s);
141 avl_add(&zsp->tree, np);
146 * Return the first string in container [zsp].
147 * Return NULL if there are no strings, or on error.
148 * This can be called multiple times to re-traverse [zsp].
149 * XXX: Not thread-safe.
152 zed_strings_first(zed_strings_t *zsp)
158 zsp->iteratorp = avl_first(&zsp->tree);
162 return (((zed_strings_node_t *) zsp->iteratorp)->string);
167 * Return the next string in container [zsp].
168 * Return NULL after the last string, or on error.
169 * This must be called after zed_strings_first().
170 * XXX: Not thread-safe.
173 zed_strings_next(zed_strings_t *zsp)
182 zsp->iteratorp = AVL_NEXT(&zsp->tree, zsp->iteratorp);
186 return (((zed_strings_node_t *)zsp->iteratorp)->string);
190 * Return the number of strings in container [zsp], or -1 on error.
193 zed_strings_count(zed_strings_t *zsp)
199 return (avl_numnodes(&zsp->tree));