forked from DeaDBeeF-Player/deadbeef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.c
356 lines (309 loc) · 10.1 KB
/
sort.c
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
DeaDBeeF -- the music player
Copyright (C) 2009-2015 Alexey Yakovenko and other contributors
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <sys/time.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "utf8.h"
#include "sort.h"
#include "tf.h"
//#define trace(...) { fprintf(stderr, __VA_ARGS__); }
#define trace(fmt,...)
static void
plt_sort_internal (playlist_t *playlist, int iter, int id, const char *format, int order, int version);
void
plt_sort_v2 (playlist_t *plt, int iter, int id, const char *format, int order) {
plt_sort_internal (plt, iter, id, format, order, 1);
}
// sort for title formatting v1
void
plt_sort (playlist_t *playlist, int iter, int id, const char *format, int order) {
plt_sort_internal (playlist, iter, id, format, order, 0);
}
static int pl_sort_is_duration;
static int pl_sort_is_track;
static int pl_sort_ascending;
static int pl_sort_id;
static int pl_sort_version; // 0: use pl_sort_format, 1: use pl_sort_tf_bytecode
static const char *pl_sort_format;
static char *pl_sort_tf_bytecode;
static ddb_tf_context_t pl_sort_tf_ctx;
static int
strcasecmp_numeric (const char *a, const char *b) {
if (isdigit (*a) && isdigit (*b)) {
int anum = *a-'0';
const char *ae = a+1;
while (*ae && isdigit (*ae)) {
anum *= 10;
anum += *ae-'0';
ae++;
}
int bnum = *b-'0';
const char *be = b+1;
while (*be && isdigit (*be)) {
bnum *= 10;
bnum += *be-'0';
be++;
}
if (anum == bnum) {
return u8_strcasecmp (ae, be);
}
return anum - bnum;
}
return u8_strcasecmp (a,b);
}
static int
pl_sort_compare_str (playItem_t *a, playItem_t *b) {
if (pl_sort_is_duration) {
float dur_a = a->_duration * 100000;
float dur_b = b->_duration * 100000;
return !pl_sort_ascending ? dur_b - dur_a : dur_a - dur_b;
}
else if (pl_sort_is_track) {
int t1;
int t2;
const char *t;
t = pl_find_meta_raw (a, "track");
if (t && !isdigit (*t)) {
t1 = 999999;
}
else {
t1 = t ? atoi (t) : -1;
}
t = pl_find_meta_raw (b, "track");
if (t && !isdigit (*t)) {
t2 = 999999;
}
else {
t2 = t ? atoi (t) : -1;
}
return !pl_sort_ascending ? t2 - t1 : t1 - t2;
}
else {
char tmp1[1024];
char tmp2[1024];
if (pl_sort_version == 0) {
pl_format_title (a, -1, tmp1, sizeof (tmp1), pl_sort_id, pl_sort_format);
pl_format_title (b, -1, tmp2, sizeof (tmp2), pl_sort_id, pl_sort_format);
}
else {
pl_sort_tf_ctx.id = pl_sort_id;
pl_sort_tf_ctx.it = (ddb_playItem_t *)a;
tf_eval(&pl_sort_tf_ctx, pl_sort_tf_bytecode, tmp1, sizeof(tmp1));
pl_sort_tf_ctx.it = (ddb_playItem_t *)b;
tf_eval(&pl_sort_tf_ctx, pl_sort_tf_bytecode, tmp2, sizeof(tmp2));
}
int res = strcasecmp_numeric (tmp1, tmp2);
if (!pl_sort_ascending) {
res = -res;
}
return res;
}
}
static int
qsort_cmp_func (const void *a, const void *b) {
playItem_t *aa = *((playItem_t **)a);
playItem_t *bb = *((playItem_t **)b);
return pl_sort_compare_str (aa, bb);
}
void
plt_sort_random (playlist_t *playlist, int iter) {
if (!playlist->head[iter] || !playlist->head[iter]->next[iter]) {
return;
}
pl_lock ();
const int playlist_count = playlist->count[iter];
playItem_t **array = malloc (playlist_count * sizeof (playItem_t *));
int idx = 0;
for (playItem_t *it = playlist->head[iter]; it; it = it->next[iter], idx++) {
array[idx] = it;
}
//randomize array
for (int swap_a = 0; swap_a < playlist_count - 1; swap_a++) {
//select random item above swap_a-1
const int swap_b = swap_a + (rand() / (float)RAND_MAX * (playlist_count - swap_a));
//swap a with b
playItem_t* const swap_temp = array[swap_a];
array[swap_a] = array[swap_b];
array[swap_b] = swap_temp;
}
playItem_t *prev = NULL;
playlist->head[iter] = 0;
for (idx = 0; idx < playlist->count[iter]; idx++) {
playItem_t *it = array[idx];
it->prev[iter] = prev;
it->next[iter] = NULL;
if (!prev) {
playlist->head[iter] = it;
}
else {
prev->next[iter] = it;
}
prev = it;
}
playlist->tail[iter] = array[playlist->count[iter]-1];
free (array);
plt_modified (playlist);
pl_unlock ();
}
// version 0: title formatting v1
// version 1: title formatting v2
void
plt_sort_internal (playlist_t *playlist, int iter, int id, const char *format, int order, int version) {
if (order == DDB_SORT_RANDOM) {
plt_sort_random (playlist, iter);
return;
}
int ascending = order == DDB_SORT_DESCENDING ? 0 : 1;
if (format == NULL || id == DB_COLUMN_FILENUMBER || !playlist->head[iter] || !playlist->head[iter]->next[iter]) {
return;
}
pl_lock ();
struct timeval tm1;
gettimeofday (&tm1, NULL);
pl_sort_ascending = ascending;
trace ("ascending: %d\n", ascending);
pl_sort_id = id;
pl_sort_version = version;
if (version == 0) {
pl_sort_format = format;
pl_sort_tf_bytecode = NULL;
}
else {
pl_sort_format = NULL;
pl_sort_tf_bytecode = tf_compile (format);
pl_sort_tf_ctx._size = sizeof (pl_sort_tf_ctx);
pl_sort_tf_ctx.it = NULL;
pl_sort_tf_ctx.plt = (ddb_playlist_t *)playlist;
pl_sort_tf_ctx.idx = -1;
pl_sort_tf_ctx.id = id;
}
if (format && id == -1
&& ((version == 0 && !strcmp (format, "%l"))
|| (version == 1 && !strcmp (format, "%length%")))
) {
pl_sort_is_duration = 1;
}
else {
pl_sort_is_duration = 0;
}
if (format && id == -1
&& ((version == 0 && !strcmp (format, "%n"))
|| (version == 1 && (!strcmp (format, "%track number%") || !strcmp (format, "%tracknumber%"))))
) {
pl_sort_is_track = 1;
}
else {
pl_sort_is_track = 0;
}
int cursor = plt_get_cursor (playlist, PL_MAIN);
playItem_t *track_under_cursor = NULL;
if (cursor != -1) {
track_under_cursor = plt_get_item_for_idx (playlist, cursor, PL_MAIN);
}
playItem_t **array = malloc (playlist->count[iter] * sizeof (playItem_t *));
int idx = 0;
for (playItem_t *it = playlist->head[iter]; it; it = it->next[iter], idx++) {
array[idx] = it;
}
#if HAVE_MERGESORT
mergesort (array, playlist->count[iter], sizeof (playItem_t *), qsort_cmp_func);
#else
qsort (array, playlist->count[iter], sizeof (playItem_t *), qsort_cmp_func);
#endif
playItem_t *prev = NULL;
playlist->head[iter] = 0;
for (idx = 0; idx < playlist->count[iter]; idx++) {
playItem_t *it = array[idx];
it->prev[iter] = prev;
it->next[iter] = NULL;
if (!prev) {
playlist->head[iter] = it;
}
else {
prev->next[iter] = it;
}
prev = it;
}
playlist->tail[iter] = array[playlist->count[iter]-1];
free (array);
if (track_under_cursor) {
cursor = plt_get_item_idx (playlist, track_under_cursor, PL_MAIN);
plt_set_cursor (playlist, PL_MAIN, cursor);
pl_item_unref (track_under_cursor);
}
struct timeval tm2;
gettimeofday (&tm2, NULL);
int ms = (tm2.tv_sec*1000+tm2.tv_usec/1000) - (tm1.tv_sec*1000+tm1.tv_usec/1000);
trace ("sort time: %f seconds\n", ms / 1000.f);
plt_modified (playlist);
if (version == 0) {
pl_sort_format = NULL;
}
if (version == 1) {
tf_free (pl_sort_tf_bytecode);
pl_sort_tf_bytecode = NULL;
memset (&pl_sort_tf_ctx, 0, sizeof (pl_sort_tf_ctx));
}
pl_unlock ();
}
void
sort_track_array (playlist_t *playlist, playItem_t **tracks, int num_tracks, const char *format, int order) {
if (order != DDB_SORT_DESCENDING && order != DDB_SORT_ASCENDING) {
// unsupported
return;
}
int ascending = order == DDB_SORT_DESCENDING ? 0 : 1;
if (format == NULL || !tracks || !num_tracks) {
return;
}
pl_lock ();
pl_sort_ascending = ascending;
pl_sort_id = -1;
pl_sort_version = 1;
pl_sort_format = NULL;
pl_sort_tf_bytecode = tf_compile (format);
pl_sort_tf_ctx._size = sizeof (pl_sort_tf_ctx);
pl_sort_tf_ctx.it = NULL;
pl_sort_tf_ctx.plt = (ddb_playlist_t *)playlist;
pl_sort_tf_ctx.idx = -1;
pl_sort_tf_ctx.id = -1;
if (format
&& !strcmp (format, "%length%")) {
pl_sort_is_duration = 1;
}
else {
pl_sort_is_duration = 0;
}
if (format
&& (!strcmp (format, "%track number%") || !strcmp (format, "%tracknumber%"))) {
pl_sort_is_track = 1;
}
else {
pl_sort_is_track = 0;
}
#if HAVE_MERGESORT
mergesort (tracks, num_tracks, sizeof (playItem_t *), qsort_cmp_func);
#else
qsort (tracks, num_tracks, sizeof (playItem_t *), qsort_cmp_func);
#endif
tf_free (pl_sort_tf_bytecode);
pl_sort_tf_bytecode = NULL;
memset (&pl_sort_tf_ctx, 0, sizeof (pl_sort_tf_ctx));
pl_unlock ();
}