annotate subreader.c @ 678:9088ebc5b5b5

Fixed segfault (microdvd format, many lines)
author laaz
date Tue, 01 May 2001 21:28:18 +0000
parents 2f321fe55bdb
children 8a7666a78f83
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
1 /*
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
2 * Subtitle reader with format autodetection
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
3 *
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
4 * Written by laaz
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
5 * Some code cleanup & realloc() by A'rpi/ESP-team
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
6 */
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
7
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
8
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
9 #include <stdio.h>
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
10 #include <stdlib.h>
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
11 #include <string.h>
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
12
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
13 #include "subreader.h"
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
14
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
15 #define ERR (void *)-1
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
16
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
17
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
18 int sub_uses_time=0;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
19 int sub_errs=0;
624
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
20 int sub_num=0; // number of subtitle structs
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
21 int sub_format=-1; // 0 for microdvd
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
22 // 1 for SubRip
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
23 // 2 for the third format (what's this?)
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
24 // 3 for SAMI (smi)
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
25
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
26 int eol(char p) {
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
27 return (p=='\r' || p=='\n' || p=='\0');
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
28 }
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
29
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
30
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
31 subtitle *sub_read_line_sami(FILE *fd, subtitle *current) {
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
32 char line[1001];
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
33 int i;
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
34 char *s, *p;
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
35
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
36 current->start=0;
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
37 do {
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
38 if (! (fgets (line, 1000, fd))) return 0;
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
39 s= strstr(line, "Start=");
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
40 if (s) {
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
41 sscanf (s, "Start=%d", &current->start);
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
42 if (strstr (s, "<P><br>")) current->start=0;
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
43 }
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
44 } while ( !current->start );
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
45
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
46 if (! (fgets (line, 1000, fd))) return 0;
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
47 s=strstr (line, "<P>")+3;
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
48
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
49 i=0;
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
50 do {
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
51 for (p=s; !eol(*p) && strncmp(p,"<br>",4); p++);
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
52 if (p==s) {
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
53 s+=4;
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
54 continue;
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
55 }
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
56 current->text[i]=(char *)malloc(p-s+1);
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
57 strncpy (current->text[i], s, p-s);
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
58 current->text[i++][p-s]='\0';
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
59 if (!strncmp(p,"<br>",4)) s=p+4;
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
60 else s=p;
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
61 } while (!eol (*p));
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
62
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
63 current->lines=i;
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
64
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
65 if (! (fgets (line, 1000, fd))) return 0;
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
66 s= strstr(line, "Start=");
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
67 if (s) {
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
68 sscanf (s, "Start=%d", &current->end);
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
69 if (!strstr (s, "<P><br>")) return ERR;
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
70 } else return ERR;
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
71
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
72 return current;
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
73 }
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
74
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
75
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
76 char *sub_readtext(char *source, char **dest) {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
77 int len=0;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
78 char *p;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
79
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
80 for (p=source;*p!='\r' && *p!='\n' && *p!='|'; p++,len++);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
81
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
82 *dest= (char *)malloc (len+1);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
83 if (!dest) {return ERR;}
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
84
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
85 strncpy(*dest, source, len);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
86 (*dest)[len]=0;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
87
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
88 while (*p=='\r' || *p=='\n' || *p=='|') p++;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
89
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
90 if (*p) return p; // not-last text field
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
91 else return NULL; // last text field
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
92 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
93
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
94 subtitle *sub_read_line_microdvd(FILE *fd,subtitle *current) {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
95 char line[1001];
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
96 char line2[1001];
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
97 char *p, *next;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
98 int i;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
99
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
100 bzero (current, sizeof(current));
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
101
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
102 do {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
103 if (!fgets (line, 1000, fd)) return NULL;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
104 } while (*line=='\n' || *line == '\r' || !*line);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
105
605
abd7391a31e3 warnings killed
szabii
parents: 509
diff changeset
106 if (sscanf (line, "{%ld}{%ld}%s", &(current->start), &(current->end),line2) <2) {return ERR;}
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
107
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
108 p=line;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
109 while (*p++!='}');
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
110 while (*p++!='}');
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
111
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
112 next=p, i=0;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
113 while ((next =sub_readtext (next, &(current->text[i])))) {
270
7d544020e398 no more garbage! (fixed by laaz)
gabucino
parents: 269
diff changeset
114 if (current->text[i]==ERR) {return ERR;}
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
115 i++;
678
9088ebc5b5b5 Fixed segfault (microdvd format, many lines)
laaz
parents: 625
diff changeset
116 if (i>=SUB_MAX_TEXT) { printf ("Too many lines in a subtitle\n");current->lines=i;return;}
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
117 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
118 current->lines=i+1;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
119
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
120 return current;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
121 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
122
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
123 subtitle *sub_read_line_subrip(FILE *fd, subtitle *current) {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
124 char line[1001];
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
125 int a1,a2,a3,a4,b1,b2,b3,b4;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
126 char *p=NULL, *q=NULL;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
127 int len;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
128
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
129 bzero (current, sizeof(current));
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
130
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
131 while (!current->text[0]) {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
132 if (!fgets (line, 1000, fd)) return NULL;
269
f5a7323af43f applied patch by lez
arpi_esp
parents: 258
diff changeset
133 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4) < 8) continue;
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
134 current->start = a1*360000+a2*6000+a3*100+a4;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
135 current->end = b1*360000+b2*6000+b3*100+b4;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
136
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
137 if (!fgets (line, 1000, fd)) return NULL;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
138
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
139 p=q=line;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
140 for (current->lines=1; current->lines < SUB_MAX_TEXT; current->lines++) {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
141 for (q=p,len=0; *p && *p!='\r' && *p!='\n' && strncmp(p,"[br]",4); p++,len++);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
142 current->text[current->lines-1]=(char *)malloc (len+1);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
143 if (!current->text[current->lines-1]) return ERR;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
144 strncpy (current->text[current->lines-1], q, len);
270
7d544020e398 no more garbage! (fixed by laaz)
gabucino
parents: 269
diff changeset
145 current->text[current->lines-1][len]='\0';
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
146 if (!*p || *p=='\r' || *p=='\n') break;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
147 while (*p++!=']');
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
148 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
149 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
150 return current;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
151 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
152
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
153 subtitle *sub_read_line_third(FILE *fd,subtitle *current) {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
154 char line[1001];
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
155 int a1,a2,a3,a4,b1,b2,b3,b4;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
156 char *p=NULL;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
157 int i,len;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
158
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
159 bzero (current, sizeof(current));
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
160
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
161 while (!current->text[0]) {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
162 if (!fgets (line, 1000, fd)) return NULL;
269
f5a7323af43f applied patch by lez
arpi_esp
parents: 258
diff changeset
163 if ((len=sscanf (line, "%d:%d:%d,%d --> %d:%d:%d,%d",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8)
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
164 continue;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
165 current->start = a1*360000+a2*6000+a3*100+a4/10;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
166 current->end = b1*360000+b2*6000+b3*100+b4/10;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
167 for (i=0; i<SUB_MAX_TEXT;) {
269
f5a7323af43f applied patch by lez
arpi_esp
parents: 258
diff changeset
168 if (!fgets (line, 1000, fd)) break;
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
169 len=0;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
170 for (p=line; *p!='\n' && *p!='\r' && *p; p++,len++);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
171 if (len) {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
172 current->text[i]=(char *)malloc (len+1);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
173 if (!current->text[i]) return ERR;
270
7d544020e398 no more garbage! (fixed by laaz)
gabucino
parents: 269
diff changeset
174 strncpy (current->text[i], line, len); current->text[i][len]='\0';
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
175 i++;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
176 } else {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
177 break;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
178 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
179 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
180 current->lines=i;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
181 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
182 return current;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
183 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
184
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
185
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
186 int sub_autodetect (FILE *fd) {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
187 char line[1001];
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
188 int i,j=0;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
189 // char *p;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
190
624
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
191 while (j < 100) {
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
192 j++;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
193 if (!fgets (line, 1000, fd))
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
194 return -1;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
195
624
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
196 if (sscanf (line, "{%d}{%d}", &i, &i)==2)
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
197 {sub_uses_time=0;return 0;}
269
f5a7323af43f applied patch by lez
arpi_esp
parents: 258
diff changeset
198 if (sscanf (line, "%d:%d:%d.%d,%d:%d:%d.%d", &i, &i, &i, &i, &i, &i, &i, &i)==8)
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
199 {sub_uses_time=1;return 1;}
269
f5a7323af43f applied patch by lez
arpi_esp
parents: 258
diff changeset
200 if (sscanf (line, "%d:%d:%d,%d --> %d:%d:%d,%d", &i, &i, &i, &i, &i, &i, &i, &i)==8)
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
201 {sub_uses_time=1;return 2;}
624
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
202 if (strstr (line, "<SAMI>"))
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
203 {sub_uses_time=0; return 3;}
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
204 }
624
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
205
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
206 return -1; // too many bad lines
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
207 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
208
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
209
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
210 subtitle* sub_read_file (char *filename) {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
211 FILE *fd;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
212 int n_max;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
213 subtitle *first;
624
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
214 subtitle * (*func[4])(FILE *fd,subtitle *dest)=
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
215 {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
216 sub_read_line_microdvd,
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
217 sub_read_line_subrip,
624
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
218 sub_read_line_third,
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
219 sub_read_line_sami
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
220 };
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
221
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
222 fd=fopen (filename, "r"); if (!fd) return NULL;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
223
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
224 sub_format=sub_autodetect (fd);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
225 if (sub_format==-1) {printf ("SUB: Could not determine file format\n");return NULL;}
624
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
226 printf ("SUB: Detected subtitle file format: %d\n",sub_format);
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
227
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
228 rewind (fd);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
229
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
230 sub_num=0;n_max=32;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
231 first=(subtitle *)malloc(n_max*sizeof(subtitle));
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
232 if(!first) return NULL;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
233
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
234 while(1){
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
235 subtitle *sub;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
236 if(sub_num>=n_max){
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
237 n_max+=16;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
238 first=realloc(first,n_max*sizeof(subtitle));
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
239 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
240 sub=func[sub_format](fd,&first[sub_num]);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
241 if(!sub) break; // EOF
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
242 if(sub==ERR) ++sub_errs; else ++sub_num; // Error vs. Valid
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
243 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
244
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
245 fclose(fd);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
246
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
247 // printf ("SUB: Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use");
269
f5a7323af43f applied patch by lez
arpi_esp
parents: 258
diff changeset
248 printf ("SUB: Read %i subtitles", sub_num);
624
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
249 if (sub_errs) printf (", %i bad line(s).\n", sub_errs);
269
f5a7323af43f applied patch by lez
arpi_esp
parents: 258
diff changeset
250 else printf (".\n");
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
251
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
252 return first;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
253 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
254
509
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
255 char * strreplace( char * in,char * what,char * whereof )
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
256 {
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
257 int i;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
258 char * tmp;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
259
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
260 if ( ( in == NULL )||( what == NULL )||( whereof == NULL )||( ( tmp=strstr( in,what ) ) == NULL ) ) return NULL;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
261 for( i=0;i<strlen( whereof );i++ ) tmp[i]=whereof[i];
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
262 if ( strlen( what ) > strlen( whereof ) ) tmp[i]=0;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
263 return in;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
264 }
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
265
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
266 char * sub_filename( char * fname )
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
267 {
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
268 char * sub_name = NULL;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
269 char * sub_tmp = NULL;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
270 int i;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
271 #define SUB_EXTS 4
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
272 char * sub_exts[SUB_EXTS] =
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
273 { ".sub",
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
274 ".SUB",
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
275 ".srt",
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
276 ".SRT" };
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
277
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
278 if ( fname == NULL ) return NULL;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
279 for( i=strlen( fname );i>0;i-- )
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
280 if( fname[i] == '.' )
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
281 {
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
282 sub_tmp=(char *)&fname[i];
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
283 break;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
284 }
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
285 if ( i == 0 ) return NULL;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
286 sub_name=strdup( fname );
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
287 for ( i=0;i<SUB_EXTS;i++ )
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
288 {
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
289 FILE * f;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
290
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
291 strcpy( sub_name,fname );
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
292 f=fopen( strreplace( sub_name,sub_tmp,sub_exts[i] ),"rt" );
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
293 if ( f != NULL )
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
294 {
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
295 fclose( f );
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
296 printf( "SUB: Detected sub file: %s\n",sub_name );
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
297 return sub_name;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
298 }
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
299 }
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
300 return NULL;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
301 }
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
302
625
2f321fe55bdb #if 0 'd main()
laaz
parents: 624
diff changeset
303 #if 0
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
304 int main(int argc, char **argv) { // for testing
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
305
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
306 int i,j;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
307 subtitle *subs;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
308 subtitle *egysub;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
309
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
310 if(argc<2){
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
311 printf("\nUsage: subreader filename.sub\n\n");
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
312 exit(1);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
313 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
314
624
44b764d348a0 added support for SAMI subtitle support (.smi)
laaz
parents: 605
diff changeset
315 subs=sub_read_file(argv[1]);
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
316 if(!subs){
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
317 printf("Couldn't load file... let's write a bugreport :)\n");
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
318 exit(1);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
319 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
320
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
321 for(j=0;j<sub_num;j++){
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
322 egysub=&subs[j];
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
323 printf ("%i line%c (%i-%i) ",
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
324 egysub->lines,
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
325 (1==egysub->lines)?' ':'s',
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
326 egysub->start,
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
327 egysub->end);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
328 for (i=0; i<egysub->lines; i++) {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
329 printf ("%s%s",egysub->text[i], i==egysub->lines-1?"":" <BREAK> ");
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
330 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
331 printf ("\n");
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
332 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
333
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
334 printf ("Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use");
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
335 printf ("Read %i subtitles, %i errors.\n", sub_num, sub_errs);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
336 return 0;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
337 }
625
2f321fe55bdb #if 0 'd main()
laaz
parents: 624
diff changeset
338 #endif