chiark / gitweb /
4e2e06e22e32487ceea76ac08e70750d710399da
[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 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/trackname.c
19  * @brief Track name calculation
20  */
21 #include "common.h"
22
23 #include <pcre.h>
24 #include <fnmatch.h>
25
26 #include "trackname.h"
27 #include "configuration.h"
28 #include "regsub.h"
29 #include "log.h"
30 #include "filepart.h"
31 #include "unicode.h"
32
33 const struct collection *find_track_collection(const char *track) {
34   int n;
35   size_t l, tl = strlen(track);
36
37   for(n = 0; n < config->collection.n; ++n) {
38     l = strlen(config->collection.s[n].root);
39     if(tl > l
40        && !strncmp(track, config->collection.s[n].root, l)
41        && track[l] == '/')
42       break;
43   }
44   if(n < config->collection.n)
45     return &config->collection.s[n];
46   else
47     return 0;
48 }
49
50 const char *find_track_root(const char *track) {
51   const struct collection *c = find_track_collection(track);
52   if(c)
53     return c->root;
54   disorder_error(0, "found track in no collection '%s'", track);
55   return 0;
56 }
57
58 const char *track_rootless(const char *track) {
59   const char *root;
60
61   if(!(root = find_track_root(track))) return 0;
62   return track + strlen(root);
63 }
64
65 const char *trackname_part(const char *track,
66                            const char *context,
67                            const char *part) {
68   int n;
69   const char *replaced, *rootless;
70
71   assert(track != 0);
72   if(!strcmp(part, "path")) return track;
73   if(!strcmp(part, "ext")) return extension(track);
74   if((rootless = track_rootless(track))) track = rootless;
75   for(n = 0; n < config->namepart.n; ++n) {
76     if(!strcmp(config->namepart.s[n].part, part)
77        && fnmatch(config->namepart.s[n].context, context, 0) == 0) {
78       if((replaced = regsub(config->namepart.s[n].re,
79                             track,
80                             config->namepart.s[n].replace,
81                             config->namepart.s[n].reflags
82                             |REGSUB_MUST_MATCH
83                             |REGSUB_REPLACE)))
84         return replaced;
85     }
86   }
87   return "";
88 }
89
90 const char *trackname_transform(const char *type,
91                                 const char *subject,
92                                 const char *context) {
93   const char *replaced;
94   int n;
95   const struct transform *k;
96
97   for(n = 0; n < config->transform.n; ++n) {
98     k = &config->transform.t[n];
99     if(strcmp(k->type, type))
100       continue;
101     if(fnmatch(k->context, context, 0) != 0)
102       continue;
103     if((replaced = regsub(k->re, subject, k->replace, k->flags)))
104       subject = replaced;
105   }
106   return subject;
107 }
108
109 /*
110 Local Variables:
111 c-basic-offset:2
112 comment-column:40
113 fill-column:79
114 End:
115 */