chiark / gitweb /
Remove arch tags throughout
[disorder] / lib / trackname.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2005, 2006 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 "words.h"
35
36const char *find_track_root(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) return 0;
48 return config->collection.s[n].root;
49}
50
51const char *track_rootless(const char *track) {
52 const char *root;
53
54 if(!(root = find_track_root(track))) return 0;
55 return track + strlen(root);
56}
57
58const char *trackname_part(const char *track,
59 const char *context,
60 const char *part) {
61 int n;
62 const char *replaced, *rootless;
63
64 assert(track != 0);
65 if(!strcmp(part, "path")) return track;
66 if(!strcmp(part, "ext")) return extension(track);
67 if((rootless = track_rootless(track))) track = rootless;
68 for(n = 0; n < config->namepart.n; ++n) {
69 if(!strcmp(config->namepart.s[n].part, part)
70 && fnmatch(config->namepart.s[n].context, context, 0) == 0) {
71 if((replaced = regsub(config->namepart.s[n].re,
72 track,
73 config->namepart.s[n].replace,
74 config->namepart.s[n].reflags
75 |REGSUB_MUST_MATCH
76 |REGSUB_REPLACE)))
77 return replaced;
78 }
79 }
80 return "";
81}
82
83const char *trackname_transform(const char *type,
84 const char *subject,
85 const char *context) {
86 const char *replaced;
87 int n;
88 const struct transform *k;
89
90 for(n = 0; n < config->transform.n; ++n) {
91 k = &config->transform.t[n];
92 if(strcmp(k->type, type))
93 continue;
94 if(fnmatch(k->context, context, 0) != 0)
95 continue;
96 if((replaced = regsub(k->re, subject, k->replace, k->flags)))
97 subject = replaced;
98 }
99 return subject;
100}
101
102int compare_tracks(const char *sa, const char *sb,
103 const char *da, const char *db,
104 const char *ta, const char *tb) {
105 int c;
106
107 if((c = strcmp(casefold(sa), casefold(sb)))) return c;
108 if((c = strcmp(sa, sb))) return c;
109 if((c = strcmp(casefold(da), casefold(db)))) return c;
110 if((c = strcmp(da, db))) return c;
111 return compare_path(ta, tb);
112}
113
114int compare_path_raw(const unsigned char *ap, size_t an,
115 const unsigned char *bp, size_t bn) {
116 while(an > 0 && bn > 0) {
117 if(*ap == *bp) {
118 ap++;
119 bp++;
120 an--;
121 bn--;
122 } else if(*ap == '/') {
123 return -1; /* /a/b < /aa/ */
124 } else if(*bp == '/') {
125 return 1; /* /aa > /a/b */
126 } else
127 return *ap - *bp;
128 }
129 if(an > 0)
130 return 1; /* /a/b > /a and /ab > /a */
131 else if(bn > 0)
132 return -1; /* /a < /ab and /a < /a/b */
133 else
134 return 0;
135}
136
137/*
138Local Variables:
139c-basic-offset:2
140comment-column:40
141fill-column:79
142End:
143*/