changeset 30344:4752e3cbe7be

Avoid duplicating code to remove subtitles (sub_remove slave command).
author reimar
date Fri, 22 Jan 2010 20:59:15 +0000
parents 4d50825554ee
children ea015e0ab0c6
files command.c
diffstat 1 files changed, 41 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/command.c	Fri Jan 22 03:26:30 2010 +0000
+++ b/command.c	Fri Jan 22 20:59:15 2010 +0000
@@ -2335,6 +2335,45 @@
     return "UNKNOWN";
 }
 
+static void remove_subtitle_range(MPContext *mpctx, int start, int count)
+{
+    int idx;
+    int end = start + count;
+    int after = mpctx->set_of_sub_size - end;
+    sub_data **subs = mpctx->set_of_subtitles;
+    if (count < 0 || count > mpctx->set_of_sub_size ||
+        start < 0 || start > mpctx->set_of_sub_size - count) {
+        mp_msg(MSGT_CPLAYER, MSGL_ERR,
+               "Cannot remove invalid subtitle range %i +%i\n", start, count);
+        return;
+    }
+    for (idx = start; idx < end; idx++) {
+        sub_data *subd = subs[idx];
+        mp_msg(MSGT_CPLAYER, MSGL_STATUS,
+               MSGTR_RemovedSubtitleFile, idx + 1,
+               filename_recode(subd->filename));
+        sub_free(subd);
+        subs[idx] = NULL;
+    }
+
+    mpctx->global_sub_size -= count;
+    mpctx->set_of_sub_size -= count;
+    if (mpctx->set_of_sub_size <= 0)
+        mpctx->global_sub_indices[SUB_SOURCE_SUBS] = -1;
+
+    memmove(subs + start, subs + end, after * sizeof(*subs));
+    memset(subs + start + after, 0, count * sizeof(*subs));
+
+    if (mpctx->set_of_sub_pos >= start && mpctx->set_of_sub_pos < end) {
+        mpctx->global_sub_pos = -2;
+        subdata = NULL;
+        mp_input_queue_cmd(mp_input_parse_cmd("sub_select"));
+    } else if (mpctx->set_of_sub_pos >= end) {
+        mpctx->set_of_sub_pos -= count;
+        mpctx->global_sub_pos -= count;
+    }
+}
+
 int run_command(MPContext * mpctx, mp_cmd_t * cmd)
 {
     sh_audio_t * const sh_audio = mpctx->sh_audio;
@@ -2908,46 +2947,10 @@
 	case MP_CMD_SUB_REMOVE:
 	    if (sh_video) {
 		int v = cmd->args[0].v.i;
-		sub_data *subd;
 		if (v < 0) {
-		    for (v = 0; v < mpctx->set_of_sub_size; ++v) {
-			subd = mpctx->set_of_subtitles[v];
-			mp_msg(MSGT_CPLAYER, MSGL_STATUS,
-			       MSGTR_RemovedSubtitleFile, v + 1,
-			       filename_recode(subd->filename));
-			sub_free(subd);
-			mpctx->set_of_subtitles[v] = NULL;
-		    }
-		    mpctx->global_sub_indices[SUB_SOURCE_SUBS] = -1;
-		    mpctx->global_sub_size -= mpctx->set_of_sub_size;
-		    mpctx->set_of_sub_size = 0;
-		    if (mpctx->set_of_sub_pos >= 0) {
-			mpctx->global_sub_pos = -2;
-			subdata = NULL;
-			mp_input_queue_cmd(mp_input_parse_cmd("sub_select"));
-		    }
+		    remove_subtitle_range(mpctx, 0, mpctx->set_of_sub_size);
 		} else if (v < mpctx->set_of_sub_size) {
-		    subd = mpctx->set_of_subtitles[v];
-		    mp_msg(MSGT_CPLAYER, MSGL_STATUS,
-			   MSGTR_RemovedSubtitleFile, v + 1,
-			   filename_recode(subd->filename));
-		    sub_free(subd);
-		    if (mpctx->set_of_sub_pos == v) {
-			mpctx->global_sub_pos = -2;
-			subdata = NULL;
-			mp_input_queue_cmd(mp_input_parse_cmd("sub_select"));
-		    } else if (mpctx->set_of_sub_pos > v) {
-			--mpctx->set_of_sub_pos;
-			--mpctx->global_sub_pos;
-		    }
-		    while (++v < mpctx->set_of_sub_size)
-			mpctx->set_of_subtitles[v - 1] =
-			    mpctx->set_of_subtitles[v];
-		    --mpctx->set_of_sub_size;
-		    --mpctx->global_sub_size;
-		    if (mpctx->set_of_sub_size <= 0)
-			mpctx->global_sub_indices[SUB_SOURCE_SUBS] = -1;
-		    mpctx->set_of_subtitles[mpctx->set_of_sub_size] = NULL;
+		    remove_subtitle_range(mpctx, v, 1);
 		}
 	    }
 	    break;