summaryrefslogtreecommitdiff
path: root/srt.c
blob: 523eb5a0bc9a27c3c3382f2fb88b76561b34b046 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * This file is part of WordExtract.
 *
 * Copyright (C) 2009 Borisov Alexandr
 *
 * WordExtract is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WordExtract is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WordExtract.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <ctype.h>
#include "main.h"
#include "srt.h"
#include "engparser.h"

static int get_srt_tag_length(char *);

/*
 *	Reads text lines from *.srt file
 *	NOTE that "\r\n" and "\n" line ends are possible
 */
int process_srt(FILE *subtitle)
{
 char line[MAXLINE] = {0};
 int lines;

 while (!feof(subtitle)) {
	 for (lines = 0; lines < 2; lines++) {	 
		 fgets(line, MAXLINE, subtitle); 
		 if (feof(subtitle)) return 0;
	 }
	 fgets(line, MAXLINE, subtitle);
	 process_srt_line(line);
	 fgets(line, MAXLINE, subtitle);
	 if (!((line[0] == '\n')||((line[0] == '\r')&&(line[1] == '\n')))) {
		 process_srt_line(line);	  
	 	 fgets(line, MAXLINE, subtitle);
	 }
 }
 return 0;
}

/*
 *	Recieves: line from *.srt file with final '\n' or "\r\n"
 */
int process_srt_line(char *line)
{
 char phrase[MAXPHRASE] = {0};
 int i;
 int taglength;

 for (i = 0; !((line[0] == '\n')||((line[0] == '\r')&&(line[1] == '\n'))); i++, line++) {
	 while (*line == '<') {
		 if (!(taglength = get_srt_tag_length(line)))
			break;
		 line += taglength;
	 }
	 if ((line[0] == '\n')||((line[0] == '\r')&&(line[1] == '\n')))
			break;
	 phrase[i] = *line;
 }
 phrase[i] = '\n';
 switch (lang) {
	 case ENG: parseengphrase(phrase);
 }
 return 0;
}

/*
 * It processes tags and calculate it's length. Possible tags are:
 * <font color="#00FF00" size="6">The <font size="35">most</font> difficult tag</font>
 * <b><i><u>Some of formatted text</u></i></b>
 * It also misses tags with mistakes
 */
static int get_srt_tag_length(char *line)
{
 char c;
 int taglength;

 if (line[1] == '/') {
	c = line[2];
	switch (c) {
		case 'b': case 'i': case 'u': 
			taglength = 4;
			break;
		case 'f':
			taglength = 7;
			break;
		default: 
			taglength = 0;
			break;
	}
 }
 else {
	c = line[1];
	switch (c) {
		case 'b': case 'i': case 'u': 
			taglength = 3;
			break;
		case 'f':
			taglength = 5;
			c = line[5];
			while (c == ' ') {
				taglength++;
				c = line[taglength];
				switch (c) {
					case 'c':
						taglength += 15;
						break;
					case 's':
						taglength += 6;
						while isdigit(line[taglength])
							taglength++;
						taglength++;
						break;
					case '>':
						break;
					default:
						taglength = 0;
				}
				c = line[taglength];
			}
			if (c == '>')
				taglength++;
			else
				taglength = 0;
			break;
		default: 
			taglength = 0;
			break;
	}
 }
 return taglength;
}