Code

a146583516553754d14d4c9da03f6362de858db3
[ncmpc.git] / src / strfsong.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 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.
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.
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 static char *
53 song_tag_locale(const struct mpd_song *song, enum mpd_tag_type tag)
54 {
55         const char *value = mpd_song_get_tag(song, tag, 0);
56         if (value == NULL)
57                 return NULL;
59         return utf8_to_locale(value);
60 }
62 static gsize
63 _strfsong(gchar *s,
64           gsize max,
65           const gchar *format,
66           const struct mpd_song *song,
67           const gchar **last)
68 {
69         const gchar *p, *end;
70         gchar *temp;
71         gsize n, length = 0;
72         gboolean found = FALSE;
74         memset(s, 0, max);
75         if (song == NULL)
76                 return 0;
78         for (p = format; *p != '\0' && length<max;) {
79                 /* OR */
80                 if (p[0] == '|') {
81                         ++p;
82                         if(!found) {
83                                 memset(s, 0, max);
84                                 length = 0;
85                         } else {
86                                 p = skip(p);
87                         }
88                         continue;
89                 }
91                 /* AND */
92                 if (p[0] == '&') {
93                         ++p;
94                         if(!found) {
95                                 p = skip(p);
96                         } else {
97                                 found = FALSE;
98                         }
99                         continue;
100                 }
102                 /* EXPRESSION START */
103                 if (p[0] == '[') {
104                         temp = g_malloc0(max);
105                         if( _strfsong(temp, max, p+1, song, &p) >0 ) {
106                                 g_strlcat(s, temp, max);
107                                 length = strlen(s);
108                                 found = TRUE;
109                         }
110                         g_free(temp);
111                         continue;
112                 }
114                 /* EXPRESSION END */
115                 if (p[0] == ']') {
116                         if(last) *last = p+1;
117                         if(!found && length) {
118                                 memset(s, 0, max);
119                                 length = 0;
120                         }
121                         return length;
122                 }
124                 /* pass-through non-escaped portions of the format string */
125                 if (p[0] != '#' && p[0] != '%' && length<max) {
126                         s[length++] = *p;
127                         p++;
128                         continue;
129                 }
131                 /* let the escape character escape itself */
132                 if (p[0] == '#' && p[1] != '\0' && length<max) {
133                         s[length++] = *(p+1);
134                         p+=2;
135                         continue;
136                 }
138                 /* advance past the esc character */
140                 /* find the extent of this format specifier (stop at \0, ' ', or esc) */
141                 temp = NULL;
142                 end  = p+1;
143                 while(*end >= 'a' && *end <= 'z') {
144                         end++;
145                 }
146                 n = end - p + 1;
147                 if(*end != '%')
148                         n--;
149                 else if (strncmp("%file%", p, n) == 0)
150                         temp = utf8_to_locale(mpd_song_get_uri(song));
151                 else if (strncmp("%artist%", p, n) == 0)
152                         temp = song_tag_locale(song, MPD_TAG_ARTIST);
153                 else if (strncmp("%title%", p, n) == 0)
154                         temp = song_tag_locale(song, MPD_TAG_TITLE);
155                 else if (strncmp("%album%", p, n) == 0)
156                         temp = song_tag_locale(song, MPD_TAG_ALBUM);
157                 else if (strncmp("%shortalbum%", p, n) == 0) {
158                         temp = song_tag_locale(song, MPD_TAG_ALBUM);
159                         if (temp) {
160                                 gchar *temp2 = g_strndup(temp, 25);
161                                 if (strlen(temp) > 25) {
162                                         temp2[24] = '.';
163                                         temp2[23] = '.';
164                                         temp2[22] = '.';
165                                 }
166                                 g_free(temp);
167                                 temp = temp2;
168                         }
169                 }
170                 else if (strncmp("%track%", p, n) == 0)
171                         temp = song_tag_locale(song, MPD_TAG_TRACK);
172                 else if (strncmp("%name%", p, n) == 0)
173                         temp = song_tag_locale(song, MPD_TAG_NAME);
174                 else if (strncmp("%date%", p, n) == 0)
175                         temp = song_tag_locale(song, MPD_TAG_DATE);
176                 else if (strncmp("%genre%", p, n) == 0)
177                         temp = song_tag_locale(song, MPD_TAG_GENRE);
178                 else if (strncmp("%shortfile%", p, n) == 0) {
179                         const char *uri = mpd_song_get_uri(song);
180                         if (strstr(uri, "://") != NULL)
181                                 temp = utf8_to_locale(uri);
182                         else
183                                 temp = utf8_to_locale(g_basename(uri));
184                 } else if (strncmp("%time%", p, n) == 0) {
185                         unsigned duration = mpd_song_get_duration(song);
187                         if (duration > 0)  {
188                                 char buffer[32];
189                                 format_duration_short(buffer, sizeof(buffer),
190                                                       duration);
191                                 temp = g_strdup(buffer);
192                         }
193                 }
195                 if( temp == NULL) {
196                         gsize templen=n;
197                         /* just pass-through any unknown specifiers (including esc) */
198                         /* drop a null char in so printf stops at the end of this specifier,
199                            but put the real character back in (pseudo-const) */
200                         if( length+templen > max )
201                                 templen = max-length;
202                         g_strlcat(s, p,max);
203                         length+=templen;
204                 } else {
205                         gsize templen = strlen(temp);
207                         found = TRUE;
208                         if( length+templen > max )
209                                 templen = max-length;
210                         g_strlcat(s, temp, max);
211                         length+=templen;
212                         g_free(temp);
213                 }
215                 /* advance past the specifier */
216                 p += n;
217         }
219         if(last) *last = p;
221         return length;
224 gsize
225 strfsong(gchar *s, gsize max, const gchar *format,
226          const struct mpd_song *song)
228         return _strfsong(s, max, format, song, NULL);