annotate subreader.c @ 543:ee0010d0ca81

Changed some code so it only kicks in when XV is disable. (Later I'll change that stuff to be detected at runtime.)
author atmosfear
date Fri, 20 Apr 2001 10:27:51 +0000
parents 1b2cd175236f
children abd7391a31e3
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;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
20 int sub_num=0; // number of subtitle structs
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
21 int sub_format=-1; // 0 for microdvd, 1 for SubRip, 2 for the third format
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
22
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
23
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
24 char *sub_readtext(char *source, char **dest) {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
25 int len=0;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
26 char *p;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
27
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
28 for (p=source;*p!='\r' && *p!='\n' && *p!='|'; p++,len++);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
29
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
30 *dest= (char *)malloc (len+1);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
31 if (!dest) {return ERR;}
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
32
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
33 strncpy(*dest, source, len);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
34 (*dest)[len]=0;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
35
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
36 while (*p=='\r' || *p=='\n' || *p=='|') p++;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
37
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
38 if (*p) return p; // not-last text field
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
39 else return NULL; // last text field
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
40 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
41
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
42
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
43
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
44 subtitle *sub_read_line_microdvd(FILE *fd,subtitle *current) {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
45 char line[1001];
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
46 char line2[1001];
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
47 char *p, *next;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
48 int i;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
49
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
50 bzero (current, sizeof(current));
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
51
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
52 do {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
53 if (!fgets (line, 1000, fd)) return NULL;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
54 } while (*line=='\n' || *line == '\r' || !*line);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
55
269
f5a7323af43f applied patch by lez
arpi_esp
parents: 258
diff changeset
56 if (sscanf (line, "{%d}{%d}%s", &(current->start), &(current->end),line2) <2) {return ERR;}
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
57
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
58 p=line;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
59 while (*p++!='}');
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
60 while (*p++!='}');
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
61
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
62 next=p, i=0;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
63 while ((next =sub_readtext (next, &(current->text[i])))) {
270
7d544020e398 no more garbage! (fixed by laaz)
gabucino
parents: 269
diff changeset
64 if (current->text[i]==ERR) {return ERR;}
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
65 i++;
270
7d544020e398 no more garbage! (fixed by laaz)
gabucino
parents: 269
diff changeset
66 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
67 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
68 current->lines=i+1;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
69
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
70 return current;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
71 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
72
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
73 subtitle *sub_read_line_subrip(FILE *fd, subtitle *current) {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
74 char line[1001];
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
75 int a1,a2,a3,a4,b1,b2,b3,b4;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
76 char *p=NULL, *q=NULL;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
77 int len;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
78
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
79 bzero (current, sizeof(current));
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
80
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
81 while (!current->text[0]) {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
82 if (!fgets (line, 1000, fd)) return NULL;
269
f5a7323af43f applied patch by lez
arpi_esp
parents: 258
diff changeset
83 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
84 current->start = a1*360000+a2*6000+a3*100+a4;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
85 current->end = b1*360000+b2*6000+b3*100+b4;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
86
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
87 if (!fgets (line, 1000, fd)) return NULL;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
88
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
89 p=q=line;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
90 for (current->lines=1; current->lines < SUB_MAX_TEXT; current->lines++) {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
91 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
92 current->text[current->lines-1]=(char *)malloc (len+1);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
93 if (!current->text[current->lines-1]) return ERR;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
94 strncpy (current->text[current->lines-1], q, len);
270
7d544020e398 no more garbage! (fixed by laaz)
gabucino
parents: 269
diff changeset
95 current->text[current->lines-1][len]='\0';
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
96 if (!*p || *p=='\r' || *p=='\n') break;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
97 while (*p++!=']');
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
98 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
99 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
100 return current;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
101 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
102
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
103 subtitle *sub_read_line_third(FILE *fd,subtitle *current) {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
104 char line[1001];
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
105 int a1,a2,a3,a4,b1,b2,b3,b4;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
106 char *p=NULL;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
107 int i,len;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
108
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
109 bzero (current, sizeof(current));
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
110
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
111 while (!current->text[0]) {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
112 if (!fgets (line, 1000, fd)) return NULL;
269
f5a7323af43f applied patch by lez
arpi_esp
parents: 258
diff changeset
113 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
114 continue;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
115 current->start = a1*360000+a2*6000+a3*100+a4/10;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
116 current->end = b1*360000+b2*6000+b3*100+b4/10;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
117 for (i=0; i<SUB_MAX_TEXT;) {
269
f5a7323af43f applied patch by lez
arpi_esp
parents: 258
diff changeset
118 if (!fgets (line, 1000, fd)) break;
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
119 len=0;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
120 for (p=line; *p!='\n' && *p!='\r' && *p; p++,len++);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
121 if (len) {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
122 current->text[i]=(char *)malloc (len+1);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
123 if (!current->text[i]) return ERR;
270
7d544020e398 no more garbage! (fixed by laaz)
gabucino
parents: 269
diff changeset
124 strncpy (current->text[i], line, len); current->text[i][len]='\0';
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
125 i++;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
126 } else {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
127 break;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
128 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
129 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
130 current->lines=i;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
131 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
132 return current;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
133 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
134
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
135
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
136 int sub_autodetect (FILE *fd) {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
137 char line[1001];
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
138 int i,j=0;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
139 // char *p;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
140
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
141 while (1) {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
142 j++;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
143 if (!fgets (line, 1000, fd))
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
144 return -1;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
145
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
146 // if (sscanf (line, "{%i}{%i}", &i, &i, p)==2) // ha valaki tudja miert 2, mondja mar el nekem ;)
269
f5a7323af43f applied patch by lez
arpi_esp
parents: 258
diff changeset
147 if (sscanf (line, "{%d}{%d}", &i, &i)==2) // ha valaki tudja miert 2, mondja mar el nekem ;)
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
148 {sub_uses_time=0;return 0;}
269
f5a7323af43f applied patch by lez
arpi_esp
parents: 258
diff changeset
149 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
150 {sub_uses_time=1;return 1;}
269
f5a7323af43f applied patch by lez
arpi_esp
parents: 258
diff changeset
151 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
152 {sub_uses_time=1;return 2;}
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
153 if (j>100) return -1; // too many bad lines or bad coder
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
154 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
155 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
156
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
157
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
158 subtitle* sub_read_file (char *filename) {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
159 FILE *fd;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
160 int n_max;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
161 subtitle *first;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
162 subtitle * (*func[3])(FILE *fd,subtitle *dest)=
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
163 {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
164 sub_read_line_microdvd,
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
165 sub_read_line_subrip,
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
166 sub_read_line_third
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
167 };
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
168
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
169 fd=fopen (filename, "r"); if (!fd) return NULL;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
170
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
171 sub_format=sub_autodetect (fd);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
172 if (sub_format==-1) {printf ("SUB: Could not determine file format\n");return NULL;}
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
173 printf ("SUB: Detected subtitle file format: %i\n",sub_format);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
174
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
175 rewind (fd);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
176
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
177 sub_num=0;n_max=32;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
178 first=(subtitle *)malloc(n_max*sizeof(subtitle));
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
179 if(!first) return NULL;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
180
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
181 while(1){
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
182 subtitle *sub;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
183 if(sub_num>=n_max){
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
184 n_max+=16;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
185 first=realloc(first,n_max*sizeof(subtitle));
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
186 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
187 sub=func[sub_format](fd,&first[sub_num]);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
188 if(!sub) break; // EOF
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
189 if(sub==ERR) ++sub_errs; else ++sub_num; // Error vs. Valid
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
190 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
191
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
192 fclose(fd);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
193
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
194 // 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
195 printf ("SUB: Read %i subtitles", sub_num);
f5a7323af43f applied patch by lez
arpi_esp
parents: 258
diff changeset
196 if (sub_errs) printf (", %i error(s).\n", sub_errs);
f5a7323af43f applied patch by lez
arpi_esp
parents: 258
diff changeset
197 else printf (".\n");
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
198
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
199 return first;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
200 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
201
509
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
202 char * strreplace( char * in,char * what,char * whereof )
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
203 {
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
204 int i;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
205 char * tmp;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
206
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
207 if ( ( in == NULL )||( what == NULL )||( whereof == NULL )||( ( tmp=strstr( in,what ) ) == NULL ) ) return NULL;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
208 for( i=0;i<strlen( whereof );i++ ) tmp[i]=whereof[i];
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
209 if ( strlen( what ) > strlen( whereof ) ) tmp[i]=0;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
210 return in;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
211 }
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
212
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
213 char * sub_filename( char * fname )
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
214 {
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
215 char * sub_name = NULL;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
216 char * sub_tmp = NULL;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
217 int i;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
218 #define SUB_EXTS 4
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
219 char * sub_exts[SUB_EXTS] =
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
220 { ".sub",
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
221 ".SUB",
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
222 ".srt",
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
223 ".SRT" };
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
224
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
225 if ( fname == NULL ) return NULL;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
226 for( i=strlen( fname );i>0;i-- )
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
227 if( fname[i] == '.' )
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
228 {
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
229 sub_tmp=(char *)&fname[i];
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
230 break;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
231 }
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
232 if ( i == 0 ) return NULL;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
233 sub_name=strdup( fname );
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
234 for ( i=0;i<SUB_EXTS;i++ )
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
235 {
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
236 FILE * f;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
237
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
238 strcpy( sub_name,fname );
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
239 f=fopen( strreplace( sub_name,sub_tmp,sub_exts[i] ),"rt" );
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
240 if ( f != NULL )
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
241 {
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
242 fclose( f );
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
243 printf( "SUB: Detected sub file: %s\n",sub_name );
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
244 return sub_name;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
245 }
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
246 }
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
247 return NULL;
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
248 }
1b2cd175236f add sub autodetect
pontscho
parents: 270
diff changeset
249
258
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
250 #if 0
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
251 int main(int argc, char **argv) { // for testing
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
252
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
253 int i,j;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
254 subtitle *subs;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
255 subtitle *egysub;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
256
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
257 if(argc<2){
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
258 printf("\nUsage: subreader filename.sub\n\n");
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
259 exit(1);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
260 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
261
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
262 subs=sub_get_subtitles(argv[1]);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
263 if(!subs){
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
264 printf("Couldn't load file... let's write a bugreport :)\n");
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
265 exit(1);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
266 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
267
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
268 for(j=0;j<sub_num;j++){
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
269 egysub=&subs[j];
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
270 printf ("%i line%c (%i-%i) ",
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
271 egysub->lines,
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
272 (1==egysub->lines)?' ':'s',
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
273 egysub->start,
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
274 egysub->end);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
275 for (i=0; i<egysub->lines; i++) {
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
276 printf ("%s%s",egysub->text[i], i==egysub->lines-1?"":" <BREAK> ");
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
277 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
278 printf ("\n");
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
279 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
280
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
281 printf ("Subtitle format %s time.\n", sub_uses_time?"uses":"doesn't use");
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
282 printf ("Read %i subtitles, %i errors.\n", sub_num, sub_errs);
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
283 return 0;
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
284 }
66017e126280 subtitles (.sub files) support
arpi_esp
parents:
diff changeset
285 #endif