/*****************************************************************************
* Preamble
*****************************************************************************/
-#include <vlc/vlc.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
#include <assert.h>
/* Needed by str_format_time */
EnsureUTF8( psz );
}
-static inline int isurlsafe( int c )
+static inline bool isurisafe( int c )
{
+ /* These are the _unreserved_ URI characters (RFC3986 §2.3) */
return ( (unsigned char)( c - 'a' ) < 26 )
|| ( (unsigned char)( c - 'A' ) < 26 )
|| ( (unsigned char)( c - '0' ) < 10 )
- /* Hmm, we should not encode character that are allowed in URLs
- * (even if they are not URL-safe), nor URL-safe characters.
- * We still encode some of them because of Microsoft's crap browser.
- */
- || ( strchr( "-_.", c ) != NULL );
-}
-
-static inline char url_hexchar( int c )
-{
- return ( c < 10 ) ? c + '0' : c + 'A' - 10;
+ || ( strchr( "-._~", c ) != NULL );
}
/**
- * encode_URI_component
- * Encodes an URI component.
+ * Encodes an URI component (RFC3986 §2).
*
- * @param psz_url nul-terminated UTF-8 representation of the component.
+ * @param psz_uri nul-terminated UTF-8 representation of the component.
* Obviously, you can't pass an URI containing a nul character, but you don't
* want to do that, do you?
*
- * @return encoded string (must be free()'d)
+ * @return encoded string (must be free()'d), or NULL for ENOMEM.
*/
-char *encode_URI_component( const char *psz_url )
+char *encode_URI_component( const char *psz_uri )
{
- char psz_enc[3 * strlen( psz_url ) + 1], *out = psz_enc;
- const uint8_t *in;
+ char *psz_enc = malloc ((3 * strlen (psz_uri)) + 1), *out = psz_enc;
- for( in = (const uint8_t *)psz_url; *in; in++ )
+ if (psz_enc == NULL)
+ return NULL;
+
+ while (*psz_uri)
{
- uint8_t c = *in;
+ static const char hex[16] = "0123456789ABCDEF";
+ uint8_t c = *psz_uri;
- if( isurlsafe( c ) )
- *out++ = (char)c;
- else
- if ( c == ' ')
- *out++ = '+';
+ if( isurisafe( c ) )
+ *out++ = c;
+ /* This is URI encoding, not HTTP forms:
+ * Space is encoded as '%20', not '+'. */
else
{
*out++ = '%';
- *out++ = url_hexchar( c >> 4 );
- *out++ = url_hexchar( c & 0xf );
+ *out++ = hex[c >> 4];
+ *out++ = hex[c & 0xf];
}
+ psz_uri++;
}
*out++ = '\0';
- return strdup( psz_enc );
+ out = realloc (psz_enc, out - psz_enc);
+ return out ? out : psz_enc; /* realloc() can fail (safe) */
+}
+
+static const struct xml_entity_s
+{
+ char psz_entity[8];
+ char psz_char[4];
+} xml_entities[] = {
+ /* Important: this list has to be in alphabetical order (psz_entity-wise) */
+ { "AElig;", "Æ" },
+ { "Aacute;", "Á" },
+ { "Acirc;", "Â" },
+ { "Agrave;", "À" },
+ { "Aring;", "Å" },
+ { "Atilde;", "Ã" },
+ { "Auml;", "Ä" },
+ { "Ccedil;", "Ç" },
+ { "Dagger;", "‡" },
+ { "ETH;", "Ð" },
+ { "Eacute;", "É" },
+ { "Ecirc;", "Ê" },
+ { "Egrave;", "È" },
+ { "Euml;", "Ë" },
+ { "Iacute;", "Í" },
+ { "Icirc;", "Î" },
+ { "Igrave;", "Ì" },
+ { "Iuml;", "Ï" },
+ { "Ntilde;", "Ñ" },
+ { "OElig;", "Œ" },
+ { "Oacute;", "Ó" },
+ { "Ocirc;", "Ô" },
+ { "Ograve;", "Ò" },
+ { "Oslash;", "Ø" },
+ { "Otilde;", "Õ" },
+ { "Ouml;", "Ö" },
+ { "Scaron;", "Š" },
+ { "THORN;", "Þ" },
+ { "Uacute;", "Ú" },
+ { "Ucirc;", "Û" },
+ { "Ugrave;", "Ù" },
+ { "Uuml;", "Ü" },
+ { "Yacute;", "Ý" },
+ { "Yuml;", "Ÿ" },
+ { "aacute;", "á" },
+ { "acirc;", "â" },
+ { "acute;", "´" },
+ { "aelig;", "æ" },
+ { "agrave;", "à" },
+ { "amp;", "&" },
+ { "apos;", "'" },
+ { "aring;", "å" },
+ { "atilde;", "ã" },
+ { "auml;", "ä" },
+ { "bdquo;", "„" },
+ { "brvbar;", "¦" },
+ { "ccedil;", "ç" },
+ { "cedil;", "¸" },
+ { "cent;", "¢" },
+ { "circ;", "ˆ" },
+ { "copy;", "©" },
+ { "curren;", "¤" },
+ { "dagger;", "†" },
+ { "deg;", "°" },
+ { "divide;", "÷" },
+ { "eacute;", "é" },
+ { "ecirc;", "ê" },
+ { "egrave;", "è" },
+ { "eth;", "ð" },
+ { "euml;", "ë" },
+ { "euro;", "€" },
+ { "frac12;", "½" },
+ { "frac14;", "¼" },
+ { "frac34;", "¾" },
+ { "gt;", ">" },
+ { "hellip;", "…" },
+ { "iacute;", "í" },
+ { "icirc;", "î" },
+ { "iexcl;", "¡" },
+ { "igrave;", "ì" },
+ { "iquest;", "¿" },
+ { "iuml;", "ï" },
+ { "laquo;", "«" },
+ { "ldquo;", "“" },
+ { "lsaquo;", "‹" },
+ { "lsquo;", "‘" },
+ { "lt;", "<" },
+ { "macr;", "¯" },
+ { "mdash;", "—" },
+ { "micro;", "µ" },
+ { "middot;", "·" },
+ { "nbsp;", "\xc2\xa0" },
+ { "ndash;", "–" },
+ { "not;", "¬" },
+ { "ntilde;", "ñ" },
+ { "oacute;", "ó" },
+ { "ocirc;", "ô" },
+ { "oelig;", "œ" },
+ { "ograve;", "ò" },
+ { "ordf;", "ª" },
+ { "ordm;", "º" },
+ { "oslash;", "ø" },
+ { "otilde;", "õ" },
+ { "ouml;", "ö" },
+ { "para;", "¶" },
+ { "permil;", "‰" },
+ { "plusmn;", "±" },
+ { "pound;", "£" },
+ { "quot;", "\"" },
+ { "raquo;", "»" },
+ { "rdquo;", "”" },
+ { "reg;", "®" },
+ { "rsaquo;", "›" },
+ { "rsquo;", "’" },
+ { "sbquo;", "‚" },
+ { "scaron;", "š" },
+ { "sect;", "§" },
+ { "shy;", "" },
+ { "sup1;", "¹" },
+ { "sup2;", "²" },
+ { "sup3;", "³" },
+ { "szlig;", "ß" },
+ { "thorn;", "þ" },
+ { "tilde;", "˜" },
+ { "times;", "×" },
+ { "trade;", "™" },
+ { "uacute;", "ú" },
+ { "ucirc;", "û" },
+ { "ugrave;", "ù" },
+ { "uml;", "¨" },
+ { "uuml;", "ü" },
+ { "yacute;", "ý" },
+ { "yen;", "¥" },
+ { "yuml;", "ÿ" },
+};
+
+static int cmp_entity (const void *key, const void *elem)
+{
+ const struct xml_entity_s *ent = elem;
+ const char *name = key;
+
+ return strncmp (name, ent->psz_entity, strlen (ent->psz_entity));
}
/**
while ( *psz_value )
{
- if( !strncmp( psz_value, "<", 4 ) )
- {
- *p_pos = '<';
- psz_value += 4;
- }
- else if( !strncmp( psz_value, ">", 4 ) )
- {
- *p_pos = '>';
- psz_value += 4;
- }
- else if( !strncmp( psz_value, "&", 5 ) )
- {
- *p_pos = '&';
- psz_value += 5;
- }
- else if( !strncmp( psz_value, """, 6 ) )
- {
- *p_pos = '\"';
- psz_value += 6;
- }
- else if( !strncmp( psz_value, "'", 6 ) )
+ if( *psz_value == '&' )
{
- *p_pos = '\'';
- psz_value += 6;
+ if( psz_value[1] == '#' )
+ { /* &#xxx; Unicode code point */
+ char *psz_end;
+ unsigned long cp = strtoul( psz_value+2, &psz_end, 10 );
+ if( *psz_end == ';' )
+ {
+ psz_value = psz_end + 1;
+ if( cp == 0 )
+ (void)0; /* skip nuls */
+ else
+ if( cp <= 0x7F )
+ {
+ *p_pos = cp;
+ }
+ else
+ /* Unicode code point outside ASCII.
+ * &#xxx; representation is longer than UTF-8 :) */
+ if( cp <= 0x7FF )
+ {
+ *p_pos++ = 0xC0 | (cp >> 6);
+ *p_pos = 0x80 | (cp & 0x3F);
+ }
+ else
+ if( cp <= 0xFFFF )
+ {
+ *p_pos++ = 0xE0 | (cp >> 12);
+ *p_pos++ = 0x80 | ((cp >> 6) & 0x3F);
+ *p_pos = 0x80 | (cp & 0x3F);
+ }
+ else
+ if( cp <= 0x1FFFFF ) /* Outside the BMP */
+ { /* Unicode stops at 10FFFF, but who cares? */
+ *p_pos++ = 0xF0 | (cp >> 18);
+ *p_pos++ = 0x80 | ((cp >> 12) & 0x3F);
+ *p_pos++ = 0x80 | ((cp >> 6) & 0x3F);
+ *p_pos = 0x80 | (cp & 0x3F);
+ }
+ }
+ else
+ {
+ /* Invalid entity number */
+ *p_pos = *psz_value;
+ psz_value++;
+ }
+ }
+ else
+ { /* Well-known XML entity */
+ const struct xml_entity_s *ent;
+
+ ent = bsearch (psz_value + 1, xml_entities,
+ sizeof (xml_entities) / sizeof (*ent),
+ sizeof (*ent), cmp_entity);
+ if (ent != NULL)
+ {
+ size_t olen = strlen (ent->psz_char);
+ memcpy (p_pos, ent->psz_char, olen);
+ p_pos += olen - 1;
+ psz_value += strlen (ent->psz_entity) + 1;
+ }
+ else
+ { /* No match */
+ *p_pos = *psz_value;
+ psz_value++;
+ }
+ }
}
else
{
int i_level;
int i_last;
- for( i_level = 0, i_last = 0; i_dst > 0 && *p != '\0'; i_dst--, p++ )
+ for( i_level = 0, i_last = 0; (size_t)( p_dst - p_start ) < i_dst && *p != '\0'; p++ )
{
const int c = b64[(unsigned int)*p];
if( c == -1 )
return p_dst;
}
-/****************************************************************************
- * String formating functions
- ****************************************************************************/
+/**
+ * Formats current time into a heap-allocated string.
+ * @param tformat time format (as with C strftime())
+ * @return an allocated string (must be free()'d), or NULL on memory error.
+ */
char *str_format_time( const char *tformat )
{
- char buffer[255];
time_t curtime;
-#if defined(HAVE_LOCALTIME_R)
struct tm loctime;
-#else
- struct tm *loctime;
-#endif
+
+ if (strcmp (tformat, "") == 0)
+ return strdup (""); /* corner case w.r.t. strftime() return value */
/* Get the current time. */
- curtime = time( NULL );
+ time( &curtime );
/* Convert it to local time representation. */
-#if defined(HAVE_LOCALTIME_R)
localtime_r( &curtime, &loctime );
- strftime( buffer, 255, tformat, &loctime );
-#else
- loctime = localtime( &curtime );
- strftime( buffer, 255, tformat, loctime );
-#endif
- return strdup( buffer );
+ for (size_t buflen = strlen (tformat) + 32;; buflen += 32)
+ {
+ char *str = malloc (buflen);
+ if (str == NULL)
+ return NULL;
+
+ size_t len = strftime (str, buflen, tformat, &loctime);
+ if (len > 0)
+ {
+ char *ret = realloc (str, len + 1);
+ return ret ? ret : str; /* <- this cannot fail */
+ }
+ }
+ assert (0);
}
-#define INSERT_STRING( check, string ) \
- if( check && string ) \
+#define INSERT_STRING( string ) \
+ if( string != NULL ) \
{ \
int len = strlen( string ); \
- dst = realloc( dst, \
- i_size = i_size + len + 1 ); \
- strncpy( d, string, len+1 ); \
+ dst = realloc( dst, i_size = i_size + len );\
+ memcpy( (dst+d), string, len ); \
d += len; \
+ free( string ); \
} \
- else \
+ else if( !b_empty_if_na ) \
{ \
- *d = '-'; \
+ *(dst+d) = '-'; \
d++; \
+ } \
+
+/* same than INSERT_STRING, except that string won't be freed */
+#define INSERT_STRING_NO_FREE( string ) \
+ { \
+ int len = strlen( string ); \
+ dst = realloc( dst, i_size = i_size + len );\
+ memcpy( dst+d, string, len ); \
+ d += len; \
}
char *__str_format_meta( vlc_object_t *p_object, const char *string )
{
const char *s = string;
- char *dst = malloc( 1000 );
- char *d = dst;
- int b_is_format = 0;
+ bool b_is_format = false;
+ bool b_empty_if_na = false;
char buf[10];
- int i_size = strlen( string );
+ int i_size = strlen( string ) + 1; /* +1 to store '\0' */
+ char *dst = strdup( string );
+ if( !dst ) return NULL;
+ int d = 0;
- playlist_t *p_playlist = pl_Yield( p_object );
- input_thread_t *p_input = p_playlist->p_input;
+ playlist_t *p_playlist = pl_Hold( p_object );
+ input_thread_t *p_input = playlist_CurrentInput( p_playlist );
input_item_t *p_item = NULL;
pl_Release( p_object );
if( p_input )
{
- vlc_object_yield( p_input );
p_item = input_GetItem(p_input);
- if( p_item )
- vlc_mutex_lock( &p_item->lock );
}
- sprintf( dst, string );
-
while( *s )
{
if( b_is_format )
switch( *s )
{
case 'a':
- INSERT_STRING( p_item && p_item->p_meta,
- p_item->p_meta->psz_artist );
+ if( p_item )
+ {
+ INSERT_STRING( input_item_GetArtist( p_item ) );
+ }
break;
case 'b':
- INSERT_STRING( p_item && p_item->p_meta,
- p_item->p_meta->psz_album );
+ if( p_item )
+ {
+ INSERT_STRING( input_item_GetAlbum( p_item ) );
+ }
break;
case 'c':
- INSERT_STRING( p_item && p_item->p_meta,
- p_item->p_meta->psz_copyright );
+ if( p_item )
+ {
+ INSERT_STRING( input_item_GetCopyright( p_item ) );
+ }
break;
case 'd':
- INSERT_STRING( p_item && p_item->p_meta,
- p_item->p_meta->psz_description );
+ if( p_item )
+ {
+ INSERT_STRING( input_item_GetDescription( p_item ) );
+ }
break;
case 'e':
- INSERT_STRING( p_item && p_item->p_meta,
- p_item->p_meta->psz_encodedby );
+ if( p_item )
+ {
+ INSERT_STRING( input_item_GetEncodedBy( p_item ) );
+ }
+ break;
+ case 'f':
+ if( p_item && p_item->p_stats )
+ {
+ vlc_mutex_lock( &p_item->p_stats->lock );
+ snprintf( buf, 10, "%d",
+ p_item->p_stats->i_displayed_pictures );
+ vlc_mutex_unlock( &p_item->p_stats->lock );
+ }
+ else
+ {
+ sprintf( buf, b_empty_if_na ? "" : "-" );
+ }
+ INSERT_STRING_NO_FREE( buf );
break;
case 'g':
- INSERT_STRING( p_item && p_item->p_meta,
- p_item->p_meta->psz_genre );
+ if( p_item )
+ {
+ INSERT_STRING( input_item_GetGenre( p_item ) );
+ }
break;
case 'l':
- INSERT_STRING( p_item && p_item->p_meta,
- p_item->p_meta->psz_language );
+ if( p_item )
+ {
+ INSERT_STRING( input_item_GetLanguage( p_item ) );
+ }
break;
case 'n':
- INSERT_STRING( p_item && p_item->p_meta,
- p_item->p_meta->psz_tracknum );
+ if( p_item )
+ {
+ INSERT_STRING( input_item_GetTrackNum( p_item ) );
+ }
break;
case 'p':
- INSERT_STRING( p_item && p_item->p_meta,
- p_item->p_meta->psz_nowplaying );
+ if( p_item )
+ {
+ INSERT_STRING( input_item_GetNowPlaying( p_item ) );
+ }
break;
case 'r':
- INSERT_STRING( p_item && p_item->p_meta,
- p_item->p_meta->psz_rating );
+ if( p_item )
+ {
+ INSERT_STRING( input_item_GetRating( p_item ) );
+ }
break;
case 's':
{
- char *lang;
+ char *lang = NULL;
if( p_input )
- {
- lang = var_GetString( p_input, "sub-language" );
- }
- else
- {
- lang = strdup( "-" );
- }
- INSERT_STRING( 1, lang );
- free( lang );
+ lang = var_GetNonEmptyString( p_input, "sub-language" );
+ if( lang == NULL )
+ lang = strdup( b_empty_if_na ? "" : "-" );
+ INSERT_STRING( lang );
break;
}
case 't':
- INSERT_STRING( p_item && p_item->p_meta,
- p_item->p_meta->psz_title );
+ if( p_item )
+ {
+ INSERT_STRING( input_item_GetTitle( p_item ) );
+ }
break;
case 'u':
- INSERT_STRING( p_item && p_item->p_meta,
- p_item->p_meta->psz_url );
+ if( p_item )
+ {
+ INSERT_STRING( input_item_GetURL( p_item ) );
+ }
break;
case 'A':
- INSERT_STRING( p_item && p_item->p_meta,
- p_item->p_meta->psz_date );
+ if( p_item )
+ {
+ INSERT_STRING( input_item_GetDate( p_item ) );
+ }
break;
case 'B':
if( p_input )
}
else
{
- sprintf( buf, "-" );
+ sprintf( buf, b_empty_if_na ? "" : "-" );
}
- INSERT_STRING( 1, buf );
+ INSERT_STRING_NO_FREE( buf );
break;
case 'C':
if( p_input )
}
else
{
- sprintf( buf, "-" );
+ sprintf( buf, b_empty_if_na ? "" : "-" );
}
- INSERT_STRING( 1, buf );
+ INSERT_STRING_NO_FREE( buf );
break;
case 'D':
if( p_item )
{
+ mtime_t i_duration = input_item_GetDuration( p_item );
sprintf( buf, "%02d:%02d:%02d",
- (int)(p_item->i_duration/(3600000000)),
- (int)((p_item->i_duration/(60000000))%60),
- (int)((p_item->i_duration/1000000)%60) );
+ (int)(i_duration/(3600000000)),
+ (int)((i_duration/(60000000))%60),
+ (int)((i_duration/1000000)%60) );
}
else
{
- sprintf( buf, "--:--:--" );
+ sprintf( buf, b_empty_if_na ? "" : "--:--:--" );
}
- INSERT_STRING( 1, buf );
+ INSERT_STRING_NO_FREE( buf );
break;
case 'F':
- INSERT_STRING( p_item, p_item->psz_uri );
+ if( p_item )
+ {
+ INSERT_STRING( input_item_GetURI( p_item ) );
+ }
break;
case 'I':
if( p_input )
}
else
{
- sprintf( buf, "-" );
+ sprintf( buf, b_empty_if_na ? "" : "-" );
}
- INSERT_STRING( 1, buf );
+ INSERT_STRING_NO_FREE( buf );
break;
case 'L':
if( p_item && p_input )
{
+ mtime_t i_duration = input_item_GetDuration( p_item );
+ int64_t i_time = var_GetInteger( p_input, "time" );
sprintf( buf, "%02d:%02d:%02d",
- (int)((p_item->i_duration-p_input->i_time)/(3600000000)),
- (int)(((p_item->i_duration-p_input->i_time)/(60000000))%60),
- (int)(((p_item->i_duration-p_input->i_time)/1000000)%60) );
+ (int)( ( i_duration - i_time ) / 3600000000 ),
+ (int)( ( ( i_duration - i_time ) / 60000000 ) % 60 ),
+ (int)( ( ( i_duration - i_time ) / 1000000 ) % 60 ) );
}
else
{
- sprintf( buf, "--:--:--" );
+ sprintf( buf, b_empty_if_na ? "" : "--:--:--" );
}
- INSERT_STRING( 1, buf );
+ INSERT_STRING_NO_FREE( buf );
break;
case 'N':
- INSERT_STRING( p_item, p_item->psz_name );
+ if( p_item )
+ {
+ INSERT_STRING( input_item_GetName( p_item ) );
+ }
break;
case 'O':
{
- char *lang;
+ char *lang = NULL;
if( p_input )
- {
- lang = var_GetString( p_input, "audio-language" );
- }
- else
- {
- lang = strdup( "-" );
- }
- INSERT_STRING( 1, lang );
- free( lang );
+ lang = var_GetNonEmptyString( p_input,
+ "audio-language" );
+ if( lang == NULL )
+ lang = strdup( b_empty_if_na ? "" : "-" );
+ INSERT_STRING( lang );
break;
}
case 'P':
}
else
{
- sprintf( buf, "--.-%%" );
+ sprintf( buf, b_empty_if_na ? "" : "--.-%%" );
}
- INSERT_STRING( 1, buf );
+ INSERT_STRING_NO_FREE( buf );
break;
case 'R':
if( p_input )
}
else
{
- sprintf( buf, "-" );
+ sprintf( buf, b_empty_if_na ? "" : "-" );
}
- INSERT_STRING( 1, buf );
+ INSERT_STRING_NO_FREE( buf );
break;
case 'S':
if( p_input )
}
else
{
- sprintf( buf, "-" );
+ sprintf( buf, b_empty_if_na ? "" : "-" );
}
- INSERT_STRING( 1, buf );
+ INSERT_STRING_NO_FREE( buf );
break;
case 'T':
if( p_input )
{
+ int64_t i_time = var_GetInteger( p_input, "time" );
sprintf( buf, "%02d:%02d:%02d",
- (int)(p_input->i_time/(3600000000)),
- (int)((p_input->i_time/(60000000))%60),
- (int)((p_input->i_time/1000000)%60) );
+ (int)( i_time / ( 3600000000 ) ),
+ (int)( ( i_time / ( 60000000 ) ) % 60 ),
+ (int)( ( i_time / 1000000 ) % 60 ) );
}
else
{
- sprintf( buf, "--:--:--" );
+ sprintf( buf, b_empty_if_na ? "" : "--:--:--" );
}
- INSERT_STRING( 1, buf );
+ INSERT_STRING_NO_FREE( buf );
break;
case 'U':
- INSERT_STRING( p_item && p_item->p_meta,
- p_item->p_meta->psz_publisher );
+ if( p_item )
+ {
+ INSERT_STRING( input_item_GetPublisher( p_item ) );
+ }
break;
case 'V':
{
audio_volume_t volume;
aout_VolumeGet( p_object, &volume );
snprintf( buf, 10, "%d", volume );
- INSERT_STRING( 1, buf );
+ INSERT_STRING_NO_FREE( buf );
break;
}
case '_':
- *d = '\n';
+ *(dst+d) = '\n';
d++;
break;
+ case ' ':
+ b_empty_if_na = true;
+ break;
+
default:
- *d = *s;
+ *(dst+d) = *s;
d++;
break;
}
- b_is_format = 0;
+ if( *s != ' ' )
+ b_is_format = false;
}
else if( *s == '$' )
{
- b_is_format = 1;
+ b_is_format = true;
+ b_empty_if_na = false;
}
else
{
- *d = *s;
+ *(dst+d) = *s;
d++;
}
s++;
}
- *d = '\0';
+ *(dst+d) = '\0';
if( p_input )
- {
vlc_object_release( p_input );
- if( p_item )
- vlc_mutex_unlock( &p_item->lock );
- }
return dst;
}
+#undef INSERT_STRING
+#undef INSERT_STRING_NO_FREE
/**
* Apply str format time and str format meta
/**
* Remove forbidden characters from filenames (including slashes)
*/
-void filename_sanitize( char *str )
+char* filename_sanitize( const char *str_origin )
{
+ char *str = strdup( str_origin );
+ char *str_base = str;
if( *str == '.' && (str[1] == '\0' || (str[1] == '.' && str[2] == '\0' ) ) )
{
while( *str )
*str = '_';
str++;
}
- return;
+ return str_base;
}
+#if defined( WIN32 )
+ // Change leading spaces into underscores
+ while( *str && *str == ' ' )
+ *str++ = '_';
+#endif
+
while( *str )
{
switch( *str )
{
case '/':
-#ifdef WIN32
+#if defined( __APPLE__ )
+ case ':':
+#elif defined( WIN32 )
case '\\':
case '*':
case '"':
}
str++;
}
+
+#if defined( WIN32 )
+ // Change trailing spaces into underscores
+ str--;
+ while( str != str_base )
+ {
+ if( *str != ' ' )
+ break;
+ *str-- = '_';
+ }
+#endif
+
+ return str_base;
}
/**
*/
void path_sanitize( char *str )
{
-#if 0
- /*
- * Uncomment the two blocks to prevent /../ or /./, i'm not sure that we
- * want to.
- */
- char *prev = str - 1;
-#endif
#ifdef WIN32
/* check drive prefix if path is absolute */
- if( isalpha(*str) && (':' == *(str+1)) )
+ if( (((unsigned char)(str[0] - 'A') < 26)
+ || ((unsigned char)(str[0] - 'a') < 26)) && (':' == str[1]) )
str += 2;
#endif
while( *str )
{
-#ifdef WIN32
- switch( *str )
- {
- case '*':
- case '"':
- case '?':
- case ':':
- case '|':
- case '<':
- case '>':
- *str = '_';
- }
-#endif
-#if 0
- if( *str == '/'
-#ifdef WIN32
- || *str == '\\'
-#endif
- )
- {
- if( str - prev == 2 && prev[1] == '.' )
- {
- prev[1] = '.';
- }
- else if( str - prev == 3 && prev[1] == '.' && prev[2] == '.' )
- {
- prev[1] = '_';
- prev[2] = '_';
- }
- prev = str;
- }
+#if defined( __APPLE__ )
+ if( *str == ':' )
+ *str = '_';
+#elif defined( WIN32 )
+ if( strchr( "*\"?:|<>", *str ) )
+ *str = '_';
+ if( *str == '/' )
+ *str = DIR_SEP_CHAR;
#endif
str++;
}