chiark / gitweb /
merge disorder.unicode branch
[disorder] / lib / trackname.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 2 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, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20
21 #include <config.h>
22 #include "types.h"
23
24 #include <pcre.h>
25 #include <fnmatch.h>
26 #include <string.h>
27 #include <assert.h>
28
29 #include "trackname.h"
30 #include "configuration.h"
31 #include "regsub.h"
32 #include "log.h"
33 #include "filepart.h"
34 #include "unicode.h"
35
36 const struct collection *find_track_collection(const char *track) {
37   int n;
38   size_t l, tl = strlen(track);
39
40   for(n = 0; n < config->collection.n; ++n) {
41     l = strlen(config->collection.s[n].root);
42     if(tl > l
43        && !strncmp(track, config->collection.s[n].root, l)
44        && track[l] == '/')
45       break;
46   }
47   if(n < config->collection.n)
48     return &config->collection.s[n];
49   else
50     return 0;
51 }
52
53 const char *find_track_root(const char *track) {
54   const struct collection *c = find_track_collection(track);
55   if(c)
56     return c->root;
57   error(0, "found track in no collection '%s'", track);
58   return 0;
59 }
60
61 const char *track_rootless(const char *track) {
62   const char *root;
63
64   if(!(root = find_track_root(track))) return 0;
65   return track + strlen(root);
66 }
67
68 const char *trackname_part(const char *track,
69                            const char *context,
70                            const char *part) {
71   int n;
72   const char *replaced, *rootless;
73
74   assert(track != 0);
75   if(!strcmp(part, "path")) return track;
76   if(!strcmp(part, "ext")) return extension(track);
77   if((rootless = track_rootless(track))) track = rootless;
78   for(n = 0; n < config->namepart.n; ++n) {
79     if(!strcmp(config->namepart.s[n].part, part)
80        && fnmatch(config->namepart.s[n].context, context, 0) == 0) {
81       if((replaced = regsub(config->namepart.s[n].re,
82                             track,
83                             config->namepart.s[n].replace,
84                             config->namepart.s[n].reflags
85                             |REGSUB_MUST_MATCH
86                             |REGSUB_REPLACE)))
87         return replaced;
88     }
89   }
90   return "";
91 }
92
93 const char *trackname_transform(const char *type,
94                                 const char *subject,
95                                 const char *context) {
96   const char *replaced;
97   int n;
98   const struct transform *k;
99
100   for(n = 0; n < config->transform.n; ++n) {
101     k = &config->transform.t[n];
102     if(strcmp(k->type, type))
103       continue;
104     if(fnmatch(k->context, context, 0) != 0)
105       continue;
106     if((replaced = regsub(k->re, subject, k->replace, k->flags)))
107       subject = replaced;
108   }
109   return subject;
110 }
111
112 int compare_tracks(const char *sa, const char *sb,
113                    const char *da, const char *db,
114                    const char *ta, const char *tb) {
115   int c;
116
117   if((c = strcmp(utf8_casefold_canon(sa, strlen(sa), 0),
118                  utf8_casefold_canon(sb, strlen(sb), 0))))
119     return c;
120   if((c = strcmp(sa, sb))) return c;
121   if((c = strcmp(utf8_casefold_canon(da, strlen(da), 0),
122                  utf8_casefold_canon(db, strlen(db), 0))))
123     return c;
124   if((c = strcmp(da, db))) return c;
125   return compare_path(ta, tb);
126 }
127
128 int compare_path_raw(const unsigned char *ap, size_t an,
129                      const unsigned char *bp, size_t bn) {
130   /* Don't change this function!  The database sort order depends on it */
131   while(an > 0 && bn > 0) {
132     if(*ap == *bp) {
133       ap++;
134       bp++;
135       an--;
136       bn--;
137     } else if(*ap == '/') {
138       return -1;                /* /a/b < /aa/ */
139     } else if(*bp == '/') {
140       return 1;                 /* /aa > /a/b */
141     } else
142       return *ap - *bp;
143   }
144   if(an > 0)
145     return 1;                   /* /a/b > /a and /ab > /a */
146   else if(bn > 0)
147     return -1;                  /* /a < /ab and /a < /a/b */
148   else
149     return 0;
150 }
151
152 /*
153 Local Variables:
154 c-basic-offset:2
155 comment-column:40
156 fill-column:79
157 End:
158 */