[PATCH 1/2] subreader: Split a monster if() into multiple parts.
Should make code more maintainable. Also make sure all input variables are properly reset before each test. --- sub/subreader.c | 59 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/sub/subreader.c b/sub/subreader.c index 0a9711c..3c42552 100644 --- a/sub/subreader.c +++ b/sub/subreader.c @@ -594,28 +594,57 @@ static subtitle *sub_read_line_rt(stream_t *st,subtitle *current, int utf16) { char line[LINE_LEN+1]; int a1,a2,a3,a4,b1,b2,b3,b4; char *p=NULL,*next=NULL; - int len,plen; + int plen; while (!current->text[0]) { + int match = 0; if (!stream_read_line (st, line, LINE_LEN, utf16)) return NULL; //TODO: it seems that format of time is not easily determined, it may be 1:12, 1:12.0 or 0:1:12.0 //to describe the same moment in time. Maybe there are even more formats in use. //if ((len=sscanf (line, "<Time Begin=\"%d:%d:%d.%d\" End=\"%d:%d:%d.%d\"",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8) - plen=a1=a2=a3=a4=b1=b2=b3=b4=0; - if ( - ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&b3,&b4,&plen)) < 4) && - ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&b2,&b3,&b4,&plen)) < 5) && - ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&plen)) < 4) && - ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&b4,&plen)) < 5) && -// ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&plen)) < 5) && - ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&b4,&plen)) < 6) && - ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\" %*[Ee]nd=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4,&plen)) < 8) && + if (!match) { + plen=a1=a2=a3=a4=b1=b2=b3=b4=0; + match = sscanf(line, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&b3,&b4,&plen) >= 4; + } + if (!match) { + plen=a1=a2=a3=a4=b1=b2=b3=b4=0; + match = sscanf(line, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&b2,&b3,&b4,&plen) >= 5; + } + if (!match) { + plen=a1=a2=a3=a4=b1=b2=b3=b4=0; + match = sscanf(line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&plen) >= 4; + } + if (!match) { + plen=a1=a2=a3=a4=b1=b2=b3=b4=0; + match = sscanf(line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&b4,&plen) >= 5; + } +// sscanf(line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&plen) >= 5 + if (!match) { + plen=a1=a2=a3=a4=b1=b2=b3=b4=0; + match = sscanf(line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&b4,&plen) >= 6; + } + if (!match) { + plen=a1=a2=a3=a4=b1=b2=b3=b4=0; + match = sscanf(line, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\" %*[Ee]nd=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4,&plen) >= 8; + } //now try it without end time - ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&plen)) < 2) && - ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&plen)) < 2) && - ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&plen)) < 3) && - ((len=sscanf (line, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1,&a2,&a3,&a4,&plen)) < 4) - ) + if (!match) { + plen=a1=a2=a3=a4=b1=b2=b3=b4=0; + match = sscanf(line, "<%*[tT]ime %*[bB]egin=\"%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&plen) >= 2; + } + if (!match) { + plen=a1=a2=a3=a4=b1=b2=b3=b4=0; + match = sscanf(line, "<%*[tT]ime %*[bB]egin=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&plen) >= 2; + } + if (!match) { + plen=a1=a2=a3=a4=b1=b2=b3=b4=0; + match = sscanf(line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&plen) >= 3; + } + if (!match) { + plen=a1=a2=a3=a4=b1=b2=b3=b4=0; + match = sscanf(line, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1,&a2,&a3,&a4,&plen) >= 4; + } + if (!match) continue; current->start = a1*360000+a2*6000+a3*100+a4/10; current->end = b1*360000+b2*6000+b3*100+b4/10; -- 2.0.0.rc4
This ensures we had an actual match against the full string. Ideally this would be possible by checking for a one larger return value, but unfortunately the standard is unclear if %n arguments should be counted or not... --- sub/subreader.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sub/subreader.c b/sub/subreader.c index 3c42552..7f7771c 100644 --- a/sub/subreader.c +++ b/sub/subreader.c @@ -605,44 +605,54 @@ static subtitle *sub_read_line_rt(stream_t *st,subtitle *current, int utf16) { if (!match) { plen=a1=a2=a3=a4=b1=b2=b3=b4=0; match = sscanf(line, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&b3,&b4,&plen) >= 4; + match &= plen > 0; } if (!match) { plen=a1=a2=a3=a4=b1=b2=b3=b4=0; match = sscanf(line, "<%*[tT]ime %*[bB]egin=\"%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&b2,&b3,&b4,&plen) >= 5; + match &= plen > 0; } if (!match) { plen=a1=a2=a3=a4=b1=b2=b3=b4=0; match = sscanf(line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&plen) >= 4; + match &= plen > 0; } if (!match) { plen=a1=a2=a3=a4=b1=b2=b3=b4=0; match = sscanf(line, "<%*[tT]ime %*[bB]egin=\"%d:%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&b2,&b3,&b4,&plen) >= 5; + match &= plen > 0; } // sscanf(line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&plen) >= 5 if (!match) { plen=a1=a2=a3=a4=b1=b2=b3=b4=0; match = sscanf(line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\" %*[Ee]nd=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&b2,&b3,&b4,&plen) >= 6; + match &= plen > 0; } if (!match) { plen=a1=a2=a3=a4=b1=b2=b3=b4=0; match = sscanf(line, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\" %*[Ee]nd=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4,&plen) >= 8; + match &= plen > 0; } //now try it without end time if (!match) { plen=a1=a2=a3=a4=b1=b2=b3=b4=0; match = sscanf(line, "<%*[tT]ime %*[bB]egin=\"%d.%d\"%*[^<]<clear/>%n",&a3,&a4,&plen) >= 2; + match &= plen > 0; } if (!match) { plen=a1=a2=a3=a4=b1=b2=b3=b4=0; match = sscanf(line, "<%*[tT]ime %*[bB]egin=\"%d:%d\"%*[^<]<clear/>%n",&a2,&a3,&plen) >= 2; + match &= plen > 0; } if (!match) { plen=a1=a2=a3=a4=b1=b2=b3=b4=0; match = sscanf(line, "<%*[tT]ime %*[bB]egin=\"%d:%d.%d\"%*[^<]<clear/>%n",&a2,&a3,&a4,&plen) >= 3; + match &= plen > 0; } if (!match) { plen=a1=a2=a3=a4=b1=b2=b3=b4=0; match = sscanf(line, "<%*[tT]ime %*[bB]egin=\"%d:%d:%d.%d\"%*[^<]<clear/>%n",&a1,&a2,&a3,&a4,&plen) >= 4; + match &= plen > 0; } if (!match) continue; -- 2.0.0.rc4
Le quintidi 5 prairial, an CCXXII, Reimar Döffinger a écrit :
This ensures we had an actual match against the full string.
Sorry to have pointed the obvious in my previous mail. The change looks completely valid.
Ideally this would be possible by checking for a one larger return value, but unfortunately the standard is unclear if %n arguments should be counted or not...
Actually, the standard is perfectly clear... with the wrong choice: "Execution of a %n conversion specification shall not increment the assignment count returned at the completion of execution of the function." Regards, -- Nicolas George
On Mon, May 26, 2014 at 11:06:18AM +0200, Nicolas George wrote:
Le quintidi 5 prairial, an CCXXII, Reimar Döffinger a écrit :
This ensures we had an actual match against the full string.
Sorry to have pointed the obvious in my previous mail. The change looks completely valid.
Ideally this would be possible by checking for a one larger return value, but unfortunately the standard is unclear if %n arguments should be counted or not...
Actually, the standard is perfectly clear... with the wrong choice:
"Execution of a %n conversion specification shall not increment the assignment count returned at the completion of execution of the function."
Yes, man sscanf quotes that as well but then continues: ..."but the Corrigendum seems to contradict this. Probably it is wise not to make any assumptions"... And I believe this issue that some implementations count is a real one, I remember the issue coming up, which is why it is >= instead of == for all scanfs that use %n.
Le quintidi 5 prairial, an CCXXII, Reimar Döffinger a écrit :
Should make code more maintainable. Also make sure all input variables are properly reset before each test.
I find this a little more readable, but this is still not terribly nice. Maybe add a comment: /* TODO refactor that to use progressive parsing */. Also, I suppose you noticed that both the original code suffer from the same bug: since %n is not counted in the return value, we do not know if the "%*[^<]<clear/>%n part of the format string has been matched, so match should not be set just to "sscanf() >= n" but to "sscanf() >= n && plen > 0". Regards, -- Nicolas George
participants (2)
-
Nicolas George -
Reimar Döffinger