chiark / gitweb /
Merge memory hygeine branch
[disorder] / lib / trackorder.c
CommitLineData
e307c6fe
RK
1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2005, 2006, 2007 Richard Kettlewell
4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
e307c6fe 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
e307c6fe 8 * (at your option) any later version.
e7eb3a27
RK
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
e307c6fe 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
e307c6fe 17 */
132a5a4a
RK
18/** @file lib/trackorder.c
19 * @brief Track ordering
20 */
05b75f8d 21#include "common.h"
e307c6fe
RK
22
23#include <pcre.h>
24#include <fnmatch.h>
e307c6fe
RK
25
26#include "trackname.h"
27#include "log.h"
28#include "unicode.h"
29
d53ef367
RK
30/** @brief Compare two tracks
31 * @param sa First sort key
32 * @param sb Second sort key
33 * @param da First display string
34 * @param db Second display string
35 * @param ta First raw track
36 * @param tb Second raw track
37 * @return -ve, 0 or +ve for a <, = or > b
38 *
39 * Tries the following comparisons until a difference is found:
40 * - case-independent comparison of sort keys
41 * - case-dependent comparison of sort keys
42 * - case-independent comparison of display strings
43 * - case-dependent comparison of display strings
44 * - case-dependent comparison of paths (see compare_path())
45 */
e307c6fe
RK
46int compare_tracks(const char *sa, const char *sb,
47 const char *da, const char *db,
48 const char *ta, const char *tb) {
49 int c;
50
51 if((c = strcmp(utf8_casefold_canon(sa, strlen(sa), 0),
52 utf8_casefold_canon(sb, strlen(sb), 0))))
53 return c;
54 if((c = strcmp(sa, sb))) return c;
55 if((c = strcmp(utf8_casefold_canon(da, strlen(da), 0),
56 utf8_casefold_canon(db, strlen(db), 0))))
57 return c;
58 if((c = strcmp(da, db))) return c;
59 return compare_path(ta, tb);
60}
61
d53ef367
RK
62/** @brief Compare two paths
63 * @param ap First path
64 * @param an Length of @p ap
65 * @param bp Second path
66 * @param bn Length @p bp
67 * @return -ve, 0 or +ve for ap <, = or > bp
68 *
69 * Sorts files within a directory together.
70 *
71 * See also compare_path().
72 */
e307c6fe
RK
73int compare_path_raw(const unsigned char *ap, size_t an,
74 const unsigned char *bp, size_t bn) {
75 /* Don't change this function! The database sort order depends on it */
76 while(an > 0 && bn > 0) {
77 if(*ap == *bp) {
78 ap++;
79 bp++;
80 an--;
81 bn--;
82 } else if(*ap == '/') {
83 return -1; /* /a/b < /aa/ */
84 } else if(*bp == '/') {
85 return 1; /* /aa > /a/b */
86 } else
87 return *ap - *bp;
88 }
89 if(an > 0)
90 return 1; /* /a/b > /a and /ab > /a */
91 else if(bn > 0)
92 return -1; /* /a < /ab and /a < /a/b */
93 else
94 return 0;
95}
96
97/*
98Local Variables:
99c-basic-offset:2
100comment-column:40
101fill-column:79
102End:
103*/