summaryrefslogtreecommitdiff
path: root/src/bookmarks.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2020-08-14 15:07:49 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2020-08-14 15:07:49 +0300
commitf8f382ed14151d3c293a9ee754f0a66f513fb7d3 (patch)
tree68b4ef771460378267b02c0499693751f4fb26ee /src/bookmarks.c
parent459f73f2623f66e2887aae6feff2ec0fab185b2c (diff)
Bookmarks: Store in a hash with unique IDs
Diffstat (limited to 'src/bookmarks.c')
-rw-r--r--src/bookmarks.c66
1 files changed, 39 insertions, 27 deletions
diff --git a/src/bookmarks.c b/src/bookmarks.c
index b023e90a..96a2b6a0 100644
--- a/src/bookmarks.c
+++ b/src/bookmarks.c
@@ -2,6 +2,7 @@
2 2
3#include <the_Foundation/file.h> 3#include <the_Foundation/file.h>
4#include <the_Foundation/path.h> 4#include <the_Foundation/path.h>
5#include <the_Foundation/hash.h>
5 6
6void init_Bookmark(iBookmark *d) { 7void init_Bookmark(iBookmark *d) {
7 init_String(&d->url); 8 init_String(&d->url);
@@ -27,25 +28,33 @@ static int cmpTimeDescending_Bookmark_(const iBookmark **a, const iBookmark **b)
27static const char *fileName_Bookmarks_ = "bookmarks.txt"; 28static const char *fileName_Bookmarks_ = "bookmarks.txt";
28 29
29struct Impl_Bookmarks { 30struct Impl_Bookmarks {
30 iArray bookmarks; 31 int idEnum;
32 iHash bookmarks;
31}; 33};
32 34
33iDefineTypeConstruction(Bookmarks) 35iDefineTypeConstruction(Bookmarks)
34 36
35void init_Bookmarks(iBookmarks *d) { 37void init_Bookmarks(iBookmarks *d) {
36 init_Array(&d->bookmarks, sizeof(iBookmark)); 38 d->idEnum = 0;
39 init_Hash(&d->bookmarks);
37} 40}
38 41
39void deinit_Bookmarks(iBookmarks *d) { 42void deinit_Bookmarks(iBookmarks *d) {
40 clear_Bookmarks(d); 43 clear_Bookmarks(d);
41 deinit_Array(&d->bookmarks); 44 deinit_Hash(&d->bookmarks);
42} 45}
43 46
44void clear_Bookmarks(iBookmarks *d) { 47void clear_Bookmarks(iBookmarks *d) {
45 iForEach(Array, i, &d->bookmarks) { 48 iForEach(Hash, i, &d->bookmarks) {
46 deinit_Bookmark(i.value); 49 delete_Bookmark((iBookmark *) i.value);
47 } 50 }
48 clear_Array(&d->bookmarks); 51 clear_Hash(&d->bookmarks);
52 d->idEnum = 0;
53}
54
55static void insert_Bookmarks_(iBookmarks *d, iBookmark *bookmark) {
56 bookmark->node.key = ++d->idEnum;
57 insert_Hash(&d->bookmarks, &bookmark->node);
49} 58}
50 59
51void load_Bookmarks(iBookmarks *d, const char *dirPath) { 60void load_Bookmarks(iBookmarks *d, const char *dirPath) {
@@ -62,19 +71,18 @@ void load_Bookmarks(iBookmarks *d, const char *dirPath) {
62 continue; 71 continue;
63 } 72 }
64 } 73 }
65 iBookmark bm; 74 iBookmark *bm = new_Bookmark();
66 init_Bookmark(&bm); 75 bm->icon = strtoul(line.start, NULL, 16);
67 bm.icon = strtoul(line.start, NULL, 16);
68 line.start += 9; 76 line.start += 9;
69 char *endPos; 77 char *endPos;
70 initSeconds_Time(&bm.when, strtod(line.start, &endPos)); 78 initSeconds_Time(&bm->when, strtod(line.start, &endPos));
71 line.start = skipSpace_CStr(endPos); 79 line.start = skipSpace_CStr(endPos);
72 setRange_String(&bm.url, line); 80 setRange_String(&bm->url, line);
73 nextSplit_Rangecc(&src, "\n", &line); 81 nextSplit_Rangecc(&src, "\n", &line);
74 setRange_String(&bm.title, line); 82 setRange_String(&bm->title, line);
75 nextSplit_Rangecc(&src, "\n", &line); 83 nextSplit_Rangecc(&src, "\n", &line);
76 setRange_String(&bm.tags, line); 84 setRange_String(&bm->tags, line);
77 pushBack_Array(&d->bookmarks, &bm); 85 insert_Bookmarks_(d, bm);
78 } 86 }
79 } 87 }
80 iRelease(f); 88 iRelease(f);
@@ -84,8 +92,8 @@ void save_Bookmarks(const iBookmarks *d, const char *dirPath) {
84 iFile *f = newCStr_File(concatPath_CStr(dirPath, fileName_Bookmarks_)); 92 iFile *f = newCStr_File(concatPath_CStr(dirPath, fileName_Bookmarks_));
85 if (open_File(f, writeOnly_FileMode | text_FileMode)) { 93 if (open_File(f, writeOnly_FileMode | text_FileMode)) {
86 iString *str = collectNew_String(); 94 iString *str = collectNew_String();
87 iConstForEach(Array, i, &d->bookmarks) { 95 iConstForEach(Hash, i, &d->bookmarks) {
88 const iBookmark *bm = i.value; 96 const iBookmark *bm = (const iBookmark *) i.value;
89 format_String(str, 97 format_String(str,
90 "%08x %lf %s\n%s\n%s\n", 98 "%08x %lf %s\n%s\n%s\n",
91 bm->icon, 99 bm->icon,
@@ -101,22 +109,26 @@ void save_Bookmarks(const iBookmarks *d, const char *dirPath) {
101 109
102void add_Bookmarks(iBookmarks *d, const iString *url, const iString *title, const iString *tags, 110void add_Bookmarks(iBookmarks *d, const iString *url, const iString *title, const iString *tags,
103 iChar icon) { 111 iChar icon) {
104 iBookmark bm; 112 iBookmark *bm = new_Bookmark();
105 init_Bookmark(&bm); 113 set_String(&bm->url, url);
106 set_String(&bm.url, url); 114 set_String(&bm->title, title);
107 set_String(&bm.title, title); 115 set_String(&bm->tags, tags);
108 set_String(&bm.tags, tags); 116 bm->icon = icon;
109 bm.icon = icon; 117 initCurrent_Time(&bm->when);
110 initCurrent_Time(&bm.when); 118 insert_Bookmarks_(d, bm);
111 pushBack_Array(&d->bookmarks, &bm); 119}
120
121void remove_Bookmarks(iBookmarks *d, uint32_t id) {
122 delete_Bookmark((iBookmark *) remove_Hash(&d->bookmarks, id));
112} 123}
113 124
114const iPtrArray *list_Bookmarks(const iBookmarks *d, iBookmarksFilterFunc filter, 125const iPtrArray *list_Bookmarks(const iBookmarks *d, iBookmarksFilterFunc filter,
115 iBookmarksCompareFunc cmp) { 126 iBookmarksCompareFunc cmp) {
116 iPtrArray *list = collectNew_PtrArray(); 127 iPtrArray *list = collectNew_PtrArray();
117 iConstForEach(Array, i, &d->bookmarks) { 128 iConstForEach(Hash, i, &d->bookmarks) {
118 if (!filter || filter(i.value)) { 129 const iBookmark *bm = (const iBookmark *) i.value;
119 pushBack_PtrArray(list, i.value); 130 if (!filter || filter(bm)) {
131 pushBack_PtrArray(list, bm);
120 } 132 }
121 } 133 }
122 if (!cmp) cmp = cmpTimeDescending_Bookmark_; 134 if (!cmp) cmp = cmpTimeDescending_Bookmark_;