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