Code

configure.ac: require libmpdclient 2.3
[ncmpc.git] / src / strfsong.c
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;
110 static gsize
111 _strfsong(gchar *s,
112           gsize max,
113           const gchar *format,
114           const struct mpd_song *song,
115           const gchar **last)
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                         if (temp == NULL) {
212                                 temp = song_tag_locale(song, MPD_TAG_PERFORMER);
213                                 if (temp == NULL)
214                                         temp = song_tag_locale(song, MPD_TAG_COMPOSER);
215                         }
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);
224                         if (temp == NULL)
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);
230                         if (temp) {
231                                 gchar *temp2 = g_strndup(temp, 25);
232                                 if (strlen(temp) > 25) {
233                                         temp2[24] = '.';
234                                         temp2[23] = '.';
235                                         temp2[22] = '.';
236                                 }
237                                 g_free(temp);
238                                 temp = temp2;
239                         }
240                 }
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);
253                         else
254                                 temp = utf8_to_locale(g_basename(uri));
255                 } else if (strncmp("%time%", p, n) == 0) {
256                         unsigned duration = mpd_song_get_duration(song);
258                         if (duration > 0)  {
259                                 char buffer[32];
260                                 format_duration_short(buffer, sizeof(buffer),
261                                                       duration);
262                                 temp = g_strdup(buffer);
263                         }
264                 }
266                 if( temp == NULL) {
267                         gsize templen=n;
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);
273                         length+=templen;
274                         g_free(ident);
276                         missed = TRUE;
277                 } else {
278                         gsize templen = strlen(temp);
280                         found = TRUE;
281                         if( length+templen > max )
282                                 templen = max-length;
283                         g_strlcat(s, temp, max);
284                         length+=templen;
285                         g_free(temp);
286                 }
288                 /* advance past the specifier */
289                 p += n;
290         }
292         if(last) *last = p;
294         return length;
297 gsize
298 strfsong(gchar *s, gsize max, const gchar *format,
299          const struct mpd_song *song)
301         return _strfsong(s, max, format, song, NULL);