summaryrefslogtreecommitdiff
path: root/src/visited.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2020-09-02 11:25:03 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2020-09-02 11:25:03 +0300
commit1e548eac8c63922315f89c96436b622615c488c7 (patch)
treed2c36fdaaa0bcc42cbc34bb804de5a7201dba2b1 /src/visited.c
parent8ed2faadc4ca2497fc3b2e9d2eae6d40921de918 (diff)
parente2b5ea14d25dbbb62a1e827803e67c30df79c6a1 (diff)
Merge branch 'master' of skyjake.fi:skyjake/lagrange
Diffstat (limited to 'src/visited.c')
-rw-r--r--src/visited.c57
1 files changed, 56 insertions, 1 deletions
diff --git a/src/visited.c b/src/visited.c
index af976be5..912a6318 100644
--- a/src/visited.c
+++ b/src/visited.c
@@ -1,8 +1,31 @@
1/* Copyright 2020 Jaakko Keränen <jaakko.keranen@iki.fi>
2
3Redistribution and use in source and binary forms, with or without
4modification, are permitted provided that the following conditions are met:
5
61. Redistributions of source code must retain the above copyright notice, this
7 list of conditions and the following disclaimer.
82. Redistributions in binary form must reproduce the above copyright notice,
9 this list of conditions and the following disclaimer in the documentation
10 and/or other materials provided with the distribution.
11
12THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
13ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
16ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
19ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
22
1#include "visited.h" 23#include "visited.h"
2#include "app.h" 24#include "app.h"
3 25
4#include <the_Foundation/file.h> 26#include <the_Foundation/file.h>
5#include <the_Foundation/path.h> 27#include <the_Foundation/path.h>
28#include <the_Foundation/ptrarray.h>
6#include <the_Foundation/sortedarray.h> 29#include <the_Foundation/sortedarray.h>
7 30
8static const size_t maxAgeVisited_Visited_ = 3600 * 24 * 30; /* one month */ 31static const size_t maxAgeVisited_Visited_ = 3600 * 24 * 30; /* one month */
@@ -73,7 +96,7 @@ void load_Visited(iVisited *d, const char *dirPath) {
73 iRangecc line = iNullRange; 96 iRangecc line = iNullRange;
74 iTime now; 97 iTime now;
75 initCurrent_Time(&now); 98 initCurrent_Time(&now);
76 while (nextSplit_Rangecc(&src, "\n", &line)) { 99 while (nextSplit_Rangecc(src, "\n", &line)) {
77 int y, m, D, H, M, S; 100 int y, m, D, H, M, S;
78 sscanf(line.start, "%04d-%02d-%02dT%02d:%02d:%02d ", &y, &m, &D, &H, &M, &S); 101 sscanf(line.start, "%04d-%02d-%02dT%02d:%02d:%02d ", &y, &m, &D, &H, &M, &S);
79 if (!y) break; 102 if (!y) break;
@@ -99,6 +122,16 @@ void clear_Visited(iVisited *d) {
99 clear_SortedArray(&d->visited); 122 clear_SortedArray(&d->visited);
100} 123}
101 124
125static size_t find_Visited_(const iVisited *d, const iString *url) {
126 iVisitedUrl visit;
127 init_VisitedUrl(&visit);
128 set_String(&visit.url, url);
129 size_t pos = iInvalidPos;
130 locate_SortedArray(&d->visited, &visit, &pos);
131 deinit_VisitedUrl(&visit);
132 return pos;
133}
134
102void visitUrl_Visited(iVisited *d, const iString *url) { 135void visitUrl_Visited(iVisited *d, const iString *url) {
103 iVisitedUrl visit; 136 iVisitedUrl visit;
104 init_VisitedUrl(&visit); 137 init_VisitedUrl(&visit);
@@ -115,6 +148,14 @@ void visitUrl_Visited(iVisited *d, const iString *url) {
115 insert_SortedArray(&d->visited, &visit); 148 insert_SortedArray(&d->visited, &visit);
116} 149}
117 150
151void removeUrl_Visited(iVisited *d, const iString *url) {
152 size_t pos = find_Visited_(d, url);
153 if (pos != iInvalidPos) {
154 deinit_VisitedUrl(at_SortedArray(&d->visited, pos));
155 remove_Array(&d->visited.values, pos);
156 }
157}
158
118iTime urlVisitTime_Visited(const iVisited *d, const iString *url) { 159iTime urlVisitTime_Visited(const iVisited *d, const iString *url) {
119 iVisitedUrl item; 160 iVisitedUrl item;
120 size_t pos; 161 size_t pos;
@@ -126,3 +167,17 @@ iTime urlVisitTime_Visited(const iVisited *d, const iString *url) {
126 deinit_String(&item.url); 167 deinit_String(&item.url);
127 return item.when; 168 return item.when;
128} 169}
170
171static int cmpWhenDescending_VisitedUrlPtr_(const void *a, const void *b) {
172 const iVisitedUrl *s = *(const void **) a, *t = *(const void **) b;
173 return -cmp_Time(&s->when, &t->when);
174}
175
176const iArray *list_Visited(const iVisited *d, size_t count) {
177 iPtrArray *urls = collectNew_PtrArray();
178 iConstForEach(Array, i, &d->visited.values) {
179 pushBack_PtrArray(urls, i.value);
180 }
181 sort_Array(urls, cmpWhenDescending_VisitedUrlPtr_);
182 return urls;
183}