chiark / gitweb /
disorder.h: more consistent approach to function attributes
[disorder] / lib / trackorder.c
1 /*
2  * This file is part of DisOrder
3  * Copyright (C) 2005, 2006, 2007 Richard Kettlewell
4  *
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.
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  * 
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/>.
17  */
18 /** @file lib/trackorder.c
19  * @brief Track ordering
20  */
21 #include "common.h"
22
23 #include <pcre.h>
24 #include <fnmatch.h>
25
26 #include "trackname.h"
27 #include "log.h"
28 #include "unicode.h"
29
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  */
46 int 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
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  */
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) {
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 /*
98 Local Variables:
99 c-basic-offset:2
100 comment-column:40
101 fill-column:79
102 End:
103 */