changeset 33904:9184861c8bcd

Get rid of some code duplication.
author reimar
date Mon, 15 Aug 2011 20:05:09 +0000
parents 95f5f330a4ff
children c0cb8c5e7fdb
files sub/subreader.c
diffstat 1 files changed, 34 insertions(+), 64 deletions(-) [+]
line wrap: on
line diff
--- a/sub/subreader.c	Mon Aug 15 18:38:24 2011 +0000
+++ b/sub/subreader.c	Mon Aug 15 20:05:09 2011 +0000
@@ -263,9 +263,9 @@
 }
 
 
-static char *sub_readtext(char *source, char **dest) {
+static const char *sub_readtext(const char *source, char **dest) {
     int len=0;
-    char *p=source;
+    const char *p=source;
 
 //    printf("src=%p  dest=%p  \n",source,dest);
 
@@ -285,11 +285,26 @@
     else return NULL;  // last text field
 }
 
+static subtitle *set_multiline_text(subtitle *current, const char *text, int start)
+{
+    int i = start;
+    while ((text = sub_readtext(text, current->text + i))) {
+        if (current->text[i] == ERR) return ERR;
+        i++;
+        if (i >= SUB_MAX_TEXT) {
+            mp_msg(MSGT_SUBREADER, MSGL_WARN, "Too many lines in a subtitle\n");
+            current->lines = i;
+            return current;
+        }
+    }
+    current->lines = i + 1;
+    return current;
+}
+
 static subtitle *sub_read_line_microdvd(stream_t *st,subtitle *current, int utf16) {
     char line[LINE_LEN+1];
     char line2[LINE_LEN+1];
-    char *p, *next;
-    int i;
+    char *p;
 
     do {
 	if (!stream_read_line (st, line, LINE_LEN, utf16)) return NULL;
@@ -308,22 +323,12 @@
 #endif
         p = line2;
 
-    next=p, i=0;
-    while ((next =sub_readtext (next, &(current->text[i])))) {
-        if (current->text[i]==ERR) {return ERR;}
-	i++;
-	if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
-    }
-    current->lines= ++i;
-
-    return current;
+    return set_multiline_text(current, p, 0);
 }
 
 static subtitle *sub_read_line_mpl2(stream_t *st,subtitle *current, int utf16) {
     char line[LINE_LEN+1];
     char line2[LINE_LEN+1];
-    char *p, *next;
-    int i;
 
     do {
 	if (!stream_read_line (st, line, LINE_LEN, utf16)) return NULL;
@@ -332,17 +337,8 @@
 		      &(current->start), &(current->end), line2) < 3));
     current->start *= 10;
     current->end *= 10;
-    p=line2;
 
-    next=p, i=0;
-    while ((next =sub_readtext (next, &(current->text[i])))) {
-        if (current->text[i]==ERR) {return ERR;}
-	i++;
-	if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
-    }
-    current->lines= ++i;
-
-    return current;
+    return set_multiline_text(current, line2, 0);
 }
 
 static subtitle *sub_read_line_subrip(stream_t* st, subtitle *current, int utf16) {
@@ -525,8 +521,8 @@
 static subtitle *sub_read_line_vplayer(stream_t *st,subtitle *current, int utf16) {
 	char line[LINE_LEN+1];
 	int a1,a2,a3;
-	char *p=NULL, *next,separator;
-	int i,len,plen;
+	char *p=NULL, separator;
+	int len,plen;
 
 	while (!current->text[0]) {
 		if (!stream_read_line (st, line, LINE_LEN, utf16)) return NULL;
@@ -553,16 +549,9 @@
                 // colon! look, what simple it can be:
                 p = &line[ plen ];
 
- 		i=0;
 		if (*p!='|') {
 			//
-			next = p,i=0;
-			while ((next =sub_readtext (next, &(current->text[i])))) {
-				if (current->text[i]==ERR) {return ERR;}
-				i++;
-				if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
-			}
-			current->lines=i+1;
+                        return set_multiline_text(current, p, 0);
 		}
 	}
 	return current;
@@ -575,7 +564,7 @@
     char line[LINE_LEN+1];
     int a1,a2,a3,a4,b1,b2,b3,b4;
     char *p=NULL,*next=NULL;
-    int i,len,plen;
+    int len,plen;
 
     while (!current->text[0]) {
 	if (!stream_read_line (st, line, LINE_LEN, utf16)) return NULL;
@@ -602,18 +591,13 @@
 	current->end   = b1*360000+b2*6000+b3*100+b4/10;
 	if (b1 == 0 && b2 == 0 && b3 == 0 && b4 == 0)
 	  current->end = current->start+200;
-	p=line;	p+=plen;i=0;
+	p=line;	p+=plen;
 	// TODO: I don't know what kind of convention is here for marking multiline subs, maybe <br/> like in xml?
 	next = strstr(line,"<clear/>");
 	if(next && strlen(next)>8){
-	  next+=8;i=0;
-	  while ((next =sub_readtext (next, &(current->text[i])))) {
-		if (current->text[i]==ERR) {return ERR;}
-		i++;
-		if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
-	  }
+	  next+=8;
+          return set_multiline_text(current, next, 0);
 	}
-			current->lines=i+1;
     }
     return current;
 }
@@ -805,8 +789,6 @@
 
 static subtitle *sub_read_line_aqt(stream_t *st,subtitle *current, int utf16) {
     char line[LINE_LEN+1];
-    char *next;
-    int i;
 
     while (1) {
     // try to locate next subtitle
@@ -835,13 +817,8 @@
     if (!stream_read_line (st, line, LINE_LEN, utf16))
 	return current;
 
-    next = line,i=1;
-    while ((next =sub_readtext (next, &(current->text[i])))) {
-	if (current->text[i]==ERR) {return ERR;}
-	i++;
-	if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
-	}
-    current->lines=i+1;
+    if (set_multiline_text(current, line, 1) == ERR)
+        return ERR;
 
     if (!strlen(current->text[0]) && !strlen(current->text[1])) {
 #ifdef CONFIG_SORTSUB
@@ -863,8 +840,7 @@
 static subtitle *sub_read_line_subrip09(stream_t *st,subtitle *current, int utf16) {
     char line[LINE_LEN+1];
     int a1,a2,a3;
-    char * next=NULL;
-    int i,len;
+    int len;
 
     while (1) {
     // try to locate next subtitle
@@ -888,18 +864,12 @@
     if (!stream_read_line (st, line, LINE_LEN, utf16))
 	return NULL;
 
-    next = line,i=0;
-
     current->text[0]=""; // just to be sure that string is clear
 
-    while ((next =sub_readtext (next, &(current->text[i])))) {
-	if (current->text[i]==ERR) {return ERR;}
-	i++;
-	if (i>=SUB_MAX_TEXT) { mp_msg(MSGT_SUBREADER,MSGL_WARN,"Too many lines in a subtitle\n");current->lines=i;return current;}
-	}
-    current->lines=i+1;
+    if (set_multiline_text(current, line, 0) == ERR)
+        return ERR;
 
-    if (!strlen(current->text[0]) && (i==0)) {
+    if (!strlen(current->text[0]) && current->lines <= 1) {
 #ifdef CONFIG_SORTSUB
 	previous_sub_end = 0;
 #else