| 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/io/InFileStream.h> |
| 26 |
#include <lsp-plug.in/io/InSequence.h> |
| 27 |
|
| 28 |
#ifdef PLATFORM_WINDOWS |
| 29 |
#define FS_CHAR_MAIN '\\' |
| 30 |
#define FS_CHAR_ALT '/' |
| 31 |
#else |
| 32 |
#define FS_CHAR_MAIN '/' |
| 33 |
#define FS_CHAR_ALT '\\' |
| 34 |
#endif /* PLATFORM_WINDOWS */ |
| 35 |
|
| 36 |
namespace lsp |
| 37 |
{ |
| 38 |
namespace bookmarks |
| 39 |
{ |
| 40 |
status_t read_bookmarks_gtk(lltl::parray<bookmark_t> *dst, io::IInSequence *in, size_t origin) |
| 41 |
{ |
| 42 |
LSPString tmp; |
| 43 |
status_t res; |
| 44 |
ssize_t split; |
| 45 |
lltl::parray<bookmark_t> vtmp; |
| 46 |
lsp_finally { destroy_bookmarks(&vtmp); }; |
| 47 |
|
| 48 |
while (true) |
| 49 |
{ |
| 50 |
// Read line |
| 51 |
res = in->read_line(&tmp, true); |
| 52 |
if (res != STATUS_OK) |
| 53 |
{ |
| 54 |
if (res == STATUS_EOF) |
| 55 |
break; |
| 56 |
destroy_bookmarks(&vtmp); |
| 57 |
return res; |
| 58 |
} |
| 59 |
|
| 60 |
// Analyze line |
| 61 |
if (!tmp.starts_with_ascii_nocase("file://")) |
| 62 |
continue; |
| 63 |
|
| 64 |
|
| 65 |
// Create bookmark |
| 66 |
bookmark_t *bm = new bookmark_t(); |
| 67 |
if (bm == NULL) |
| 68 |
return STATUS_NO_MEM; |
| 69 |
bm->origin = origin; |
| 70 |
|
| 71 |
// Is there alias? |
| 72 |
split = tmp.index_of(' '); |
| 73 |
|
| 74 |
// Decode bookmark respectively to the presence of alias |
| 75 |
if ((res = url::decode(&bm->path, &tmp, 7, (split < 0) ? tmp.length() : split)) != STATUS_OK) |
| 76 |
{ |
| 77 |
delete bm; |
| 78 |
return STATUS_NO_MEM; |
| 79 |
} |
| 80 |
|
| 81 |
// Obtain the last name in path |
| 82 |
if (split < 0) |
| 83 |
{ |
| 84 |
split = lsp_max(bm->path.rindex_of(FS_CHAR_MAIN), split = bm->path.rindex_of(FS_CHAR_ALT)); |
| 85 |
if (split < 0) |
| 86 |
split = -1; |
| 87 |
if (!bm->name.set(&bm->path, split + 1)) |
| 88 |
{ |
| 89 |
delete bm; |
| 90 |
return STATUS_NO_MEM; |
| 91 |
} |
| 92 |
} |
| 93 |
else if (!bm->name.set(&tmp, split + 1)) |
| 94 |
{ |
| 95 |
delete bm; |
| 96 |
return STATUS_NO_MEM; |
| 97 |
} |
| 98 |
|
| 99 |
// Add to list |
| 100 |
if (!vtmp.add(bm)) |
| 101 |
{ |
| 102 |
delete bm; |
| 103 |
return STATUS_NO_MEM; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
dst->swap(&vtmp); |
| 108 |
|
| 109 |
return STATUS_OK; |
| 110 |
} |
| 111 |
|
| 112 |
status_t read_bookmarks_gtk(lltl::parray<bookmark_t> *dst, const char *path, const char *charset, size_t origin) |
| 113 |
{ |
| 114 |
if ((path == NULL) || (dst == NULL)) |
| 115 |
return STATUS_BAD_ARGUMENTS; |
| 116 |
|
| 117 |
io::InSequence is; |
| 118 |
status_t res; |
| 119 |
lltl::parray<bookmark_t> tmp; |
| 120 |
lsp_finally { destroy_bookmarks(&tmp); }; |
| 121 |
|
| 122 |
if ((res = is.open(path, charset)) == STATUS_OK) |
| 123 |
{ |
| 124 |
res = read_bookmarks_gtk(&tmp, &is, origin); |
| 125 |
if (res == STATUS_OK) |
| 126 |
res = is.close(); |
| 127 |
else |
| 128 |
is.close(); |
| 129 |
} |
| 130 |
|
| 131 |
if (res == STATUS_OK) |
| 132 |
dst->swap(&tmp); |
| 133 |
|
| 134 |
return res; |
| 135 |
} |
| 136 |
|
| 137 |
status_t read_bookmarks_gtk(lltl::parray<bookmark_t> *dst, const LSPString *path, const char *charset, size_t origin) |
| 138 |
{ |
| 139 |
if ((path == NULL) || (dst == NULL)) |
| 140 |
return STATUS_BAD_ARGUMENTS; |
| 141 |
|
| 142 |
io::InSequence is; |
| 143 |
status_t res; |
| 144 |
lltl::parray<bookmark_t> tmp; |
| 145 |
lsp_finally { destroy_bookmarks(&tmp); }; |
| 146 |
|
| 147 |
if ((res = is.open(path, charset)) == STATUS_OK) |
| 148 |
{ |
| 149 |
res = read_bookmarks_gtk(&tmp, &is, origin); |
| 150 |
if (res == STATUS_OK) |
| 151 |
res = is.close(); |
| 152 |
else |
| 153 |
is.close(); |
| 154 |
} |
| 155 |
|
| 156 |
if (res == STATUS_OK) |
| 157 |
dst->swap(&tmp); |
| 158 |
|
| 159 |
return res; |
| 160 |
} |
| 161 |
|
| 162 |
status_t read_bookmarks_gtk(lltl::parray<bookmark_t> *dst, const io::Path *path, const char *charset, size_t origin) |
| 163 |
{ |
| 164 |
if ((path == NULL) || (dst == NULL)) |
| 165 |
return STATUS_BAD_ARGUMENTS; |
| 166 |
|
| 167 |
io::InSequence is; |
| 168 |
status_t res; |
| 169 |
lltl::parray<bookmark_t> tmp; |
| 170 |
lsp_finally { destroy_bookmarks(&tmp); }; |
| 171 |
|
| 172 |
if ((res = is.open(path, charset)) == STATUS_OK) |
| 173 |
{ |
| 174 |
res = read_bookmarks_gtk(&tmp, &is, origin); |
| 175 |
if (res == STATUS_OK) |
| 176 |
res = is.close(); |
| 177 |
else |
| 178 |
is.close(); |
| 179 |
} |
| 180 |
|
| 181 |
if (res == STATUS_OK) |
| 182 |
dst->swap(&tmp); |
| 183 |
|
| 184 |
return res; |
| 185 |
} |
| 186 |
|
| 187 |
status_t read_bookmarks_gtk2(lltl::parray<bookmark_t> *dst, const char *path, const char *charset) |
| 188 |
{ |
| 189 |
return read_bookmarks_gtk(dst, path, charset, BM_GTK2); |
| 190 |
} |
| 191 |
|
| 192 |
status_t read_bookmarks_gtk2(lltl::parray<bookmark_t> *dst, const LSPString *path, const char *charset) |
| 193 |
{ |
| 194 |
return read_bookmarks_gtk(dst, path, charset, BM_GTK2); |
| 195 |
} |
| 196 |
|
| 197 |
status_t read_bookmarks_gtk2(lltl::parray<bookmark_t> *dst, const io::Path *path, const char *charset) |
| 198 |
{ |
| 199 |
return read_bookmarks_gtk(dst, path, charset, BM_GTK2); |
| 200 |
} |
| 201 |
|
| 202 |
status_t read_bookmarks_gtk2(lltl::parray<bookmark_t> *dst, io::IInSequence *in) |
| 203 |
{ |
| 204 |
return read_bookmarks_gtk(dst, in, BM_GTK2); |
| 205 |
} |
| 206 |
|
| 207 |
status_t read_bookmarks_gtk3(lltl::parray<bookmark_t> *dst, const char *path, const char *charset) |
| 208 |
{ |
| 209 |
return read_bookmarks_gtk(dst, path, charset, BM_GTK3); |
| 210 |
} |
| 211 |
|
| 212 |
status_t read_bookmarks_gtk3(lltl::parray<bookmark_t> *dst, const LSPString *path, const char *charset) |
| 213 |
{ |
| 214 |
return read_bookmarks_gtk(dst, path, charset, BM_GTK3); |
| 215 |
} |
| 216 |
|
| 217 |
status_t read_bookmarks_gtk3(lltl::parray<bookmark_t> *dst, const io::Path *path, const char *charset) |
| 218 |
{ |
| 219 |
return read_bookmarks_gtk(dst, path, charset, BM_GTK3); |
| 220 |
} |
| 221 |
|
| 222 |
status_t read_bookmarks_gtk3(lltl::parray<bookmark_t> *dst, io::IInSequence *in) |
| 223 |
{ |
| 224 |
return read_bookmarks_gtk(dst, in, BM_GTK3); |
| 225 |
} |
| 226 |
|
| 227 |
} /* namespace bookmarks */ |
| 228 |
} /* namespace lsp */ |
| 229 |
|
| 230 |
|
| 231 |
|