| 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: 14 февр. 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/Object.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 Object::valid() const |
| 32 |
{ |
| 33 |
return is_object(); |
| 34 |
} |
| 35 |
|
| 36 |
status_t Object::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_OBJECT; |
| 44 |
node->pObject = new lltl::pphash<LSPString, node_t>(); |
| 45 |
if (node->pObject == 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 |
Object *Object::allocate() |
| 58 |
{ |
| 59 |
Object *res = new Object(); |
| 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 |
Object Object::build() |
| 70 |
{ |
| 71 |
Object res; |
| 72 |
res.create(); |
| 73 |
return res; |
| 74 |
} |
| 75 |
|
| 76 |
size_t Object::size() const |
| 77 |
{ |
| 78 |
return (is_object()) ? pNode->pObject->size() : 0; |
| 79 |
} |
| 80 |
|
| 81 |
size_t Object::capacity() const |
| 82 |
{ |
| 83 |
return (is_object()) ? pNode->pObject->capacity() : 0; |
| 84 |
} |
| 85 |
|
| 86 |
Node Object::get(const char *field) |
| 87 |
{ |
| 88 |
LSPString tmp; |
| 89 |
if (!tmp.set_utf8(field)) |
| 90 |
return Node(); |
| 91 |
return get(&tmp); |
| 92 |
} |
| 93 |
|
| 94 |
Node Object::get(const LSPString *field) |
| 95 |
{ |
| 96 |
if (!is_object()) |
| 97 |
return Node(); |
| 98 |
|
| 99 |
node_t *ref = pNode->pObject->get(field); |
| 100 |
return Node(ref); |
| 101 |
} |
| 102 |
|
| 103 |
bool Object::contains(const char *field) const |
| 104 |
{ |
| 105 |
LSPString tmp; |
| 106 |
if (!tmp.set_utf8(field)) |
| 107 |
return false; |
| 108 |
return contains(&tmp); |
| 109 |
} |
| 110 |
|
| 111 |
bool Object::contains(const LSPString *field) const |
| 112 |
{ |
| 113 |
return (is_object()) ? pNode->pObject->contains(field) : false; |
| 114 |
} |
| 115 |
|
| 116 |
status_t Object::remove(const char *field) |
| 117 |
{ |
| 118 |
LSPString tmp; |
| 119 |
if (!tmp.set_utf8(field)) |
| 120 |
return STATUS_NO_MEM; |
| 121 |
return remove(&tmp); |
| 122 |
} |
| 123 |
|
| 124 |
status_t Object::remove(const LSPString *field) |
| 125 |
{ |
| 126 |
if (!is_object()) |
| 127 |
return STATUS_NOT_FOUND; |
| 128 |
|
| 129 |
node_t *ref = NULL; |
| 130 |
if (!pNode->pObject->remove(field, &ref)) |
| 131 |
return STATUS_NOT_FOUND; |
| 132 |
|
| 133 |
release_ref(ref); |
| 134 |
return STATUS_OK; |
| 135 |
} |
| 136 |
|
| 137 |
status_t Object::set(const char *field, const Node *node) |
| 138 |
{ |
| 139 |
LSPString tmp; |
| 140 |
if (!tmp.set_utf8(field)) |
| 141 |
return STATUS_NO_MEM; |
| 142 |
return set(&tmp, node); |
| 143 |
} |
| 144 |
|
| 145 |
status_t Object::set(const LSPString *field, const Node *node) |
| 146 |
{ |
| 147 |
if (!is_object()) |
| 148 |
return STATUS_NOT_FOUND; |
| 149 |
|
| 150 |
Node null; |
| 151 |
if (node == NULL) |
| 152 |
node = &null; |
| 153 |
|
| 154 |
node_t *nref = node->make_ref(); |
| 155 |
if (nref == NULL) |
| 156 |
return STATUS_NO_MEM; |
| 157 |
|
| 158 |
node_t *oref = NULL; |
| 159 |
if (!pNode->pObject->put(field, nref, &oref)) |
| 160 |
{ |
| 161 |
release_ref(nref); |
| 162 |
return STATUS_NO_MEM; |
| 163 |
} |
| 164 |
|
| 165 |
release_ref(oref); |
| 166 |
return STATUS_OK; |
| 167 |
} |
| 168 |
|
| 169 |
status_t Object::set(const char *field, const Node &node) |
| 170 |
{ |
| 171 |
return set(field, &node); |
| 172 |
} |
| 173 |
|
| 174 |
status_t Object::set(const LSPString *field, const Node &node) |
| 175 |
{ |
| 176 |
return set(field, &node); |
| 177 |
} |
| 178 |
|
| 179 |
status_t Object::fields(lltl::parray<LSPString> *list) |
| 180 |
{ |
| 181 |
if (!is_object()) |
| 182 |
return STATUS_BAD_TYPE; |
| 183 |
if (!pNode->pObject->keys(list)) |
| 184 |
return STATUS_NO_MEM; |
| 185 |
return STATUS_OK; |
| 186 |
} |
| 187 |
|
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
|