summaryrefslogtreecommitdiff
path: root/src/bookmarks.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2020-08-14 08:29:50 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2020-08-14 08:29:50 +0300
commit9981de5692c358411304a03c767408cf6ebeb770 (patch)
treebe6c077b5573d612ed47c3641b9bcf8670397009 /src/bookmarks.c
parent395615d0aff7499e905eb5e768a2a46395bb8dc2 (diff)
Added Bookmarks
Diffstat (limited to 'src/bookmarks.c')
-rw-r--r--src/bookmarks.c109
1 files changed, 109 insertions, 0 deletions
diff --git a/src/bookmarks.c b/src/bookmarks.c
new file mode 100644
index 00000000..babfc323
--- /dev/null
+++ b/src/bookmarks.c
@@ -0,0 +1,109 @@
1#include "bookmarks.h"
2
3#include <the_Foundation/file.h>
4#include <the_Foundation/path.h>
5
6void init_Bookmark(iBookmark *d) {
7 init_String(&d->url);
8 init_String(&d->title);
9 init_String(&d->tags);
10 iZap(d->when);
11}
12
13void deinit_Bookmark(iBookmark *d) {
14 deinit_String(&d->tags);
15 deinit_String(&d->title);
16 deinit_String(&d->url);
17}
18
19iDefineTypeConstruction(Bookmark)
20
21static int cmpTimeDescending_Bookmark_(const iBookmark **a, const iBookmark **b) {
22 return iCmp(seconds_Time(&(*b)->when), seconds_Time(&(*a)->when));
23}
24
25/*----------------------------------------------------------------------------------------------*/
26
27static const char *fileName_Bookmarks_ = "bookmarks.txt";
28
29struct Impl_Bookmarks {
30 iArray bookmarks;
31};
32
33iDefineTypeConstruction(Bookmarks)
34
35void init_Bookmarks(iBookmarks *d) {
36 init_Array(&d->bookmarks, sizeof(iBookmark));
37}
38
39void deinit_Bookmarks(iBookmarks *d) {
40 clear_Bookmarks(d);
41 deinit_Array(&d->bookmarks);
42}
43
44void clear_Bookmarks(iBookmarks *d) {
45 iForEach(Array, i, &d->bookmarks) {
46 deinit_Bookmark(i.value);
47 }
48 clear_Array(&d->bookmarks);
49}
50
51void load_Bookmarks(iBookmarks *d, const char *dirPath) {
52 clear_Bookmarks(d);
53 iFile *f = newCStr_File(concatPath_CStr(dirPath, fileName_Bookmarks_));
54 if (open_File(f, readOnly_FileMode | text_FileMode)) {
55 const iRangecc src = range_Block(collect_Block(readAll_File(f)));
56 iRangecc line = iNullRange;
57 while (nextSplit_Rangecc(&src, "\n", &line)) {
58 iBookmark bm;
59 init_Bookmark(&bm);
60 char *endPos;
61 initSeconds_Time(&bm.when, strtod(line.start, &endPos));
62 line.start = skipSpace_CStr(endPos);
63 setRange_String(&bm.url, line);
64 nextSplit_Rangecc(&src, "\n", &line);
65 setRange_String(&bm.title, line);
66 nextSplit_Rangecc(&src, "\n", &line);
67 setRange_String(&bm.tags, line);
68 pushBack_Array(&d->bookmarks, &bm);
69 }
70 }
71 iRelease(f);
72}
73
74void save_Bookmarks(const iBookmarks *d, const char *dirPath) {
75 iFile *f = newCStr_File(concatPath_CStr(dirPath, fileName_Bookmarks_));
76 if (open_File(f, writeOnly_FileMode | text_FileMode)) {
77 iString *str = collectNew_String();
78 iConstForEach(Array, i, &d->bookmarks) {
79 const iBookmark *bm = i.value;
80 format_String(str, "%lf %s\%s\n%s\n", seconds_Time(&bm->when), cstr_String(&bm->url),
81 cstr_String(&bm->title), cstr_String(&bm->tags));
82 writeData_File(f, cstr_String(str), size_String(str));
83 }
84 }
85 iRelease(f);
86}
87
88void add_Bookmarks(iBookmarks *d, const iString *url, const iString *title, const iString *tags) {
89 iBookmark bm;
90 init_Bookmark(&bm);
91 set_String(&bm.url, url);
92 set_String(&bm.title, title);
93 set_String(&bm.tags, tags);
94 initCurrent_Time(&bm.when);
95 pushBack_Array(&d->bookmarks, &bm);
96}
97
98const iPtrArray *list_Bookmarks(const iBookmarks *d, iBookmarksFilterFunc filter,
99 iBookmarksCompareFunc cmp) {
100 iPtrArray *list = collectNew_PtrArray();
101 iConstForEach(Array, i, &d->bookmarks) {
102 if (!filter || filter(i.value)) {
103 pushBack_PtrArray(list, i.value);
104 }
105 }
106 if (!cmp) cmp = cmpTimeDescending_Bookmark_;
107 sort_Array(list, (int (*)(const void *, const void *)) cmp);
108 return list;
109}