chiark / gitweb /
Merge from disorder.dev.
[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;
54 error(0, "found track in no collection '%s'", track);
55 return 0;
460b9539 56}
57
58const 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
65const 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
90const 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
460b9539 109/*
110Local Variables:
111c-basic-offset:2
112comment-column:40
113fill-column:79
114End:
115*/