direct3d9: rename dxva2_deinterlace to direct3d9_deinterlace
[vlc.git] / src / text / strings.c
blobec68de3fc2d8c51e6f23dbbcd531618c1a0de4c4
1 /*****************************************************************************
2 * strings.c: String related functions
3 *****************************************************************************
4 * Copyright (C) 2006 VLC authors and VideoLAN
5 * Copyright (C) 2008-2009 Rémi Denis-Courmont
6 * $Id$
8 * Authors: Antoine Cellerier <dionoea at videolan dot org>
9 * Daniel Stranger <vlc at schmaller dot de>
10 * Rémi Denis-Courmont <rem # videolan org>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
28 * Preamble
29 *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include <vlc_common.h>
35 #include <assert.h>
37 /* Needed by vlc_strftime */
38 #include <time.h>
39 #include <limits.h>
40 #include <math.h>
42 /* Needed by vlc_strfinput */
43 #include <vlc_input.h>
44 #include <vlc_meta.h>
45 #include <vlc_aout.h>
46 #include <vlc_memstream.h>
48 #include <vlc_strings.h>
49 #include <vlc_charset.h>
50 #include <vlc_arrays.h>
51 #include <libvlc.h>
52 #include <errno.h>
54 static const struct xml_entity_s
56 char psz_entity[8];
57 char psz_char[4];
58 } xml_entities[] = {
59 /* Important: this list has to be in alphabetical order (psz_entity-wise) */
60 { "AElig;", "Æ" },
61 { "Aacute;", "Á" },
62 { "Acirc;", "Â" },
63 { "Agrave;", "À" },
64 { "Aring;", "Å" },
65 { "Atilde;", "Ã" },
66 { "Auml;", "Ä" },
67 { "Ccedil;", "Ç" },
68 { "Dagger;", "‡" },
69 { "ETH;", "Ð" },
70 { "Eacute;", "É" },
71 { "Ecirc;", "Ê" },
72 { "Egrave;", "È" },
73 { "Euml;", "Ë" },
74 { "Iacute;", "Í" },
75 { "Icirc;", "Î" },
76 { "Igrave;", "Ì" },
77 { "Iuml;", "Ï" },
78 { "Ntilde;", "Ñ" },
79 { "OElig;", "Œ" },
80 { "Oacute;", "Ó" },
81 { "Ocirc;", "Ô" },
82 { "Ograve;", "Ò" },
83 { "Oslash;", "Ø" },
84 { "Otilde;", "Õ" },
85 { "Ouml;", "Ö" },
86 { "Scaron;", "Š" },
87 { "THORN;", "Þ" },
88 { "Uacute;", "Ú" },
89 { "Ucirc;", "Û" },
90 { "Ugrave;", "Ù" },
91 { "Uuml;", "Ü" },
92 { "Yacute;", "Ý" },
93 { "Yuml;", "Ÿ" },
94 { "aacute;", "á" },
95 { "acirc;", "â" },
96 { "acute;", "´" },
97 { "aelig;", "æ" },
98 { "agrave;", "à" },
99 { "amp;", "&" },
100 { "apos;", "'" },
101 { "aring;", "å" },
102 { "atilde;", "ã" },
103 { "auml;", "ä" },
104 { "bdquo;", "„" },
105 { "brvbar;", "¦" },
106 { "ccedil;", "ç" },
107 { "cedil;", "¸" },
108 { "cent;", "¢" },
109 { "circ;", "ˆ" },
110 { "copy;", "©" },
111 { "curren;", "¤" },
112 { "dagger;", "†" },
113 { "deg;", "°" },
114 { "divide;", "÷" },
115 { "eacute;", "é" },
116 { "ecirc;", "ê" },
117 { "egrave;", "è" },
118 { "eth;", "ð" },
119 { "euml;", "ë" },
120 { "euro;", "€" },
121 { "frac12;", "½" },
122 { "frac14;", "¼" },
123 { "frac34;", "¾" },
124 { "gt;", ">" },
125 { "hellip;", "…" },
126 { "iacute;", "í" },
127 { "icirc;", "î" },
128 { "iexcl;", "¡" },
129 { "igrave;", "ì" },
130 { "iquest;", "¿" },
131 { "iuml;", "ï" },
132 { "laquo;", "«" },
133 { "ldquo;", "“" },
134 { "lsaquo;", "‹" },
135 { "lsquo;", "‘" },
136 { "lt;", "<" },
137 { "macr;", "¯" },
138 { "mdash;", "—" },
139 { "micro;", "µ" },
140 { "middot;", "·" },
141 { "nbsp;", "\xc2\xa0" },
142 { "ndash;", "–" },
143 { "not;", "¬" },
144 { "ntilde;", "ñ" },
145 { "oacute;", "ó" },
146 { "ocirc;", "ô" },
147 { "oelig;", "œ" },
148 { "ograve;", "ò" },
149 { "ordf;", "ª" },
150 { "ordm;", "º" },
151 { "oslash;", "ø" },
152 { "otilde;", "õ" },
153 { "ouml;", "ö" },
154 { "para;", "¶" },
155 { "permil;", "‰" },
156 { "plusmn;", "±" },
157 { "pound;", "£" },
158 { "quot;", "\"" },
159 { "raquo;", "»" },
160 { "rdquo;", "”" },
161 { "reg;", "®" },
162 { "rsaquo;", "›" },
163 { "rsquo;", "’" },
164 { "sbquo;", "‚" },
165 { "scaron;", "š" },
166 { "sect;", "§" },
167 { "shy;", "­" },
168 { "sup1;", "¹" },
169 { "sup2;", "²" },
170 { "sup3;", "³" },
171 { "szlig;", "ß" },
172 { "thorn;", "þ" },
173 { "tilde;", "˜" },
174 { "times;", "×" },
175 { "trade;", "™" },
176 { "uacute;", "ú" },
177 { "ucirc;", "û" },
178 { "ugrave;", "ù" },
179 { "uml;", "¨" },
180 { "uuml;", "ü" },
181 { "yacute;", "ý" },
182 { "yen;", "¥" },
183 { "yuml;", "ÿ" },
186 static int cmp_entity (const void *key, const void *elem)
188 const struct xml_entity_s *ent = elem;
189 const char *name = key;
191 return strncmp (name, ent->psz_entity, strlen (ent->psz_entity));
194 void vlc_xml_decode( char *psz_value )
196 char *p_pos = psz_value;
198 while ( *psz_value )
200 if( *psz_value == '&' )
202 if( psz_value[1] == '#' )
203 { /* &#DDD; or &#xHHHH; Unicode code point */
204 char *psz_end;
205 unsigned long cp;
207 if( psz_value[2] == 'x' ) /* The x must be lower-case. */
208 cp = strtoul( psz_value + 3, &psz_end, 16 );
209 else
210 cp = strtoul( psz_value + 2, &psz_end, 10 );
212 if( *psz_end == ';' )
214 psz_value = psz_end + 1;
215 if( cp == 0 )
216 (void)0; /* skip nulls */
217 else
218 if( cp <= 0x7F )
220 *p_pos = cp;
222 else
223 /* Unicode code point outside ASCII.
224 * &#xxx; representation is longer than UTF-8 :) */
225 if( cp <= 0x7FF )
227 *p_pos++ = 0xC0 | (cp >> 6);
228 *p_pos = 0x80 | (cp & 0x3F);
230 else
231 if( cp <= 0xFFFF )
233 *p_pos++ = 0xE0 | (cp >> 12);
234 *p_pos++ = 0x80 | ((cp >> 6) & 0x3F);
235 *p_pos = 0x80 | (cp & 0x3F);
237 else
238 if( cp <= 0x1FFFFF ) /* Outside the BMP */
239 { /* Unicode stops at 10FFFF, but who cares? */
240 *p_pos++ = 0xF0 | (cp >> 18);
241 *p_pos++ = 0x80 | ((cp >> 12) & 0x3F);
242 *p_pos++ = 0x80 | ((cp >> 6) & 0x3F);
243 *p_pos = 0x80 | (cp & 0x3F);
246 else
248 /* Invalid entity number */
249 *p_pos = *psz_value;
250 psz_value++;
253 else
254 { /* Well-known XML entity */
255 const struct xml_entity_s *ent;
257 ent = bsearch (psz_value + 1, xml_entities,
258 ARRAY_SIZE (xml_entities),
259 sizeof (*ent), cmp_entity);
260 if (ent != NULL)
262 size_t olen = strlen (ent->psz_char);
263 memcpy (p_pos, ent->psz_char, olen);
264 p_pos += olen - 1;
265 psz_value += strlen (ent->psz_entity) + 1;
267 else
268 { /* No match */
269 *p_pos = *psz_value;
270 psz_value++;
274 else
276 *p_pos = *psz_value;
277 psz_value++;
280 p_pos++;
283 *p_pos = '\0';
286 char *vlc_xml_encode (const char *str)
288 struct vlc_memstream stream;
289 size_t n;
290 uint32_t cp;
292 assert(str != NULL);
293 vlc_memstream_open(&stream);
295 while ((n = vlc_towc (str, &cp)) != 0)
297 if (unlikely(n == (size_t)-1))
299 if (vlc_memstream_close(&stream) == 0)
300 free(stream.ptr);
301 errno = EILSEQ;
302 return NULL;
305 switch (cp)
307 case '\"':
308 vlc_memstream_puts(&stream, "&quot;");
309 break;
310 case '&':
311 vlc_memstream_puts(&stream, "&amp;");
312 break;
313 case '\'':
314 vlc_memstream_puts(&stream, "&#39;");
315 break;
316 case '<':
317 vlc_memstream_puts(&stream, "&lt;");
318 break;
319 case '>':
320 vlc_memstream_puts(&stream, "&gt;");
321 break;
322 default:
323 if (cp < 32) /* C0 code not allowed (except 9, 10 and 13) */
324 break;
325 if (cp >= 128 && cp < 160) /* C1 code encoded (except 133) */
327 vlc_memstream_printf(&stream, "&#%"PRIu32";", cp);
328 break;
330 /* fall through */
331 case 9:
332 case 10:
333 case 13:
334 case 133:
335 vlc_memstream_write(&stream, str, n);
336 break;
338 str += n;
341 if (vlc_memstream_close(&stream))
342 return NULL;
343 return stream.ptr;
346 /* Base64 encoding */
347 char *vlc_b64_encode_binary( const uint8_t *src, size_t i_src )
349 static const char b64[] =
350 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
352 char *ret = malloc( ( i_src + 4 ) * 4 / 3 );
353 char *dst = ret;
355 if( dst == NULL )
356 return NULL;
358 while( i_src > 0 )
360 /* pops (up to) 3 bytes of input, push 4 bytes */
361 uint32_t v;
363 /* 1/3 -> 1/4 */
364 v = ((unsigned)*src++) << 24;
365 *dst++ = b64[v >> 26];
366 v = v << 6;
368 /* 2/3 -> 2/4 */
369 if( i_src >= 2 )
370 v |= *src++ << 22;
371 *dst++ = b64[v >> 26];
372 v = v << 6;
374 /* 3/3 -> 3/4 */
375 if( i_src >= 3 )
376 v |= *src++ << 20; // 3/3
377 *dst++ = ( i_src >= 2 ) ? b64[v >> 26] : '='; // 3/4
378 v = v << 6;
380 /* -> 4/4 */
381 *dst++ = ( i_src >= 3 ) ? b64[v >> 26] : '='; // 4/4
383 if( i_src <= 3 )
384 break;
385 i_src -= 3;
388 *dst = '\0';
390 return ret;
393 char *vlc_b64_encode( const char *src )
395 if( src )
396 return vlc_b64_encode_binary( (const uint8_t*)src, strlen(src) );
397 else
398 return vlc_b64_encode_binary( (const uint8_t*)"", 0 );
401 /* Base64 decoding */
402 size_t vlc_b64_decode_binary_to_buffer( uint8_t *p_dst, size_t i_dst, const char *p_src )
404 static const int b64[256] = {
405 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
406 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
407 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
408 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
409 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
410 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
411 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
412 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
413 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
414 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
415 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
416 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
417 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
418 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
419 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
420 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
422 uint8_t *p_start = p_dst;
423 uint8_t *p = (uint8_t *)p_src;
425 int i_level;
426 int i_last;
428 for( i_level = 0, i_last = 0; (size_t)( p_dst - p_start ) < i_dst && *p != '\0'; p++ )
430 const int c = b64[(unsigned int)*p];
431 if( c == -1 )
432 break;
434 switch( i_level )
436 case 0:
437 i_level++;
438 break;
439 case 1:
440 *p_dst++ = ( i_last << 2 ) | ( ( c >> 4)&0x03 );
441 i_level++;
442 break;
443 case 2:
444 *p_dst++ = ( ( i_last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
445 i_level++;
446 break;
447 case 3:
448 *p_dst++ = ( ( i_last &0x03 ) << 6 ) | c;
449 i_level = 0;
451 i_last = c;
454 return p_dst - p_start;
456 size_t vlc_b64_decode_binary( uint8_t **pp_dst, const char *psz_src )
458 const int i_src = strlen( psz_src );
459 uint8_t *p_dst;
461 *pp_dst = p_dst = malloc( i_src );
462 if( !p_dst )
463 return 0;
464 return vlc_b64_decode_binary_to_buffer( p_dst, i_src, psz_src );
466 char *vlc_b64_decode( const char *psz_src )
468 const int i_src = strlen( psz_src );
469 char *p_dst = malloc( i_src + 1 );
470 size_t i_dst;
471 if( !p_dst )
472 return NULL;
474 i_dst = vlc_b64_decode_binary_to_buffer( (uint8_t*)p_dst, i_src, psz_src );
475 p_dst[i_dst] = '\0';
477 return p_dst;
480 char *vlc_strftime( const char *tformat )
482 time_t curtime;
483 struct tm loctime;
485 if (strcmp (tformat, "") == 0)
486 return strdup (""); /* corner case w.r.t. strftime() return value */
488 /* Get the current time. */
489 time( &curtime );
491 /* Convert it to local time representation. */
492 localtime_r( &curtime, &loctime );
493 for (size_t buflen = strlen (tformat) + 32;; buflen += 32)
495 char *str = malloc (buflen);
496 if (str == NULL)
497 return NULL;
499 size_t len = strftime (str, buflen, tformat, &loctime);
500 if (len > 0)
502 char *ret = realloc (str, len + 1);
503 return ret ? ret : str; /* <- this cannot fail */
505 free (str);
507 vlc_assert_unreachable ();
510 static void write_duration(struct vlc_memstream *stream, int64_t duration)
512 lldiv_t d;
513 long long sec;
515 duration /= CLOCK_FREQ;
516 d = lldiv(duration, 60);
517 sec = d.rem;
518 d = lldiv(d.quot, 60);
519 vlc_memstream_printf(stream, "%02lld:%02lld:%02lld", d.quot, d.rem, sec);
522 static int write_meta(struct vlc_memstream *stream, input_item_t *item,
523 vlc_meta_type_t type)
525 if (item == NULL)
526 return EOF;
528 char *value = input_item_GetMeta(item, type);
529 if (value == NULL)
530 return EOF;
532 vlc_memstream_puts(stream, value);
533 free(value);
534 return 0;
537 char *vlc_strfinput(input_thread_t *input, const char *s)
539 struct vlc_memstream stream[1];
541 input_item_t *item = (input != NULL) ? input_GetItem(input) : NULL;
543 char c;
544 bool b_is_format = false;
545 bool b_empty_if_na = false;
547 assert(s != NULL);
549 vlc_memstream_open(stream);
551 while ((c = *s) != '\0')
553 s++;
555 if (!b_is_format)
557 if (c == '$')
559 b_is_format = true;
560 b_empty_if_na = false;
561 continue;
564 vlc_memstream_putc(stream, c);
565 continue;
568 b_is_format = false;
570 switch (c)
572 case 'a':
573 write_meta(stream, item, vlc_meta_Artist);
574 break;
575 case 'b':
576 write_meta(stream, item, vlc_meta_Album);
577 break;
578 case 'c':
579 write_meta(stream, item, vlc_meta_Copyright);
580 break;
581 case 'd':
582 write_meta(stream, item, vlc_meta_Description);
583 break;
584 case 'e':
585 write_meta(stream, item, vlc_meta_EncodedBy);
586 break;
587 case 'f':
588 if (item != NULL && item->p_stats != NULL)
590 vlc_mutex_lock(&item->p_stats->lock);
591 vlc_memstream_printf(stream, "%"PRIi64,
592 item->p_stats->i_displayed_pictures);
593 vlc_mutex_unlock(&item->p_stats->lock);
595 else if (!b_empty_if_na)
596 vlc_memstream_putc(stream, '-');
597 break;
598 case 'g':
599 write_meta(stream, item, vlc_meta_Genre);
600 break;
601 case 'l':
602 write_meta(stream, item, vlc_meta_Language);
603 break;
604 case 'n':
605 write_meta(stream, item, vlc_meta_TrackNumber);
606 break;
607 case 'o':
608 write_meta(stream, item, vlc_meta_TrackTotal);
609 break;
610 case 'p':
611 if (item == NULL)
612 break;
614 char *value = input_item_GetNowPlayingFb(item);
615 if (value == NULL)
616 break;
618 vlc_memstream_puts(stream, value);
619 free(value);
621 break;
622 case 'r':
623 write_meta(stream, item, vlc_meta_Rating);
624 break;
625 case 's':
627 char *lang = NULL;
629 if (input != NULL)
630 lang = var_GetNonEmptyString(input, "sub-language");
631 if (lang != NULL)
633 vlc_memstream_puts(stream, lang);
634 free(lang);
636 else if (!b_empty_if_na)
637 vlc_memstream_putc(stream, '-');
638 break;
640 case 't':
641 write_meta(stream, item, vlc_meta_Title);
642 break;
643 case 'u':
644 write_meta(stream, item, vlc_meta_URL);
645 break;
646 case 'A':
647 write_meta(stream, item, vlc_meta_Date);
648 break;
649 case 'B':
650 if (input != NULL)
651 vlc_memstream_printf(stream, "%"PRId64,
652 var_GetInteger(input, "bit-rate") / 1000);
653 else if (!b_empty_if_na)
654 vlc_memstream_putc(stream, '-');
655 break;
656 case 'C':
657 if (input != NULL)
658 vlc_memstream_printf(stream, "%"PRId64,
659 var_GetInteger(input, "chapter"));
660 else if (!b_empty_if_na)
661 vlc_memstream_putc(stream, '-');
662 break;
663 case 'D':
664 if (item != NULL)
665 write_duration(stream, input_item_GetDuration(item));
666 else if (!b_empty_if_na)
667 vlc_memstream_puts(stream, "--:--:--");
668 break;
669 case 'F':
670 if (item != NULL)
672 char *uri = input_item_GetURI(item);
673 if (uri != NULL)
675 vlc_memstream_puts(stream, uri);
676 free(uri);
679 break;
680 case 'I':
681 if (input != NULL)
682 vlc_memstream_printf(stream, "%"PRId64,
683 var_GetInteger(input, "title"));
684 else if (!b_empty_if_na)
685 vlc_memstream_putc(stream, '-');
686 break;
687 case 'L':
688 if (item != NULL)
690 assert(input != NULL);
691 write_duration(stream, input_item_GetDuration(item)
692 - var_GetInteger(input, "time"));
694 else if (!b_empty_if_na)
695 vlc_memstream_puts(stream, "--:--:--");
696 break;
697 case 'N':
698 if (item != NULL)
700 char *name = input_item_GetName(item);
701 if (name != NULL)
703 vlc_memstream_puts(stream, name);
704 free(name);
707 break;
708 case 'O':
710 char *lang = NULL;
712 if (input != NULL)
713 lang = var_GetNonEmptyString(input, "audio-language");
714 if (lang != NULL)
716 vlc_memstream_puts(stream, lang);
717 free(lang);
719 else if (!b_empty_if_na)
720 vlc_memstream_putc(stream, '-');
721 break;
723 case 'P':
724 if (input != NULL)
725 vlc_memstream_printf(stream, "%2.1f",
726 var_GetFloat(input, "position") * 100.f);
727 else if (!b_empty_if_na)
728 vlc_memstream_puts(stream, "--.-%");
729 break;
730 case 'R':
731 if (input != NULL)
732 vlc_memstream_printf(stream, "%.3f",
733 var_GetFloat(input, "rate"));
734 else if (!b_empty_if_na)
735 vlc_memstream_putc(stream, '-');
736 break;
737 case 'S':
738 if (input != NULL)
740 int rate = var_GetInteger(input, "sample-rate");
741 div_t dr = div((rate + 50) / 100, 10);
743 vlc_memstream_printf(stream, "%d.%01d", dr.quot, dr.rem);
745 else if (!b_empty_if_na)
746 vlc_memstream_putc(stream, '-');
747 break;
748 case 'T':
749 if (input != NULL)
750 write_duration(stream, var_GetInteger(input, "time"));
751 else if (!b_empty_if_na)
752 vlc_memstream_puts(stream, "--:--:--");
753 break;
754 case 'U':
755 write_meta(stream, item, vlc_meta_Publisher);
756 break;
757 case 'V':
759 float vol = 0.f;
761 if (input != NULL)
763 audio_output_t *aout = input_GetAout(input);
764 if (aout != NULL)
766 vol = aout_VolumeGet(aout);
767 vlc_object_release(aout);
770 if (vol >= 0.f)
771 vlc_memstream_printf(stream, "%ld", lroundf(vol * 256.f));
772 else if (!b_empty_if_na)
773 vlc_memstream_puts(stream, "---");
774 break;
776 case '_':
777 vlc_memstream_putc(stream, '\n');
778 break;
779 case 'Z':
780 if (item == NULL)
781 break;
783 char *value = input_item_GetNowPlayingFb(item);
784 if (value != NULL)
786 vlc_memstream_puts(stream, value);
787 free(value);
789 else
791 char *title = input_item_GetTitleFbName(item);
793 if (write_meta(stream, item, vlc_meta_Artist) >= 0
794 && title != NULL)
795 vlc_memstream_puts(stream, " - ");
797 if (title != NULL)
799 vlc_memstream_puts(stream, title);
800 free(title);
804 break;
805 case ' ':
806 b_empty_if_na = true;
807 b_is_format = true;
808 break;
809 default:
810 vlc_memstream_putc(stream, c);
811 break;
815 if (vlc_memstream_close(stream))
816 return NULL;
817 return stream->ptr;
821 * Sanitize a file name.
823 * Remove forbidden, potentially forbidden and otherwise evil characters from
824 * file names. That includes slashes, and popular characters like colon
825 * (on Unix anyway).
827 * \warning This function should only be used for automatically generated
828 * file names. Do not use this on full paths, only single file names without
829 * any directory separator!
831 void filename_sanitize( char *str )
833 unsigned char c;
835 /* Special file names, not allowed */
836 if( !strcmp( str, "." ) || !strcmp( str, ".." ) )
838 while( *str )
839 *(str++) = '_';
840 return;
843 /* On platforms not using UTF-8, VLC cannot access non-Unicode paths.
844 * Also, some file systems require Unicode file names.
845 * NOTE: This may inserts '?' thus is done replacing '?' with '_'. */
846 EnsureUTF8( str );
848 /* Avoid leading spaces to please Windows. */
849 while( (c = *str) != '\0' )
851 if( c != ' ' )
852 break;
853 *(str++) = '_';
856 char *start = str;
858 while( (c = *str) != '\0' )
860 /* Non-printable characters are not a good idea */
861 if( c < 32 )
862 *str = '_';
863 /* This is the list of characters not allowed by Microsoft.
864 * We also black-list them on Unix as they may be confusing, and are
865 * not supported by some file system types (notably CIFS). */
866 else if( strchr( "/:\\*\"?|<>", c ) != NULL )
867 *str = '_';
868 str++;
871 /* Avoid trailing spaces also to please Windows. */
872 while( str > start )
874 if( *(--str) != ' ' )
875 break;
876 *str = '_';