| 1 |
/* |
| 2 |
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2020 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 30 авг. 2019 г. |
| 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/java/defs.h> |
| 23 |
#include <lsp-plug.in/fmt/java/RawArray.h> |
| 24 |
|
| 25 |
namespace lsp |
| 26 |
{ |
| 27 |
namespace java |
| 28 |
{ |
| 29 |
const char *RawArray::CLASS_NAME = "["; |
| 30 |
|
| 31 |
RawArray::RawArray(const char *xc): Object(xc) |
| 32 |
{ |
| 33 |
nLength = 0; |
| 34 |
enItemType = JFT_UNKNOWN; |
| 35 |
pData = NULL; |
| 36 |
} |
| 37 |
|
| 38 |
RawArray::~RawArray() |
| 39 |
{ |
| 40 |
if (pData != NULL) |
| 41 |
{ |
| 42 |
::free(pData); |
| 43 |
pData = NULL; |
| 44 |
} |
| 45 |
|
| 46 |
nLength = 0; |
| 47 |
} |
| 48 |
|
| 49 |
status_t RawArray::allocate(size_t items) |
| 50 |
{ |
| 51 |
LSPString tmp; |
| 52 |
if (!tmp.set_utf8(class_name())) |
| 53 |
return STATUS_NO_MEM; |
| 54 |
if (tmp.length() < 2) |
| 55 |
return STATUS_BAD_TYPE; |
| 56 |
if (tmp.first() != PTC_ARRAY) |
| 57 |
return STATUS_BAD_TYPE; |
| 58 |
|
| 59 |
// Decode item type |
| 60 |
enItemType = decode_primitive_type(tmp.char_at(1)); |
| 61 |
if (enItemType == JFT_UNKNOWN) |
| 62 |
return STATUS_BAD_TYPE; |
| 63 |
|
| 64 |
// Get item size |
| 65 |
nLength = items; |
| 66 |
|
| 67 |
// Patch item type |
| 68 |
const char *prim = primitive_type_name(enItemType); |
| 69 |
if (prim == NULL) |
| 70 |
{ |
| 71 |
if (!tmp.remove(0, 2)) |
| 72 |
return STATUS_BAD_TYPE; |
| 73 |
if (tmp.last() == ';') |
| 74 |
{ |
| 75 |
if (!tmp.remove_last()) |
| 76 |
return STATUS_BAD_TYPE; |
| 77 |
} |
| 78 |
} |
| 79 |
else if (!tmp.set_ascii(prim)) |
| 80 |
return STATUS_NO_MEM; |
| 81 |
sItemType.swap(&tmp); |
| 82 |
|
| 83 |
// Allocate empty data |
| 84 |
pData = ::calloc(items, size_of(enItemType)); |
| 85 |
if (pData == NULL) |
| 86 |
return STATUS_NO_MEM; |
| 87 |
|
| 88 |
return STATUS_OK; |
| 89 |
} |
| 90 |
|
| 91 |
bool RawArray::instanceof(const char *name) const |
| 92 |
{ |
| 93 |
if (name == CLASS_NAME) |
| 94 |
return true; |
| 95 |
if (!::strcmp(name, CLASS_NAME)) |
| 96 |
return true; |
| 97 |
return Object::instanceof(name); |
| 98 |
} |
| 99 |
|
| 100 |
status_t RawArray::to_string_padded(LSPString *dst, size_t pad) |
| 101 |
{ |
| 102 |
if (!dst->fmt_append_utf8("*%p = new %s[%d] ", this, sItemType.get_utf8(), nLength)) |
| 103 |
return STATUS_NO_MEM; |
| 104 |
if (nLength <= 0) |
| 105 |
return (dst->append_ascii("{ }\n")) ? STATUS_OK : STATUS_NO_MEM; |
| 106 |
|
| 107 |
prim_ptr_t ptr; |
| 108 |
ptr.p_void = pData; |
| 109 |
if ((enItemType == JFT_ARRAY) || (enItemType == JFT_OBJECT)) |
| 110 |
{ |
| 111 |
if (!dst->append_ascii("{\n")) |
| 112 |
return STATUS_NO_MEM; |
| 113 |
++pad; |
| 114 |
for (size_t i=0; i<nLength; ++i) |
| 115 |
{ |
| 116 |
if (!pad_string(dst, pad)) |
| 117 |
return STATUS_NO_MEM; |
| 118 |
Object *obj = *(ptr.p_object++); |
| 119 |
bool res = (obj != NULL) ? |
| 120 |
(obj->to_string_padded(dst, pad) == STATUS_OK) : |
| 121 |
dst->append_ascii("null\n"); |
| 122 |
if (! res) |
| 123 |
return STATUS_NO_MEM; |
| 124 |
} |
| 125 |
--pad; |
| 126 |
if (!pad_string(dst, pad)) |
| 127 |
return STATUS_NO_MEM; |
| 128 |
if (!dst->append_ascii("}\n")) |
| 129 |
return STATUS_NO_MEM; |
| 130 |
} |
| 131 |
else |
| 132 |
{ |
| 133 |
if (!dst->append_ascii("{ ")) |
| 134 |
return STATUS_NO_MEM; |
| 135 |
|
| 136 |
for (size_t i=0; i<nLength; ++i) |
| 137 |
{ |
| 138 |
bool res = (i > 0) ? dst->append_ascii(", ") : true; |
| 139 |
if (! res) |
| 140 |
return STATUS_NO_MEM; |
| 141 |
|
| 142 |
switch (enItemType) |
| 143 |
{ |
| 144 |
case JFT_BYTE: res = dst->fmt_append_utf8("%d", *(ptr.p_byte++)); break; |
| 145 |
case JFT_DOUBLE: res = dst->fmt_append_utf8("%f", *(ptr.p_double++)); break; |
| 146 |
case JFT_FLOAT: res = dst->fmt_append_utf8("%f", *(ptr.p_float++)); break; |
| 147 |
case JFT_INTEGER: res = dst->fmt_append_utf8("%d", int(*(ptr.p_int++))); break; |
| 148 |
case JFT_LONG: res = dst->fmt_append_utf8("%lld", (long long)(*(ptr.p_long++))); break; |
| 149 |
case JFT_SHORT: res = dst->fmt_append_utf8("%d", int(*(ptr.p_short++))); break; |
| 150 |
case JFT_BOOL: res = dst->fmt_append_utf8("%s", (*(ptr.p_bool++)) ? "true" : "false"); break; |
| 151 |
case JFT_CHAR: |
| 152 |
res = dst->append('\''); |
| 153 |
if (res) |
| 154 |
res = dst->append(lsp_wchar_t(*(ptr.p_char++))); |
| 155 |
if (res) |
| 156 |
res = dst->append('\''); |
| 157 |
break; |
| 158 |
default: |
| 159 |
return STATUS_CORRUPTED; |
| 160 |
} |
| 161 |
if (! res) |
| 162 |
return STATUS_NO_MEM; |
| 163 |
} |
| 164 |
if (!dst->append_ascii(" }\n")) |
| 165 |
return STATUS_NO_MEM; |
| 166 |
} |
| 167 |
|
| 168 |
return STATUS_OK; |
| 169 |
} |
| 170 |
|
| 171 |
} /* namespace java */ |
| 172 |
} /* namespace lsp */ |