Debian Codegraph @

modules/lsp-runtime-lib/src/main/fmt/json/dom/Array.cpp

raw
1 /*
2 * Copyright (C) 2021 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2021 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 13 февр. 2021 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/fmt/json/dom/Array.h>
23 #include <lsp-plug.in/io/InStringSequence.h>
24 #include <lsp-plug.in/expr/Tokenizer.h>
25 #include <lsp-plug.in/expr/types.h>
26
27 namespace lsp
28 {
29 namespace json
30 {
31 bool Array::valid() const
32 {
33 return is_array();
34 }
35
36 status_t Array::create()
37 {
38 node_t *node = new node_t();
39 if (node == NULL)
40 return STATUS_NO_MEM;
41
42 node->refs = 1;
43 node->type = JN_ARRAY;
44 node->pArray = new lltl::parray<node_t>();
45 if (node->pArray == NULL)
46 {
47 delete node;
48 return STATUS_NO_MEM;
49 }
50
51 release_ref(pNode);
52 pNode = node;
53
54 return STATUS_OK;
55 }
56
57 Array *Array::allocate()
58 {
59 Array *res = new Array();
60 if (res == NULL)
61 return NULL;
62 else if (res->create() == STATUS_OK)
63 return res;
64
65 delete res;
66 return NULL;
67 }
68
69 Array Array::build()
70 {
71 Array res;
72 res.create();
73 return res;
74 }
75
76 size_t Array::size() const
77 {
78 return (is_array()) ? pNode->pArray->size() : 0;
79 }
80
81 size_t Array::capacity() const
82 {
83 return (is_array()) ? pNode->pArray->capacity() : 0;
84 }
85
86 Node Array::get(size_t index)
87 {
88 if (!is_array())
89 return Node();
90
91 node_t *node = pNode->pArray->get(index);
92 return Node(node);
93 }
94
95 status_t Array::add(const Node *node)
96 {
97 if (!is_array())
98 return STATUS_BAD_TYPE;
99
100 Node null;
101 if (node == NULL)
102 node = &null;
103 node_t *ref = node->make_ref();
104 if (ref == NULL)
105 return STATUS_NO_MEM;
106 if (pNode->pArray->add(ref))
107 return STATUS_OK;
108
109 release_ref(ref);
110 return STATUS_NO_MEM;
111 }
112
113 status_t Array::add(const Node &node)
114 {
115 return add(&node);
116 }
117
118 status_t Array::append(const Node *node)
119 {
120 if (!is_array())
121 return STATUS_BAD_TYPE;
122
123 Node null;
124 if (node == NULL)
125 node = &null;
126 node_t *ref = node->make_ref();
127 if (ref == NULL)
128 return STATUS_NO_MEM;
129 if (pNode->pArray->append(ref))
130 return STATUS_OK;
131
132 release_ref(ref);
133 return STATUS_NO_MEM;
134 }
135
136 status_t Array::append(const Node &node)
137 {
138 return append(&node);
139 }
140
141 status_t Array::prepend(const Node *node)
142 {
143 if (!is_array())
144 return STATUS_BAD_TYPE;
145
146 Node null;
147 if (node == NULL)
148 node = &null;
149 node_t *ref = node->make_ref();
150 if (ref == NULL)
151 return STATUS_NO_MEM;
152 if (pNode->pArray->prepend(ref))
153 return STATUS_OK;
154
155 release_ref(ref);
156 return STATUS_NO_MEM;
157 }
158
159 status_t Array::prepend(const Node &node)
160 {
161 return prepend(&node);
162 }
163
164 status_t Array::insert(size_t index, const Node *node)
165 {
166 if (!is_array())
167 return STATUS_BAD_TYPE;
168
169 Node null;
170 if (node == NULL)
171 node = &null;
172 node_t *ref = node->make_ref();
173 if (ref == NULL)
174 return STATUS_NO_MEM;
175 if (pNode->pArray->insert(index, ref))
176 return STATUS_OK;
177
178 release_ref(ref);
179 return STATUS_NO_MEM;
180 }
181
182 status_t Array::insert(size_t index, const Node &node)
183 {
184 return insert(index, &node);
185 }
186
187 status_t Array::remove(size_t index)
188 {
189 if (!is_array())
190 return STATUS_BAD_TYPE;
191
192 node_t *ref = pNode->pArray->remove(index);
193 if (ref == NULL)
194 return STATUS_NOT_FOUND;
195
196 release_ref(ref);
197 return STATUS_OK;
198 }
199
200 status_t Array::remove_n(size_t index, size_t count)
201 {
202 if (!is_array())
203 return STATUS_BAD_TYPE;
204
205 lltl::parray<node_t> rm;
206 if (!pNode->pArray->remove_n(index, count, &rm))
207 return STATUS_NO_MEM;
208
209 for (size_t i=0, n=rm.size(); i<n; ++i)
210 {
211 node_t *ref = rm.uget(i);
212 release_ref(ref);
213 }
214
215 return STATUS_OK;
216 }
217 }
218 }
219
220