From e5ab09bb64448e8d60f081891fb85d742c83c9a0 Mon Sep 17 00:00:00 2001 From: uau Date: Sat, 24 Feb 2007 07:11:57 +0000 Subject: Move parsing of the -ss option to the option code. Also fixes a memory leak of the parameter string. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@22330 b3059339-0415-0410-9bf9-f77b7e298cf2 --- cfg-common.h | 2 +- m_option.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++----------- m_option.h | 2 ++ mencoder.c | 13 ++----------- mplayer.c | 14 +++----------- 5 files changed, 56 insertions(+), 34 deletions(-) diff --git a/cfg-common.h b/cfg-common.h index caf7248fd2..ab01146d92 100644 --- a/cfg-common.h +++ b/cfg-common.h @@ -93,7 +93,7 @@ // seek to byte/seconds position {"sb", &seek_to_byte, CONF_TYPE_POSITION, CONF_MIN, 0, 0, NULL}, - {"ss", &seek_to_sec, CONF_TYPE_STRING, CONF_MIN, 0, 0, NULL}, + {"ss", &seek_to_sec, CONF_TYPE_TIME, 0, 0, 0, NULL}, // stop at given position {"endpos", &end_at, CONF_TYPE_TIME_SIZE, 0, 0, 0, NULL}, diff --git a/m_option.c b/m_option.c index ca60174f69..996806fe39 100644 --- a/m_option.c +++ b/m_option.c @@ -1188,13 +1188,56 @@ m_option_type_t m_option_type_afmt = { }; +static double parse_timestring(char *str) +{ + int a, b; + double d; + if (sscanf(str, "%d:%d:%lf", &a, &b, &d) == 3) + return 3600*a + 60*b + d; + else if (sscanf(str, "%d:%lf", &a, &d) == 2) + return 60*a + d; + else if (sscanf(str, "%lf", &d) == 1) + return d; + return -1e100; +} + + +static int parse_time(m_option_t* opt,char *name, char *param, void* dst, int src) +{ + if (param == NULL || strlen(param) == 0) + return M_OPT_MISSING_PARAM; + + double time = parse_timestring(param); + if (time == -1e100) { + mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: invalid time: '%s'\n", + name,param); + return M_OPT_INVALID; + } + + if (dst) + *(double *)dst = time; + return 1; +} + +m_option_type_t m_option_type_time = { + "Time", + "", + sizeof(double), + 0, + parse_time, + NULL, + copy_opt, + copy_opt, + NULL, + NULL +}; + + // Time or size (-endpos) static int parse_time_size(m_option_t* opt,char *name, char *param, void* dst, int src) { m_time_size_t ts; char unit[4]; - int a,b; - float d; double end_at; if (param == NULL || strlen(param) == 0) @@ -1221,15 +1264,9 @@ static int parse_time_size(m_option_t* opt,char *name, char *param, void* dst, i } } - /* End at time parsing. This has to be last because of - * sscanf("%f", ...) below */ - if (sscanf(param, "%d:%d:%f", &a, &b, &d) == 3) - end_at = 3600*a + 60*b + d; - else if (sscanf(param, "%d:%f", &a, &d) == 2) - end_at = 60*a + d; - else if (sscanf(param, "%f", &d) == 1) - end_at = d; - else { + /* End at time parsing. This has to be last because the parsing accepts + * even a number followed by garbage */ + if ((end_at = parse_timestring(param)) == -1e100) { mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Option %s: invalid time or size: '%s'\n", name,param); return M_OPT_INVALID; diff --git a/m_option.h b/m_option.h index 443b5d8d4e..3b37fa1cd4 100644 --- a/m_option.h +++ b/m_option.h @@ -28,6 +28,7 @@ extern m_option_type_t m_option_type_double; extern m_option_type_t m_option_type_string; extern m_option_type_t m_option_type_string_list; extern m_option_type_t m_option_type_position; +extern m_option_type_t m_option_type_time; extern m_option_type_t m_option_type_time_size; extern m_option_type_t m_option_type_print; @@ -168,6 +169,7 @@ extern m_obj_params_t m_span_params_def; #define CONF_TYPE_OBJ_PRESETS (&m_option_type_obj_presets) #define CONF_TYPE_CUSTOM_URL (&m_option_type_custom_url) #define CONF_TYPE_OBJ_PARAMS (&m_option_type_obj_params) +#define CONF_TYPE_TIME (&m_option_type_time) #define CONF_TYPE_TIME_SIZE (&m_option_type_time_size) ///////////////////////////////////////////////////////////////////////////////////////////// diff --git a/mencoder.c b/mencoder.c index 1bc47508ff..b690c7563c 100644 --- a/mencoder.c +++ b/mencoder.c @@ -250,7 +250,7 @@ static int cfg_include(m_option_t *conf, char *filename){ return m_config_parse_config_file(mconfig, filename); } -static char *seek_to_sec=NULL; +static double seek_to_sec; static off_t seek_to_byte=0; static m_time_size_t end_at = { .type = END_AT_NONE, .pos = 0 }; @@ -1035,16 +1035,7 @@ else { } if (seek_to_sec) { - int a,b; float d; - - if (sscanf(seek_to_sec, "%d:%d:%f", &a,&b,&d)==3) - d += 3600*a + 60*b; - else if (sscanf(seek_to_sec, "%d:%f", &a, &d)==2) - d += 60*a; - else - sscanf(seek_to_sec, "%f", &d); - - demux_seek(demuxer, d, audio_delay, 1); + demux_seek(demuxer, seek_to_sec, audio_delay, 1); // there is 2 way to handle the -ss option in 3-pass mode: // > 1. do the first pass for the whole file, and use -ss for 2nd/3rd pases only // > 2. do all the 3 passes with the same -ss value diff --git a/mplayer.c b/mplayer.c index a691bd56f2..922b36c986 100644 --- a/mplayer.c +++ b/mplayer.c @@ -239,7 +239,7 @@ int term_osd = 1; static char* term_osd_esc = "\x1b[A\r\x1b[K"; static char* playing_msg = NULL; // seek: -static char *seek_to_sec=NULL; +static double seek_to_sec; static off_t seek_to_byte=0; static off_t step_sec=0; static int loop_times=-1; @@ -3537,16 +3537,8 @@ if(step_sec>0) { mpctx->was_paused = 0; if (seek_to_sec) { - int a,b; float d; - - if (sscanf(seek_to_sec, "%d:%d:%f", &a,&b,&d)==3) - rel_seek_secs += 3600*a +60*b +d ; - else if (sscanf(seek_to_sec, "%d:%f", &a, &d)==2) - rel_seek_secs += 60*a +d; - else if (sscanf(seek_to_sec, "%f", &d)==1) - rel_seek_secs += d; - - seek_to_sec = NULL; + rel_seek_secs += seek_to_sec; + seek_to_sec = 0; } if (end_at.type != END_AT_NONE) { -- cgit v1.2.3