| 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: 14 окт. 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/url.h> |
| 23 |
#include <lsp-plug.in/fmt/bookmarks.h> |
| 24 |
#include <lsp-plug.in/fmt/json/Parser.h> |
| 25 |
#include <lsp-plug.in/fmt/json/Serializer.h> |
| 26 |
#include <lsp-plug.in/io/InFileStream.h> |
| 27 |
#include <lsp-plug.in/io/InSequence.h> |
| 28 |
|
| 29 |
namespace lsp |
| 30 |
{ |
| 31 |
namespace bookmarks |
| 32 |
{ |
| 33 |
void destroy_bookmarks(lltl::parray<bookmark_t> *list) |
| 34 |
{ |
| 35 |
if (list == NULL) |
| 36 |
return; |
| 37 |
|
| 38 |
for (size_t i=0, n=list->size(); i<n; ++i) |
| 39 |
{ |
| 40 |
bookmark_t *ptr = list->uget(i); |
| 41 |
if (ptr != NULL) |
| 42 |
delete ptr; |
| 43 |
} |
| 44 |
list->flush(); |
| 45 |
} |
| 46 |
|
| 47 |
static bool bookmark_exists(const lltl::parray<bookmark_t> *search, const LSPString *path) |
| 48 |
{ |
| 49 |
for (size_t i=0, n=search->size(); i<n; ++i) |
| 50 |
{ |
| 51 |
const bookmark_t *bm = search->uget(i); |
| 52 |
if ((bm != NULL) && (bm->path.equals(path))) |
| 53 |
return true; |
| 54 |
} |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
status_t merge_bookmarks(lltl::parray<bookmark_t> *dst, size_t *changes, const lltl::parray<bookmark_t> *src, bm_origin_t origin) |
| 59 |
{ |
| 60 |
if ((dst == NULL) || (src == NULL) || (origin == 0)) |
| 61 |
return STATUS_BAD_ARGUMENTS; |
| 62 |
|
| 63 |
size_t nc = 0; |
| 64 |
|
| 65 |
// Step 1: check presence of all bookmarks of 'dst' in 'src' |
| 66 |
for (size_t i=0; i<dst->size(); ) |
| 67 |
{ |
| 68 |
bookmark_t *bm = dst->uget(i); |
| 69 |
if (bm == NULL) // Remove all NULL entries |
| 70 |
{ |
| 71 |
size_t sz = dst->size(); |
| 72 |
if (dst->remove(i)) |
| 73 |
++nc; |
| 74 |
else if (dst->size() == sz) |
| 75 |
return STATUS_NO_MEM; |
| 76 |
continue; |
| 77 |
} |
| 78 |
else |
| 79 |
++i; |
| 80 |
|
| 81 |
if (bookmark_exists(src, &bm->path)) |
| 82 |
{ |
| 83 |
if (!(bm->origin & origin)) |
| 84 |
{ |
| 85 |
bm->origin |= origin; |
| 86 |
++nc; |
| 87 |
} |
| 88 |
} |
| 89 |
else if (bm->origin & origin) |
| 90 |
{ |
| 91 |
bm->origin &= ~origin; |
| 92 |
++nc; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
// Step 2: check presence of all bookmarks of 'src' in 'dst' |
| 97 |
for (size_t i=0, n=src->size(); i<n; ++i) |
| 98 |
{ |
| 99 |
const bookmark_t *bm = src->uget(i); |
| 100 |
if ((bm == NULL) || (bookmark_exists(dst, &bm->path))) |
| 101 |
continue; |
| 102 |
|
| 103 |
// Copy bookmark |
| 104 |
bookmark_t *dm = new bookmark_t; |
| 105 |
if (dm == NULL) |
| 106 |
return STATUS_NO_MEM; |
| 107 |
|
| 108 |
if ((!dm->path.set(&bm->path)) || |
| 109 |
(!dm->name.set(&bm->name))) |
| 110 |
{ |
| 111 |
delete dm; |
| 112 |
return STATUS_NO_MEM; |
| 113 |
} |
| 114 |
|
| 115 |
if (!dst->add(dm)) |
| 116 |
{ |
| 117 |
delete dm; |
| 118 |
return STATUS_NO_MEM; |
| 119 |
} |
| 120 |
|
| 121 |
// Mark with origin + LSP flag |
| 122 |
dm->origin = (origin | BM_LSP); |
| 123 |
++nc; |
| 124 |
} |
| 125 |
|
| 126 |
// Step 3: remove all bookmarks with empty flags |
| 127 |
for (size_t i=0; i<dst->size();) |
| 128 |
{ |
| 129 |
bookmark_t *bm = dst->uget(i); |
| 130 |
if (bm->origin == 0) |
| 131 |
{ |
| 132 |
if (!dst->remove(i)) |
| 133 |
return STATUS_NO_MEM; |
| 134 |
delete bm; |
| 135 |
++nc; |
| 136 |
continue; |
| 137 |
} |
| 138 |
else |
| 139 |
++i; |
| 140 |
} |
| 141 |
|
| 142 |
// Store number of changes |
| 143 |
if (changes != NULL) |
| 144 |
*changes += nc; |
| 145 |
|
| 146 |
return STATUS_OK; |
| 147 |
} |
| 148 |
|
| 149 |
} /* namespace bookmarks */ |
| 150 |
} /* namespace lsp */ |
| 151 |
|
| 152 |
|
| 153 |
|