1 /*-
2 * Copyright (c) 2014 Baptiste Daroussin <bapt@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <assert.h>
28 #include <ucl.h>
29 #include "pkg.h"
30 #include "private/pkg.h"
31
32 char *
pkg_object_dump(const pkg_object * o)33 pkg_object_dump(const pkg_object *o)
34 {
35 if (o == NULL)
36 return xstrdup("");
37
38 return (ucl_object_emit(o, UCL_EMIT_CONFIG));
39 }
40
41 void
pkg_object_free(pkg_object * o)42 pkg_object_free(pkg_object *o)
43 {
44 ucl_object_unref(o);
45 }
46
47 const char *
pkg_object_key(const pkg_object * o)48 pkg_object_key(const pkg_object *o)
49 {
50 if (o == NULL)
51 return (NULL);
52
53 return (ucl_object_key(o));
54 }
55
56 const pkg_object *
pkg_object_iterate(const pkg_object * o,pkg_iter * it)57 pkg_object_iterate(const pkg_object *o, pkg_iter *it)
58 {
59 if (o == NULL)
60 return (NULL);
61
62 return (ucl_iterate_object(o, it, true));
63 }
64
65 pkg_object_t
pkg_object_type(const pkg_object * o)66 pkg_object_type(const pkg_object *o)
67 {
68
69 if (o == NULL)
70 return (PKG_NULL);
71
72 switch (o->type) {
73 case UCL_OBJECT:
74 return (PKG_OBJECT);
75 case UCL_BOOLEAN:
76 return (PKG_BOOL);
77 case UCL_STRING:
78 return (PKG_STRING);
79 case UCL_INT:
80 return (PKG_INT);
81 case UCL_ARRAY:
82 return (PKG_ARRAY);
83 default:
84 return (PKG_NULL);
85 };
86
87 }
88
89 bool
pkg_object_bool(const pkg_object * o)90 pkg_object_bool(const pkg_object *o)
91 {
92 if (o == NULL || o->type != UCL_BOOLEAN)
93 return (false);
94
95 return (ucl_object_toboolean(o));
96 }
97
98 const char *
pkg_object_string(const pkg_object * o)99 pkg_object_string(const pkg_object *o)
100 {
101 const char *ret;
102
103 if (o == NULL)
104 return (NULL);
105
106 ret = ucl_object_tostring_forced(o);
107
108 if (ret && *ret == '\0')
109 return (NULL);
110 return (ret);
111 }
112
113 int64_t
pkg_object_int(const pkg_object * o)114 pkg_object_int(const pkg_object *o)
115 {
116 if (o == NULL || o->type != UCL_INT)
117 return (0);
118
119 return (ucl_object_toint(o));
120 }
121
122 unsigned
pkg_object_count(const pkg_object * o)123 pkg_object_count(const pkg_object *o)
124 {
125 return (UCL_COUNT(o));
126 }
127
128 const pkg_object *
pkg_object_find(const pkg_object * o,const char * key)129 pkg_object_find(const pkg_object *o, const char *key)
130 {
131 if (o == NULL)
132 return (NULL);
133
134 return (ucl_object_find_key(o, key));
135 }
136