1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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.
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.
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.
24 #include <mpd/client.h>
36 if (*p
== '#' && p
[1] != '\0') {
37 /* skip escaped stuff */
40 if(*p
== ']') stack
--;
42 if(*p
== '&' || *p
== '|' || *p
== ']') {
55 concat_tag_values(const char *a
, const char *b
)
57 return g_strconcat(a
, ", ", b
, NULL
);
61 song_more_tag_values(const struct mpd_song
*song
, enum mpd_tag_type tag
,
64 const char *p
= mpd_song_get_tag(song
, tag
, 1);
68 char *buffer
= concat_tag_values(first
, p
);
69 for (unsigned i
= 2; (p
= mpd_song_get_tag(song
, tag
, i
)) != NULL
;
72 buffer
= concat_tag_values(buffer
, p
);
79 #endif /* !NCMPC_MINI */
82 song_tag_locale(const struct mpd_song
*song
, enum mpd_tag_type tag
)
84 const char *value
= mpd_song_get_tag(song
, tag
, 0);
89 char *all
= song_more_tag_values(song
, tag
, value
);
92 #endif /* !NCMPC_MINI */
94 char *result
= utf8_to_locale(value
);
98 #endif /* !NCMPC_MINI */
107 const struct mpd_song
*song
,
111 /* "missed" helps handling the case of mere literal text like
112 found==true instead of found==false. */
122 for (p
= format
; *p
!= '\0' && length
<max
;) {
126 if(missed
&& !found
) {
139 if(missed
&& !found
) {
148 /* EXPRESSION START */
150 char *temp
= g_malloc0(max
);
151 if( _strfsong(temp
, max
, p
+1, song
, &p
) >0 ) {
152 g_strlcat(s
, temp
, max
);
164 if(last
) *last
= p
+1;
165 if(missed
&& !found
&& length
) {
172 /* pass-through non-escaped portions of the format string */
173 if (p
[0] != '#' && p
[0] != '%' && length
<max
) {
180 /* let the escape character escape itself */
181 if (p
[0] == '#' && p
[1] != '\0' && length
<max
) {
182 s
[length
++] = *(p
+1);
188 /* advance past the esc character */
190 /* find the extent of this format specifier (stop at \0, ' ', or esc) */
192 const char *end
= p
+ 1;
193 while(*end
>= 'a' && *end
<= 'z') {
196 size_t n
= end
- p
+ 1;
199 else if (strncmp("%file%", p
, n
) == 0)
200 temp
= utf8_to_locale(mpd_song_get_uri(song
));
201 else if (strncmp("%artist%", p
, n
) == 0) {
202 temp
= song_tag_locale(song
, MPD_TAG_ARTIST
);
204 temp
= song_tag_locale(song
, MPD_TAG_PERFORMER
);
206 temp
= song_tag_locale(song
, MPD_TAG_COMPOSER
);
208 } else if (strncmp("%albumartist", p
, n
) == 0)
209 temp
= song_tag_locale(song
, MPD_TAG_ALBUM_ARTIST
);
210 else if (strncmp("%composer%", p
, n
) == 0)
211 temp
= song_tag_locale(song
, MPD_TAG_COMPOSER
);
212 else if (strncmp("%performer%", p
, n
) == 0)
213 temp
= song_tag_locale(song
, MPD_TAG_PERFORMER
);
214 else if (strncmp("%title%", p
, n
) == 0) {
215 temp
= song_tag_locale(song
, MPD_TAG_TITLE
);
217 temp
= song_tag_locale(song
, MPD_TAG_NAME
);
218 } else if (strncmp("%album%", p
, n
) == 0)
219 temp
= song_tag_locale(song
, MPD_TAG_ALBUM
);
220 else if (strncmp("%shortalbum%", p
, n
) == 0) {
221 temp
= song_tag_locale(song
, MPD_TAG_ALBUM
);
223 gchar
*temp2
= g_strndup(temp
, 25);
224 if (strlen(temp
) > 25) {
233 else if (strncmp("%track%", p
, n
) == 0)
234 temp
= song_tag_locale(song
, MPD_TAG_TRACK
);
235 else if (strncmp("%disc%", p
, n
) == 0)
236 temp
= song_tag_locale(song
, MPD_TAG_DISC
);
237 else if (strncmp("%name%", p
, n
) == 0)
238 temp
= song_tag_locale(song
, MPD_TAG_NAME
);
239 else if (strncmp("%date%", p
, n
) == 0)
240 temp
= song_tag_locale(song
, MPD_TAG_DATE
);
241 else if (strncmp("%genre%", p
, n
) == 0)
242 temp
= song_tag_locale(song
, MPD_TAG_GENRE
);
243 else if (strncmp("%shortfile%", p
, n
) == 0) {
244 const char *uri
= mpd_song_get_uri(song
);
245 if (strstr(uri
, "://") != NULL
)
246 temp
= utf8_to_locale(uri
);
248 temp
= utf8_to_locale(g_basename(uri
));
249 } else if (strncmp("%time%", p
, n
) == 0) {
250 unsigned duration
= mpd_song_get_duration(song
);
254 format_duration_short(buffer
, sizeof(buffer
),
256 temp
= g_strdup(buffer
);
262 /* just pass-through any unknown specifiers (including esc) */
263 if( length
+templen
> max
)
264 templen
= max
-length
;
265 gchar
*ident
= g_strndup(p
, templen
);
266 g_strlcat(s
, ident
, max
);
272 gsize templen
= strlen(temp
);
275 if( length
+templen
> max
)
276 templen
= max
-length
;
277 g_strlcat(s
, temp
, max
);
282 /* advance past the specifier */
292 strfsong(gchar
*s
, gsize max
, const gchar
*format
,
293 const struct mpd_song
*song
)
295 return _strfsong(s
, max
, format
, song
, NULL
);