chiark / gitweb /
Switch to GPL v3
[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
19 #include "common.h"
20
21 #include <pcre.h>
22 #include <fnmatch.h>
23
24 #include "trackname.h"
25 #include "log.h"
26 #include "unicode.h"
27
28 int compare_tracks(const char *sa, const char *sb,
29                    const char *da, const char *db,
30                    const char *ta, const char *tb) {
31   int c;
32
33   if((c = strcmp(utf8_casefold_canon(sa, strlen(sa), 0),
34                  utf8_casefold_canon(sb, strlen(sb), 0))))
35     return c;
36   if((c = strcmp(sa, sb))) return c;
37   if((c = strcmp(utf8_casefold_canon(da, strlen(da), 0),
38                  utf8_casefold_canon(db, strlen(db), 0))))
39     return c;
40   if((c = strcmp(da, db))) return c;
41   return compare_path(ta, tb);
42 }
43
44 int compare_path_raw(const unsigned char *ap, size_t an,
45                      const unsigned char *bp, size_t bn) {
46   /* Don't change this function!  The database sort order depends on it */
47   while(an > 0 && bn > 0) {
48     if(*ap == *bp) {
49       ap++;
50       bp++;
51       an--;
52       bn--;
53     } else if(*ap == '/') {
54       return -1;                /* /a/b < /aa/ */
55     } else if(*bp == '/') {
56       return 1;                 /* /aa > /a/b */
57     } else
58       return *ap - *bp;
59   }
60   if(an > 0)
61     return 1;                   /* /a/b > /a and /ab > /a */
62   else if(bn > 0)
63     return -1;                  /* /a < /ab and /a < /a/b */
64   else
65     return 0;
66 }
67
68 /*
69 Local Variables:
70 c-basic-offset:2
71 comment-column:40
72 fill-column:79
73 End:
74 */