| 1 |
/* |
| 2 |
* Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2025 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 12 авг. 2022 г. |
| 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/url.h> |
| 23 |
#include <lsp-plug.in/fmt/bookmarks.h> |
| 24 |
#include <lsp-plug.in/fmt/xml/PushParser.h> |
| 25 |
#include <lsp-plug.in/io/InFileStream.h> |
| 26 |
#include <lsp-plug.in/io/InSequence.h> |
| 27 |
#include <lsp-plug.in/io/OutMemoryStream.h> |
| 28 |
|
| 29 |
#ifdef PLATFORM_WINDOWS |
| 30 |
#define FS_CHAR_MAIN '\\' |
| 31 |
#define FS_CHAR_ALT '/' |
| 32 |
#else |
| 33 |
#define FS_CHAR_MAIN '/' |
| 34 |
#define FS_CHAR_ALT '\\' |
| 35 |
#endif /* PLATFORM_WINDOWS */ |
| 36 |
|
| 37 |
namespace lsp |
| 38 |
{ |
| 39 |
namespace bookmarks |
| 40 |
{ |
| 41 |
// Xbel file format parser |
| 42 |
class XbelParser: public xml::IXMLHandler |
| 43 |
{ |
| 44 |
private: |
| 45 |
lltl::parray<bookmark_t> *vList; |
| 46 |
size_t nOrigin; |
| 47 |
bookmark_t *pCurr; |
| 48 |
bool bTitle; |
| 49 |
LSPString sPath; |
| 50 |
|
| 51 |
protected: |
| 52 |
static void flush_os(LSPString *dst, io::OutMemoryStream *os) |
| 53 |
{ |
| 54 |
if (os->size() <= 0) |
| 55 |
return; |
| 56 |
dst->append_utf8(reinterpret_cast<const char *>(os->data()), os->size()); |
| 57 |
os->clear(); |
| 58 |
} |
| 59 |
|
| 60 |
static int hexdigit(lsp_wchar_t ch) |
| 61 |
{ |
| 62 |
if ((ch >= '0') && (ch <= '9')) |
| 63 |
return ch - '0'; |
| 64 |
if ((ch >= 'A') && (ch <= 'F')) |
| 65 |
return ch - 'A' + 10; |
| 66 |
if ((ch >= 'a') && (ch <= 'f')) |
| 67 |
return ch - 'f' + 10; |
| 68 |
return -1; |
| 69 |
} |
| 70 |
|
| 71 |
static void get_bookmark_name(LSPString *dst, const LSPString *href) |
| 72 |
{ |
| 73 |
ssize_t idx = lsp_max(href->rindex_of(FS_CHAR_MAIN), href->rindex_of(FS_CHAR_ALT)); |
| 74 |
idx = lsp_max(idx + 1, ssize_t(0)); |
| 75 |
|
| 76 |
io::OutMemoryStream os; |
| 77 |
lsp_finally { os.close(); }; |
| 78 |
|
| 79 |
for (ssize_t len = href->length(); idx < len; ++idx) |
| 80 |
{ |
| 81 |
lsp_wchar_t ch = href->at(idx); |
| 82 |
|
| 83 |
// Special case? |
| 84 |
if (ch == '%') |
| 85 |
{ |
| 86 |
int d1 = hexdigit(href->at(idx+1)); |
| 87 |
int d2 = hexdigit(href->at(idx+2)); |
| 88 |
if ((d1 >= 0) && (d2 >= 0)) |
| 89 |
{ |
| 90 |
os.writeb((d1 << 4) | d2); |
| 91 |
idx += 2; |
| 92 |
continue; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
flush_os(dst, &os); |
| 97 |
dst->append(ch); |
| 98 |
} |
| 99 |
|
| 100 |
flush_os(dst, &os); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
public: |
| 105 |
explicit XbelParser(lltl::parray<bookmark_t> *list, size_t origin) |
| 106 |
{ |
| 107 |
vList = list; |
| 108 |
nOrigin = origin; |
| 109 |
pCurr = NULL; |
| 110 |
bTitle = false; |
| 111 |
} |
| 112 |
|
| 113 |
public: |
| 114 |
virtual status_t doctype(const LSPString *doctype, const LSPString *pub, const LSPString *sys) |
| 115 |
{ |
| 116 |
if ((doctype != NULL) && (!doctype->equals_ascii_nocase("xbel"))) |
| 117 |
return STATUS_BAD_FORMAT; |
| 118 |
return STATUS_OK; |
| 119 |
} |
| 120 |
|
| 121 |
virtual status_t start_element(const LSPString *name, const LSPString * const *atts) |
| 122 |
{ |
| 123 |
// Append tag name to path |
| 124 |
if (!sPath.append('/')) |
| 125 |
return STATUS_NO_MEM; |
| 126 |
if (!sPath.append(name)) |
| 127 |
return STATUS_NO_MEM; |
| 128 |
|
| 129 |
// Check path |
| 130 |
if (sPath.equals_ascii("/xbel/bookmark")) |
| 131 |
{ |
| 132 |
LSPString href; |
| 133 |
|
| 134 |
// Scan for 'href' attribute |
| 135 |
for ( ; *atts != NULL; atts += 2) |
| 136 |
if (atts[0]->equals_ascii("href")) |
| 137 |
{ |
| 138 |
if (atts[1]->starts_with_ascii("file://")) |
| 139 |
{ |
| 140 |
if (!href.set(atts[1], 7)) |
| 141 |
return STATUS_NO_MEM; |
| 142 |
} |
| 143 |
break; |
| 144 |
} |
| 145 |
|
| 146 |
// Do we have a reference? |
| 147 |
if (href.length() > 0) |
| 148 |
{ |
| 149 |
// Allocate bookmark descriptor |
| 150 |
bookmark_t *bm = new bookmark_t(); |
| 151 |
if (bm == NULL) |
| 152 |
return STATUS_NO_MEM; |
| 153 |
if (!vList->add(bm)) |
| 154 |
{ |
| 155 |
delete bm; |
| 156 |
return STATUS_NO_MEM; |
| 157 |
} |
| 158 |
|
| 159 |
// Initialize bookmark |
| 160 |
get_bookmark_name(&bm->name, &href); |
| 161 |
bm->origin = BM_LSP | nOrigin; |
| 162 |
bm->path.swap(&href); |
| 163 |
|
| 164 |
// Save pointer to bookmark |
| 165 |
pCurr = bm; |
| 166 |
bTitle = false; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
return STATUS_OK; |
| 171 |
} |
| 172 |
|
| 173 |
virtual status_t characters(const LSPString *text) |
| 174 |
{ |
| 175 |
if (sPath.equals_ascii("/xbel/bookmark/title")) |
| 176 |
{ |
| 177 |
if (pCurr == NULL) |
| 178 |
return STATUS_OK; |
| 179 |
|
| 180 |
bool success = (bTitle) ? pCurr->name.append(text) : pCurr->name.set(text); |
| 181 |
if (!success) |
| 182 |
return STATUS_NO_MEM; |
| 183 |
bTitle = true; |
| 184 |
} |
| 185 |
return STATUS_OK; |
| 186 |
} |
| 187 |
|
| 188 |
virtual status_t end_element(const LSPString *name) |
| 189 |
{ |
| 190 |
// Forget about current bookmark |
| 191 |
if (sPath.equals_ascii("/xbel/bookmark")) |
| 192 |
{ |
| 193 |
pCurr = NULL; |
| 194 |
bTitle = false; |
| 195 |
} |
| 196 |
|
| 197 |
// Reduce path |
| 198 |
ssize_t idx = sPath.rindex_of('/'); |
| 199 |
sPath.set_length((idx >= 0) ? idx : 0); |
| 200 |
return STATUS_OK; |
| 201 |
} |
| 202 |
}; |
| 203 |
|
| 204 |
status_t read_bookmarks_qt5(lltl::parray<bookmark_t> *dst, const char *path, const char *charset) |
| 205 |
{ |
| 206 |
lltl::parray<bookmark_t> tmp; |
| 207 |
status_t res; |
| 208 |
|
| 209 |
xml::PushParser p; |
| 210 |
XbelParser h(&tmp, BM_QT5); |
| 211 |
if ((res = p.parse_file(&h, path, charset)) == STATUS_OK) |
| 212 |
dst->swap(&tmp); |
| 213 |
|
| 214 |
destroy_bookmarks(&tmp); |
| 215 |
return res; |
| 216 |
} |
| 217 |
|
| 218 |
status_t read_bookmarks_qt5(lltl::parray<bookmark_t> *dst, const LSPString *path, const char *charset) |
| 219 |
{ |
| 220 |
lltl::parray<bookmark_t> tmp; |
| 221 |
status_t res; |
| 222 |
|
| 223 |
xml::PushParser p; |
| 224 |
XbelParser h(&tmp, BM_QT5); |
| 225 |
if ((res = p.parse_file(&h, path, charset)) == STATUS_OK) |
| 226 |
dst->swap(&tmp); |
| 227 |
|
| 228 |
destroy_bookmarks(&tmp); |
| 229 |
return res; |
| 230 |
} |
| 231 |
|
| 232 |
status_t read_bookmarks_qt5(lltl::parray<bookmark_t> *dst, const io::Path *path, const char *charset) |
| 233 |
{ |
| 234 |
lltl::parray<bookmark_t> tmp; |
| 235 |
status_t res; |
| 236 |
|
| 237 |
xml::PushParser p; |
| 238 |
XbelParser h(&tmp, BM_QT5); |
| 239 |
if ((res = p.parse_file(&h, path, charset)) == STATUS_OK) |
| 240 |
dst->swap(&tmp); |
| 241 |
|
| 242 |
destroy_bookmarks(&tmp); |
| 243 |
return res; |
| 244 |
} |
| 245 |
|
| 246 |
status_t read_bookmarks_qt5(lltl::parray<bookmark_t> *dst, io::IInSequence *in) |
| 247 |
{ |
| 248 |
lltl::parray<bookmark_t> tmp; |
| 249 |
status_t res; |
| 250 |
|
| 251 |
xml::PushParser p; |
| 252 |
XbelParser h(&tmp, BM_QT5); |
| 253 |
if ((res = p.parse_data(&h, in)) == STATUS_OK) |
| 254 |
dst->swap(&tmp); |
| 255 |
|
| 256 |
destroy_bookmarks(&tmp); |
| 257 |
return res; |
| 258 |
} |
| 259 |
|
| 260 |
} /* namespace bookmarks */ |
| 261 |
} /* namespace lsp */ |