1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 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.
18 */
20 #include "strfsong.h"
21 #include "charset.h"
23 #include <mpd/client.h>
25 #include <string.h>
27 static const gchar *
28 skip(const gchar * p)
29 {
30 gint stack = 0;
32 while (*p != '\0') {
33 if (*p == '[')
34 stack++;
35 if (*p == '#' && p[1] != '\0') {
36 /* skip escaped stuff */
37 ++p;
38 } else if (stack) {
39 if(*p == ']') stack--;
40 } else {
41 if(*p == '&' || *p == '|' || *p == ']') {
42 break;
43 }
44 }
45 ++p;
46 }
48 return p;
49 }
51 static char *
52 song_tag_locale(const struct mpd_song *song, enum mpd_tag_type tag)
53 {
54 const char *value = mpd_song_get_tag(song, tag, 0);
55 if (value == NULL)
56 return NULL;
58 return utf8_to_locale(value);
59 }
61 static gsize
62 _strfsong(gchar *s,
63 gsize max,
64 const gchar *format,
65 const struct mpd_song *song,
66 const gchar **last)
67 {
68 const gchar *p, *end;
69 gchar *temp;
70 gsize n, length = 0;
71 gboolean found = FALSE;
73 memset(s, 0, max);
74 if (song == NULL)
75 return 0;
77 for (p = format; *p != '\0' && length<max;) {
78 /* OR */
79 if (p[0] == '|') {
80 ++p;
81 if(!found) {
82 memset(s, 0, max);
83 length = 0;
84 } else {
85 p = skip(p);
86 }
87 continue;
88 }
90 /* AND */
91 if (p[0] == '&') {
92 ++p;
93 if(!found) {
94 p = skip(p);
95 } else {
96 found = FALSE;
97 }
98 continue;
99 }
101 /* EXPRESSION START */
102 if (p[0] == '[') {
103 temp = g_malloc0(max);
104 if( _strfsong(temp, max, p+1, song, &p) >0 ) {
105 g_strlcat(s, temp, max);
106 length = strlen(s);
107 found = TRUE;
108 }
109 g_free(temp);
110 continue;
111 }
113 /* EXPRESSION END */
114 if (p[0] == ']') {
115 if(last) *last = p+1;
116 if(!found && length) {
117 memset(s, 0, max);
118 length = 0;
119 }
120 return length;
121 }
123 /* pass-through non-escaped portions of the format string */
124 if (p[0] != '#' && p[0] != '%' && length<max) {
125 s[length++] = *p;
126 p++;
127 continue;
128 }
130 /* let the escape character escape itself */
131 if (p[0] == '#' && p[1] != '\0' && length<max) {
132 s[length++] = *(p+1);
133 p+=2;
134 continue;
135 }
137 /* advance past the esc character */
139 /* find the extent of this format specifier (stop at \0, ' ', or esc) */
140 temp = NULL;
141 end = p+1;
142 while(*end >= 'a' && *end <= 'z') {
143 end++;
144 }
145 n = end - p + 1;
146 if(*end != '%')
147 n--;
148 else if (strncmp("%file%", p, n) == 0)
149 temp = utf8_to_locale(mpd_song_get_uri(song));
150 else if (strncmp("%artist%", p, n) == 0)
151 temp = song_tag_locale(song, MPD_TAG_ARTIST);
152 else if (strncmp("%title%", p, n) == 0)
153 temp = song_tag_locale(song, MPD_TAG_TITLE);
154 else if (strncmp("%album%", p, n) == 0)
155 temp = song_tag_locale(song, MPD_TAG_ALBUM);
156 else if (strncmp("%shortalbum%", p, n) == 0) {
157 temp = song_tag_locale(song, MPD_TAG_ALBUM);
158 if (temp) {
159 gchar *temp2 = g_strndup(temp, 25);
160 if (strlen(temp) > 25) {
161 temp2[24] = '.';
162 temp2[23] = '.';
163 temp2[22] = '.';
164 }
165 g_free(temp);
166 temp = temp2;
167 }
168 }
169 else if (strncmp("%track%", p, n) == 0)
170 temp = song_tag_locale(song, MPD_TAG_TRACK);
171 else if (strncmp("%name%", p, n) == 0)
172 temp = song_tag_locale(song, MPD_TAG_NAME);
173 else if (strncmp("%date%", p, n) == 0)
174 temp = song_tag_locale(song, MPD_TAG_DATE);
175 else if (strncmp("%genre%", p, n) == 0)
176 temp = song_tag_locale(song, MPD_TAG_GENRE);
177 else if (strncmp("%shortfile%", p, n) == 0) {
178 const char *uri = mpd_song_get_uri(song);
179 if (strstr(uri, "://") != NULL)
180 temp = utf8_to_locale(uri);
181 else
182 temp = utf8_to_locale(g_basename(uri));
183 } else if (strncmp("%time%", p, n) == 0) {
184 unsigned duration = mpd_song_get_duration(song);
186 if (duration > 0) {
187 if (duration > 3600) {
188 temp = g_strdup_printf("%d:%02d:%02d",
189 duration / 3600,
190 (duration % 3600) / 60,
191 duration % 60);
192 } else {
193 temp = g_strdup_printf("%d:%02d",
194 duration / 60,
195 duration % 60);
196 }
197 }
198 }
200 if( temp == NULL) {
201 gsize templen=n;
202 /* just pass-through any unknown specifiers (including esc) */
203 /* drop a null char in so printf stops at the end of this specifier,
204 but put the real character back in (pseudo-const) */
205 if( length+templen > max )
206 templen = max-length;
207 g_strlcat(s, p,max);
208 length+=templen;
209 } else {
210 gsize templen = strlen(temp);
212 found = TRUE;
213 if( length+templen > max )
214 templen = max-length;
215 g_strlcat(s, temp, max);
216 length+=templen;
217 g_free(temp);
218 }
220 /* advance past the specifier */
221 p += n;
222 }
224 if(last) *last = p;
226 return length;
227 }
229 gsize
230 strfsong(gchar *s, gsize max, const gchar *format,
231 const struct mpd_song *song)
232 {
233 return _strfsong(s, max, format, song, NULL);
234 }