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