po: update Ukrainian translation
[ncmpc.git] / src / strfsong.c
blobac0e0490986ccf1740ab6a248cc0abbb9724c67f
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.
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)
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;
46 ++p;
49 return p;
52 #ifndef NCMPC_MINI
54 static char *
55 concat_tag_values(const char *a, const char *b)
57 return g_strconcat(a, ", ", b, NULL);
60 static char *
61 song_more_tag_values(const struct mpd_song *song, enum mpd_tag_type tag,
62 const char *first)
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);
78 return buffer;
81 #endif /* !NCMPC_MINI */
83 static char *
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);
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;
122 memset(s, 0, max);
123 if (song == NULL)
124 return 0;
126 for (p = format; *p != '\0' && length<max;) {
127 /* OR */
128 if (p[0] == '|') {
129 ++p;
130 if(!found) {
131 memset(s, 0, max);
132 length = 0;
133 } else {
134 p = skip(p);
136 continue;
139 /* AND */
140 if (p[0] == '&') {
141 ++p;
142 if(!found) {
143 p = skip(p);
144 } else {
145 found = FALSE;
147 continue;
150 /* EXPRESSION START */
151 if (p[0] == '[') {
152 temp = g_malloc0(max);
153 if( _strfsong(temp, max, p+1, song, &p) >0 ) {
154 g_strlcat(s, temp, max);
155 length = strlen(s);
156 found = TRUE;
158 g_free(temp);
159 continue;
162 /* EXPRESSION END */
163 if (p[0] == ']') {
164 if(last) *last = p+1;
165 if(!found && length) {
166 memset(s, 0, max);
167 length = 0;
169 return length;
172 /* pass-through non-escaped portions of the format string */
173 if (p[0] != '#' && p[0] != '%' && length<max) {
174 s[length++] = *p;
175 p++;
176 continue;
179 /* let the escape character escape itself */
180 if (p[0] == '#' && p[1] != '\0' && length<max) {
181 s[length++] = *(p+1);
182 p+=2;
183 continue;
186 /* advance past the esc character */
188 /* find the extent of this format specifier (stop at \0, ' ', or esc) */
189 temp = NULL;
190 end = p+1;
191 while(*end >= 'a' && *end <= 'z') {
192 end++;
194 n = end - p + 1;
195 if(*end != '%')
196 n--;
197 else if (strncmp("%file%", p, n) == 0)
198 temp = utf8_to_locale(mpd_song_get_uri(song));
199 else if (strncmp("%artist%", p, n) == 0)
200 temp = song_tag_locale(song, MPD_TAG_ARTIST);
201 else if (strncmp("%title%", p, n) == 0)
202 temp = song_tag_locale(song, MPD_TAG_TITLE);
203 else if (strncmp("%album%", p, n) == 0)
204 temp = song_tag_locale(song, MPD_TAG_ALBUM);
205 else if (strncmp("%shortalbum%", p, n) == 0) {
206 temp = song_tag_locale(song, MPD_TAG_ALBUM);
207 if (temp) {
208 gchar *temp2 = g_strndup(temp, 25);
209 if (strlen(temp) > 25) {
210 temp2[24] = '.';
211 temp2[23] = '.';
212 temp2[22] = '.';
214 g_free(temp);
215 temp = temp2;
218 else if (strncmp("%track%", p, n) == 0)
219 temp = song_tag_locale(song, MPD_TAG_TRACK);
220 else if (strncmp("%name%", p, n) == 0)
221 temp = song_tag_locale(song, MPD_TAG_NAME);
222 else if (strncmp("%date%", p, n) == 0)
223 temp = song_tag_locale(song, MPD_TAG_DATE);
224 else if (strncmp("%genre%", p, n) == 0)
225 temp = song_tag_locale(song, MPD_TAG_GENRE);
226 else if (strncmp("%shortfile%", p, n) == 0) {
227 const char *uri = mpd_song_get_uri(song);
228 if (strstr(uri, "://") != NULL)
229 temp = utf8_to_locale(uri);
230 else
231 temp = utf8_to_locale(g_basename(uri));
232 } else if (strncmp("%time%", p, n) == 0) {
233 unsigned duration = mpd_song_get_duration(song);
235 if (duration > 0) {
236 char buffer[32];
237 format_duration_short(buffer, sizeof(buffer),
238 duration);
239 temp = g_strdup(buffer);
243 if( temp == NULL) {
244 gsize templen=n;
245 /* just pass-through any unknown specifiers (including esc) */
246 /* drop a null char in so printf stops at the end of this specifier,
247 but put the real character back in (pseudo-const) */
248 if( length+templen > max )
249 templen = max-length;
250 g_strlcat(s, p,max);
251 length+=templen;
252 } else {
253 gsize templen = strlen(temp);
255 found = TRUE;
256 if( length+templen > max )
257 templen = max-length;
258 g_strlcat(s, temp, max);
259 length+=templen;
260 g_free(temp);
263 /* advance past the specifier */
264 p += n;
267 if(last) *last = p;
269 return length;
272 gsize
273 strfsong(gchar *s, gsize max, const gchar *format,
274 const struct mpd_song *song)
276 return _strfsong(s, max, format, song, NULL);