2 * This file is part of DisOrder
3 * Copyright (C) 2005, 2006, 2007 Richard Kettlewell
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file lib/trackorder.c
19 * @brief Track ordering
26 #include "trackname.h"
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
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())
46 int compare_tracks(const char *sa, const char *sb,
47 const char *da, const char *db,
48 const char *ta, const char *tb) {
51 if((c = strcmp(utf8_casefold_canon(sa, strlen(sa), 0),
52 utf8_casefold_canon(sb, strlen(sb), 0))))
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))))
58 if((c = strcmp(da, db))) return c;
59 return compare_path(ta, tb);
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
69 * Sorts files within a directory together.
71 * See also compare_path().
73 int 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) {
82 } else if(*ap == '/') {
83 return -1; /* /a/b < /aa/ */
84 } else if(*bp == '/') {
85 return 1; /* /aa > /a/b */
90 return 1; /* /a/b > /a and /ab > /a */
92 return -1; /* /a < /ab and /a < /a/b */