# HG changeset patch # User albeu # Date 1142520171 0 # Node ID 0ca3fb62d2daaf596032f813094504895cea07f8 # Parent ab0a3e3d32eded006bd295316db2064d096350d3 Move the subread from FILE to stream_t. diff -r ab0a3e3d32ed -r 0ca3fb62d2da libmpdemux/stream.h --- a/libmpdemux/stream.h Thu Mar 16 14:24:22 2006 +0000 +++ b/libmpdemux/stream.h Thu Mar 16 14:42:51 2006 +0000 @@ -208,6 +208,31 @@ return total; } +inline static unsigned char* stream_read_line(stream_t *s,unsigned char* mem, int max) { + int len; + unsigned char* end,*ptr = mem;; + do { + len = s->buf_len-s->buf_pos; + // try to fill the buffer + if(len <= 0 && + (!cache_stream_fill_buffer(s) || + (len = s->buf_len-s->buf_pos) <= 0)) break; + end = memchr((void*)(s->buffer+s->buf_pos),'\n',len); + if(end) len = end - (s->buffer+s->buf_pos) + 1; + if(len > 0 && max > 1) { + int l = len > max-1 ? max-1 : len; + memcpy(ptr,s->buffer+s->buf_pos,l); + max -= l; + ptr += l; + } + s->buf_pos += len; + } while(!end); + if(s->eof && ptr == mem) return NULL; + if(max > 0) ptr[0] = 0; + return mem; +} + + inline static int stream_eof(stream_t *s){ return s->eof; } diff -r ab0a3e3d32ed -r 0ca3fb62d2da subreader.c --- a/subreader.c Thu Mar 16 14:24:22 2006 +0000 +++ b/subreader.c Thu Mar 16 14:42:51 2006 +0000 @@ -18,6 +18,7 @@ #include "config.h" #include "mp_msg.h" #include "subreader.h" +#include "libmpdemux/stream.h" #ifdef HAVE_ENCA #include @@ -94,7 +95,7 @@ return NULL; } -subtitle *sub_read_line_sami(FILE *fd, subtitle *current) { +subtitle *sub_read_line_sami(stream_t* st, subtitle *current) { static char line[LINE_LEN+1]; static char *s = NULL, *slacktime_s; char text[LINE_LEN+1], *p=NULL, *q; @@ -106,7 +107,7 @@ /* read the first line */ if (!s) - if (!(s = fgets(line, LINE_LEN, fd))) return 0; + if (!(s = stream_read_line(st, line, LINE_LEN))) return 0; do { switch (state) { @@ -213,7 +214,7 @@ } /* read next line */ - if (state != 99 && !(s = fgets (line, LINE_LEN, fd))) { + if (state != 99 && !(s = stream_read_line (st, line, LINE_LEN))) { if (current->start > 0) { break; // if it is the last subtitle } else { @@ -257,14 +258,14 @@ else return NULL; // last text field } -subtitle *sub_read_line_microdvd(FILE *fd,subtitle *current) { +subtitle *sub_read_line_microdvd(stream_t *st,subtitle *current) { char line[LINE_LEN+1]; char line2[LINE_LEN+1]; char *p, *next; int i; do { - if (!fgets (line, LINE_LEN, fd)) return NULL; + if (!stream_read_line (st, line, LINE_LEN)) return NULL; } while ((sscanf (line, "{%ld}{}%[^\r\n]", &(current->start), line2) < 2) && @@ -285,14 +286,14 @@ return current; } -subtitle *sub_read_line_mpl2(FILE *fd,subtitle *current) { +subtitle *sub_read_line_mpl2(stream_t *st,subtitle *current) { char line[LINE_LEN+1]; char line2[LINE_LEN+1]; char *p, *next; int i; do { - if (!fgets (line, LINE_LEN, fd)) return NULL; + if (!stream_read_line (st, line, LINE_LEN)) return NULL; } while ((sscanf (line, "[%ld][%ld]%[^\r\n]", &(current->start), &(current->end), line2) < 3)); @@ -311,19 +312,19 @@ return current; } -subtitle *sub_read_line_subrip(FILE *fd, subtitle *current) { +subtitle *sub_read_line_subrip(stream_t* st, subtitle *current) { char line[LINE_LEN+1]; int a1,a2,a3,a4,b1,b2,b3,b4; char *p=NULL, *q=NULL; int len; while (1) { - if (!fgets (line, LINE_LEN, fd)) return NULL; + if (!stream_read_line (st, line, LINE_LEN)) return NULL; if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4) < 8) continue; current->start = a1*360000+a2*6000+a3*100+a4; current->end = b1*360000+b2*6000+b3*100+b4; - if (!fgets (line, LINE_LEN, fd)) return NULL; + if (!stream_read_line (st, line, LINE_LEN)) return NULL; p=q=line; for (current->lines=1; current->lines < SUB_MAX_TEXT; current->lines++) { @@ -341,20 +342,20 @@ return current; } -subtitle *sub_read_line_subviewer(FILE *fd,subtitle *current) { +subtitle *sub_read_line_subviewer(stream_t *st,subtitle *current) { char line[LINE_LEN+1]; int a1,a2,a3,a4,b1,b2,b3,b4; char *p=NULL; int i,len; while (!current->text[0]) { - if (!fgets (line, LINE_LEN, fd)) return NULL; + if (!stream_read_line (st, line, LINE_LEN)) return NULL; if ((len=sscanf (line, "%d:%d:%d%[,.:]%d --> %d:%d:%d%[,.:]%d",&a1,&a2,&a3,(char *)&i,&a4,&b1,&b2,&b3,(char *)&i,&b4)) < 10) continue; current->start = a1*360000+a2*6000+a3*100+a4/10; current->end = b1*360000+b2*6000+b3*100+b4/10; for (i=0; itext[0]) { - if (!fgets (line, LINE_LEN, fd)) return NULL; + if (!stream_read_line (st, line, LINE_LEN)) return NULL; if (line[0]!='{') continue; if ((len=sscanf (line, "{T %d:%d:%d:%d",&a1,&a2,&a3,&a4)) < 4) continue; current->start = a1*360000+a2*6000+a3*100+a4/10; for (i=0; itext[0]) { - if (!fgets (line, LINE_LEN, fd)) return NULL; + if (!stream_read_line (st, line, LINE_LEN)) return NULL; if ((len=sscanf (line, "%d:%d:%d%c%n",&a1,&a2,&a3,&separator,&plen)) < 4) continue; @@ -469,7 +470,7 @@ return current; } -subtitle *sub_read_line_rt(FILE *fd,subtitle *current) { +subtitle *sub_read_line_rt(stream_t *st,subtitle *current) { //TODO: This format uses quite rich (sub/super)set of xhtml // I couldn't check it since DTD is not included. // WARNING: full XML parses can be required for proper parsing @@ -479,7 +480,7 @@ int i,len,plen; while (!current->text[0]) { - if (!fgets (line, LINE_LEN, fd)) return NULL; + if (!stream_read_line (st, line, LINE_LEN)) 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, "