chiark / gitweb /
Switch to GPL v3
[disorder] / lib / trackname.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
eb525fcd 3 * Copyright (C) 2005, 2006, 2007 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
e7eb3a27
RK
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 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
18
05b75f8d 19#include "common.h"
460b9539 20
21#include <pcre.h>
22#include <fnmatch.h>
460b9539 23
24#include "trackname.h"
25#include "configuration.h"
26#include "regsub.h"
27#include "log.h"
28#include "filepart.h"
c85b7022 29#include "unicode.h"
460b9539 30
d1694464 31const struct collection *find_track_collection(const char *track) {
460b9539 32 int n;
33 size_t l, tl = strlen(track);
34
35 for(n = 0; n < config->collection.n; ++n) {
36 l = strlen(config->collection.s[n].root);
37 if(tl > l
38 && !strncmp(track, config->collection.s[n].root, l)
39 && track[l] == '/')
40 break;
41 }
d1694464 42 if(n < config->collection.n)
43 return &config->collection.s[n];
44 else
a8747834 45 return 0;
d1694464 46}
47
48const char *find_track_root(const char *track) {
49 const struct collection *c = find_track_collection(track);
50 if(c)
51 return c->root;
52 error(0, "found track in no collection '%s'", track);
53 return 0;
460b9539 54}
55
56const char *track_rootless(const char *track) {
57 const char *root;
58
59 if(!(root = find_track_root(track))) return 0;
60 return track + strlen(root);
61}
62
63const char *trackname_part(const char *track,
64 const char *context,
65 const char *part) {
66 int n;
67 const char *replaced, *rootless;
68
69 assert(track != 0);
70 if(!strcmp(part, "path")) return track;
71 if(!strcmp(part, "ext")) return extension(track);
72 if((rootless = track_rootless(track))) track = rootless;
73 for(n = 0; n < config->namepart.n; ++n) {
74 if(!strcmp(config->namepart.s[n].part, part)
75 && fnmatch(config->namepart.s[n].context, context, 0) == 0) {
76 if((replaced = regsub(config->namepart.s[n].re,
77 track,
78 config->namepart.s[n].replace,
79 config->namepart.s[n].reflags
80 |REGSUB_MUST_MATCH
81 |REGSUB_REPLACE)))
82 return replaced;
83 }
84 }
85 return "";
86}
87
88const char *trackname_transform(const char *type,
89 const char *subject,
90 const char *context) {
91 const char *replaced;
92 int n;
93 const struct transform *k;
94
95 for(n = 0; n < config->transform.n; ++n) {
96 k = &config->transform.t[n];
97 if(strcmp(k->type, type))
98 continue;
99 if(fnmatch(k->context, context, 0) != 0)
100 continue;
101 if((replaced = regsub(k->re, subject, k->replace, k->flags)))
102 subject = replaced;
103 }
104 return subject;
105}
106
460b9539 107/*
108Local Variables:
109c-basic-offset:2
110comment-column:40
111fill-column:79
112End:
113*/