| 1 |
/* |
| 2 |
* Copyright (C) 2026 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2026 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 22 янв. 2026 г. |
| 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/xpm/Extension.h> |
| 23 |
#include <lsp-plug.in/common/alloc.h> |
| 24 |
#include <lsp-plug.in/stdlib/string.h> |
| 25 |
|
| 26 |
namespace lsp |
| 27 |
{ |
| 28 |
namespace xpm |
| 29 |
{ |
| 30 |
Extension::Extension() noexcept |
| 31 |
{ |
| 32 |
sName = NULL; |
| 33 |
} |
| 34 |
|
| 35 |
Extension::Extension(const char * name) |
| 36 |
{ |
| 37 |
sName = (name != NULL) ? strdup(name) : NULL; |
| 38 |
} |
| 39 |
|
| 40 |
Extension::Extension(const char * name, size_t len) |
| 41 |
{ |
| 42 |
sName = (name != NULL) ? strmemdup(name, len) : NULL; |
| 43 |
} |
| 44 |
|
| 45 |
bool Extension::copy(lltl::parray<char> & dst, const lltl::parray<char> & src) |
| 46 |
{ |
| 47 |
if (!dst.reserve(src.size())) |
| 48 |
return false; |
| 49 |
|
| 50 |
for (size_t i=0; i<src.size(); ++i) |
| 51 |
{ |
| 52 |
const char *sc = src.uget(i); |
| 53 |
if (sc == NULL) |
| 54 |
return false; |
| 55 |
char *cc = strdup(sc); |
| 56 |
if (cc == NULL) |
| 57 |
return false; |
| 58 |
if (!dst.add(cc)) |
| 59 |
{ |
| 60 |
free(cc); |
| 61 |
return false; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
void Extension::destroy(lltl::parray<char> & src) |
| 69 |
{ |
| 70 |
for (size_t i=0; i<src.size(); ++i) |
| 71 |
{ |
| 72 |
char *sc = src.uget(i); |
| 73 |
if (sc != NULL) |
| 74 |
free(sc); |
| 75 |
} |
| 76 |
src.flush(); |
| 77 |
} |
| 78 |
|
| 79 |
Extension::Extension(const Extension & src) |
| 80 |
{ |
| 81 |
sName = (src.sName != NULL) ? strdup(src.sName) : NULL; |
| 82 |
copy(vData, src.vData); |
| 83 |
} |
| 84 |
|
| 85 |
Extension::Extension(Extension && src) noexcept |
| 86 |
{ |
| 87 |
sName = lsp::exchange(src.sName, static_cast<char *>(NULL)); |
| 88 |
|
| 89 |
vData.swap(src.vData); |
| 90 |
destroy(src.vData); |
| 91 |
} |
| 92 |
|
| 93 |
Extension::~Extension() |
| 94 |
{ |
| 95 |
if (sName != NULL) |
| 96 |
{ |
| 97 |
free(sName); |
| 98 |
sName = NULL; |
| 99 |
} |
| 100 |
destroy(vData); |
| 101 |
} |
| 102 |
|
| 103 |
bool Extension::set_name(const char *name) |
| 104 |
{ |
| 105 |
if (name == NULL) |
| 106 |
{ |
| 107 |
clear_name(); |
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
char *str = strdup(name); |
| 112 |
if (str == NULL) |
| 113 |
return false; |
| 114 |
|
| 115 |
if (sName != NULL) |
| 116 |
free(sName); |
| 117 |
sName = str; |
| 118 |
|
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
bool Extension::set_name(const char *name, size_t len) |
| 123 |
{ |
| 124 |
if (name == NULL) |
| 125 |
{ |
| 126 |
clear_name(); |
| 127 |
return true; |
| 128 |
} |
| 129 |
|
| 130 |
char *str = strmemdup(name, len); |
| 131 |
if (str == NULL) |
| 132 |
return false; |
| 133 |
|
| 134 |
if (sName != NULL) |
| 135 |
free(sName); |
| 136 |
sName = str; |
| 137 |
|
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
void Extension::clear_name() |
| 142 |
{ |
| 143 |
if (sName != NULL) |
| 144 |
{ |
| 145 |
sName = NULL; |
| 146 |
free(sName); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
bool Extension::add_row(const char *row) |
| 151 |
{ |
| 152 |
if (row == NULL) |
| 153 |
return false; |
| 154 |
|
| 155 |
char *str = strdup(row); |
| 156 |
if (str == NULL) |
| 157 |
return false; |
| 158 |
|
| 159 |
if (vData.add(str)) |
| 160 |
return true; |
| 161 |
|
| 162 |
free(str); |
| 163 |
return false; |
| 164 |
} |
| 165 |
|
| 166 |
bool Extension::add_row(const char *row, size_t length) |
| 167 |
{ |
| 168 |
if (row == NULL) |
| 169 |
return false; |
| 170 |
|
| 171 |
char *str = strmemdup(row, length); |
| 172 |
if (str == NULL) |
| 173 |
return false; |
| 174 |
|
| 175 |
if (vData.add(str)) |
| 176 |
return true; |
| 177 |
|
| 178 |
free(str); |
| 179 |
return false; |
| 180 |
} |
| 181 |
|
| 182 |
bool Extension::set(const Extension & src) |
| 183 |
{ |
| 184 |
if (&src == this) |
| 185 |
return true; |
| 186 |
|
| 187 |
Extension tmp; |
| 188 |
if (!tmp.set_name(src.sName)) |
| 189 |
return false; |
| 190 |
if (!copy(tmp.vData, src.vData)) |
| 191 |
return false; |
| 192 |
|
| 193 |
swap(tmp); |
| 194 |
return true; |
| 195 |
} |
| 196 |
|
| 197 |
bool Extension::has_name(const char *name) const noexcept |
| 198 |
{ |
| 199 |
if (name == sName) // Handle if NULL has been passed |
| 200 |
return true; |
| 201 |
if (sName == NULL) |
| 202 |
return false; |
| 203 |
|
| 204 |
return strcmp(sName, name) == 0; |
| 205 |
} |
| 206 |
|
| 207 |
Extension & Extension::operator = (const Extension & src) |
| 208 |
{ |
| 209 |
set(src); |
| 210 |
return *this; |
| 211 |
} |
| 212 |
|
| 213 |
Extension & Extension::operator = (Extension && src) noexcept |
| 214 |
{ |
| 215 |
Extension tmp(lsp::move(src)); |
| 216 |
swap(tmp); |
| 217 |
return *this; |
| 218 |
} |
| 219 |
|
| 220 |
void Extension::swap(Extension & src) noexcept |
| 221 |
{ |
| 222 |
lsp::swap(sName, src.sName); |
| 223 |
vData.swap(src.vData); |
| 224 |
} |
| 225 |
|
| 226 |
void Extension::swap(Extension * src) noexcept |
| 227 |
{ |
| 228 |
lsp::swap(sName, src->sName); |
| 229 |
vData.swap(src->vData); |
| 230 |
} |
| 231 |
|
| 232 |
} /* namespace xpm */ |
| 233 |
} /* namespace lsp */ |
| 234 |
|