chiark / gitweb /
Remove ifdeffery for `HAVE_PCRE_H'.
[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 <fnmatch.h>
24
25 #include "trackname.h"
26 #include "log.h"
27 #include "unicode.h"
28
29 /** @brief Compare two tracks
30  * @param sa First sort key
31  * @param sb Second sort key
32  * @param da First display string
33  * @param db Second display string
34  * @param ta First raw track
35  * @param tb Second raw track
36  * @return -ve, 0 or +ve for a <, = or > b
37  *
38  * Tries the following comparisons until a difference is found:
39  * - case-independent comparison of sort keys
40  * - case-dependent comparison of sort keys
41  * - case-independent comparison of display strings
42  * - case-dependent comparison of display strings
43  * - case-dependent comparison of paths (see compare_path())
44  */
45 int compare_tracks(const char *sa, const char *sb,
46                    const char *da, const char *db,
47                    const char *ta, const char *tb) {
48   int c;
49
50   if((c = strcmp(utf8_casefold_canon(sa, strlen(sa), 0),
51                  utf8_casefold_canon(sb, strlen(sb), 0))))
52     return c;
53   if((c = strcmp(sa, sb))) return c;
54   if((c = strcmp(utf8_casefold_canon(da, strlen(da), 0),
55                  utf8_casefold_canon(db, strlen(db), 0))))
56     return c;
57   if((c = strcmp(da, db))) return c;
58   return compare_path(ta, tb);
59 }
60
61 /** @brief Compare two paths
62  * @param ap First path
63  * @param an Length of @p ap
64  * @param bp Second path
65  * @param bn Length @p bp
66  * @return -ve, 0 or +ve for ap <, = or > bp
67  *
68  * Sorts files within a directory together.
69  *
70  * See also compare_path().
71  */
72 int compare_path_raw(const unsigned char *ap, size_t an,
73                      const unsigned char *bp, size_t bn) {
74   /* Don't change this function!  The database sort order depends on it */
75   while(an > 0 && bn > 0) {
76     if(*ap == *bp) {
77       ap++;
78       bp++;
79       an--;
80       bn--;
81     } else if(*ap == '/') {
82       return -1;                /* /a/b < /aa/ */
83     } else if(*bp == '/') {
84       return 1;                 /* /aa > /a/b */
85     } else
86       return *ap - *bp;
87   }
88   if(an > 0)
89     return 1;                   /* /a/b > /a and /ab > /a */
90   else if(bn > 0)
91     return -1;                  /* /a < /ab and /a < /a/b */
92   else
93     return 0;
94 }
95
96 /*
97 Local Variables:
98 c-basic-offset:2
99 comment-column:40
100 fill-column:79
101 End:
102 */