annotate TOOLS/subreader.c @ 361:3e0c68209600

dunno :)
author szabii
date Wed, 11 Apr 2001 20:55:14 +0000
parents 7b7c7e26c3a7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
223
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
1 /*
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
2 * Subtitle reader with format autodetection
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
3 * Mier nem muxik realloccal!?!?! - nekem muxik :)
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
4 *
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
5 * Written by laaz
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
6 * Some code cleanup & realloc() by A'rpi/ESP-team
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
7 */
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
8
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
9
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
10 #include <stdio.h>
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
11 #include <stdlib.h>
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
12 #include <string.h>
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
13
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
14 #define ERR (void *)-1
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
15 #define MAX_TEXT 5
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
16
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
17
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
18 int sub_uses_time=0;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
19 int sub_errs=0;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
20 int sub_num=0; // number of subtitle structs
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
21 int sub_format=-1; // 0 for microdvd, 1 for SubRip, 2 for the third format
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
22
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
23 struct subtitle {
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
24
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
25 int lines;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
26
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
27 unsigned long start;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
28 unsigned long end;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
29
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
30 char *text[MAX_TEXT];
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
31 };
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
32
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
33 char *sub_readtext(char *source, char **dest) {
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
34 int len=0;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
35 char *p;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
36
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
37 for (p=source;*p!='\r' && *p!='\n' && *p!='|'; p++,len++);
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
38
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
39 *dest= (char *)malloc (len+1);
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
40 if (!dest) {return ERR;}
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
41
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
42 strncpy(*dest, source, len);
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
43 (*dest)[len]=0;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
44
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
45 while (*p=='\r' || *p=='\n' || *p=='|') p++;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
46
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
47 if (*p) return p; // not-last text field
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
48 else return NULL; // last text field
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
49 }
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
50
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
51
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
52
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
53 struct subtitle *sub_read_line_microdvd(FILE *fd,struct subtitle *current) {
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
54 char line[1001];
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
55 char line2[1001];
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
56 char *p, *next;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
57 int i;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
58
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
59 bzero (current, sizeof(current));
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
60
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
61 do {
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
62 if (!fgets (line, 1000, fd)) return NULL;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
63 } while (*line=='\n' || *line == '\r' || !*line);
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
64
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
65 if (sscanf (line, "{%i}{%i}%s", &(current->start), &(current->end),line2) <2) {return ERR;}
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
66
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
67 p=line;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
68 while (*p++!='}');
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
69 while (*p++!='}');
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
70
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
71 next=p, i=0;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
72 while ((next =sub_readtext (next, &(current->text[i])))) {
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
73 if (current->text[i]==ERR || current->text[i]==ERR) {return ERR;}
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
74 i++;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
75 if (i>MAX_TEXT) { printf ("Too many lines in a subtitle\n");return ERR;}
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
76 }
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
77 current->lines=i+1;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
78
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
79 return current;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
80 }
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
81
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
82 struct subtitle *sub_read_line_subrip(FILE *fd, struct subtitle *current) {
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
83 char line[1001];
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
84 int a1,a2,a3,a4,b1,b2,b3,b4;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
85 char *p=NULL, *q=NULL;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
86 int len;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
87
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
88 bzero (current, sizeof(current));
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
89
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
90 while (!current->text[0]) {
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
91 if (!fgets (line, 1000, fd)) return NULL;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
92 if (sscanf (line, "%i:%i:%i.%i,%i:%i:%i.%i",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4) < 8) continue;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
93 current->start = a1*360000+a2*6000+a3*100+a4;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
94 current->end = b1*360000+b2*6000+b3*100+b4;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
95
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
96 if (!fgets (line, 1000, fd)) return NULL;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
97
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
98 p=q=line;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
99 for (current->lines=1; current->lines < MAX_TEXT; current->lines++) {
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
100 for (q=p,len=0; *p && *p!='\r' && *p!='\n' && strncmp(p,"[br]",4); p++,len++);
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
101 current->text[current->lines-1]=(char *)malloc (len+1);
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
102 if (!current->text[current->lines-1]) return ERR;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
103 strncpy (current->text[current->lines-1], q, len);
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
104 if (!*p || *p=='\r' || *p=='\n') break;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
105 while (*p++!=']');
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
106 }
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
107 }
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
108 return current;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
109 }
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
110
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
111 struct subtitle *sub_read_line_third(FILE *fd,struct subtitle *current) {
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
112 char line[1001];
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
113 int a1,a2,a3,a4,b1,b2,b3,b4;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
114 char *p=NULL;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
115 int i,len;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
116
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
117 bzero (current, sizeof(current));
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
118
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
119 while (!current->text[0]) {
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
120 if (!fgets (line, 1000, fd)) return NULL;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
121 if ((len=sscanf (line, "%i:%i:%i,%i --> %i:%i:%i,%i",&a1,&a2,&a3,&a4,&b1,&b2,&b3,&b4)) < 8)
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
122 continue;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
123 current->start = a1*360000+a2*6000+a3*100+a4/10;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
124 current->end = b1*360000+b2*6000+b3*100+b4/10;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
125 for (i=0; i<MAX_TEXT;) {
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
126 if (!fgets (line, 1000, fd)) return NULL;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
127 len=0;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
128 for (p=line; *p!='\n' && *p!='\r' && *p; p++,len++);
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
129 if (len) {
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
130 current->text[i]=(char *)malloc (len+1);
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
131 if (!current->text[i]) return ERR;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
132 strncpy (current->text[i], line, len);
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
133 i++;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
134 } else {
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
135 break;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
136 }
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
137 }
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
138 current->lines=i;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
139 }
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
140 return current;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
141 }
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
142
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
143
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
144 int sub_autodetect (FILE *fd) {
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
145 char line[1001];
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
146 int i,j=0;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
147 // char *p;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
148
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
149 while (1) {
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
150 j++;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
151 if (!fgets (line, 1000, fd))
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
152 return -1;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
153
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
154 // if (sscanf (line, "{%i}{%i}", &i, &i, p)==2) // ha valaki tudja miert 2, mondja mar el nekem ;)
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
155 if (sscanf (line, "{%i}{%i}", &i, &i)==2) // ha valaki tudja miert 2, mondja mar el nekem ;)
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
156 {sub_uses_time=0;return 0;}
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
157 if (sscanf (line, "%i:%i:%i.%i,%i:%i:%i.%i", &i, &i, &i, &i, &i, &i, &i, &i)==8)
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
158 {sub_uses_time=1;return 1;}
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
159 if (sscanf (line, "%i:%i:%i,%i --> %i:%i:%i,%i", &i, &i, &i, &i, &i, &i, &i, &i)==8)
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
160 {sub_uses_time=1;return 2;}
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
161 if (j>100) return -1; // too many bad lines or bad coder
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
162 }
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
163 }
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
164
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
165
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
166 struct subtitle * sub_get_subtitles (char *filename) {
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
167 FILE *fd;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
168 int n_max;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
169 struct subtitle *first;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
170 struct subtitle * (*func[3])(FILE *fd,struct subtitle *dest)=
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
171 {
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
172 sub_read_line_microdvd,
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
173 sub_read_line_subrip,
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
174 sub_read_line_third
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
175 };
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
176
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
177 fd=fopen (filename, "r"); if (!fd) return NULL;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
178
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
179 sub_format=sub_autodetect (fd);
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
180 if (sub_format==-1) {printf ("SUB: Could not determine file format\n");return NULL;}
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
181 printf ("SUB: Detected subtitle file format: %i\n",sub_format);
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
182
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
183 rewind (fd);
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
184
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
185 sub_num=0;n_max=32;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
186 first=(struct subtitle *)malloc(n_max*sizeof(struct subtitle));
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
187 if(!first) return NULL;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
188
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
189 while(1){
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
190 struct subtitle *sub;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
191 if(sub_num>=n_max){
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
192 n_max+=16;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
193 first=realloc(first,n_max*sizeof(struct subtitle));
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
194 }
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
195 sub=func[sub_format](fd,&first[sub_num]);
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
196 if(!sub) break; // EOF
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
197 if(sub==ERR) ++sub_errs; else ++sub_num; // Error vs. Valid
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
198 }
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
199
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
200 fclose(fd);
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
201
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
202 return first;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
203 }
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
204
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
205
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
206 int main(int argc, char **argv) { // for testing
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
207
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
208 int i,j;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
209 struct subtitle *subs;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
210 struct subtitle *egysub;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
211
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
212 if(argc<2){
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
213 printf("\nUsage: subreader filename.sub\n\n");
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
214 exit(1);
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
215 }
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
216
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
217 subs=sub_get_subtitles(argv[1]);
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
218 if(!subs){
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
219 printf("Couldn't load file... let's write a bugreport :)\n");
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
220 exit(1);
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
221 }
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
222
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
223 for(j=0;j<sub_num;j++){
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
224 egysub=&subs[j];
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
225 printf ("%i line%c (%i-%i) ",
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
226 egysub->lines,
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
227 (1==egysub->lines)?' ':'s',
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
228 egysub->start,
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
229 egysub->end);
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
230 for (i=0; i<egysub->lines; i++) {
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
231 printf ("%s%s",egysub->text[i], i==egysub->lines-1?"":" <BREAK> ");
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
232 }
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
233 printf ("\n");
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
234 }
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
235
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
236 printf ("Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use");
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
237 printf ("Read %i subtitles, %i errors.\n", sub_num, sub_errs);
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
238 return 0;
7b7c7e26c3a7 added subreader by laaz
arpi_esp
parents:
diff changeset
239 }