| 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 | /* 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; |
| 58 | disorder_error(0, "found track in no collection '%s'", track); |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | const 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 | |
| 69 | const 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 | |
| 94 | const 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 | |
| 113 | /* |
| 114 | Local Variables: |
| 115 | c-basic-offset:2 |
| 116 | comment-column:40 |
| 117 | fill-column:79 |
| 118 | End: |
| 119 | */ |