-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy patharticle.c
More file actions
296 lines (254 loc) · 6.36 KB
/
article.c
File metadata and controls
296 lines (254 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* $Id$ */
/*
* Copyright (c) 2014, 2017, 2019 Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <expat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "extern.h"
/*
* All sort types can have the order overriden on an article-specific
* basis by the SORT_FIRST or SORT_LAST override being applied.
* Return non-zero if we do this; zero to fall through to our sort.
*/
static int
cmpoverride(const struct article *s1, const struct article *s2)
{
if (s1->sort != s2->sort) {
if (s2->sort == SORT_FIRST || s1->sort == SORT_LAST)
return -1;
if (s2->sort == SORT_LAST || s1->sort == SORT_FIRST)
return 1;
}
return 0;
}
static int
rcmdlinecmp(const void *p1, const void *p2)
{
const struct article *s1 = p1, *s2 = p2;
int rc;
if ((rc = cmpoverride(s1, s2)) != 0)
return rc;
return s2->order - s1->order;
}
static int
cmdlinecmp(const void *p1, const void *p2)
{
const struct article *s1 = p1, *s2 = p2;
int rc;
if ((rc = cmpoverride(s1, s2)) != 0)
return rc;
return s1->order - s2->order;
}
static int
rfilenamecmp(const void *p1, const void *p2)
{
const struct article *s1 = p1, *s2 = p2;
int rc;
if ((rc = cmpoverride(s1, s2)) != 0)
return rc;
return strcmp(s2->src, s1->src);
}
static int
filenamecmp(const void *p1, const void *p2)
{
const struct article *s1 = p1, *s2 = p2;
int rc;
if ((rc = cmpoverride(s1, s2)) != 0)
return rc;
return strcmp(s1->src, s2->src);
}
static int
rititlecmp(const void *p1, const void *p2)
{
const struct article *s1 = p1, *s2 = p2;
const char *t1, *t2;
int rc;
t1 = (s1->titletext == NULL) ? "" : s1->titletext;
t2 = (s2->titletext == NULL) ? "" : s2->titletext;
if ((rc = cmpoverride(s1, s2)) != 0)
return rc;
return strcasecmp(t2, t1);
}
static int
ititlecmp(const void *p1, const void *p2)
{
const struct article *s1 = p1, *s2 = p2;
const char *t1, *t2;
int rc;
t1 = (s1->titletext == NULL) ? "" : s1->titletext;
t2 = (s2->titletext == NULL) ? "" : s2->titletext;
if ((rc = cmpoverride(s1, s2)) != 0)
return rc;
return strcasecmp(t1, t2);
}
static int
rtitlecmp(const void *p1, const void *p2)
{
const struct article *s1 = p1, *s2 = p2;
const char *t1, *t2;
int rc;
t1 = (s1->titletext == NULL) ? "" : s1->titletext;
t2 = (s2->titletext == NULL) ? "" : s2->titletext;
if ((rc = cmpoverride(s1, s2)) != 0)
return rc;
return strcmp(t2, t1);
}
static int
titlecmp(const void *p1, const void *p2)
{
const struct article *s1 = p1, *s2 = p2;
const char *t1, *t2;
int rc;
t1 = (s1->titletext == NULL) ? "" : s1->titletext;
t2 = (s2->titletext == NULL) ? "" : s2->titletext;
if ((rc = cmpoverride(s1, s2)) != 0)
return rc;
return strcmp(t1, t2);
}
static int
rdatecmp(const void *p1, const void *p2)
{
const struct article *s1 = p1, *s2 = p2;
int rc;
if ((rc = cmpoverride(s1, s2)) != 0)
return rc;
return difftime(s1->time, s2->time);
}
static int
datecmp(const void *p1, const void *p2)
{
const struct article *s1 = p1, *s2 = p2;
int rc;
if ((rc = cmpoverride(s1, s2)) != 0)
return rc;
return difftime(s2->time, s1->time);
}
static void
article_free(struct article *p)
{
size_t i;
if (p == NULL)
return;
free(p->img);
free(p->base);
free(p->src);
free(p->stripsrc);
free(p->stripbase);
free(p->striplangbase);
free(p->title);
free(p->titletext);
free(p->author);
free(p->authortext);
free(p->aside);
free(p->asidetext);
free(p->article);
free(p->real);
free(p->stripreal);
free(p->realbase);
free(p->striprealbase);
free(p->striplangrealbase);
for (i = 0; i < p->tagmapsz; i++)
free(p->tagmap[i]);
for (i = 0; i < p->setmapsz; i++)
free(p->setmap[i]);
free(p->tagmap);
free(p->setmap);
}
void
sblg_free(struct article *p, size_t sz)
{
size_t i;
if (p == NULL)
return;
for (i = 0; i < sz; i++)
article_free(&p[i]);
free(p);
}
/*
* Look up the sort type.
* Return zero if not found, non-zero if found.
* Sets "sort" if found.
*/
int
sblg_sort_lookup(const char *s, enum asort *sort)
{
if (strcasecmp(s, "date") == 0)
*sort = ASORT_DATE;
else if (strcasecmp(s, "rdate") == 0)
*sort = ASORT_RDATE;
else if (strcasecmp(s, "filename") == 0)
*sort = ASORT_FILENAME;
else if (strcasecmp(s, "rfilename") == 0)
*sort = ASORT_RFILENAME;
else if (strcasecmp(s, "cmdline") == 0)
*sort = ASORT_CMDLINE;
else if (strcasecmp(s, "rcmdline") == 0)
*sort = ASORT_RCMDLINE;
else if (strcasecmp(s, "title") == 0)
*sort = ASORT_TITLE;
else if (strcasecmp(s, "rtitle") == 0)
*sort = ASORT_RTITLE;
else if (strcasecmp(s, "ititle") == 0)
*sort = ASORT_ITITLE;
else if (strcasecmp(s, "rititle") == 0)
*sort = ASORT_RITITLE;
else
return 0;
return 1;
}
/*
* Sort the list of articles in the manner given by "sort".
* This will take into account per-article sort ordering.
*/
void
sblg_sort(struct article *p, size_t sz, enum asort sort)
{
switch (sort) {
case ASORT_DATE:
qsort(p, sz, sizeof(struct article), datecmp);
break;
case ASORT_RDATE:
qsort(p, sz, sizeof(struct article), rdatecmp);
break;
case ASORT_FILENAME:
qsort(p, sz, sizeof(struct article), filenamecmp);
break;
case ASORT_RFILENAME:
qsort(p, sz, sizeof(struct article), rfilenamecmp);
break;
case ASORT_CMDLINE:
qsort(p, sz, sizeof(struct article), cmdlinecmp);
break;
case ASORT_RCMDLINE:
qsort(p, sz, sizeof(struct article), rcmdlinecmp);
break;
case ASORT_TITLE:
qsort(p, sz, sizeof(struct article), titlecmp);
break;
case ASORT_RTITLE:
qsort(p, sz, sizeof(struct article), rtitlecmp);
break;
case ASORT_ITITLE:
qsort(p, sz, sizeof(struct article), ititlecmp);
break;
case ASORT_RITITLE:
qsort(p, sz, sizeof(struct article), rititlecmp);
break;
}
}