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);
70 buffer
= concat_tag_values(first
, p
);
71 for (unsigned i
= 2; (p
= mpd_song_get_tag(song
, tag
, i
)) != NULL
;
74 buffer
= concat_tag_values(buffer
, p
);
81 #endif /* !NCMPC_MINI */
84 song_tag_locale(const struct mpd_song
*song
, enum mpd_tag_type tag
)
86 const char *value
= mpd_song_get_tag(song
, tag
, 0);
90 #endif /* !NCMPC_MINI */
96 all
= song_more_tag_values(song
, tag
, value
);
99 #endif /* !NCMPC_MINI */
101 result
= utf8_to_locale(value
);
105 #endif /* !NCMPC_MINI */
114 const struct mpd_song
*song
,
117 const gchar
*p
, *end
;
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
;
130 for (p
= format
; *p
!= '\0' && length
<max
;) {
134 if(missed
&& !found
) {
147 if(missed
&& !found
) {
156 /* EXPRESSION START */
158 temp
= g_malloc0(max
);
159 if( _strfsong(temp
, max
, p
+1, song
, &p
) >0 ) {
160 g_strlcat(s
, temp
, max
);
172 if(last
) *last
= p
+1;
173 if(missed
&& !found
&& length
) {
180 /* pass-through non-escaped portions of the format string */
181 if (p
[0] != '#' && p
[0] != '%' && length
<max
) {
188 /* let the escape character escape itself */
189 if (p
[0] == '#' && p
[1] != '\0' && length
<max
) {
190 s
[length
++] = *(p
+1);
196 /* advance past the esc character */
198 /* find the extent of this format specifier (stop at \0, ' ', or esc) */
201 while(*end
>= 'a' && *end
<= 'z') {
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
);
212 temp
= song_tag_locale(song
, MPD_TAG_PERFORMER
);
214 temp
= song_tag_locale(song
, MPD_TAG_COMPOSER
);
216 } else if (strncmp("%albumartist", p
, n
) == 0)
217 temp
= song_tag_locale(song
, MPD_TAG_ALBUM_ARTIST
);
218 else if (strncmp("%composer%", p
, n
) == 0)
219 temp
= song_tag_locale(song
, MPD_TAG_COMPOSER
);
220 else if (strncmp("%performer%", p
, n
) == 0)
221 temp
= song_tag_locale(song
, MPD_TAG_PERFORMER
);
222 else if (strncmp("%title%", p
, n
) == 0) {
223 temp
= song_tag_locale(song
, MPD_TAG_TITLE
);
225 temp
= song_tag_locale(song
, MPD_TAG_NAME
);
226 } else if (strncmp("%album%", p
, n
) == 0)
227 temp
= song_tag_locale(song
, MPD_TAG_ALBUM
);
228 else if (strncmp("%shortalbum%", p
, n
) == 0) {
229 temp
= song_tag_locale(song
, MPD_TAG_ALBUM
);
231 gchar
*temp2
= g_strndup(temp
, 25);
232 if (strlen(temp
) > 25) {
241 else if (strncmp("%track%", p
, n
) == 0)
242 temp
= song_tag_locale(song
, MPD_TAG_TRACK
);
243 else if (strncmp("%name%", p
, n
) == 0)
244 temp
= song_tag_locale(song
, MPD_TAG_NAME
);
245 else if (strncmp("%date%", p
, n
) == 0)
246 temp
= song_tag_locale(song
, MPD_TAG_DATE
);
247 else if (strncmp("%genre%", p
, n
) == 0)
248 temp
= song_tag_locale(song
, MPD_TAG_GENRE
);
249 else if (strncmp("%shortfile%", p
, n
) == 0) {
250 const char *uri
= mpd_song_get_uri(song
);
251 if (strstr(uri
, "://") != NULL
)
252 temp
= utf8_to_locale(uri
);
254 temp
= utf8_to_locale(g_basename(uri
));
255 } else if (strncmp("%time%", p
, n
) == 0) {
256 unsigned duration
= mpd_song_get_duration(song
);
260 format_duration_short(buffer
, sizeof(buffer
),
262 temp
= g_strdup(buffer
);
268 /* just pass-through any unknown specifiers (including esc) */
269 if( length
+templen
> max
)
270 templen
= max
-length
;
271 gchar
*ident
= g_strndup(p
, templen
);
272 g_strlcat(s
, ident
, max
);
278 gsize templen
= strlen(temp
);
281 if( length
+templen
> max
)
282 templen
= max
-length
;
283 g_strlcat(s
, temp
, max
);
288 /* advance past the specifier */
298 strfsong(gchar
*s
, gsize max
, const gchar
*format
,
299 const struct mpd_song
*song
)
301 return _strfsong(s
, max
, format
, song
, NULL
);