Commit 53cf4a01 authored by Antoine Cellerier's avatar Antoine Cellerier

Move string formating functions used in marq to the core.

parent 8da13714
......@@ -26,6 +26,7 @@
#ifndef _VLC__INPUT_H
#define _VLC__INPUT_H 1
#include <vlc_es.h>
#include <vlc_playlist.h>
#include <vlc_meta.h>
......
......@@ -34,6 +34,10 @@
VLC_EXPORT( void, resolve_xml_special_chars, ( char *psz_value ) );
VLC_EXPORT( char *, convert_xml_special_chars, ( const char *psz_content ) );
VLC_EXPORT( char *, str_format_time, ( char * ) );
#define str_format_meta( a, b ) __str_format_meta( VLC_OBJECT( a ), b )
VLC_EXPORT( char *, __str_format_meta, ( vlc_object_t *, char * ) );
/**
* @}
*/
......
......@@ -555,6 +555,8 @@ struct module_symbols_t
char * (*config_ChainCreate_inner) (char **, config_chain_t **, char *);
int (*utf8_open_inner) (const char *filename, int flags, mode_t mode);
int (*net_ListenSingle_inner) (vlc_object_t *p_this, const char *psz_host, int i_port, int family, int socktype, int protocol);
char * (*str_format_time_inner) (char *);
char * (*__str_format_meta_inner) (vlc_object_t *, char *);
};
# if defined (__PLUGIN__)
# define aout_FiltersCreatePipeline (p_symbols)->aout_FiltersCreatePipeline_inner
......@@ -1033,6 +1035,8 @@ struct module_symbols_t
# define config_ChainCreate (p_symbols)->config_ChainCreate_inner
# define utf8_open (p_symbols)->utf8_open_inner
# define net_ListenSingle (p_symbols)->net_ListenSingle_inner
# define str_format_time (p_symbols)->str_format_time_inner
# define __str_format_meta (p_symbols)->__str_format_meta_inner
# elif defined (HAVE_DYNAMIC_PLUGINS) && !defined (__BUILTIN__)
/******************************************************************
* STORE_SYMBOLS: store VLC APIs into p_symbols for plugin access.
......@@ -1514,6 +1518,8 @@ struct module_symbols_t
((p_symbols)->config_ChainCreate_inner) = config_ChainCreate; \
((p_symbols)->utf8_open_inner) = utf8_open; \
((p_symbols)->net_ListenSingle_inner) = net_ListenSingle; \
((p_symbols)->str_format_time_inner) = str_format_time; \
((p_symbols)->__str_format_meta_inner) = __str_format_meta; \
(p_symbols)->net_ConvertIPv4_deprecated = NULL; \
(p_symbols)->__sout_CfgParse_deprecated = NULL; \
(p_symbols)->sout_CfgCreate_deprecated = NULL; \
......
......@@ -29,18 +29,14 @@
#include <stdlib.h> /* malloc(), free() */
#include <string.h>
#include <time.h>
#include <vlc/vlc.h>
#include <vlc/vout.h>
#include "vlc_filter.h"
#include "vlc_block.h"
#include "vlc_osd.h"
#include "vlc_playlist.h"
#include "vlc_meta.h"
#include "vlc_input.h"
#include <vlc/aout.h>
#include "vlc_strings.h"
/*****************************************************************************
* Local prototypes
......@@ -243,325 +239,6 @@ static void DestroyFilter( vlc_object_t *p_this )
free( p_sys );
}
/****************************************************************************
* String formating functions
****************************************************************************/
static char *FormatTime(char *tformat )
{
char buffer[255];
time_t curtime;
#if defined(HAVE_LOCALTIME_R)
struct tm loctime;
#else
struct tm *loctime;
#endif
/* Get the current time. */
curtime = time( NULL );
/* 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 );
}
#define INSERT_STRING( check, string ) \
if( check && string ) \
{ \
int len = strlen( string ); \
dst = realloc( dst, \
i_size = i_size + len + 1 ); \
strncpy( d, string, len+1 ); \
d += len; \
} \
else \
{ \
*d = '-'; \
d++; \
}
char *FormatMeta( vlc_object_t *p_object, char *string )
{
char *s = string;
char *dst = malloc( 1000 );
char *d = dst;
int b_is_format = 0;
char buf[10];
int i_size = strlen( string );
playlist_t *p_playlist = pl_Yield( p_object );
input_thread_t *p_input = p_playlist->p_input;
input_item_t *p_item = NULL;
pl_Release( p_object );
if( p_input )
{
vlc_object_yield( p_input );
p_item = p_input->input.p_item;
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 );
break;
case 'b':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_album );
break;
case 'c':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_copyright );
break;
case 'd':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_description );
break;
case 'e':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_encodedby );
break;
case 'g':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_genre );
break;
case 'l':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_language );
break;
case 'n':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_tracknum );
break;
case 'p':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_nowplaying );
break;
case 'r':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_rating );
break;
case 's':
{
char *lang;
if( p_input )
{
lang = var_GetString( p_input, "sub-language" );
}
else
{
lang = strdup( "-" );
}
INSERT_STRING( 1, lang );
free( lang );
break;
}
case 't':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_title );
break;
case 'u':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_url );
break;
case 'A':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_date );
break;
case 'B':
if( p_input )
{
snprintf( buf, 10, "%d",
var_GetInteger( p_input, "bit-rate" )/1000 );
}
else
{
sprintf( buf, "-" );
}
INSERT_STRING( 1, buf );
break;
case 'C':
if( p_input )
{
snprintf( buf, 10, "%d",
var_GetInteger( p_input, "chapter" ) );
}
else
{
sprintf( buf, "-" );
}
INSERT_STRING( 1, buf );
break;
case 'D':
if( 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) );
}
else
{
sprintf( buf, "--:--:--" );
}
INSERT_STRING( 1, buf );
break;
case 'F':
INSERT_STRING( p_item, p_item->psz_uri );
break;
case 'I':
if( p_input )
{
snprintf( buf, 10, "%d",
var_GetInteger( p_input, "title" ) );
}
else
{
sprintf( buf, "-" );
}
INSERT_STRING( 1, buf );
break;
case 'L':
if( p_item && p_input )
{
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) );
}
else
{
sprintf( buf, "--:--:--" );
}
INSERT_STRING( 1, buf );
break;
case 'N':
INSERT_STRING( p_item, p_item->psz_name );
break;
case 'O':
{
char *lang;
if( p_input )
{
lang = var_GetString( p_input, "audio-language" );
}
else
{
lang = strdup( "-" );
}
INSERT_STRING( 1, lang );
free( lang );
break;
}
case 'P':
if( p_input )
{
snprintf( buf, 10, "%2.1lf",
var_GetFloat( p_input, "position" ) * 100. );
}
else
{
sprintf( buf, "--.-%%" );
}
INSERT_STRING( 1, buf );
break;
case 'R':
if( p_input )
{
int r = var_GetInteger( p_input, "rate" );
snprintf( buf, 10, "%d.%d", r/1000, r%1000 );
}
else
{
sprintf( buf, "-" );
}
INSERT_STRING( 1, buf );
break;
case 'S':
if( p_input )
{
int r = var_GetInteger( p_input, "sample-rate" );
snprintf( buf, 10, "%d.%d", r/1000, (r/100)%10 );
}
else
{
sprintf( buf, "-" );
}
INSERT_STRING( 1, buf );
break;
case 'T':
if( p_input )
{
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) );
}
else
{
sprintf( buf, "--:--:--" );
}
INSERT_STRING( 1, buf );
break;
case 'U':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_publisher );
break;
case 'V':
{
audio_volume_t volume;
aout_VolumeGet( p_object, &volume );
snprintf( buf, 10, "%d", volume );
INSERT_STRING( 1, buf );
break;
}
case '_':
*d = '\n';
d++;
break;
default:
*d = *s;
d++;
break;
}
b_is_format = 0;
}
else if( *s == '$' )
{
b_is_format = 1;
}
else
{
*d = *s;
d++;
}
s++;
}
*d = '\0';
if( p_input )
{
vlc_object_release( p_input );
if( p_item )
vlc_mutex_unlock( &p_item->lock );
}
return dst;
}
/****************************************************************************
* Filter: the whole thing
......@@ -612,8 +289,8 @@ static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
{
p_sys->b_need_update = VLC_FALSE;
}
buf = FormatTime( p_sys->psz_marquee );
p_spu->p_region->psz_text = FormatMeta( VLC_OBJECT( p_filter ), buf );
buf = str_format_time( p_sys->psz_marquee );
p_spu->p_region->psz_text = str_format_meta( p_filter, buf );
free( buf );
p_spu->i_start = date;
p_spu->i_stop = p_sys->i_timeout == 0 ? 0 : date + p_sys->i_timeout * 1000;
......
......@@ -32,6 +32,15 @@
#include <stdlib.h>
#include <assert.h>
/* Needed by str_format_time */
#include <time.h>
/* Needed by str_format_meta */
#include "vlc_input.h"
#include "vlc_meta.h"
#include "vlc_playlist.h"
#include <vlc/aout.h>
#include "vlc_strings.h"
#include "vlc_url.h"
#include "charset.h"
......@@ -318,3 +327,322 @@ char *convert_xml_special_chars( const char *psz_content )
return psz_temp;
}
/****************************************************************************
* String formating functions
****************************************************************************/
char *str_format_time(char *tformat )
{
char buffer[255];
time_t curtime;
#if defined(HAVE_LOCALTIME_R)
struct tm loctime;
#else
struct tm *loctime;
#endif
/* Get the current time. */
curtime = time( NULL );
/* 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 );
}
#define INSERT_STRING( check, string ) \
if( check && string ) \
{ \
int len = strlen( string ); \
dst = realloc( dst, \
i_size = i_size + len + 1 ); \
strncpy( d, string, len+1 ); \
d += len; \
} \
else \
{ \
*d = '-'; \
d++; \
}
char *__str_format_meta( vlc_object_t *p_object, char *string )
{
char *s = string;
char *dst = malloc( 1000 );
char *d = dst;
int b_is_format = 0;
char buf[10];
int i_size = strlen( string );
playlist_t *p_playlist = pl_Yield( p_object );
input_thread_t *p_input = p_playlist->p_input;
input_item_t *p_item = NULL;
pl_Release( p_object );
if( p_input )
{
vlc_object_yield( p_input );
p_item = p_input->input.p_item;
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 );
break;
case 'b':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_album );
break;
case 'c':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_copyright );
break;
case 'd':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_description );
break;
case 'e':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_encodedby );
break;
case 'g':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_genre );
break;
case 'l':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_language );
break;
case 'n':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_tracknum );
break;
case 'p':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_nowplaying );
break;
case 'r':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_rating );
break;
case 's':
{
char *lang;
if( p_input )
{
lang = var_GetString( p_input, "sub-language" );
}
else
{
lang = strdup( "-" );
}
INSERT_STRING( 1, lang );
free( lang );
break;
}
case 't':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_title );
break;
case 'u':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_url );
break;
case 'A':
INSERT_STRING( p_item && p_item->p_meta,
p_item->p_meta->psz_date );
break;
case 'B':
if( p_input )
{
snprintf( buf, 10, "%d",
var_GetInteger( p_input, "bit-rate" )/1000 );
}
else
{
sprintf( buf, "-" );
}
INSERT_STRING( 1, buf );
break;
case 'C':
if( p_input )
{
snprintf( buf, 10, "%d",
var_GetInteger( p_input, "chapter" ) );
}
else
{
sprintf( buf, "-" );
}
INSERT_STRING( 1, buf );
break;
case 'D':
if( 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) );
}
else
{
sprintf( buf, "--:--:--" );
}
INSERT_STRING( 1, buf );
break;
case 'F':
INSERT_STRING( p_item, p_item->psz_uri );
break;
case 'I':
if( p_input )
{
snprintf( buf, 10, "%d",
var_GetInteger( p_input, "title" ) );
}
else
{
sprintf( buf, "-" );
}
INSERT_STRING( 1, buf );
break;
case 'L':
if( p_item && p_input )
{
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) );
}
else
{
sprintf( buf, "--:--:--" );
}
INSERT_STRING( 1, buf );
break;
case 'N':
INSERT_STRING( p_item, p_item->psz_name );
break;
case 'O':
{
char *lang;
if( p_input )
{
lang = var_GetString( p_input, "audio-language" );
}
else
{
lang = strdup( "-" );
}
INSERT_STRING( 1, lang );
free( lang );
break;