| 1 |
/* |
| 2 |
* Copyright (C) 2022 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2022 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 |
|
| 23 |
#include <lsp-plug.in/fmt/url.h> |
| 24 |
#include <lsp-plug.in/fmt/bookmarks.h> |
| 25 |
#include <lsp-plug.in/fmt/json/Parser.h> |
| 26 |
#include <lsp-plug.in/fmt/json/Serializer.h> |
| 27 |
#include <lsp-plug.in/io/InFileStream.h> |
| 28 |
#include <lsp-plug.in/io/InSequence.h> |
| 29 |
|
| 30 |
#ifdef PLATFORM_WINDOWS |
| 31 |
#define FS_CHAR_MAIN '\\' |
| 32 |
#define FS_CHAR_ALT '/' |
| 33 |
#else |
| 34 |
#define FS_CHAR_MAIN '/' |
| 35 |
#define FS_CHAR_ALT '\\' |
| 36 |
#endif /* PLATFORM_WINDOWS */ |
| 37 |
|
| 38 |
namespace lsp |
| 39 |
{ |
| 40 |
namespace bookmarks |
| 41 |
{ |
| 42 |
status_t read_json_origin(size_t *origin, json::Parser &p) |
| 43 |
{ |
| 44 |
json::event_t ev; |
| 45 |
|
| 46 |
status_t res = p.read_next(&ev); |
| 47 |
if (res != STATUS_OK) |
| 48 |
return res; |
| 49 |
if (ev.type != json::JE_ARRAY_START) |
| 50 |
return STATUS_CORRUPTED; |
| 51 |
|
| 52 |
while (true) |
| 53 |
{ |
| 54 |
// Check that it is not end of array |
| 55 |
status_t res = p.read_next(&ev); |
| 56 |
if (res != STATUS_OK) |
| 57 |
return res; |
| 58 |
if (ev.type == json::JE_ARRAY_END) |
| 59 |
break; |
| 60 |
else if (ev.type != json::JE_STRING) |
| 61 |
return STATUS_CORRUPTED; |
| 62 |
|
| 63 |
// Analyze string |
| 64 |
if (ev.sValue.equals_ascii("lsp")) |
| 65 |
*origin |= BM_LSP; |
| 66 |
else if (ev.sValue.equals_ascii("gtk2")) |
| 67 |
*origin |= BM_GTK2; |
| 68 |
else if (ev.sValue.equals_ascii("gtk3")) |
| 69 |
*origin |= BM_GTK3; |
| 70 |
else if (ev.sValue.equals_ascii("qt5")) |
| 71 |
*origin |= BM_QT5; |
| 72 |
else if (ev.sValue.equals_ascii("lnk")) |
| 73 |
*origin |= BM_LNK; |
| 74 |
} |
| 75 |
|
| 76 |
return STATUS_OK; |
| 77 |
} |
| 78 |
|
| 79 |
status_t read_json_item(bookmark_t *item, json::Parser &p) |
| 80 |
{ |
| 81 |
json::event_t ev; |
| 82 |
|
| 83 |
while (true) |
| 84 |
{ |
| 85 |
// Check that it is not end of array |
| 86 |
status_t res = p.read_next(&ev); |
| 87 |
if (res != STATUS_OK) |
| 88 |
return res; |
| 89 |
if (ev.type == json::JE_OBJECT_END) |
| 90 |
break; |
| 91 |
else if (ev.type != json::JE_PROPERTY) |
| 92 |
return STATUS_CORRUPTED; |
| 93 |
|
| 94 |
// Read properties |
| 95 |
if (ev.sValue.equals_ascii("path")) |
| 96 |
{ |
| 97 |
if ((res = p.read_string(&item->path)) != STATUS_OK) |
| 98 |
return res; |
| 99 |
} |
| 100 |
else if (ev.sValue.equals_ascii("name")) |
| 101 |
{ |
| 102 |
if ((res = p.read_string(&item->name)) != STATUS_OK) |
| 103 |
return res; |
| 104 |
} |
| 105 |
else if (ev.sValue.equals_ascii("origin")) |
| 106 |
{ |
| 107 |
if ((res = read_json_origin(&item->origin, p)) != STATUS_OK) |
| 108 |
return res; |
| 109 |
} |
| 110 |
else if ((res = p.skip_next()) != STATUS_OK) |
| 111 |
return res; |
| 112 |
} |
| 113 |
|
| 114 |
return STATUS_OK; |
| 115 |
} |
| 116 |
|
| 117 |
status_t read_json_bookmarks(lltl::parray<bookmark_t> *dst, json::Parser &p) |
| 118 |
{ |
| 119 |
json::event_t ev; |
| 120 |
|
| 121 |
status_t res = p.read_next(&ev); |
| 122 |
if (res != STATUS_OK) |
| 123 |
return res; |
| 124 |
if (ev.type != json::JE_ARRAY_START) |
| 125 |
return STATUS_CORRUPTED; |
| 126 |
|
| 127 |
while (true) |
| 128 |
{ |
| 129 |
// Check that it is not end of array |
| 130 |
status_t res = p.read_next(&ev); |
| 131 |
if (res != STATUS_OK) |
| 132 |
return res; |
| 133 |
if (ev.type == json::JE_ARRAY_END) |
| 134 |
break; |
| 135 |
else if (ev.type != json::JE_OBJECT_START) |
| 136 |
return STATUS_CORRUPTED; |
| 137 |
|
| 138 |
// Read bookmark item |
| 139 |
bookmark_t *item = new bookmark_t(); |
| 140 |
item->origin = 0; |
| 141 |
|
| 142 |
res = read_json_item(item, p); |
| 143 |
if (res != STATUS_OK) |
| 144 |
{ |
| 145 |
if (res == STATUS_NULL) |
| 146 |
res = STATUS_CORRUPTED; |
| 147 |
delete item; |
| 148 |
return res; |
| 149 |
} |
| 150 |
else if (!dst->add(item)) |
| 151 |
{ |
| 152 |
delete item; |
| 153 |
return STATUS_NO_MEM; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
return res; |
| 158 |
} |
| 159 |
|
| 160 |
status_t read_bookmarks(lltl::parray<bookmark_t> *dst, json::Parser &p) |
| 161 |
{ |
| 162 |
lltl::parray<bookmark_t> tmp; |
| 163 |
status_t res = read_json_bookmarks(&tmp, p); |
| 164 |
if (res == STATUS_OK) |
| 165 |
res = p.close(); |
| 166 |
|
| 167 |
if (res != STATUS_OK) |
| 168 |
p.close(); |
| 169 |
else |
| 170 |
dst->swap(&tmp); |
| 171 |
destroy_bookmarks(&tmp); |
| 172 |
|
| 173 |
return res; |
| 174 |
} |
| 175 |
|
| 176 |
status_t read_bookmarks(lltl::parray<bookmark_t> *dst, const char *path, const char *charset) |
| 177 |
{ |
| 178 |
if (dst == NULL) |
| 179 |
return STATUS_BAD_ARGUMENTS; |
| 180 |
|
| 181 |
json::Parser p; |
| 182 |
status_t res = p.open(path, json::JSON_VERSION5, charset); |
| 183 |
return (res == STATUS_OK) ? read_bookmarks(dst, p) : res; |
| 184 |
} |
| 185 |
|
| 186 |
status_t read_bookmarks(lltl::parray<bookmark_t> *dst, const LSPString *path, const char *charset) |
| 187 |
{ |
| 188 |
if (dst == NULL) |
| 189 |
return STATUS_BAD_ARGUMENTS; |
| 190 |
|
| 191 |
json::Parser p; |
| 192 |
status_t res = p.open(path, json::JSON_VERSION5, charset); |
| 193 |
return (res == STATUS_OK) ? read_bookmarks(dst, p) : res; |
| 194 |
} |
| 195 |
|
| 196 |
status_t read_bookmarks(lltl::parray<bookmark_t> *dst, const io::Path *path, const char *charset) |
| 197 |
{ |
| 198 |
if (dst == NULL) |
| 199 |
return STATUS_BAD_ARGUMENTS; |
| 200 |
|
| 201 |
json::Parser p; |
| 202 |
status_t res = p.open(path, json::JSON_VERSION5, charset); |
| 203 |
return (res == STATUS_OK) ? read_bookmarks(dst, p) : res; |
| 204 |
} |
| 205 |
|
| 206 |
status_t read_bookmarks(lltl::parray<bookmark_t> *dst, io::IInSequence *in) |
| 207 |
{ |
| 208 |
if (dst == NULL) |
| 209 |
return STATUS_BAD_ARGUMENTS; |
| 210 |
|
| 211 |
json::Parser p; |
| 212 |
lltl::parray<bookmark_t> tmp; |
| 213 |
status_t res = p.wrap(in, json::JSON_VERSION5, WRAP_NONE); |
| 214 |
return (res == STATUS_OK) ? read_bookmarks(dst, p) : res; |
| 215 |
} |
| 216 |
|
| 217 |
void init_settings(json::serial_flags_t *s) |
| 218 |
{ |
| 219 |
s->version = json::JSON_VERSION5; |
| 220 |
s->identifiers = false; |
| 221 |
s->ident = ' '; |
| 222 |
s->padding = 4; |
| 223 |
s->separator = true; |
| 224 |
s->multiline = true; |
| 225 |
} |
| 226 |
|
| 227 |
status_t save_item(const bookmark_t *item, json::Serializer &s) |
| 228 |
{ |
| 229 |
status_t res; |
| 230 |
if ((res = s.start_object()) != STATUS_OK) |
| 231 |
return res; |
| 232 |
{ |
| 233 |
// Path |
| 234 |
if ((res = s.write_property("path")) != STATUS_OK) |
| 235 |
return res; |
| 236 |
if ((res = s.write_string(&item->path)) != STATUS_OK) |
| 237 |
return res; |
| 238 |
|
| 239 |
// Name |
| 240 |
if ((res = s.write_property("name")) != STATUS_OK) |
| 241 |
return res; |
| 242 |
if ((res = s.write_string(&item->name)) != STATUS_OK) |
| 243 |
return res; |
| 244 |
|
| 245 |
// Origin |
| 246 |
if ((res = s.write_property("origin")) != STATUS_OK) |
| 247 |
return res; |
| 248 |
bool ml = s.set_multiline(false); |
| 249 |
if ((res = s.start_array()) != STATUS_OK) |
| 250 |
return res; |
| 251 |
{ |
| 252 |
if ((item->origin & BM_LSP) && ((res = s.write_string("lsp")) != STATUS_OK)) |
| 253 |
return res; |
| 254 |
if ((item->origin & BM_GTK2) && ((res = s.write_string("gtk2")) != STATUS_OK)) |
| 255 |
return res; |
| 256 |
if ((item->origin & BM_GTK3) && ((res = s.write_string("gtk3")) != STATUS_OK)) |
| 257 |
return res; |
| 258 |
if ((item->origin & BM_QT5) && ((res = s.write_string("qt5")) != STATUS_OK)) |
| 259 |
return res; |
| 260 |
if ((item->origin & BM_LNK) && ((res = s.write_string("lnk")) != STATUS_OK)) |
| 261 |
return res; |
| 262 |
} |
| 263 |
if ((res = s.end_array()) != STATUS_OK) |
| 264 |
return res; |
| 265 |
s.set_multiline(ml); |
| 266 |
} |
| 267 |
|
| 268 |
if ((res = s.end_object()) != STATUS_OK) |
| 269 |
return res; |
| 270 |
|
| 271 |
return res; |
| 272 |
} |
| 273 |
|
| 274 |
status_t save_bookmarks(const lltl::parray<bookmark_t> *src, json::Serializer &s) |
| 275 |
{ |
| 276 |
static const char *comment = "\n" |
| 277 |
" * This file contains list of bookmarked directories.\n" |
| 278 |
" * \n" |
| 279 |
" "; |
| 280 |
|
| 281 |
// Write header |
| 282 |
status_t res = s.write_comment(comment); |
| 283 |
if (res == STATUS_OK) |
| 284 |
res = s.writeln(); |
| 285 |
if (res == STATUS_OK) |
| 286 |
res = s.start_array(); |
| 287 |
|
| 288 |
// Serialize body |
| 289 |
if (res == STATUS_OK) |
| 290 |
{ |
| 291 |
for (size_t i=0, n=src->size(); i<n; ++i) |
| 292 |
{ |
| 293 |
const bookmark_t *bm = src->uget(i); |
| 294 |
if ((bm == NULL) || (bm->origin == 0)) // Skip bookmarks with empty origin |
| 295 |
continue; |
| 296 |
if ((res = save_item(bm, s)) != STATUS_OK) |
| 297 |
break; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
if (res == STATUS_OK) |
| 302 |
res = s.end_array(); |
| 303 |
|
| 304 |
// Close serializer |
| 305 |
if (res == STATUS_OK) |
| 306 |
res = s.close(); |
| 307 |
else |
| 308 |
s.close(); |
| 309 |
|
| 310 |
return res; |
| 311 |
} |
| 312 |
|
| 313 |
status_t save_bookmarks(const lltl::parray<bookmark_t> *src, const char *path, const char *charset) |
| 314 |
{ |
| 315 |
json::Serializer s; |
| 316 |
json::serial_flags_t flags; |
| 317 |
init_settings(&flags); |
| 318 |
status_t res = s.open(path, &flags, charset); |
| 319 |
return (res == STATUS_OK) ? save_bookmarks(src, s) : res; |
| 320 |
} |
| 321 |
|
| 322 |
status_t save_bookmarks(const lltl::parray<bookmark_t> *src, const LSPString *path, const char *charset) |
| 323 |
{ |
| 324 |
json::Serializer s; |
| 325 |
json::serial_flags_t flags; |
| 326 |
init_settings(&flags); |
| 327 |
status_t res = s.open(path, &flags, charset); |
| 328 |
return (res == STATUS_OK) ? save_bookmarks(src, s) : res; |
| 329 |
} |
| 330 |
|
| 331 |
status_t save_bookmarks(const lltl::parray<bookmark_t> *src, const io::Path *path, const char *charset) |
| 332 |
{ |
| 333 |
json::Serializer s; |
| 334 |
json::serial_flags_t flags; |
| 335 |
init_settings(&flags); |
| 336 |
status_t res = s.open(path, &flags, charset); |
| 337 |
return (res == STATUS_OK) ? save_bookmarks(src, s) : res; |
| 338 |
} |
| 339 |
|
| 340 |
status_t save_bookmarks(const lltl::parray<bookmark_t> *src, io::IOutSequence *out) |
| 341 |
{ |
| 342 |
json::Serializer s; |
| 343 |
json::serial_flags_t flags; |
| 344 |
init_settings(&flags); |
| 345 |
status_t res = s.wrap(out, &flags, WRAP_NONE); |
| 346 |
return (res == STATUS_OK) ? save_bookmarks(src, s) : res; |
| 347 |
} |
| 348 |
|
| 349 |
} /* namespace bookmarks */ |
| 350 |
} /* namespace lsp */ |
| 351 |
|