1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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,
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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "strfsong.h"
21 #include "charset.h"
22 #include "utils.h"
24 #include <mpd/client.h>
26 #include <string.h>
28 static const gchar *
29 skip(const gchar * p)
30 {
31 gint stack = 0;
33 while (*p != '\0') {
34 if (*p == '[')
35 stack++;
36 if (*p == '#' && p[1] != '\0') {
37 /* skip escaped stuff */
38 ++p;
39 } else if (stack) {
40 if(*p == ']') stack--;
41 } else {
42 if(*p == '&' || *p == '|' || *p == ']') {
43 break;
44 }
45 }
46 ++p;
47 }
49 return p;
50 }
52 #ifndef NCMPC_MINI
54 static char *
55 concat_tag_values(const char *a, const char *b)
56 {
57 return g_strconcat(a, ", ", b, NULL);
58 }
60 static char *
61 song_more_tag_values(const struct mpd_song *song, enum mpd_tag_type tag,
62 const char *first)
63 {
64 const char *p = mpd_song_get_tag(song, tag, 1);
65 char *buffer, *prev;
67 if (p == NULL)
68 return NULL;
70 buffer = concat_tag_values(first, p);
71 for (unsigned i = 2; (p = mpd_song_get_tag(song, tag, i)) != NULL;
72 ++i) {
73 prev = buffer;
74 buffer = concat_tag_values(buffer, p);
75 g_free(prev);
76 }
78 return buffer;
79 }
81 #endif /* !NCMPC_MINI */
83 static char *
84 song_tag_locale(const struct mpd_song *song, enum mpd_tag_type tag)
85 {
86 const char *value = mpd_song_get_tag(song, tag, 0);
87 char *result;
88 #ifndef NCMPC_MINI
89 char *all;
90 #endif /* !NCMPC_MINI */
92 if (value == NULL)
93 return NULL;
95 #ifndef NCMPC_MINI
96 all = song_more_tag_values(song, tag, value);
97 if (all != NULL)
98 value = all;
99 #endif /* !NCMPC_MINI */
101 result = utf8_to_locale(value);
103 #ifndef NCMPC_MINI
104 g_free(all);
105 #endif /* !NCMPC_MINI */
107 return result;
108 }
110 static gsize
111 _strfsong(gchar *s,
112 gsize max,
113 const gchar *format,
114 const struct mpd_song *song,
115 const gchar **last)
116 {
117 const gchar *p, *end;
118 gchar *temp;
119 gsize n, length = 0;
120 gboolean found = FALSE;
121 /* "missed" helps handling the case of mere literal text like
122 found==TRUE instead of found==FALSE. */
123 gboolean missed = FALSE;
125 s[0] = '\0';
127 if (song == NULL)
128 return 0;
130 for (p = format; *p != '\0' && length<max;) {
131 /* OR */
132 if (p[0] == '|') {
133 ++p;
134 if(missed && !found) {
135 s[0] = '\0';
136 length = 0;
137 missed = FALSE;
138 } else {
139 p = skip(p);
140 }
141 continue;
142 }
144 /* AND */
145 if (p[0] == '&') {
146 ++p;
147 if(missed && !found) {
148 p = skip(p);
149 } else {
150 found = FALSE;
151 missed = FALSE;
152 }
153 continue;
154 }
156 /* EXPRESSION START */
157 if (p[0] == '[') {
158 temp = g_malloc0(max);
159 if( _strfsong(temp, max, p+1, song, &p) >0 ) {
160 g_strlcat(s, temp, max);
161 length = strlen(s);
162 found = TRUE;
163 } else {
164 missed = TRUE;
165 }
166 g_free(temp);
167 continue;
168 }
170 /* EXPRESSION END */
171 if (p[0] == ']') {
172 if(last) *last = p+1;
173 if(missed && !found && length) {
174 s[0] = '\0';
175 length = 0;
176 }
177 return length;
178 }
180 /* pass-through non-escaped portions of the format string */
181 if (p[0] != '#' && p[0] != '%' && length<max) {
182 s[length++] = *p;
183 s[length] = '\0';
184 p++;
185 continue;
186 }
188 /* let the escape character escape itself */
189 if (p[0] == '#' && p[1] != '\0' && length<max) {
190 s[length++] = *(p+1);
191 s[length] = '\0';
192 p+=2;
193 continue;
194 }
196 /* advance past the esc character */
198 /* find the extent of this format specifier (stop at \0, ' ', or esc) */
199 temp = NULL;
200 end = p+1;
201 while(*end >= 'a' && *end <= 'z') {
202 end++;
203 }
204 n = end - p + 1;
205 if(*end != '%')
206 n--;
207 else if (strncmp("%file%", p, n) == 0)
208 temp = utf8_to_locale(mpd_song_get_uri(song));
209 else if (strncmp("%artist%", p, n) == 0)
210 temp = song_tag_locale(song, MPD_TAG_ARTIST);
211 else if (strncmp("%title%", p, n) == 0)
212 temp = song_tag_locale(song, MPD_TAG_TITLE);
213 else if (strncmp("%album%", p, n) == 0)
214 temp = song_tag_locale(song, MPD_TAG_ALBUM);
215 else if (strncmp("%shortalbum%", p, n) == 0) {
216 temp = song_tag_locale(song, MPD_TAG_ALBUM);
217 if (temp) {
218 gchar *temp2 = g_strndup(temp, 25);
219 if (strlen(temp) > 25) {
220 temp2[24] = '.';
221 temp2[23] = '.';
222 temp2[22] = '.';
223 }
224 g_free(temp);
225 temp = temp2;
226 }
227 }
228 else if (strncmp("%track%", p, n) == 0)
229 temp = song_tag_locale(song, MPD_TAG_TRACK);
230 else if (strncmp("%name%", p, n) == 0)
231 temp = song_tag_locale(song, MPD_TAG_NAME);
232 else if (strncmp("%date%", p, n) == 0)
233 temp = song_tag_locale(song, MPD_TAG_DATE);
234 else if (strncmp("%genre%", p, n) == 0)
235 temp = song_tag_locale(song, MPD_TAG_GENRE);
236 else if (strncmp("%shortfile%", p, n) == 0) {
237 const char *uri = mpd_song_get_uri(song);
238 if (strstr(uri, "://") != NULL)
239 temp = utf8_to_locale(uri);
240 else
241 temp = utf8_to_locale(g_basename(uri));
242 } else if (strncmp("%time%", p, n) == 0) {
243 unsigned duration = mpd_song_get_duration(song);
245 if (duration > 0) {
246 char buffer[32];
247 format_duration_short(buffer, sizeof(buffer),
248 duration);
249 temp = g_strdup(buffer);
250 }
251 }
253 if( temp == NULL) {
254 gsize templen=n;
255 /* just pass-through any unknown specifiers (including esc) */
256 if( length+templen > max )
257 templen = max-length;
258 gchar *ident = g_strndup(p, templen);
259 g_strlcat(s, ident, max);
260 length+=templen;
261 g_free(ident);
263 missed = TRUE;
264 } else {
265 gsize templen = strlen(temp);
267 found = TRUE;
268 if( length+templen > max )
269 templen = max-length;
270 g_strlcat(s, temp, max);
271 length+=templen;
272 g_free(temp);
273 }
275 /* advance past the specifier */
276 p += n;
277 }
279 if(last) *last = p;
281 return length;
282 }
284 gsize
285 strfsong(gchar *s, gsize max, const gchar *format,
286 const struct mpd_song *song)
287 {
288 return _strfsong(s, max, format, song, NULL);
289 }